git.iko.soy/typeable/pandoc
on master help sign in
Use readerExtensions instead of readerStrict in readers.

Test individually for the extensions.

commit33fdea67b5757919dbf8a0913513690ea6bfaee4 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 26 Jul 2012, 20:29 (14 years ago) parent5186da9
3 files changed+118−123
src/Text/Pandoc/Options.hs+27−13 src/Text/Pandoc/Readers/HTML.hs+19−26 src/Text/Pandoc/Readers/Markdown.hs+72−84
src/Text/Pandoc/Options.hs+27−13
@@ -37,19 +37,33 @@ import qualified Data.Set as Set
3737import Data.Default
3838
3939-- | Individually selectable syntax extensions.
40−data Extension = Footnotes
41− | Tex_math
42− | Delimited_code_blocks
43− | Markdown_in_html_blocks
44− | Fancy_lists
45− | Startnum
46− | Definition_lists
47− | Header_identifiers
48− | All_symbols_escapable
49− | Intraword_underscores
50− | Blank_before_blockquote
51− | Blank_before_header
52− | Significant_bullets
40+data Extension = Ext_footnotes
41+ | Ext_inline_notes
42+ | Ext_pandoc_title_blocks
43+ | Ext_table_captions
44+ | Ext_simple_tables
45+ | Ext_multiline_tables
46+ | Ext_grid_tables
47+ | Ext_pipe_tables
48+ | Ext_citations
49+ | Ext_raw_tex
50+ | Ext_tex_math
51+ | Ext_latex_macros
52+ | Ext_delimited_code_blocks
53+ | Ext_markdown_in_html_blocks
54+ | Ext_autolink_code_spans
55+ | Ext_fancy_lists
56+ | Ext_startnum
57+ | Ext_definition_lists
58+ | Ext_header_identifiers
59+ | Ext_all_symbols_escapable
60+ | Ext_intraword_underscores
61+ | Ext_blank_before_blockquote
62+ | Ext_blank_before_header
63+ | Ext_significant_bullets
64+ | Ext_strikeout
65+ | Ext_superscript
66+ | Ext_subscript
5367 deriving (Show, Read, Enum, Eq, Ord, Bounded)
5468
5569data ReaderOptions = ReaderOptions{
src/Text/Pandoc/Readers/HTML.hs+19−26
@@ -125,25 +125,22 @@ pBulletList = try $ do
125125pOrderedList :: TagParser [Block]
126126pOrderedList = try $ do
127127 TagOpen _ attribs <- pSatisfy (~== TagOpen "ol" [])
128− st <- getState
129− let (start, style) = if readerStrict (stateOptions st)
130− then (1, DefaultStyle)
131− else (sta', sty')
132− where sta = fromMaybe "1" $
133− lookup "start" attribs
134− sta' = if all isDigit sta
135− then read sta
136− else 1
137− sty = fromMaybe (fromMaybe "" $
138− lookup "style" attribs) $
139− lookup "class" attribs
140− sty' = case sty of
141− "lower-roman" -> LowerRoman
142− "upper-roman" -> UpperRoman
143− "lower-alpha" -> LowerAlpha
144− "upper-alpha" -> UpperAlpha
145− "decimal" -> Decimal
146− _ -> DefaultStyle
128+ let (start, style) = (sta', sty')
129+ where sta = fromMaybe "1" $
130+ lookup "start" attribs
131+ sta' = if all isDigit sta
132+ then read sta
133+ else 1
134+ sty = fromMaybe (fromMaybe "" $
135+ lookup "style" attribs) $
136+ lookup "class" attribs
137+ sty' = case sty of
138+ "lower-roman" -> LowerRoman
139+ "upper-roman" -> UpperRoman
140+ "lower-alpha" -> LowerAlpha
141+ "upper-alpha" -> UpperAlpha
142+ "decimal" -> Decimal
143+ _ -> DefaultStyle
147144 let nonItem = pSatisfy (\t ->
148145 not (tagOpen (`elem` ["li","ol","ul","dl"]) (const True) t) &&
149146 not (t ~== TagClose "ol"))
@@ -280,10 +277,7 @@ pCodeBlock = try $ do
280277 let attribsId = fromMaybe "" $ lookup "id" attr
281278 let attribsClasses = words $ fromMaybe "" $ lookup "class" attr
282279 let attribsKV = filter (\(k,_) -> k /= "class" && k /= "id") attr
283− st <- getState
284− let attribs = if readerStrict (stateOptions st)
285− then ("",[],[])
286− else (attribsId, attribsClasses, attribsKV)
280+ let attribs = (attribsId, attribsClasses, attribsKV)
287281 return [CodeBlock attribs result]
288282
289283inline :: TagParser [Inline]
@@ -331,14 +325,13 @@ pStrong :: TagParser [Inline]
331325pStrong = pInlinesInTags "strong" Strong <|> pInlinesInTags "b" Strong
332326
333327pSuperscript :: TagParser [Inline]
334−pSuperscript = failIfStrict >> pInlinesInTags "sup" Superscript
328+pSuperscript = pInlinesInTags "sup" Superscript
335329
336330pSubscript :: TagParser [Inline]
337−pSubscript = failIfStrict >> pInlinesInTags "sub" Subscript
331+pSubscript = pInlinesInTags "sub" Subscript
338332
339333pStrikeout :: TagParser [Inline]
340334pStrikeout = do
341− failIfStrict
342335 pInlinesInTags "s" Strikeout <|>
343336 pInlinesInTags "strike" Strikeout <|>
344337 pInlinesInTags "del" Strikeout <|>
src/Text/Pandoc/Readers/Markdown.hs+72−84
@@ -112,12 +112,6 @@ litChar = escapedChar'
112112 <|> noneOf "\n"
113113 <|> (newline >> notFollowedBy blankline >> return ' ')
114114
115−-- | Fail unless we're at beginning of a line.
116−failUnlessBeginningOfLine :: Parser [tok] st ()
117−failUnlessBeginningOfLine = do
118− pos <- getPosition
119− if sourceColumn pos == 1 then return () else fail "not beginning of line"
120−
121115-- | Parse a sequence of inline elements between square brackets,
122116-- including inlines between balanced pairs of square brackets.
123117inlinesInBalancedBrackets :: Parser [Char] ParserState Inline
@@ -165,7 +159,7 @@ dateLine = try $ do
165159
166160titleBlock :: Parser [Char] ParserState ([Inline], [[Inline]], [Inline])
167161titleBlock = try $ do
168− failIfStrict
162+ guardEnabled Ext_pandoc_title_blocks
169163 title <- option [] titleLine
170164 author <- option [] authorsLine
171165 date <- option [] dateLine
@@ -181,10 +175,10 @@ parseMarkdown = do
181175 startPos <- getPosition
182176 -- go through once just to get list of reference keys and notes
183177 -- docMinusKeys is the raw document with blanks where the keys/notes were...
184− st <- getState
185178 let firstPassParser = referenceKey
186− <|> (if readerStrict (stateOptions st) then mzero else noteBlock)
187− <|> liftM snd (withRaw codeBlockDelimited)
179+ <|> (guardEnabled Ext_footnotes >> noteBlock)
180+ <|> (guardEnabled Ext_delimited_code_blocks >>
181+ liftM snd (withRaw codeBlockDelimited))
188182 <|> lineClump
189183 docMinusKeys <- liftM concat $ manyTill firstPassParser eof
190184 setInput docMinusKeys
@@ -292,35 +286,22 @@ parseBlocks :: Parser [Char] ParserState [Block]
292286parseBlocks = manyTill block eof
293287
294288block :: Parser [Char] ParserState Block
295−block = do
296− st <- getState
297− choice (if readerStrict (stateOptions st)
298− then [ header
299− , codeBlockIndented
300− , blockQuote
301− , hrule
302− , bulletList
303− , orderedList
304− , htmlBlock
305− , para
306− , plain
307− , nullBlock ]
308− else [ codeBlockDelimited
309− , macro
310− , header
311− , table
312− , codeBlockIndented
313− , lhsCodeBlock
314− , blockQuote
315− , hrule
316− , bulletList
317− , orderedList
318− , definitionList
319− , rawTeXBlock
320− , para
321− , rawHtmlBlocks
322− , plain
323− , nullBlock ]) <?> "block"
289+block = choice [ codeBlockDelimited
290+ , guardEnabled Ext_latex_macros >> macro
291+ , header
292+ , table
293+ , codeBlockIndented
294+ , lhsCodeBlock
295+ , blockQuote
296+ , hrule
297+ , bulletList
298+ , orderedList
299+ , definitionList
300+ , rawTeXBlock
301+ , para
302+ , htmlBlock
303+ , plain
304+ , nullBlock ] <?> "block"
324305
325306--
326307-- header blocks
@@ -431,8 +412,9 @@ keyValAttr = try $ do
431412 <|> many nonspaceChar
432413 return ("",[],[(key,val)])
433414
434−codeBlockDelimited :: Parser [Char] st Block
415+codeBlockDelimited :: Parser [Char] ParserState Block
435416codeBlockDelimited = try $ do
417+ guardEnabled Ext_delimited_code_blocks
436418 (size, attr, c) <- blockDelimiter (\c -> c == '~' || c == '`') Nothing
437419 contents <- manyTill anyLine (blockDelimiter (== c) (Just size))
438420 blanklines
@@ -635,6 +617,7 @@ defListMarker = do
635617
636618definitionListItem :: Parser [Char] ParserState ([Inline], [[Block]])
637619definitionListItem = try $ do
620+ guardEnabled Ext_definition_lists
638621 -- first, see if this has any chance of being a definition list:
639622 lookAhead (anyLine >> optional blankline >> defListMarker)
640623 term <- manyTill inline newline
@@ -694,9 +677,9 @@ para = try $ do
694677 guard $ not . all isHtmlOrBlank $ result
695678 option (Plain result) $ try $ do
696679 newline
697− blanklines <|>
698− (getState >>= guard . readerStrict . stateOptions >>
699− lookAhead (blockQuote <|> header) >> return "")
680+ (blanklines >> return Null)
681+ <|> (guardDisabled Ext_blank_before_blockquote >> lookAhead blockQuote)
682+ <|> (guardDisabled Ext_blank_before_header >> lookAhead header)
700683 return $ Para result
701684
702685plain :: Parser [Char] ParserState Block
@@ -710,12 +693,16 @@ htmlElement :: Parser [Char] ParserState [Char]
710693htmlElement = strictHtmlBlock <|> liftM snd (htmlTag isBlockTag)
711694
712695htmlBlock :: Parser [Char] ParserState Block
713−htmlBlock = try $ do
714− failUnlessBeginningOfLine
696+htmlBlock = RawBlock "html" `fmap`
697+ ((guardEnabled Ext_markdown_in_html_blocks >> rawHtmlBlocks)
698+ <|> htmlBlock')
699+
700+htmlBlock' :: Parser [Char] ParserState String
701+htmlBlock' = try $ do
715702 first <- htmlElement
716703 finalSpace <- many spaceChar
717704 finalNewlines <- many newline
718− return $ RawBlock "html" $ first ++ finalSpace ++ finalNewlines
705+ return $ first ++ finalSpace ++ finalNewlines
719706
720707strictHtmlBlock :: Parser [Char] ParserState [Char]
721708strictHtmlBlock = htmlInBalanced (not . isInlineTag)
@@ -730,13 +717,13 @@ rawVerbatimBlock = try $ do
730717
731718rawTeXBlock :: Parser [Char] ParserState Block
732719rawTeXBlock = do
733− failIfStrict
720+ guardEnabled Ext_raw_tex
734721 result <- liftM (RawBlock "latex") rawLaTeXBlock
735722 <|> liftM (RawBlock "context") rawConTeXtEnvironment
736723 spaces
737724 return result
738725
739−rawHtmlBlocks :: Parser [Char] ParserState Block
726+rawHtmlBlocks :: Parser [Char] ParserState String
740727rawHtmlBlocks = do
741728 htmlBlocks <- many1 $ do blk <- rawVerbatimBlock <|>
742729 liftM snd (htmlTag isBlockTag)
@@ -750,8 +737,7 @@ rawHtmlBlocks = do
750737 -- by a blank line
751738 return $ blk ++ sps
752739 let combined = concat htmlBlocks
753− let combined' = if last combined == '\n' then init combined else combined
754− return $ RawBlock "html" combined'
740+ return $ if last combined == '\n' then init combined else combined
755741
756742--
757743-- Tables
@@ -826,6 +812,7 @@ multilineRow indices = do
826812-- and followed by blank lines.
827813tableCaption :: Parser [Char] ParserState [Inline]
828814tableCaption = try $ do
815+ guardEnabled Ext_table_captions
829816 skipNonindentSpaces
830817 string ":" <|> string "Table:"
831818 result <- many1 inline
@@ -961,10 +948,14 @@ table :: Parser [Char] ParserState Block
961948table = try $ do
962949 frontCaption <- option [] tableCaption
963950 Table _ aligns widths heads lines' <-
964− try (scanForPipe >> (pipeTable True <|> pipeTable False)) <|>
965− multilineTable False <|> simpleTable True <|>
966− simpleTable False <|> multilineTable True <|>
967− gridTable False <|> gridTable True <?> "table"
951+ try (guardEnabled Ext_pipe_tables >> scanForPipe >>
952+ (pipeTable True <|> pipeTable False)) <|>
953+ try (guardEnabled Ext_multiline_tables >>
954+ (multilineTable False <|> simpleTable True)) <|>
955+ try (guardEnabled Ext_simple_tables >>
956+ (simpleTable False <|> multilineTable True)) <|>
957+ try (guardEnabled Ext_grid_tables >>
958+ (gridTable False <|> gridTable True)) <?> "table"
968959 optional blanklines
969960 caption <- if null frontCaption
970961 then option [] tableCaption
@@ -1008,10 +999,8 @@ inlineParsers = [ whitespace
1008999escapedChar' :: Parser [Char] ParserState Char
10091000escapedChar' = try $ do
10101001 char '\\'
1011− state <- getState
1012− if readerStrict (stateOptions state)
1013− then oneOf "\\`*_{}[]()>#+-.!~"
1014− else satisfy (not . isAlphaNum)
1002+ (guardEnabled Ext_all_symbols_escapable >> satisfy (not . isAlphaNum))
1003+ <|> oneOf "\\`*_{}[]()>#+-.!~"
10151004
10161005escapedChar :: Parser [Char] ParserState Inline
10171006escapedChar = do
@@ -1023,10 +1012,9 @@ escapedChar = do
10231012
10241013ltSign :: Parser [Char] ParserState Inline
10251014ltSign = do
1026− st <- getState
1027− if readerStrict (stateOptions st)
1028− then char '<'
1029− else notFollowedBy' rawHtmlBlocks >> char '<' -- unless it starts html
1015+ guardDisabled Ext_markdown_in_html_blocks
1016+ <|> (notFollowedBy' rawHtmlBlocks >> return ())
1017+ char '<'
10301018 return $ Str ['<']
10311019
10321020exampleRef :: Parser [Char] ParserState Inline
@@ -1072,13 +1060,13 @@ math = (mathDisplay >>= applyMacros' >>= return . Math DisplayMath)
10721060
10731061mathDisplay :: Parser [Char] ParserState String
10741062mathDisplay = try $ do
1075− failIfStrict
1063+ guardEnabled Ext_tex_math
10761064 string "$$"
10771065 many1Till (noneOf "\n" <|> (newline >>~ notFollowedBy' blankline)) (try $ string "$$")
10781066
10791067mathInline :: Parser [Char] ParserState String
10801068mathInline = try $ do
1081− failIfStrict
1069+ guardEnabled Ext_tex_math
10821070 char '$'
10831071 notFollowedBy space
10841072 words' <- sepBy1 mathWord (many1 (spaceChar <|> (newline >>~ notFollowedBy' blankline)))
@@ -1135,18 +1123,18 @@ strong = Strong `liftM` nested
11351123
11361124strikeout :: Parser [Char] ParserState Inline
11371125strikeout = Strikeout `liftM`
1138− (failIfStrict >> inlinesBetween strikeStart strikeEnd)
1126+ (guardEnabled Ext_strikeout >> inlinesBetween strikeStart strikeEnd)
11391127 where strikeStart = string "~~" >> lookAhead nonspaceChar
11401128 >> notFollowedBy (char '~')
11411129 strikeEnd = try $ string "~~"
11421130
11431131superscript :: Parser [Char] ParserState Inline
1144−superscript = failIfStrict >> enclosed (char '^') (char '^')
1132+superscript = guardEnabled Ext_superscript >> enclosed (char '^') (char '^')
11451133 (notFollowedBy spaceChar >> inline) >>= -- may not contain Space
11461134 return . Superscript
11471135
11481136subscript :: Parser [Char] ParserState Inline
1149−subscript = failIfStrict >> enclosed (char '~') (char '~')
1137+subscript = guardEnabled Ext_subscript >> enclosed (char '~') (char '~')
11501138 (notFollowedBy spaceChar >> inline) >>= -- may not contain Space
11511139 return . Subscript
11521140
@@ -1163,7 +1151,8 @@ str = do
11631151 smart <- (readerSmart . stateOptions) `fmap` getState
11641152 a <- alphaNum
11651153 as <- many $ alphaNum
1166− <|> (try $ char '_' >>~ lookAhead alphaNum)
1154+ <|> (guardEnabled Ext_intraword_underscores >>
1155+ try (char '_' >>~ lookAhead alphaNum))
11671156 <|> if smart
11681157 then (try $ satisfy (\c -> c == '\'' || c == '\x2019') >>
11691158 lookAhead alphaNum >> return '\x2019')
@@ -1200,11 +1189,10 @@ endline :: Parser [Char] ParserState Inline
12001189endline = try $ do
12011190 newline
12021191 notFollowedBy blankline
1203− st <- getState
1204− when (readerStrict (stateOptions st)) $ do
1205− notFollowedBy emailBlockQuoteStart
1206− notFollowedBy (char '#') -- atx header
1192+ guardEnabled Ext_blank_before_blockquote <|> notFollowedBy emailBlockQuoteStart
1193+ guardEnabled Ext_blank_before_header <|> notFollowedBy (char '#') -- atx header
12071194 -- parse potential list-starts differently if in a list:
1195+ st <- getState
12081196 when (stateParserContext st == ListItemState) $ do
12091197 notFollowedBy' bulletListStart
12101198 notFollowedBy' anyOrderedListStart
@@ -1282,10 +1270,9 @@ autoLink = try $ do
12821270 char '<'
12831271 (orig, src) <- uri <|> emailAddress
12841272 char '>'
1285− st <- getState
1286− return $ if readerStrict (stateOptions st)
1287− then Link [Str orig] (src, "")
1288− else Link [Code ("",["url"],[]) orig] (src, "")
1273+ (guardEnabled Ext_autolink_code_spans >>
1274+ return (Link [Code ("",["url"],[]) orig] (src, "")))
1275+ <|> return (Link [Str orig] (src, ""))
12891276
12901277image :: Parser [Char] ParserState Inline
12911278image = try $ do
@@ -1296,7 +1283,7 @@ image = try $ do
12961283
12971284note :: Parser [Char] ParserState Inline
12981285note = try $ do
1299− failIfStrict
1286+ guardEnabled Ext_footnotes
13001287 ref <- noteMarker
13011288 state <- getState
13021289 let notes = stateNotes state
@@ -1313,14 +1300,14 @@ note = try $ do
13131300
13141301inlineNote :: Parser [Char] ParserState Inline
13151302inlineNote = try $ do
1316− failIfStrict
1303+ guardEnabled Ext_inline_notes
13171304 char '^'
13181305 contents <- inlinesInBalancedBrackets inline
13191306 return $ Note [Para contents]
13201307
13211308rawLaTeXInline' :: Parser [Char] ParserState Inline
13221309rawLaTeXInline' = try $ do
1323− failIfStrict
1310+ guardEnabled Ext_raw_tex
13241311 lookAhead $ char '\\' >> notFollowedBy' (string "start") -- context env
13251312 RawInline _ s <- rawLaTeXInline
13261313 return $ RawInline "tex" s -- "tex" because it might be context or latex
@@ -1343,17 +1330,18 @@ inBrackets parser = do
13431330
13441331rawHtmlInline :: Parser [Char] ParserState Inline
13451332rawHtmlInline = do
1346− st <- getState
1347− (_,result) <- if readerStrict (stateOptions st)
1348− then htmlTag (not . isTextTag)
1349− else htmlTag isInlineTag
1333+ mdInHtml <- option False $
1334+ guardEnabled Ext_markdown_in_html_blocks >> return True
1335+ (_,result) <- if mdInHtml
1336+ then htmlTag isInlineTag
1337+ else htmlTag (not . isTextTag)
13501338 return $ RawInline "html" result
13511339
13521340-- Citations
13531341
13541342cite :: Parser [Char] ParserState Inline
13551343cite = do
1356− failIfStrict
1344+ guardEnabled Ext_citations
13571345 citations <- textualCite <|> normalCite
13581346 return $ Cite citations []
13591347