git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: Make header attributes work outside top level.

Previously they only appeared on top level header elements. Now they work e.g. in blockquotes. Closes #1711.

commite16683b5394d03a09167036747a1fa8f064fe5a1 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 23 Oct 2014, 10:23 (12 years ago) parente4f3475
3 files changed+18−4
src/Text/Pandoc/Writers/HTML.hs+14−4 tests/markdown-reader-more.native+2 tests/markdown-reader-more.txt+2
src/Text/Pandoc/Writers/HTML.hs+14−4
@@ -73,11 +73,13 @@ data WriterState = WriterState
7373 , stQuotes :: Bool -- ^ <q> tag is used
7474 , stHighlighting :: Bool -- ^ Syntax highlighting is used
7575 , stSecNum :: [Int] -- ^ Number of current section
76+ , stElement :: Bool -- ^ Processing an Element
7677 }
7778
7879defaultWriterState :: WriterState
7980defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False,
80− stHighlighting = False, stSecNum = []}
81+ stHighlighting = False, stSecNum = [],
82+ stElement = False}
8183
8284-- Helpers to render HTML with the appropriate function.
8385
@@ -280,7 +282,13 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
280282 let titleSlide = slide && level < slideLevel
281283 header' <- if title' == [Str "\0"] -- marker for hrule
282284 then return mempty
283− else blockToHtml opts (Header level' (id',classes,keyvals) title')
285+ else do
286+ modify (\st -> st{ stElement = True})
287+ res <- blockToHtml opts
288+ (Header level' (id',classes,keyvals) title')
289+ modify (\st -> st{ stElement = False})
290+ return res
291+
284292 let isSec (Sec _ _ _ _ _) = True
285293 isSec (Blk _) = False
286294 let isPause (Blk x) = x == Para [Str ".",Space,Str ".",Space,Str "."]
@@ -491,7 +499,7 @@ blockToHtml opts (BlockQuote blocks) =
491499 else do
492500 contents <- blockListToHtml opts blocks
493501 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
495503 contents <- inlineListToHtml opts lst
496504 secnum <- liftM stSecNum get
497505 let contents' = if writerNumberSections opts && not (null secnum)
@@ -499,7 +507,9 @@ blockToHtml opts (Header level (_,classes,_) lst) = do
499507 then (H.span ! A.class_ "header-section-number" $ toHtml
500508 $ showSecNum secnum) >> strToHtml " " >> contents
501509 else contents
502− return $ case level of
510+ inElement <- gets stElement
511+ return $ (if inElement then id else addAttrs opts attr)
512+ $ case level of
503513 1 -> H.h1 contents'
504514 2 -> H.h2 contents'
505515 3 -> H.h3 contents'
tests/markdown-reader-more.native+2
@@ -79,6 +79,8 @@
7979,Para [Str "Link",Space,Str "to",Space,Link [Str "Explicit",Space,Str "header",Space,Str "attributes"] ("#foobar",""),Str "."]
8080,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."]
8181,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"]]
8284,Header 2 ("line-blocks",[],[]) [Str "Line",Space,Str "blocks"]
8385,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?"]
8486,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.
176176
177177## Explicit header attributes {#foobar .baz key="val"}
178178
179+> ## Header attributes inside block quote {#foobar .baz key="val"}
180+
179181## Line blocks
180182
181183| But can a bee be said to be