aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers/HTML.hs
blob: 216ce2d3327381ef20abb7a2d272cf00c1a42f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
{-# OPTIONS_GHC -fno-warn-deprecations #-}
{-
Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- |
   Module      : Text.Pandoc.Writers.HTML
   Copyright   : Copyright (C) 2006-2010 John MacFarlane
   License     : GNU GPL, version 2 or above

   Maintainer  : John MacFarlane <jgm@berkeley.edu>
   Stability   : alpha
   Portability : portable

Conversion of 'Pandoc' documents to HTML.
-}
module Text.Pandoc.Writers.HTML ( writeHtml , writeHtmlString ) where
import Text.Pandoc.Definition
import Text.Pandoc.CharacterReferences ( decodeCharacterReferences )
import Text.Pandoc.Shared
import Text.Pandoc.Templates
import Text.Pandoc.Readers.TeXMath
import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss )
import Text.Pandoc.XML (stripTags, escapeStringForXML)
import Network.HTTP ( urlEncode )
import Numeric ( showHex )
import Data.Char ( ord, toLower )
import Data.List ( isPrefixOf, intersperse )
import Data.Maybe ( catMaybes )
import Control.Monad.State
import Text.XHtml.Transitional hiding ( stringToHtml, unordList, ordList )
import qualified Text.XHtml.Transitional as XHtml
import Text.TeXMath
import Text.XML.Light.Output
import System.FilePath (takeExtension)

data WriterState = WriterState
    { stNotes            :: [Html]  -- ^ List of notes
    , stMath             :: Bool    -- ^ Math is used in document
    , stHighlighting     :: Bool    -- ^ Syntax highlighting is used
    , stSecNum           :: [Int]   -- ^ Number of current section
    } deriving Show

defaultWriterState :: WriterState
defaultWriterState = WriterState {stNotes= [], stMath = False, stHighlighting = False, stSecNum = []}

-- Helpers to render HTML with the appropriate function.

-- | Modified version of Text.XHtml's stringToHtml.
-- Use unicode characters wherever possible.
stringToHtml :: WriterOptions -> String -> Html
stringToHtml opts = if writerAscii opts
                       then XHtml.stringToHtml
                       else primHtml . escapeStringForXML

-- | Hard linebreak.
nl :: WriterOptions -> Html
nl opts = if writerWrapText opts
             then primHtml "\n"
             else noHtml

-- | Convert Pandoc document to Html string.
writeHtmlString :: WriterOptions -> Pandoc -> String
writeHtmlString opts d =
  let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
                                                 defaultWriterState
  in  if writerStandalone opts
         then inTemplate opts tit auths date toc body' newvars
         else dropWhile (=='\n') $ showHtmlFragment body'

-- | Convert Pandoc document to Html structure.
writeHtml :: WriterOptions -> Pandoc -> Html
writeHtml opts d =
  let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
                                                 defaultWriterState
  in  if writerStandalone opts
         then inTemplate opts tit auths date toc body' newvars
         else body'

-- result is (title, authors, date, toc, body, new variables)
pandocToHtml :: WriterOptions
             -> Pandoc
             -> State WriterState (Html, [Html], Html, Maybe Html, Html, [(String,String)])
pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
  let standalone = writerStandalone opts
  tit <- if standalone
            then inlineListToHtml opts title'
            else return noHtml
  auths <- if standalone
              then mapM (inlineListToHtml opts) authors'
              else return []
  date <- if standalone
             then inlineListToHtml opts date'
             else return noHtml
  let splitHrule (HorizontalRule : Header 1 xs : ys)
                                       = Header 1 xs : splitHrule ys
      splitHrule (HorizontalRule : xs) = Header 1 [] : splitHrule xs
      splitHrule (x : xs)              = x : splitHrule xs
      splitHrule []                    = []
  let ensureStartWithH1 bs@(Header 1 _:_) = bs
      ensureStartWithH1 bs                = Header 1 [] : bs
  let sects = hierarchicalize $
              if writerSlideVariant opts == NoSlides
                 then blocks
                 else ensureStartWithH1 $ splitHrule blocks
  toc <- if writerTableOfContents opts
            then tableOfContents opts sects
            else return Nothing
  blocks' <- liftM (toHtmlFromList . intersperse (nl opts)) $
                 mapM (elementToHtml opts) sects
  st <- get
  let notes = reverse (stNotes st)
  let thebody = blocks' +++ footnoteSection opts notes
  let  math = if stMath st
                then case writerHTMLMathMethod opts of
                           LaTeXMathML (Just url) ->
                              script !
                              [src url, thetype "text/javascript"] $ noHtml
                           MathML (Just url) ->
                              script !
                              [src url, thetype "text/javascript"] $ noHtml
                           MathJax url ->
                              script ! [src url, thetype "text/javascript"] $ noHtml
                           JsMath (Just url) ->
                              script !
                              [src url, thetype "text/javascript"] $ noHtml
                           _ -> case lookup "mathml-script" (writerVariables opts) of
                                      Just s ->
                                        script ! [thetype "text/javascript"] <<
                                           primHtml ("/*<![CDATA[*/\n" ++ s ++
                                                     "/*]]>*/\n")
                                      Nothing -> noHtml
                else noHtml
  let newvars = [("highlighting-css", defaultHighlightingCss) |
                   stHighlighting st] ++
                [("math", showHtmlFragment math) | stMath st]
  return (tit, auths, date, toc, thebody, newvars)

inTemplate :: TemplateTarget a
           => WriterOptions
           -> Html
           -> [Html]
           -> Html
           -> Maybe Html
           -> Html
           -> [(String,String)]
           -> a
