@@ -28,7 +28,9 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828
2929Conversion of 'Pandoc' documents to HTML.
3030-}
31−module Text.Pandoc.Writers.HTML ( writeHtml , writeHtmlString ) where
31+module Text.Pandoc.Writers.HTML (
32+ writeHtml4, writeHtml4String,
33+ writeHtml5, writeHtml5String ) where
3234import Text.Pandoc.Definition
3335import Text.Pandoc.Walk
3436import Data.Monoid ((<>))
@@ -80,12 +82,13 @@ data WriterState = WriterState
8082 , stHighlighting :: Bool -- ^ Syntax highlighting is used
8183 , stSecNum :: [Int] -- ^ Number of current section
8284 , stElement :: Bool -- ^ Processing an Element
85+ , stHtml5 :: Bool -- ^ Use HTML5
8386 }
8487
8588defaultWriterState :: WriterState
8689defaultWriterState = WriterState {stNotes= [], stMath = False, stQuotes = False,
8790 stHighlighting = False, stSecNum = [],
88− stElement = False}
91+ stElement = False, stHtml5 = False}
8992
9093-- Helpers to render HTML with the appropriate function.
9194
@@ -102,19 +105,35 @@ nl opts = if writerWrapText opts == WrapNone
102105 then mempty
103106 else preEscapedString "\n"
104107
105−-- | Convert Pandoc document to Html string.
106−writeHtmlString :: PandocMonad m => WriterOptions -> Pandoc -> m String
107−writeHtmlString opts d = do
108− (body, context) <- evalStateT (pandocToHtml opts d) defaultWriterState
108+-- | Convert Pandoc document to Html 5 string.
109+writeHtml5String :: PandocMonad m => WriterOptions -> Pandoc -> m String
110+writeHtml5String = writeHtmlString' True
111+
112+-- | Convert Pandoc document to Html 5 structure.
113+writeHtml5 :: PandocMonad m => WriterOptions -> Pandoc -> m Html
114+writeHtml5 = writeHtml' True
115+
116+-- | Convert Pandoc document to Html 4 string.
117+writeHtml4String :: PandocMonad m => WriterOptions -> Pandoc -> m String
118+writeHtml4String = writeHtmlString' False
119+
120+-- | Convert Pandoc document to Html 4 structure.
121+writeHtml4 :: PandocMonad m => WriterOptions -> Pandoc -> m Html
122+writeHtml4 = writeHtml' False
123+
124+writeHtmlString' :: PandocMonad m => Bool -> WriterOptions -> Pandoc -> m String
125+writeHtmlString' html5 opts d = do
126+ (body, context) <- evalStateT (pandocToHtml opts d)
127+ defaultWriterState{ stHtml5 = html5 }
109128 return $ case writerTemplate opts of
110129 Nothing -> renderHtml body
111130 Just tpl -> renderTemplate' tpl $
112131 defField "body" (renderHtml body) context
113132
114−-- | Convert Pandoc document to Html structure.
115−writeHtml :: PandocMonad m => WriterOptions -> Pandoc -> m Html
116−writeHtml opts d = do
117− (body, context) <- evalStateT (pandocToHtml opts d) defaultWriterState
133+writeHtml' :: PandocMonad m => Bool -> WriterOptions -> Pandoc -> m Html
134+writeHtml' html5 opts d = do
135+ (body, context) <- evalStateT (pandocToHtml opts d)
136+ defaultWriterState{ stHtml5 = html5 }
118137 return $ case writerTemplate opts of
119138 Nothing -> body
120139 Just tpl -> renderTemplate' tpl $
@@ -144,8 +163,8 @@ pandocToHtml opts (Pandoc meta blocks) = do
144163 blocks' <- liftM (mconcat . intersperse (nl opts)) $
145164 mapM (elementToHtml slideLevel opts) sects
146165 st <- get
147− let notes = reverse (stNotes st)
148− let thebody = blocks' >> footnoteSection opts notes
166+ notes <- footnoteSection opts (reverse (stNotes st))
167+ let thebody = blocks' >> notes
149168 let math = case writerHTMLMathMethod opts of
150169 LaTeXMathML (Just url) ->
151170 H.script ! A.src (toValue url)
@@ -172,7 +191,7 @@ pandocToHtml opts (Pandoc meta blocks) = do
172191 (H.link ! A.rel "stylesheet" ! A.href (toValue css)) <>
173192 (H.script ! A.type_ "text/javascript" $ toHtml renderKaTeX)
174193 _ -> case lookup "mathml-script" (writerVariables opts) of
175− Just s | not (writerHtml5 opts) ->
194+ Just s | not (stHtml5 st) ->
176195 H.script ! A.type_ "text/javascript"
177196 $ preEscapedString
178197 ("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
@@ -199,7 +218,7 @@ pandocToHtml opts (Pandoc meta blocks) = do
199218 defField "slideous-url" ("slideous" :: String) $
200219 defField "revealjs-url" ("reveal.js" :: String) $
201220 defField "s5-url" ("s5/default" :: String) $
202− defField "html5" (writerHtml5 opts) $
221+ defField "html5" (stHtml5 st) $
203222 metadata
204223 return (thebody, context)
205224
@@ -277,6 +296,7 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
277296 let slide = writerSlideVariant opts /= NoSlides && level <= slideLevel
278297 let num' = zipWith (+) num (writerNumberOffset opts ++ repeat 0)
279298 modify $ \st -> st{stSecNum = num'} -- update section number
299+ html5 <- gets stHtml5
280300 let titleSlide = slide && level < slideLevel
281301 header' <- if title' == [Str "\0"] -- marker for hrule
282302 then return mempty
@@ -307,10 +327,10 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
307327 let inNl x = mconcat $ nl opts : intersperse (nl opts) x ++ [nl opts]
308328 let classes' = ["titleslide" | titleSlide] ++ ["slide" | slide] ++
309329 ["section" | (slide || writerSectionDivs opts) &&
310− not (writerHtml5 opts) ] ++
330+ not html5 ] ++
311331 ["level" ++ show level | slide || writerSectionDivs opts ]
312332 ++ classes
313− let secttag = if writerHtml5 opts
333+ let secttag = if html5
314334 then H5.section
315335 else H.div
316336 let attr = (id',classes',keyvals)
@@ -327,19 +347,22 @@ elementToHtml slideLevel opts (Sec level num (id',classes,keyvals) title' elemen
327347
328348-- | Convert list of Note blocks to a footnote <div>.
329349-- Assumes notes are sorted.
330−footnoteSection :: WriterOptions -> [Html] -> Html
331−footnoteSection opts notes =
332− if null notes
333− then mempty
334− else nl opts >> (container
335− $ nl opts >> hrtag >> nl opts >>
336− H.ol (mconcat notes >> nl opts) >> nl opts)
337− where container x = if writerHtml5 opts
338− then H5.section ! A.class_ "footnotes" $ x
339− else if writerSlideVariant opts /= NoSlides
340− then H.div ! A.class_ "footnotes slide" $ x
341− else H.div ! A.class_ "footnotes" $ x
342− hrtag = if writerHtml5 opts then H5.hr else H.hr
350+footnoteSection :: PandocMonad m
351+ => WriterOptions -> [Html] -> StateT WriterState m Html
352+footnoteSection opts notes = do
353+ html5 <- gets stHtml5
354+ let hrtag = if html5 then H5.hr else H.hr
355+ let container x = if html5
356+ then H5.section ! A.class_ "footnotes" $ x
357+ else if writerSlideVariant opts /= NoSlides
358+ then H.div ! A.class_ "footnotes slide" $ x
359+ else H.div ! A.class_ "footnotes" $ x
360+ return $
361+ if null notes
362+ then mempty
363+ else nl opts >> (container
364+ $ nl opts >> hrtag >> nl opts >>
365+ H.ol (mconcat notes >> nl opts) >> nl opts)
343366
344367-- | Parse a mailto link; return Just (name, domain) or Nothing.
345368parseMailto :: String -> Maybe (String, String)
@@ -448,13 +471,14 @@ blockToHtml opts (Plain lst) = inlineListToHtml opts lst
448471-- title beginning with fig: indicates that the image is a figure
449472blockToHtml opts (Para [Image attr txt (s,'f':'i':'g':':':tit)]) = do
450473 img <- inlineToHtml opts (Image attr txt (s,tit))
451− let tocapt = if writerHtml5 opts
474+ html5 <- gets stHtml5
475+ let tocapt = if html5
452476 then H5.figcaption
453477 else H.p ! A.class_ "caption"
454478 capt <- if null txt
455479 then return mempty
456480 else tocapt `fmap` inlineListToHtml opts txt
457− return $ if writerHtml5 opts
481+ return $ if html5
458482 then H5.figure $ mconcat
459483 [nl opts, img, capt, nl opts]
460484 else H.div ! A.class_ "figure" $ mconcat
@@ -475,12 +499,13 @@ blockToHtml opts (LineBlock lns) =
475499 htmlLines <- mconcat . intersperse lf <$> mapM (inlineListToHtml opts) lns
476500 return $ H.div ! A.style "white-space: pre-line;" $ htmlLines
477501blockToHtml opts (Div attr@(ident, classes, kvs) bs) = do
502+ html5 <- gets stHtml5
478503 let speakerNotes = "notes" `elem` classes
479504 -- we don't want incremental output inside speaker notes, see #1394
480505 let opts' = if speakerNotes then opts{ writerIncremental = False } else opts
481506 contents <- blockListToHtml opts' bs
482507 let contents' = nl opts >> contents >> nl opts
483− let (divtag, classes') = if writerHtml5 opts && "section" `elem` classes
508+ let (divtag, classes') = if html5 && "section" `elem` classes
484509 then (H5.section, filter (/= "section") classes)
485510 else (H.div, classes)
486511 return $
@@ -498,7 +523,9 @@ blockToHtml opts (RawBlock f str)
498523 allowsMathEnvironments (writerHTMLMathMethod opts) &&
499524 isMathEnvironment str = blockToHtml opts $ Plain [Math DisplayMath str]
500525 | otherwise = return mempty
501−blockToHtml opts (HorizontalRule) = return $ if writerHtml5 opts then H5.hr else H.hr
526+blockToHtml _ (HorizontalRule) = do
527+ html5 <- gets stHtml5
528+ return $ if html5 then H5.hr else H.hr
502529blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
503530 let tolhs = isEnabled Ext_literate_haskell opts &&
504531 any (\c -> map toLower c == "haskell") classes &&
@@ -564,6 +591,7 @@ blockToHtml opts (BulletList lst) = do
564591 return $ unordList opts contents
565592blockToHtml opts (OrderedList (startnum, numstyle, _) lst) = do
566593 contents <- mapM (blockListToHtml opts) lst
594+ html5 <- gets stHtml5
567595 let numstyle' = case numstyle of
568596 Example -> "decimal"
569597 _ -> camelCaseToHyphenated $ show numstyle
@@ -574,7 +602,7 @@ blockToHtml opts (OrderedList (startnum, numstyle, _) lst) = do
574602 then [A.class_ "example"]
575603 else []) ++
576604 (if numstyle /= DefaultStyle
577− then if writerHtml5 opts
605+ then if html5
578606 then [A.type_ $
579607 case numstyle of
580608 Decimal -> "1"
@@ -603,6 +631,7 @@ blockToHtml opts (Table capt aligns widths headers rows') = do
603631 else do
604632 cs <- inlineListToHtml opts capt
605633 return $ H.caption cs >> nl opts
634+ html5 <- gets stHtml5
606635 let percent w = show (truncate (100*w) :: Integer) ++ "%"
607636 let coltags = if all (== 0.0) widths
608637 then mempty
@@ -610,7 +639,7 @@ blockToHtml opts (Table capt aligns widths headers rows') = do
610639 H.colgroup $ do
611640 nl opts
612641 mapM_ (\w -> do
613− if writerHtml5 opts
642+ if html5
614643 then H.col ! A.style (toValue $ "width: " ++
615644 percent w)
616645 else H.col ! A.width (toValue $ percent w)
@@ -666,8 +695,9 @@ tableItemToHtml :: PandocMonad m
666695 -> StateT WriterState m Html
667696tableItemToHtml opts tag' align' item = do
668697 contents <- blockListToHtml opts item
698+ html5 <- gets stHtml5
669699 let alignStr = alignmentToString align'
670− let attribs = if writerHtml5 opts
700+ let attribs = if html5
671701 then A.style (toValue $ "text-align: " ++ alignStr ++ ";")
672702 else A.align (toValue alignStr)
673703 let tag'' = if null alignStr
@@ -707,7 +737,8 @@ annotateMML e tex = math (unode "semantics" [cs, unode "annotation" (annotAttrs,
707737-- | Convert Pandoc inline element to HTML.
708738inlineToHtml :: PandocMonad m
709739 => WriterOptions -> Inline -> StateT WriterState m Html
710−inlineToHtml opts inline =
740+inlineToHtml opts inline = do
741+ html5 <- gets stHtml5
711742 case inline of
712743 (Str str) -> return $ strToHtml str
713744 (Space) -> return $ strToHtml " "
@@ -715,7 +746,7 @@ inlineToHtml opts inline =
715746 WrapNone -> preEscapedString " "
716747 WrapAuto -> preEscapedString " "
717748 WrapPreserve -> preEscapedString "\n"
718− (LineBreak) -> return $ (if writerHtml5 opts then H5.br else H.br)
749+ (LineBreak) -> return $ (if html5 then H5.br else H.br)
719750 <> strToHtml "\n"
720751 (Span (id',classes,kvs) ils)
721752 -> inlineListToHtml opts ils >>=
@@ -784,12 +815,12 @@ inlineToHtml opts inline =
784815 InlineMath -> H.span ! A.class_ mathClass $ m
785816 DisplayMath -> H.div ! A.class_ mathClass $ m
786817 WebTeX url -> do
787− let imtag = if writerHtml5 opts then H5.img else H.img
818+ let imtag = if html5 then H5.img else H.img
788819 let m = imtag ! A.style "vertical-align:middle"
789820 ! A.src (toValue $ url ++ urlEncode str)
790821 ! A.alt (toValue str)
791822 ! A.title (toValue str)
792− let brtag = if writerHtml5 opts then H5.br else H.br
823+ let brtag = if html5 then H5.br else H.br
793824 return $ case t of
794825 InlineMath -> m
795826 DisplayMath -> brtag >> m >> brtag
@@ -817,7 +848,7 @@ inlineToHtml opts inline =
817848 PlainMath -> do
818849 x <- lift (texMathToInlines t str) >>= inlineListToHtml opts
819850 let m = H.span ! A.class_ mathClass $ x
820− let brtag = if writerHtml5 opts then H5.br else H.br
851+ let brtag = if html5 then H5.br else H.br
821852 return $ case t of
822853 InlineMath -> m
823854 DisplayMath -> brtag >> m >> brtag
@@ -847,7 +878,7 @@ inlineToHtml opts inline =
847878 [A.title $ toValue tit | not (null tit)] ++
848879 [A.alt $ toValue alternate' | not (null txt)] ++
849880 imgAttrsToHtml opts attr
850− let tag = if writerHtml5 opts then H5.img else H.img
881+ let tag = if html5 then H5.img else H.img
851882 return $ foldl (!) tag attributes
852883 -- note: null title included, as in Markdown.pl
853884 (Image attr _ (s,tit)) -> do
@@ -880,7 +911,7 @@ inlineToHtml opts inline =
880911 (Cite cits il)-> do contents <- inlineListToHtml opts il
881912 let citationIds = unwords $ map citationId cits
882913 let result = H.span ! A.class_ "citation" $ contents
883− return $ if writerHtml5 opts
914+ return $ if html5
884915 then result ! customAttribute "data-cites" (toValue citationIds)
885916 else result
886917