git.iko.soy/typeable/pandoc
on master help sign in
Textile reader: Handle attributes on headers.
5e9145bJohn MacFarlane
src/Text/Pandoc/Readers/Textile.hs+32−15
@@ -36,9 +36,7 @@ Implemented and parsed:
36 - Inlines : strong, emph, cite, code, deleted, superscript,
36 - Inlines : strong, emph, cite, code, deleted, superscript,
37 subscript, links
37 subscript, links
38 - footnotes
38 - footnotes
39−
40−Implemented but discarded:
41− - HTML-specific and CSS-specific attributes
39+ - HTML-specific and CSS-specific attributes on headers
42
40
43Left to be implemented:
41Left to be implemented:
44 - dimension sign
42 - dimension sign
@@ -171,19 +169,16 @@ header :: Parser [Char] ParserState Block
171header = try $ do
169header = try $ do
172 char 'h'
170 char 'h'
173 level <- digitToInt <$> oneOf "123456"
171 level <- digitToInt <$> oneOf "123456"
174− attr <- option "" attributes
172+ attr <- attributes
175− let ident = case attr of
176− '#':xs -> xs
177− _ -> ""
178 char '.'
173 char '.'
179 whitespace
174 whitespace
180 name <- normalizeSpaces <$> manyTill inline blockBreak
175 name <- normalizeSpaces <$> manyTill inline blockBreak
181− return $ Header level (ident,[],[]) name
176+ return $ Header level attr name
182
177
183-- | Blockquote of the form "bq. content"
178-- | Blockquote of the form "bq. content"
184blockQuote :: Parser [Char] ParserState Block
179blockQuote :: Parser [Char] ParserState Block
185blockQuote = try $ do
180blockQuote = try $ do
186− string "bq" >> optional attributes >> char '.' >> whitespace
181+ string "bq" >> attributes >> char '.' >> whitespace
187 BlockQuote . singleton <$> para
182 BlockQuote . singleton <$> para
188
183
189-- Horizontal rule
184-- Horizontal rule
@@ -237,7 +232,7 @@ orderedListItemAtDepth = genericListItemAtDepth '#'
237-- | Common implementation of list items
232-- | Common implementation of list items
238genericListItemAtDepth :: Char -> Int -> Parser [Char] ParserState [Block]
233genericListItemAtDepth :: Char -> Int -> Parser [Char] ParserState [Block]
239genericListItemAtDepth c depth = try $ do
234genericListItemAtDepth c depth = try $ do
240− count depth (char c) >> optional attributes >> whitespace
235+ count depth (char c) >> attributes >> whitespace
241 p <- many listInline
236 p <- many listInline
242 newline
237 newline
243 sublist <- option [] (singleton <$> anyListAtDepth (depth + 1))
238 sublist <- option [] (singleton <$> anyListAtDepth (depth + 1))
@@ -348,7 +343,7 @@ maybeExplicitBlock :: String -- ^ block tag name
348 -> Parser [Char] ParserState Block -- ^ implicit block
343 -> Parser [Char] ParserState Block -- ^ implicit block
349 -> Parser [Char] ParserState Block
344 -> Parser [Char] ParserState Block
350maybeExplicitBlock name blk = try $ do
345maybeExplicitBlock name blk = try $ do
351− optional $ try $ string name >> optional attributes >> char '.' >>
346+ optional $ try $ string name >> attributes >> char '.' >>
352 optional whitespace >> optional endline
347 optional whitespace >> optional endline
353 blk
348 blk
354
349
@@ -553,10 +548,32 @@ code2 = do
553 Code nullAttr <$> manyTill anyChar (try $ htmlTag $ tagClose (=="tt"))
548 Code nullAttr <$> manyTill anyChar (try $ htmlTag $ tagClose (=="tt"))
554
549
555-- | Html / CSS attributes
550-- | Html / CSS attributes
556−attributes :: Parser [Char] ParserState String
551+attributes :: Parser [Char] ParserState Attr
557−attributes = choice [ enclosed (char '(') (char ')') anyChar,
552+attributes = (foldl (flip ($)) ("",[],[])) `fmap` many attribute
553+
554+attribute :: Parser [Char] ParserState (Attr -> Attr)
555+attribute = classIdAttr <|> styleAttr <|> langAttr
556+
557+classIdAttr :: Parser [Char] ParserState (Attr -> Attr)
558+classIdAttr = try $ do -- (class class #id)
559+ char '('
560+ ws <- words `fmap` manyTill anyChar (char ')')
561+ case reverse ws of
562+ [] -> return $ \(_,_,keyvals) -> ("",[],keyvals)
563+ (('#':ident'):classes') -> return $ \(_,_,keyvals) ->
564+ (ident',classes',keyvals)
565+ classes' -> return $ \(_,_,keyvals) ->
566+ ("",classes',keyvals)
567+
568+styleAttr :: Parser [Char] ParserState (Attr -> Attr)
569+styleAttr = do
558− enclosed (char '{') (char '}') anyChar,
570+ style <- try $ enclosed (char '{') (char '}') anyChar
571+ return $ \(id',classes,keyvals) -> (id',classes,("style",style):keyvals)
572+
573+langAttr :: Parser [Char] ParserState (Attr -> Attr)
574+langAttr = do
559− enclosed (char '[') (char ']') anyChar]
575+ lang <- try $ enclosed (char '[') (char ']') anyChar
576+ return $ \(id',classes,keyvals) -> (id',classes,("lang",lang):keyvals)
560
577
561-- | Parses material surrounded by a parser.
578-- | Parses material surrounded by a parser.
562surrounded :: Parser [Char] st t -- ^ surrounding parser
579surrounded :: Parser [Char] st t -- ^ surrounding parser
tests/textile-reader.native+1−1
@@ -123,7 +123,7 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
123,Header 1 ("",[],[]) [Str "Images"]
123,Header 1 ("",[],[]) [Str "Images"]
124,Para [Str "Textile",Space,Str "inline",Space,Str "image",Space,Str "syntax",Str ",",Space,Str "like",Space,LineBreak,Str "here",Space,Image [Str "this is the alt text"] ("this_is_an_image.png","this is the alt text"),LineBreak,Str "and",Space,Str "here",Space,Image [Str ""] ("this_is_an_image.png",""),Str "."]
124,Para [Str "Textile",Space,Str "inline",Space,Str "image",Space,Str "syntax",Str ",",Space,Str "like",Space,LineBreak,Str "here",Space,Image [Str "this is the alt text"] ("this_is_an_image.png","this is the alt text"),LineBreak,Str "and",Space,Str "here",Space,Image [Str ""] ("this_is_an_image.png",""),Str "."]
125,Header 1 ("",[],[]) [Str "Attributes"]
125,Header 1 ("",[],[]) [Str "Attributes"]
126−,Header 2 ("",[],[]) [Str "HTML",Space,Str "and",Space,Str "CSS",Space,Str "attributes",Space,Str "are",Space,Str "ignored"]
126+,Header 2 ("ident",["bar","foo"],[("style","color:red"),("lang","en")]) [Str "HTML",Space,Str "and",Space,Str "CSS",Space,Str "attributes",Space,Str "are",Space,Str "parsed",Space,Str "in",Space,Str "headers",Str "."]
127,Para [Str "as",Space,Str "well",Space,Str "as",Space,Strong [Str "inline",Space,Str "attributes"],Space,Str "of",Space,Str " all kind"]
127,Para [Str "as",Space,Str "well",Space,Str "as",Space,Strong [Str "inline",Space,Str "attributes"],Space,Str "of",Space,Str " all kind"]
128,Para [Str "and",Space,Str "paragraph",Space,Str "attributes",Str ",",Space,Str "and",Space,Str "table",Space,Str "attributes",Str "."]
128,Para [Str "and",Space,Str "paragraph",Space,Str "attributes",Str ",",Space,Str "and",Space,Str "table",Space,Str "attributes",Str "."]
129,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0]
129,Table [] [AlignDefault,AlignDefault,AlignDefault] [0.0,0.0,0.0]
tests/textile-reader.textile+1−1
@@ -193,7 +193,7 @@ and here !this_is_an_image.png!.
193
193
194h1. Attributes
194h1. Attributes
195
195
196−h2{color:red}. HTML and CSS attributes are ignored
196+h2[en]{color:red}(foo bar #ident). HTML and CSS attributes are parsed in headers.
197
197
198as well as *(foo)inline attributes* of %{color:red} all kind%
198as well as *(foo)inline attributes* of %{color:red} all kind%
199
199