inTemplate opts tit auths date toc body' newvars =
  let renderedTit = showHtmlFragment tit
      topTitle'   = stripTags renderedTit
      authors     = map (stripTags . showHtmlFragment) auths
      date'       = stripTags $ showHtmlFragment date
      variables   = writerVariables opts ++ newvars
      context     = variables ++
                    [ ("body", dropWhile (=='\n') $ showHtmlFragment body')
                    , ("pagetitle", topTitle')
                    , ("title", dropWhile (=='\n') $ showHtmlFragment tit)
                    , ("date", date')
                    , ("idprefix", writerIdentifierPrefix opts)
                    , ("slidy-url", "http://www.w3.org/Talks/Tools/Slidy2")
                    , ("s5-url", "s5/default") ] ++
                    [ ("html5","true") | writerHtml5 opts ] ++
                    (case toc of
                         Just t  -> [ ("toc", showHtmlFragment t)]
                         Nothing -> [])  ++
                    [ ("author", a) | a <- authors ]
  in  renderTemplate context $ writerTemplate opts

-- | Like Text.XHtml's identifier, but adds the writerIdentifierPrefix
prefixedId :: WriterOptions -> String -> HtmlAttr
prefixedId opts s = identifier $ writerIdentifierPrefix opts ++ s

-- | Replacement for Text.XHtml's unordList.
unordList :: WriterOptions -> ([Html] -> Html)
unordList opts items = ulist << toListItems opts items

-- | Replacement for Text.XHtml's ordList.
ordList :: WriterOptions -> ([Html] -> Html)
ordList opts items = olist << toListItems opts items

-- | Construct table of contents from list of elements.
tableOfContents :: WriterOptions -> [Element] -> State WriterState (Maybe Html)
tableOfContents _ [] = return Nothing
tableOfContents opts sects = do
  let opts'        = opts { writerIgnoreNotes = True }
  contents  <- mapM (elementToListItem opts') sects
  let tocList = catMaybes contents
  return $ if null tocList
              then Nothing
              else Just $ unordList opts tocList

-- | Convert section number to string
showSecNum :: [Int] -> String
showSecNum = concat . intersperse "." . map show

-- | Converts an Element to a list item for a table of contents,
-- retrieving the appropriate identifier from state.
elementToListItem :: WriterOptions -> Element -> State WriterState (Maybe Html)
elementToListItem _ (Blk _) = return Nothing
elementToListItem opts (Sec _ num id' headerText subsecs) = do
  let sectnum = if writerNumberSections opts
                   then (thespan ! [theclass "toc-section-number"] << showSecNum num) +++
                         stringToHtml opts" "
                   else noHtml
  txt <- liftM (sectnum +++) $ inlineListToHtml opts headerText
  subHeads <- mapM (elementToListItem opts) subsecs >>= return . catMaybes
  let subList = if null subHeads
                   then noHtml
                   else unordList opts subHeads
  return $ Just $ (anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ id')] $ txt) +++ subList

-- | Convert an Element to Html.
elementToHtml :: WriterOptions -> Element -> State WriterState Html
elementToHtml opts (Blk block) = blockToHtml opts block
elementToHtml opts (Sec level num id' title' elements) = do
  modify $ \st -> st{stSecNum = num}  -- update section number
  header' <- blockToHtml opts (Header level title')
  innerContents <- mapM (elementToHtml opts) elements
  let header'' = header' !  [prefixedId opts id' |
                             not (writerStrictMarkdown opts ||
                                  writerSectionDivs opts ||
                                  writerSlideVariant opts == S5Slides)]
  let stuff = header'' : innerContents
  let slide = writerSlideVariant opts /= NoSlides && level == 1
  let titleSlide = slide && null elements
  let attrs = [prefixedId opts id' | writerSectionDivs opts] ++
              [theclass "titleslide" | titleSlide] ++
              [theclass "slide" | slide]
  let inNl x = nl opts : intersperse (nl opts) x ++ [nl opts]
  return $ if writerSectionDivs opts || slide
              then if writerHtml5 opts
                      then tag "section" ! attrs << inNl stuff
                      else thediv ! attrs << inNl stuff
              else toHtmlFromList $ intersperse (nl opts) stuff

-- | Convert list of Note blocks to a footnote <div>.
-- Assumes notes are sorted.
footnoteSection :: WriterOptions -> [Html] -> Html
footnoteSection opts notes =
  if null notes
     then noHtml
     else nl opts +++ (container
          $ nl opts +++ hr +++ nl opts +++
            (olist << (notes ++ [nl opts])) +++ nl opts)
   where container = if writerHtml5 opts
                        then tag "section" ! [theclass "footnotes"]
                        else thediv ! [theclass "footnotes"]

-- | Parse a mailto link; return Just (name, domain) or Nothing.
parseMailto :: String -> Maybe (String, String)
parseMailto ('m':'a':'i':'l':'t':'o':':':addr) =
  let (name', rest) = span (/='@') addr
      domain = drop 1 rest
  in  Just (name', domain)
parseMailto _ = Nothing

-- | Obfuscate a "mailto:" link.
obfuscateLink :: WriterOptions -> String -> String -> Html
obfuscateLink opts txt s | writerEmailObfuscation opts == NoObfuscation =
  anchor ! [href s] << txt
obfuscateLink opts txt s =
  let meth = writerEmailObfuscation opts
      s' = map toLower s
  in  case parseMailto s' of
        (Just (name', domain)) ->
          let domain'  = substitute "." " dot " domain
              at'      = obfuscateChar '@'
              (linkText, altText) =
                 if txt == drop 7 s' -- autolink
                    then ("'<code>'+e+'</code>'", name' ++ " at " ++ domain')
                    else ("'" ++ txt ++ "'", txt ++ " (" ++ name' ++ " at " ++
                          domain' ++ ")")
          in  case meth of
                ReferenceObfuscation ->
                     -- need to use primHtml or &'s are escaped to &amp; in URL
                     primHtml $ "<a href=\"" ++ (obfuscateString s')
                     ++ "\">" ++ (obfuscateString txt) ++ "</a>"
                JavascriptObfuscation ->
                     (script ! [thetype "text/javascript"] $
                     primHtml ("\n<!--\nh='" ++
                     obfuscateString domain ++ "';a='" ++ at' ++ "';n='" ++
                     obfuscateString name' ++ "';e=n+a+h;\n" ++
                     "document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+" ++
                     linkText  ++ "+'<\\/'+'a'+'>');\n// -->\n")) +++
                     noscript (primHtml $ obfuscateString altText)
                _ -> error $ "Unknown obfuscation method: " ++ show meth
        _ -> anchor ! [href s] $ stringToHtml opts txt  -- malformed email

-- | Obfuscate character as entity.
obfuscateChar :: Char -> String
obfuscateChar char =
  let num    = ord char
      numstr = if even num then show num else "x" ++ showHex num ""
  in  "&#" ++ numstr ++ ";"

-- | Obfuscate string using entities.
obfuscateString :: String -> String
obfuscateString = concatMap obfuscateChar . decodeCharacterReferences

attrsToHtml :: WriterOptions -> Attr -> [HtmlAttr]
attrsToHtml opts (id',classes',keyvals) =
  [theclass (unwords classes') | not (null classes')] ++
  [prefixedId opts id' | not (null id')] ++
  map (\(x,y) -> strAttr x y) keyvals

imageExts :: [String]
imageExts = [ "art", "bmp", "cdr", "cdt", "cpt", "cr2", "crw", "djvu", "erf",
              "gif", "ico", "ief", "jng", "jpg", "jpeg", "nef", "orf", "pat", "pbm",
              "pcx", "pgm", "png", "pnm", "ppm", "psd", "ras", "rgb", "svg", "tiff",
              "wbmp", "xbm", "xpm", "xwd" ]

treatAsImage :: FilePath -> Bool
treatAsImage fp =
  let ext = map toLower $ drop 1 $ takeExtension fp
  in  null ext || ext `elem` imageExts

-- | Convert Pandoc block element to HTML.
blockToHtml :: WriterOptions -> Block -> State WriterState Html
blockToHtml _ Null = return noHtml
blockToHtml opts (Plain lst) = inlineListToHtml opts lst
blockToHtml opts (Para [Image txt (s,tit)]) = do
  img <- inlineToHtml opts (Image txt (s,tit))
  capt <- inlineListToHtml opts txt
  return $ if writerHtml5 opts
              then tag "figure" <<
                    [nl opts, img, tag "figcaption" << capt, nl opts]
              else thediv ! [theclass "figure"] <<
                    [nl opts, img, paragraph ! [theclass "caption"] << capt,
                    nl opts]
blockToHtml opts (Para lst) = do
  contents <- inlineListToHtml opts lst
  return $ paragraph contents
blockToHtml _ (RawBlock "html" str) = return $ primHtml str
blockToHtml _ (RawBlock _ _) = return noHtml
blockToHtml _ (HorizontalRule) = return hr
blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
  let classes' = if writerLiterateHaskell opts
                    then classes
                    else filter (/= "literate") classes
  case highlightHtml False (id',classes',keyvals) rawCode of
         Left _  -> -- change leading newlines into <br /> tags, because some
                    -- browsers ignore leading newlines in pre blocks
                    let (leadingBreaks, rawCode') = span (=='\n') rawCode
                        attrs = attrsToHtml opts (id', classes', keyvals)
                        addBird = if "literate" `elem` classes'
                                     then unlines . map ("> " ++) . lines
                                     else unlines . lines
                    in  return $ pre ! attrs $ thecode <<
                                 (replicate (length leadingBreaks) br +++
                                 [stringToHtml opts $ addBird rawCode'])
         Right h -> modify (\st -> st{ stHighlighting = True }) >>
                    return h
blockToHtml opts (BlockQuote blocks) =
  -- in S5, treat list in blockquote specially
  -- if default is incremental, make it nonincremental;
  -- otherwise incremental
  if writerSlideVariant opts /= NoSlides
     then let inc = not (writerIncremental opts) in
          case blocks of
             [BulletList lst]  -> blockToHtml (opts {writerIncremental = inc})
                                  (BulletList lst)
             [OrderedList attribs lst] ->
                                  blockToHtml (opts {writerIncremental = inc})
                                  (OrderedList attribs lst)
             _                 -> do contents <- blockListToHtml opts blocks
                                     return $ blockquote (nl opts +++
                                               contents +++ nl opts)
     else do
       contents <- blockListToHtml opts blocks
       return $ blockquote (nl opts +++ contents +++ nl opts)
blockToHtml opts (Header level lst) = do
  contents <- inlineListToHtml opts lst
  secnum <- liftM stSecNum get
  let contents' = if writerNumberSections opts
                     then (thespan ! [theclass "header-section-number"] << showSecNum secnum) +++
                            stringToHtml opts " " +++ contents
                     else contents
  let contents''  = if writerTableOfContents opts
                       then anchor ! [href $ "#" ++ writerIdentifierPrefix opts ++ "TOC"] $ contents'
                       else contents'
  return $ (case level of
              1 -> h1 contents''
              2 -> h2 contents''
              3 -> h3 contents''
              4 -> h4 contents''
              5 -> h5 contents''
              6 -> h6 contents''
              _ -> paragraph contents'')
blockToHtml opts (BulletList lst) = do
  contents <- mapM (blockListToHtml opts) lst
  let attribs = if writerIncremental opts
                   then [theclass "incremental"]
                   else []
  return $ (unordList opts contents) ! attribs
blockToHtml opts (OrderedList (startnum, numstyle, _) lst) = do
  contents <- mapM (blockListToHtml opts) lst
  let numstyle' = camelCaseToHyphenated $ show numstyle
  let attribs = (if writerIncremental opts
                   then [theclass "incremental"]
                   else []) ++
                (if startnum /= 1
                   then [start startnum]
                   else []) ++
                (if numstyle /= DefaultStyle
                   then if writerHtml5 opts
                           then [strAttr "type" $
                                 case numstyle of
                                      Decimal    -> "1"
                                      LowerAlpha -> "a"
                                      UpperAlpha -> "A"
                                      LowerRoman -> "i"
                                      UpperRoman -> "I"
                                      _          -> "1"]
                           else [thestyle $ "list-style-type: " ++
                                   numstyle']
                   else [])
  return $ (ordList opts contents) ! attribs
blockToHtml opts (DefinitionList lst) = do
  contents <- mapM (\(term, defs) ->
                  do term' <- liftM (dterm <<) $ inlineListToHtml opts term
                     defs' <- mapM ((liftM (\x -> ddef << (x +++ nl opts))) .
                                    blockListToHtml opts) defs
                     return $ nl opts : term' : nl opts : defs') lst
  let attribs = if writerIncremental opts
                   then [theclass "incremental"]
                   else []
  return $ dlist ! attribs << (concat contents +++ nl opts)
blockToHtml opts (Table capt aligns widths headers rows') = do
  captionDoc <- if null capt
                   then return noHtml
                   else do
                     cs <- inlineListToHtml opts capt
                     return $ caption cs +++ nl opts
  let percent w = show (truncate (100*w) :: Integer) ++ "%"
  let widthAttrs w = if writerHtml5 opts
                        then [thestyle $ "width: " ++ percent w]
                        else [width $ percent w]
  let coltags = if all (== 0.0) widths
                   then noHtml
                   else concatHtml $ map
                         (\w -> (col ! (widthAttrs w)) noHtml +++ nl opts)
                         widths
  head' <- if all null headers
              then return noHtml
              else do
                contents <- tableRowToHtml opts aligns 0 headers
                return $ thead << (nl opts +++ contents) +++ nl opts
  body' <- liftM (\x -> tbody << (nl opts +++ x)) $
               zipWithM (tableRowToHtml opts aligns) [1..] rows'
  return $ table $ nl opts +++ captionDoc +++ coltags +++ head' +++
                   body' +++ nl opts

tableRowToHtml :: WriterOptions
               -> [Alignment]
               -> Int
               -> [[Block]]
               -> State WriterState Html
tableRowToHtml opts aligns rownum cols' = do
  let mkcell = if rownum == 0 then th else td
  let rowclass = case rownum of
                      0                  -> "header"
                      x | x `rem` 2 == 1 -> "odd"
                      _                  -> "even"
  cols'' <- sequence $ zipWith
            (\alignment item -> tableItemToHtml opts mkcell alignment item)
            aligns cols'
  return $ (tr ! [theclass rowclass] $ nl opts +++ toHtmlFromList cols'')
          +++ nl opts

alignmentToString :: Alignment -> [Char]
alignmentToString alignment = case alignment of
                                 AlignLeft    -> "left"
                                 AlignRight   -> "right"
                                 AlignCenter  -> "center"
                                 AlignDefault -> "left"

tableItemToHtml :: WriterOptions
                -> (Html -> Html)
                -> Alignment
                -> [Block]
                -> State WriterState Html
tableItemToHtml opts tag' align' item = do
  contents <- blockListToHtml opts item
  let alignAttrs = if writerHtml5 opts
                      then [thestyle $ "align: " ++ alignmentToString align']
                      else [align $ alignmentToString align']
  return $ (tag' ! alignAttrs) contents +++ nl opts

toListItems :: WriterOptions -> [Html] -> [Html]
toListItems opts items = map (toListItem opts) items ++ [nl opts]

toListItem :: WriterOptions -> Html -> Html
toListItem opts item = nl opts +++ li item

blockListToHtml :: WriterOptions -> [Block] -> State WriterState Html
blockListToHtml opts lst =
  mapM (blockToHtml opts) lst >>=
  return . toHtmlFromList . intersperse (nl opts)

-- | Convert list of Pandoc inline elements to HTML.
inlineListToHtml :: WriterOptions -> [Inline] -> State WriterState Html
inlineListToHtml opts lst =
  mapM (inlineToHtml opts) lst >>= return . toHtmlFromList

-- | Convert Pandoc inline element to HTML.
inlineToHtml :: WriterOptions -> Inline -> State WriterState Html
inlineToHtml opts inline =
  case inline of
    (Str str)        -> return $ stringToHtml opts str
    (Space)          -> return $ stringToHtml opts " "
    (LineBreak)      -> return br
    (EmDash)         -> return $ stringToHtml opts "—"
    (EnDash)         -> return $ stringToHtml opts "–"
    (Ellipses)       -> return $ stringToHtml opts "…"
    (Apostrophe)     -> return $ stringToHtml opts "’"
    (Emph lst)       -> inlineListToHtml opts lst >>= return . emphasize
    (Strong lst)     -> inlineListToHtml opts lst >>= return . strong
    (Code attr str)  -> case highlightHtml True attr str of
                             Left _  -> return
                                        $ thecode ! (attrsToHtml opts attr)
                                        $ stringToHtml opts str
                             Right h -> return h
    (Strikeout lst)  -> inlineListToHtml opts lst >>=
                        return . (thespan ! [thestyle "text-decoration: line-through;"])
    (SmallCaps lst)   -> inlineListToHtml opts lst >>=
                         return . (thespan ! [thestyle "font-variant: small-caps;"])
    (Superscript lst) -> inlineListToHtml opts lst >>= return . sup
    (Subscript lst)   -> inlineListToHtml opts lst >>= return . sub
    (Quoted quoteType lst) ->
                        let (leftQuote, rightQuote) = case quoteType of
                              SingleQuote -> (stringToHtml opts "‘",
                                              stringToHtml opts "’")
                              DoubleQuote -> (stringToHtml opts "“",
                                              stringToHtml opts "”")
                        in  do contents <- inlineListToHtml opts lst
                               return $ leftQuote +++ contents +++ rightQuote
    (Math t str) ->     modify (\st -> st {stMath = True}) >>
                        (case writerHTMLMathMethod opts of
                               LaTeXMathML _ ->
                                  -- putting LaTeXMathML in container with class "LaTeX" prevents
                                  -- non-math elements on the page from being treated as math by
                                  -- the javascript
                                  return $ thespan ! [theclass "LaTeX"] $
                                         case t of
                                           InlineMath  -> primHtml ("$" ++ str ++ "$")
                                           DisplayMath -> primHtml ("$$" ++ str ++ "$$")
                               JsMath _ -> do
                                  let m = primHtml str
                                  return $ case t of
                                           InlineMath -> thespan ! [theclass "math"] $ m
                                           DisplayMath -> thediv ! [theclass "math"] $ m
                               WebTeX url -> do
                                  let m = image ! [src (url ++ urlEncode str),
                                                         alt str, title str]
                                  return $ case t of
                                            InlineMath  -> m
                                            DisplayMath -> br +++ m +++ br
                               GladTeX ->
                                  return $ case t of
                                             InlineMath -> primHtml $ "<EQ ENV=\"math\">" ++ str ++ "</EQ>"
                                             DisplayMath -> primHtml $ "<EQ ENV=\"displaymath\">" ++ str ++ "</EQ>"
                               MathML _ -> do
                                  let dt = if t == InlineMath
                                              then DisplayInline
                                              else DisplayBlock
                                  let conf = useShortEmptyTags (const False)
                                               defaultConfigPP
                                  case texMathToMathML dt str of
                                        Right r -> return $ primHtml $
                                                    ppcElement conf r
                                        Left  _ -> inlineListToHtml opts
                                                   (readTeXMath str) >>= return .
                                                     (thespan !  [theclass "math"])
                               MathJax _ -> return $ primHtml $
                                  case t of
                                    InlineMath  -> "\\(" ++ str ++ "\\)"
                                    DisplayMath -> "\\[" ++ str ++ "\\]"
                               PlainMath -> do
                                  x <- inlineListToHtml opts (readTeXMath str)
                                  let m = thespan ! [theclass "math"] $ x
                                  return  $ case t of
                                             InlineMath  -> m
                                             DisplayMath -> br +++ m +++ br )
    (RawInline "latex" str) -> case writerHTMLMathMethod opts of
                               LaTeXMathML _ -> do modify (\st -> st {stMath = True})
                                                   return $ primHtml str
                               _             -> return noHtml
    (RawInline "html" str) -> return $ primHtml str
    (RawInline _ _) -> return noHtml
    (Link [Code _ str] (s,_)) | "mailto:" `isPrefixOf` s ->
                        return $ obfuscateLink opts str s
    (Link txt (s,_)) | "mailto:" `isPrefixOf` s -> do
                        linkText <- inlineListToHtml opts txt
                        return $ obfuscateLink opts (show linkText) s
    (Link txt (s,tit)) -> do
                        linkText <- inlineListToHtml opts txt
                        return $ anchor ! ([href s] ++
                                 if null tit then [] else [title tit]) $
                                 linkText
    (Image txt (s,tit)) | treatAsImage s -> do
                        let alternate' = stringify txt
                        let attributes = [src s] ++
                                         (if null tit
                                            then []
                                            else [title tit]) ++
                                         if null txt
                                            then []
                                            else [alt alternate']
                        return $ image ! attributes
                        -- note:  null title included, as in Markdown.pl
    (Image _ (s,tit)) -> do
                        let attributes = [src s] ++
                                         (if null tit
                                            then []
                                            else [title tit])
                        return $ itag "embed" ! attributes
                        -- note:  null title included, as in Markdown.pl
    (Note contents)          -> do
                        st <- get
                        let notes = stNotes st
                        let number = (length notes) + 1
                        let ref = show number
                        htmlContents <- blockListToNote opts ref contents
                        -- push contents onto front of notes
                        put $ st {stNotes = (htmlContents:notes)}
                        return $ sup <<
                                 anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ "fn" ++ ref),
                                           theclass "footnoteRef",
                                           prefixedId opts ("fnref" ++ ref)] << ref
    (Cite _ il)  -> inlineListToHtml opts il

blockListToNote :: WriterOptions -> String -> [Block] -> State WriterState Html
blockListToNote opts ref blocks =
  -- If last block is Para or Plain, include the backlink at the end of
  -- that block. Otherwise, insert a new Plain block with the backlink.
  let backlink = [RawInline "html" $ " <a href=\"#" ++ writerIdentifierPrefix opts ++ "fnref" ++ ref ++
                 "\" class=\"footnoteBackLink\">" ++
                 (if writerAscii opts then "&#8617;" else "↩") ++ "</a>"]
      blocks'  = if null blocks
                    then []
                    else let lastBlock   = last blocks
                             otherBlocks = init blocks
                         in  case lastBlock of
                                  (Para lst)  -> otherBlocks ++
                                                 [Para (lst ++ backlink)]
                                  (Plain lst) -> otherBlocks ++
                                                 [Plain (lst ++ backlink)]
                                  _           -> otherBlocks ++ [lastBlock,
                                                 Plain backlink]
  in  do contents <- blockListToHtml opts blocks'
         return $ nl opts +++ (li ! [prefixedId opts ("fn" ++ ref)]) contents