git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: Support header attributes.

Note: The attributes go on the enclosing section or div if `--section-divs` is specified. Also fixed a regression (only now noticed) in html+lhs output. Previously the bird tracks were being omitted.

commit90f0dd15b6a986e1aa04b455b27601f3a7ca58a1 authorJohn MacFarlane <fiddlosopher@gmail.com> dateThu 14 Feb 2013, 19:35 (14 years ago) parentcdee226
4 files changed+29−26
src/Text/Pandoc/Writers/HTML.hs+22−19 tests/lhs-test.html+lhs+3−3 tests/s5.basic.html+2−2 tests/s5.fancy.html+2−2
src/Text/Pandoc/Writers/HTML.hs+22−19
@@ -250,9 +250,10 @@ showSecNum = concat . intersperse "." . map show
250250-- | Converts an Element to a list item for a table of contents,
251251-- retrieving the appropriate identifier from state.
252252elementToListItem :: WriterOptions -> Element -> State WriterState (Maybe Html)
253−elementToListItem opts (Sec lev num (id',classes,keyvals) headerText subsecs)
253+elementToListItem opts (Sec lev num (id',classes,_) headerText subsecs)
254254 | lev <= writerTOCDepth opts = do
255− let sectnum = if writerNumberSections opts && not (null num)
255+ let sectnum = if writerNumberSections opts && not (null num) &&
256+ "unnumbered" `notElem` classes
256257 then (H.span ! A.class_ "toc-section-number"
257258 $ toHtml $ showSecNum num) >> preEscapedString " "
258259 else mempty
@@ -287,22 +288,24 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
287288 -- title slides have no content of their own
288289 then filter isSec elements
289290 else elements
290− let header'' = if (writerSectionDivs opts ||
291− writerSlideVariant opts == S5Slides ||
292− slide)
293− then header'
294− else header' ! prefixedId opts id'
295291 let inNl x = mconcat $ nl opts : intersperse (nl opts) x ++ [nl opts]
296− let classes = ["titleslide" | titleSlide] ++ ["slide" | slide] ++
297− ["level" ++ show level]
292+ let classes' = ["titleslide" | titleSlide] ++ ["slide" | slide] ++
293+ ["section" | (slide || writerSectionDivs opts) &&
294+ not (writerHtml5 opts) ] ++
295+ ["level" ++ show level | slide || writerSectionDivs opts ]
296+ ++ classes
298297 let secttag = if writerHtml5 opts
299− then H5.section ! A.class_ (toValue $ unwords classes)
300− else H.div ! A.class_ (toValue $ unwords ("section":classes))
298+ then H5.section
299+ else H.div
300+ let attr = (id',classes',keyvals)
301301 return $ if titleSlide
302− then mconcat $ (secttag ! prefixedId opts id' $ header'') : innerContents
302+ then mconcat $
303+ (addAttrs opts attr $ secttag $ header') : innerContents
303304 else if writerSectionDivs opts || slide
304− then secttag ! prefixedId opts id' $ inNl $ header'' : innerContents
305− else mconcat $ intersperse (nl opts) $ header'' : innerContents
305+ then addAttrs opts attr
306+ $ secttag $ inNl $ header' : innerContents
307+ else mconcat $ intersperse (nl opts)
308+ $ addAttrs opts attr header' : innerContents
306309
307310-- | Convert list of Note blocks to a footnote <div>.
308311-- Assumes notes are sorted.
@@ -376,8 +379,8 @@ addAttrs opts attr h = foldl (!) h (attrsToHtml opts attr)
376379
377380attrsToHtml :: WriterOptions -> Attr -> [Attribute]
378381attrsToHtml opts (id',classes',keyvals) =
379− [A.class_ (toValue $ unwords classes') | not (null classes')] ++
380382 [prefixedId opts id' | not (null id')] ++
383+ [A.class_ (toValue $ unwords classes') | not (null classes')] ++
381384 map (\(x,y) -> customAttribute (fromString x) (toValue y)) keyvals
382385
383386imageExts :: [String]
@@ -423,11 +426,11 @@ blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
423426 then map (\c -> if map toLower c == "haskell"
424427 then "literatehaskell"
425428 else c) classes
426− else filter (/= "literate") classes
429+ else classes
427430 adjCode = if tolhs
428431 then unlines . map ("> " ++) . lines $ rawCode
429432 else rawCode
430− case highlight formatHtmlBlock (id',classes,keyvals) adjCode of
433+ case highlight formatHtmlBlock (id',classes',keyvals) adjCode of
431434 Nothing -> return $ addAttrs opts (id',classes,keyvals)
432435 $ H.pre $ H.code $ toHtml adjCode
433436 Just h -> modify (\st -> st{ stHighlighting = True }) >>
@@ -461,14 +464,14 @@ blockToHtml opts (Header level (ident,_,_) lst) = do
461464 then H.a ! A.href (toValue $
462465 '#' : writerIdentifierPrefix opts ++ ident) $ contents'
463466 else contents'
464− return $ (case level of
467+ return $ case level of
465468 1 -> H.h1 contents''
466469 2 -> H.h2 contents''
467470 3 -> H.h3 contents''
468471 4 -> H.h4 contents''
469472 5 -> H.h5 contents''
470473 6 -> H.h6 contents''
471− _ -> H.p contents'')
474+ _ -> H.p contents''
472475blockToHtml opts (BulletList lst) = do
473476 contents <- mapM (blockListToHtml opts) lst
474477 let lst' = unordList opts contents
tests/lhs-test.html+lhs+3−3
@@ -29,9 +29,9 @@ code > span.er { color: #ff0000; font-weight: bold; }
2929<body>
3030<h1>lhs test</h1>
3131<p><code>unsplit</code> is an arrow that takes a pair of values and combines them to return a single value:</p>
32−<pre class="sourceCode literate haskell"><code class="sourceCode haskell"><span class="fu">&gt;</span><span class="ot"> unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d
33−<span class="fu">&gt;</span> unsplit <span class="fu">=</span> arr <span class="fu">.</span> <span class="fu">uncurry</span>
34−<span class="fu">&gt;</span> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></code></pre>
32+<pre class="sourceCode literate literatehaskell"><code class="sourceCode literatehaskell"><span class="ot">&gt; unsplit ::</span> (<span class="dt">Arrow</span> a) <span class="ot">=&gt;</span> (b <span class="ot">-&gt;</span> c <span class="ot">-&gt;</span> d) <span class="ot">-&gt;</span> a (b, c) d
33+<span class="ot">&gt;</span> unsplit <span class="fu">=</span> arr <span class="fu">.</span> <span class="fu">uncurry</span>
34+<span class="ot">&gt;</span> <span class="co">-- arr (\op (x,y) -&gt; x `op` y)</span></code></pre>
3535<p><code>(***)</code> combines two arrows into a new arrow by running the two arrows on a pair of values (one arrow on the first item of the pair and one arrow on the second item of the pair).</p>
3636<pre><code>f *** g = first f &gt;&gt;&gt; second g</code></pre>
3737<p>Block quote:</p>
tests/s5.basic.html+2−2
@@ -36,14 +36,14 @@
3636 <h2>Sam Smith<br/>Jen Jones</h2>
3737 <h3>July 15, 2006</h3>
3838</div>
39−<div class="section slide level1" id="first-slide">
39+<div id="first-slide" class="slide section level1">
4040<h1>First slide</h1>
4141<ul>
4242<li>first bullet</li>
4343<li>second bullet</li>
4444</ul>
4545</div>
46−<div class="section slide level1" id="math">
46+<div id="math" class="slide section level1">
4747<h1>Math</h1>
4848<ul>
4949<li><span class="math">$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$</span></li>
tests/s5.fancy.html+2−2
@@ -237,14 +237,14 @@
237237 <h2>Sam Smith<br/>Jen Jones</h2>
238238 <h3>July 15, 2006</h3>
239239</div>
240−<div class="section slide level1" id="first-slide">
240+<div id="first-slide" class="slide section level1">
241241<h1>First slide</h1>
242242<ul class="incremental">
243243<li>first bullet</li>
244244<li>second bullet</li>
245245</ul>
246246</div>
247−<div class="section slide level1" id="math">
247+<div id="math" class="slide section level1">
248248<h1>Math</h1>
249249<ul class="incremental">
250250<li><span class="LaTeX">$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$</span></li>