git.iko.soy/typeable/pandoc
on master help sign in
Textile reader: Handle attributes on headers.

Includes `[lang]`, `(class #id)`, `{color:red}` styles.

commit5e9145bb626f9f96ef956f52ceb47ab69f8d6844 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSat 16 Feb 2013, 18:29 (14 years ago) parent8dd00b9
3 files changed+34−17
src/Text/Pandoc/Readers/Textile.hs+32−15 tests/textile-reader.native+1−1 tests/textile-reader.textile+1−1
src/Text/Pandoc/Readers/Textile.hs+32−15
@@ -36,9 +36,7 @@ Implemented and parsed:
3636 - Inlines : strong, emph, cite, code, deleted, superscript,
3737 subscript, links
3838 - footnotes
39−
40−Implemented but discarded:
41− - HTML-specific and CSS-specific attributes
39+ - HTML-specific and CSS-specific attributes on headers
4240
4341Left to be implemented:
4442 - dimension sign
@@ -171,19 +169,16 @@ header :: Parser [Char] ParserState Block
171169header = try $ do
172170 char 'h'
173171 level <- digitToInt <$> oneOf "123456"
174− attr <- option "" attributes
175− let ident = case attr of
176− '#':xs -> xs
177− _ -> ""
172+ attr <- attributes
178173 char '.'
179174 whitespace
180175 name <- normalizeSpaces <$> manyTill inline blockBreak
181− return $ Header level (ident,[],[]) name
176+ return $ Header level attr name
182177
183178-- | Blockquote of the form "bq. content"
184179blockQuote :: Parser [Char] ParserState Block
185180blockQuote = try $ do
186− string "bq" >> optional attributes >> char '.' >> whitespace
181+ string "bq" >> attributes >> char '.' >> whitespace
187182 BlockQuote . singleton <$> para
188183
189184-- Horizontal rule
@@ -237,7 +232,7 @@ orderedListItemAtDepth = genericListItemAtDepth '#'
237232-- | Common implementation of list items
238233genericListItemAtDepth :: Char -> Int -> Parser [Char] ParserState [Block]
239234genericListItemAtDepth c depth = try $ do
240− count depth (char c) >> optional attributes >> whitespace
235+ count depth (char c) >> attributes >> whitespace
241236 p <- many listInline
242237 newline
243238 sublist <- option [] (singleton <$> anyListAtDepth (depth + 1))
@@ -348,7 +343,7 @@ maybeExplicitBlock :: String -- ^ block tag name
348343 -> Parser [Char] ParserState Block -- ^ implicit block
349344 -> Parser [Char] ParserState Block
350345maybeExplicitBlock name blk = try $ do
351− optional $ try $ string name >> optional attributes >> char '.' >>
346+ optional $ try $ string name >> attributes >> char '.' >>
352347 optional whitespace >> optional endline
353348 blk
354349
@@ -553,10 +548,32 @@ code2 = do
553548 Code nullAttr <$> manyTill anyChar (try $ htmlTag $ tagClose (=="tt"))
554549
555550-- | Html / CSS attributes
556−attributes :: Parser [Char] ParserState String
557−attributes = choice [ enclosed (char '(') (char ')') anyChar,
558− enclosed (char '{') (char '}') anyChar,
559− enclosed (char '[') (char ']') anyChar]
551+attributes :: Parser [Char] ParserState Attr
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
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
575+ lang <- try $ enclosed (char '[') (char ']') anyChar
576+ return $ \(id',classes,keyvals) -> (id',classes,("lang",lang):keyvals)
560577
561578-- | Parses material surrounded by a parser.
562579surrounded :: Parser [Char] st t -- ^ surrounding parser
tests/textile-reader.native+1−1
@@ -123,7 +123,7 @@ Pandoc (Meta {docTitle = [], docAuthors = [], docDate = []})
123123,Header 1 ("",[],[]) [Str "Images"]
124124,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 "."]
125125,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 "."]
127127,Para [Str "as",Space,Str "well",Space,Str "as",Space,Strong [Str "inline",Space,Str "attributes"],Space,Str "of",Space,Str " all kind"]
128128,Para [Str "and",Space,Str "paragraph",Space,Str "attributes",Str ",",Space,Str "and",Space,Str "table",Space,Str "attributes",Str "."]
129129,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!.
193193
194194h1. Attributes
195195
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.
197197
198198as well as *(foo)inline attributes* of %{color:red} all kind%
199199