git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: Make header attributes work outside top level.
e16683bJohn MacFarlane
src/Text/Pandoc/Writers/HTML.hs+14−4
@@ -73,11 +73,13 @@ data WriterState = WriterState
73 , stQuotes :: Bool -- ^ <q> tag is used
73 , stQuotes :: Bool -- ^ <q> tag is used
74 , stHighlighting :: Bool -- ^ Syntax highlighting is used
74 , stHighlighting :: Bool -- ^ Syntax highlighting is used
75 , stSecNum :: [Int] -- ^ Number of current section
75 , stSecNum :: [Int] -- ^ Number of current section
76+ , stElement :: Bool -- ^ Processing an Element
76 }
77 }
77
78
78defaultWriterState :: WriterState
79defaultWriterState :: WriterState
79defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False,
80defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False,
80− stHighlighting = False, stSecNum = []}
81+ stHighlighting = False, stSecNum = [],
82+ stElement = False}
81
83
82-- Helpers to render HTML with the appropriate function.
84-- Helpers to render HTML with the appropriate function.
83
85
@@ -280,7 +282,13 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
280 let titleSlide = slide && level < slideLevel
282 let titleSlide = slide && level < slideLevel
281 header' <- if title' == [Str "\0"] -- marker for hrule
283 header' <- if title' == [Str "\0"] -- marker for hrule
282 then return mempty
284 then return mempty
285+ else do
286+ modify (\st -> st{ stElement = True})
287+ res <- blockToHtml opts
283− else blockToHtml opts (Header level' (id',classes,keyvals) title')
288+ (Header level' (id',classes,keyvals) title')
289+ modify (\st -> st{ stElement = False})
290+ return res
291+
284 let isSec (Sec _ _ _ _ _) = True
292 let isSec (Sec _ _ _ _ _) = True
285 isSec (Blk _) = False
293 isSec (Blk _) = False
286 let isPause (Blk x) = x == Para [Str ".",Space,Str ".",Space,Str "."]
294 let isPause (Blk x) = x == Para [Str ".",Space,Str ".",Space,Str "."]
@@ -491,7 +499,7 @@ blockToHtml opts (BlockQuote blocks) =
491 else do
499 else do
492 contents <- blockListToHtml opts blocks
500 contents <- blockListToHtml opts blocks
493 return $ H.blockquote $ nl opts >> contents >> nl opts
501 return $ H.blockquote $ nl opts >> contents >> nl opts
494−blockToHtml opts (Header level (_,classes,_) lst) = do
502+blockToHtml opts (Header level attr@(_,classes,_) lst) = do
495 contents <- inlineListToHtml opts lst
503 contents <- inlineListToHtml opts lst
496 secnum <- liftM stSecNum get
504 secnum <- liftM stSecNum get
497 let contents' = if writerNumberSections opts && not (null secnum)
505 let contents' = if writerNumberSections opts && not (null secnum)
@@ -499,7 +507,9 @@ blockToHtml opts (Header level (_,classes,_) lst) = do
499 then (H.span ! A.class_ "header-section-number" $ toHtml
507 then (H.span ! A.class_ "header-section-number" $ toHtml
500 $ showSecNum secnum) >> strToHtml " " >> contents
508 $ showSecNum secnum) >> strToHtml " " >> contents
501 else contents
509 else contents
510+ inElement <- gets stElement
511+ return $ (if inElement then id else addAttrs opts attr)
502− return $ case level of
512+ $ case level of
503 1 -> H.h1 contents'
513 1 -> H.h1 contents'
504 2 -> H.h2 contents'
514 2 -> H.h2 contents'
505 3 -> H.h3 contents'
515 3 -> H.h3 contents'
tests/markdown-reader-more.native+2
@@ -79,6 +79,8 @@
79,Para [Str "Link",Space,Str "to",Space,Link [Str "Explicit",Space,Str "header",Space,Str "attributes"] ("#foobar",""),Str "."]
79,Para [Str "Link",Space,Str "to",Space,Link [Str "Explicit",Space,Str "header",Space,Str "attributes"] ("#foobar",""),Str "."]
80,Para [Str "But",Space,Str "this",Space,Str "is",Space,Str "not",Space,Str "a",Space,Str "link",Space,Str "to",Space,Link [Str "My",Space,Str "other",Space,Str "header"] ("/foo",""),Str ",",Space,Str "since",Space,Str "the",Space,Str "reference",Space,Str "is",Space,Str "defined."]
80,Para [Str "But",Space,Str "this",Space,Str "is",Space,Str "not",Space,Str "a",Space,Str "link",Space,Str "to",Space,Link [Str "My",Space,Str "other",Space,Str "header"] ("/foo",""),Str ",",Space,Str "since",Space,Str "the",Space,Str "reference",Space,Str "is",Space,Str "defined."]
81,Header 2 ("foobar",["baz"],[("key","val")]) [Str "Explicit",Space,Str "header",Space,Str "attributes"]
81,Header 2 ("foobar",["baz"],[("key","val")]) [Str "Explicit",Space,Str "header",Space,Str "attributes"]
82+,BlockQuote
83+ [Header 2 ("foobar",["baz"],[("key","val")]) [Str "Header",Space,Str "attributes",Space,Str "inside",Space,Str "block",Space,Str "quote"]]
82,Header 2 ("line-blocks",[],[]) [Str "Line",Space,Str "blocks"]
84,Header 2 ("line-blocks",[],[]) [Str "Line",Space,Str "blocks"]
83,Para [Str "But",Space,Str "can",Space,Str "a",Space,Str "bee",Space,Str "be",Space,Str "said",Space,Str "to",Space,Str "be",LineBreak,Str "\160\160\160\160or",Space,Str "not",Space,Str "to",Space,Str "be",Space,Str "an",Space,Str "entire",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160when",Space,Str "half",Space,Str "the",Space,Str "bee",Space,Str "is",Space,Str "not",Space,Str "a",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160\160\160\160\160due",Space,Str "to",Space,Str "some",Space,Str "ancient",Space,Str "injury?"]
85,Para [Str "But",Space,Str "can",Space,Str "a",Space,Str "bee",Space,Str "be",Space,Str "said",Space,Str "to",Space,Str "be",LineBreak,Str "\160\160\160\160or",Space,Str "not",Space,Str "to",Space,Str "be",Space,Str "an",Space,Str "entire",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160when",Space,Str "half",Space,Str "the",Space,Str "bee",Space,Str "is",Space,Str "not",Space,Str "a",Space,Str "bee,",LineBreak,Str "\160\160\160\160\160\160\160\160\160\160\160\160due",Space,Str "to",Space,Str "some",Space,Str "ancient",Space,Str "injury?"]
84,Para [Str "Continuation",Space,Str "line",LineBreak,Str "\160\160and",Space,Str "another"]
86,Para [Str "Continuation",Space,Str "line",LineBreak,Str "\160\160and",Space,Str "another"]
tests/markdown-reader-more.txt+2
@@ -176,6 +176,8 @@ But this is not a link to [My other header], since the reference is defined.
176
176
177## Explicit header attributes {#foobar .baz key="val"}
177## Explicit header attributes {#foobar .baz key="val"}
178
178
179+> ## Header attributes inside block quote {#foobar .baz key="val"}
180+
179## Line blocks
181## Line blocks
180
182
181| But can a bee be said to be
183| But can a bee be said to be