2008-08-01 21:00:50 +00:00
|
|
|
|
{-# OPTIONS_GHC -fno-warn-deprecations #-}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
{-
|
2008-01-08 17:26:16 +00:00
|
|
|
|
Copyright (C) 2006-8 John MacFarlane <jgm@berkeley.edu>
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
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
|
2008-01-08 17:26:16 +00:00
|
|
|
|
Copyright : Copyright (C) 2006-8 John MacFarlane
|
2007-11-03 23:27:58 +00:00
|
|
|
|
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
|
2009-12-31 01:08:46 +00:00
|
|
|
|
import Text.Pandoc.Templates
|
2007-12-02 00:36:32 +00:00
|
|
|
|
import Text.Pandoc.Readers.TeXMath
|
2009-12-31 01:13:26 +00:00
|
|
|
|
import Text.Pandoc.Highlighting ( highlightHtml )
|
2010-01-01 04:11:54 +00:00
|
|
|
|
import Text.Pandoc.XML (stripTags, escapeStringForXML)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Numeric ( showHex )
|
2009-04-25 00:29:58 +00:00
|
|
|
|
import Data.Char ( ord, toLower )
|
2009-12-31 01:15:42 +00:00
|
|
|
|
import Data.List ( isPrefixOf, intersperse )
|
2009-04-25 00:29:58 +00:00
|
|
|
|
import Data.Maybe ( catMaybes )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Control.Monad.State
|
2008-07-27 03:54:07 +00:00
|
|
|
|
import Text.XHtml.Transitional hiding ( stringToHtml )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
data WriterState = WriterState
|
2009-12-31 01:13:26 +00:00
|
|
|
|
{ stNotes :: [Html] -- ^ List of notes
|
|
|
|
|
, stMath :: Bool -- ^ Math is used in document
|
|
|
|
|
, stHighlighting :: Bool -- ^ Syntax highlighting is used
|
|
|
|
|
, stSecNum :: [Int] -- ^ Number of current section
|
2007-11-03 23:27:58 +00:00
|
|
|
|
} deriving Show
|
|
|
|
|
|
|
|
|
|
defaultWriterState :: WriterState
|
2009-12-31 01:13:26 +00:00
|
|
|
|
defaultWriterState = WriterState {stNotes= [], stMath = False, stHighlighting = False, stSecNum = []}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- Helpers to render HTML with the appropriate function.
|
2008-07-13 17:08:55 +00:00
|
|
|
|
|
|
|
|
|
renderFragment :: (HTML html) => WriterOptions -> html -> String
|
2007-11-03 23:27:58 +00:00
|
|
|
|
renderFragment opts = if writerWrapText opts
|
|
|
|
|
then renderHtmlFragment
|
|
|
|
|
else showHtmlFragment
|
|
|
|
|
|
2010-01-01 04:11:48 +00:00
|
|
|
|
-- | Modified version of Text.XHtml's stringToHtml.
|
|
|
|
|
-- Use unicode characters wherever possible.
|
2008-07-27 03:54:07 +00:00
|
|
|
|
stringToHtml :: String -> Html
|
2010-01-01 04:11:54 +00:00
|
|
|
|
stringToHtml = primHtml . escapeStringForXML
|
2008-07-27 03:54:07 +00:00
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Convert Pandoc document to Html string.
|
|
|
|
|
writeHtmlString :: WriterOptions -> Pandoc -> String
|
2009-12-31 01:13:26 +00:00
|
|
|
|
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 renderFragment opts body'
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc document to Html structure.
|
|
|
|
|
writeHtml :: WriterOptions -> Pandoc -> Html
|
2009-12-31 01:13:26 +00:00
|
|
|
|
writeHtml opts d =
|
|
|
|
|
let (tit, auths, date, toc, body', newvars) = evalState (pandocToHtml opts d)
|
|
|
|
|
defaultWriterState
|
|
|
|
|
in if writerStandalone opts
|
2009-12-31 01:14:35 +00:00
|
|
|
|
then inTemplate opts tit auths date toc body' newvars
|
2009-12-31 01:13:26 +00:00
|
|
|
|
else body'
|
|
|
|
|
|
|
|
|
|
-- result is (title, authors, date, toc, body, new variables)
|
|
|
|
|
pandocToHtml :: WriterOptions
|
|
|
|
|
-> Pandoc
|
|
|
|
|
-> State WriterState (Html, [Html], Html, 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 sects = hierarchicalize blocks
|
|
|
|
|
toc <- if writerTableOfContents opts
|
|
|
|
|
then tableOfContents opts sects
|
|
|
|
|
else return noHtml
|
|
|
|
|
blocks' <- liftM toHtmlFromList $ mapM (elementToHtml opts) sects
|
|
|
|
|
st <- get
|
|
|
|
|
let notes = reverse (stNotes st)
|
|
|
|
|
let before = primHtml $ writerIncludeBefore opts
|
|
|
|
|
let after = primHtml $ writerIncludeAfter opts
|
|
|
|
|
let thebody = before +++ blocks' +++ footnoteSection notes +++ after
|
|
|
|
|
let math = if stMath st
|
|
|
|
|
then case writerHTMLMathMethod opts of
|
|
|
|
|
LaTeXMathML (Just url) ->
|
|
|
|
|
script !
|
|
|
|
|
[src url, thetype "text/javascript"] $ noHtml
|
|
|
|
|
JsMath (Just url) ->
|
|
|
|
|
script !
|
|
|
|
|
[src url, thetype "text/javascript"] $ noHtml
|
|
|
|
|
_ -> case lookup "latexmathml-script" (writerVariables opts) of
|
|
|
|
|
Just s ->
|
|
|
|
|
script ! [thetype "text/javascript"] <<
|
|
|
|
|
primHtml s
|
|
|
|
|
Nothing -> noHtml
|
|
|
|
|
else noHtml
|
|
|
|
|
let newvars = [("highlighting","yes") | stHighlighting st] ++
|
|
|
|
|
[("math", renderHtmlFragment math) | stMath st]
|
|
|
|
|
return (tit, auths, date, toc, thebody, newvars)
|
|
|
|
|
|
2009-12-31 01:14:35 +00:00
|
|
|
|
inTemplate :: TemplateTarget a
|
|
|
|
|
=> WriterOptions
|
2009-12-31 01:13:26 +00:00
|
|
|
|
-> Html
|
|
|
|
|
-> [Html]
|
|
|
|
|
-> Html
|
|
|
|
|
-> Html
|
|
|
|
|
-> Html
|
|
|
|
|
-> [(String,String)]
|
2009-12-31 01:14:35 +00:00
|
|
|
|
-> a
|
2009-12-31 01:13:26 +00:00
|
|
|
|
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", renderHtmlFragment body')
|
|
|
|
|
, ("pagetitle", topTitle')
|
|
|
|
|
, ("toc", renderHtmlFragment toc)
|
|
|
|
|
, ("title", renderHtmlFragment tit)
|
2009-12-31 01:15:42 +00:00
|
|
|
|
, ("date", date') ] ++
|
|
|
|
|
[ ("author", a) | a <- authors ]
|
2009-12-31 01:13:26 +00:00
|
|
|
|
in renderTemplate context $ writerTemplate opts
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2009-12-05 17:56:02 +00:00
|
|
|
|
-- | Like Text.XHtml's identifier, but adds the writerIdentifierPrefix
|
|
|
|
|
prefixedId :: WriterOptions -> String -> HtmlAttr
|
|
|
|
|
prefixedId opts s = identifier $ writerIdentifierPrefix opts ++ s
|
|
|
|
|
|
2009-04-25 00:29:58 +00:00
|
|
|
|
-- | Construct table of contents from list of elements.
|
|
|
|
|
tableOfContents :: WriterOptions -> [Element] -> State WriterState Html
|
|
|
|
|
tableOfContents _ [] = return noHtml
|
|
|
|
|
tableOfContents opts sects = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let opts' = opts { writerIgnoreNotes = True }
|
2009-04-25 00:29:58 +00:00
|
|
|
|
contents <- mapM (elementToListItem opts') sects
|
2009-12-05 17:56:02 +00:00
|
|
|
|
return $ thediv ! [prefixedId opts' "TOC"] $ unordList $ catMaybes contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2009-12-09 04:58:29 +00:00
|
|
|
|
-- | Convert section number to string
|
|
|
|
|
showSecNum :: [Int] -> String
|
|
|
|
|
showSecNum = concat . intersperse "." . map show
|
2009-12-08 02:36:16 +00:00
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Converts an Element to a list item for a table of contents,
|
|
|
|
|
-- retrieving the appropriate identifier from state.
|
2009-04-25 00:29:58 +00:00
|
|
|
|
elementToListItem :: WriterOptions -> Element -> State WriterState (Maybe Html)
|
|
|
|
|
elementToListItem _ (Blk _) = return Nothing
|
2009-12-08 02:36:16 +00:00
|
|
|
|
elementToListItem opts (Sec _ num id' headerText subsecs) = do
|
2009-12-09 04:58:29 +00:00
|
|
|
|
let sectnum = if writerNumberSections opts
|
|
|
|
|
then (thespan ! [theclass "toc-section-number"] << showSecNum num) +++
|
|
|
|
|
stringToHtml " "
|
|
|
|
|
else noHtml
|
|
|
|
|
txt <- liftM (sectnum +++) $ inlineListToHtml opts headerText
|
2009-04-25 00:29:58 +00:00
|
|
|
|
subHeads <- mapM (elementToListItem opts) subsecs >>= return . catMaybes
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let subList = if null subHeads
|
|
|
|
|
then noHtml
|
2009-04-25 00:29:58 +00:00
|
|
|
|
else unordList subHeads
|
2009-12-05 17:56:02 +00:00
|
|
|
|
return $ Just $ (anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ id')] $ txt) +++ subList
|
2009-04-25 00:29:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert an Element to Html.
|
|
|
|
|
elementToHtml :: WriterOptions -> Element -> State WriterState Html
|
|
|
|
|
elementToHtml opts (Blk block) = blockToHtml opts block
|
2009-12-08 02:36:16 +00:00
|
|
|
|
elementToHtml opts (Sec level num id' title' elements) = do
|
2009-04-25 00:29:58 +00:00
|
|
|
|
innerContents <- mapM (elementToHtml opts) elements
|
2009-12-09 04:58:29 +00:00
|
|
|
|
modify $ \st -> st{stSecNum = num} -- update section number
|
|
|
|
|
header' <- blockToHtml opts (Header level title')
|
2009-04-25 00:29:58 +00:00
|
|
|
|
return $ if writerS5 opts || (writerStrictMarkdown opts && not (writerTableOfContents opts))
|
|
|
|
|
-- S5 gets confused by the extra divs around sections
|
|
|
|
|
then toHtmlFromList (header' : innerContents)
|
2009-12-05 17:56:02 +00:00
|
|
|
|
else thediv ! [prefixedId opts id'] << (header' : innerContents)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Note blocks to a footnote <div>.
|
|
|
|
|
-- Assumes notes are sorted.
|
2008-07-13 17:08:55 +00:00
|
|
|
|
footnoteSection :: [Html] -> Html
|
|
|
|
|
footnoteSection notes =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
if null notes
|
|
|
|
|
then noHtml
|
|
|
|
|
else thediv ! [theclass "footnotes"] $ hr +++ (olist << notes)
|
|
|
|
|
|
2008-02-09 03:21:04 +00:00
|
|
|
|
|
|
|
|
|
-- | Parse a mailto link; return Just (name, domain) or Nothing.
|
|
|
|
|
parseMailto :: String -> Maybe (String, String)
|
2008-07-13 17:08:55 +00:00
|
|
|
|
parseMailto ('m':'a':'i':'l':'t':'o':':':addr) =
|
|
|
|
|
let (name', rest) = span (/='@') addr
|
2008-02-09 03:21:04 +00:00
|
|
|
|
domain = drop 1 rest
|
2008-07-13 17:08:55 +00:00
|
|
|
|
in Just (name', domain)
|
2008-02-09 03:21:04 +00:00
|
|
|
|
parseMailto _ = Nothing
|
|
|
|
|
|
2009-01-24 19:58:48 +00:00
|
|
|
|
-- | Obfuscate a "mailto:" link.
|
2007-11-03 23:27:58 +00:00
|
|
|
|
obfuscateLink :: WriterOptions -> String -> String -> Html
|
2009-01-24 19:58:48 +00:00
|
|
|
|
obfuscateLink opts txt s | writerEmailObfuscation opts == NoObfuscation =
|
|
|
|
|
anchor ! [href s] << txt
|
2008-07-13 17:08:55 +00:00
|
|
|
|
obfuscateLink opts txt s =
|
2009-01-24 19:58:48 +00:00
|
|
|
|
let meth = writerEmailObfuscation opts
|
|
|
|
|
s' = map toLower s
|
2008-07-13 17:08:55 +00:00
|
|
|
|
in case parseMailto s' of
|
|
|
|
|
(Just (name', domain)) ->
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let domain' = substitute "." " dot " domain
|
|
|
|
|
at' = obfuscateChar '@'
|
|
|
|
|
(linkText, altText) =
|
2008-07-13 17:08:55 +00:00
|
|
|
|
if txt == drop 7 s' -- autolink
|
|
|
|
|
then ("'<code>'+e+'</code>'", name' ++ " at " ++ domain')
|
|
|
|
|
else ("'" ++ txt ++ "'", txt ++ " (" ++ name' ++ " at " ++
|
2007-11-03 23:27:58 +00:00
|
|
|
|
domain' ++ ")")
|
2009-01-24 19:58:48 +00:00
|
|
|
|
in case meth of
|
|
|
|
|
ReferenceObfuscation ->
|
|
|
|
|
-- need to use primHtml or &'s are escaped to & in URL
|
2008-07-13 17:08:55 +00:00
|
|
|
|
primHtml $ "<a href=\"" ++ (obfuscateString s')
|
|
|
|
|
++ "\">" ++ (obfuscateString txt) ++ "</a>"
|
2009-01-24 19:58:48 +00:00
|
|
|
|
JavascriptObfuscation ->
|
|
|
|
|
(script ! [thetype "text/javascript"] $
|
2007-11-03 23:27:58 +00:00
|
|
|
|
primHtml ("\n<!--\nh='" ++
|
|
|
|
|
obfuscateString domain ++ "';a='" ++ at' ++ "';n='" ++
|
2008-07-13 17:08:55 +00:00
|
|
|
|
obfuscateString name' ++ "';e=n+a+h;\n" ++
|
2007-11-03 23:27:58 +00:00
|
|
|
|
"document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+" ++
|
|
|
|
|
linkText ++ "+'<\\/'+'a'+'>');\n// -->\n")) +++
|
|
|
|
|
noscript (primHtml $ obfuscateString altText)
|
2009-01-24 19:58:48 +00:00
|
|
|
|
_ -> error $ "Unknown obfuscation method: " ++ show meth
|
2010-01-01 04:11:48 +00:00
|
|
|
|
_ -> anchor ! [href s] $ stringToHtml txt -- malformed email
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | 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
|
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc block element to HTML.
|
|
|
|
|
blockToHtml :: WriterOptions -> Block -> State WriterState Html
|
2008-07-13 17:08:55 +00:00
|
|
|
|
blockToHtml _ Null = return $ noHtml
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToHtml opts (Plain lst) = inlineListToHtml opts lst
|
|
|
|
|
blockToHtml opts (Para lst) = inlineListToHtml opts lst >>= (return . paragraph)
|
2008-07-13 17:08:55 +00:00
|
|
|
|
blockToHtml _ (RawHtml str) = return $ primHtml str
|
|
|
|
|
blockToHtml _ (HorizontalRule) = return $ hr
|
2009-11-21 04:40:59 +00:00
|
|
|
|
blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
|
|
|
|
|
let classes' = if writerLiterateHaskell opts
|
|
|
|
|
then classes
|
|
|
|
|
else filter (/= "literate") classes
|
|
|
|
|
case highlightHtml (id',classes',keyvals) rawCode of
|
2008-05-15 00:54:33 +00:00
|
|
|
|
Left _ -> -- change leading newlines into <br /> tags, because some
|
|
|
|
|
-- browsers ignore leading newlines in pre blocks
|
|
|
|
|
let (leadingBreaks, rawCode') = span (=='\n') rawCode
|
2009-11-21 04:40:59 +00:00
|
|
|
|
attrs = [theclass (unwords classes') | not (null classes')] ++
|
2009-12-05 17:56:02 +00:00
|
|
|
|
[prefixedId opts id' | not (null id')] ++
|
2009-11-21 04:40:59 +00:00
|
|
|
|
map (\(x,y) -> strAttr x y) keyvals
|
|
|
|
|
in return $ pre ! attrs $ thecode <<
|
|
|
|
|
(replicate (length leadingBreaks) br +++
|
|
|
|
|
[stringToHtml $ rawCode' ++ "\n"])
|
2009-12-31 01:13:26 +00:00
|
|
|
|
Right h -> modify (\st -> st{ stHighlighting = True }) >> return h
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToHtml opts (BlockQuote blocks) =
|
|
|
|
|
-- in S5, treat list in blockquote specially
|
|
|
|
|
-- if default is incremental, make it nonincremental;
|
|
|
|
|
-- otherwise incremental
|
|
|
|
|
if writerS5 opts
|
|
|
|
|
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)
|
2008-07-13 17:08:55 +00:00
|
|
|
|
_ -> blockListToHtml opts blocks >>=
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(return . blockquote)
|
|
|
|
|
else blockListToHtml opts blocks >>= (return . blockquote)
|
|
|
|
|
blockToHtml opts (Header level lst) = do
|
|
|
|
|
contents <- inlineListToHtml opts lst
|
2009-12-09 04:58:29 +00:00
|
|
|
|
secnum <- liftM stSecNum get
|
|
|
|
|
let contents' = if writerNumberSections opts
|
|
|
|
|
then (thespan ! [theclass "header-section-number"] << showSecNum secnum) +++
|
|
|
|
|
stringToHtml " " +++ contents
|
|
|
|
|
else contents
|
|
|
|
|
let contents'' = if writerTableOfContents opts
|
|
|
|
|
then anchor ! [href $ "#" ++ writerIdentifierPrefix opts ++ "TOC"] $ contents'
|
|
|
|
|
else contents'
|
2007-11-03 23:27:58 +00:00
|
|
|
|
return $ case level of
|
2009-12-09 04:58:29 +00:00
|
|
|
|
1 -> h1 contents''
|
|
|
|
|
2 -> h2 contents''
|
|
|
|
|
3 -> h3 contents''
|
|
|
|
|
4 -> h4 contents''
|
|
|
|
|
5 -> h5 contents''
|
|
|
|
|
6 -> h6 contents''
|
|
|
|
|
_ -> paragraph contents''
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToHtml opts (BulletList lst) = do
|
|
|
|
|
contents <- mapM (blockListToHtml opts) lst
|
|
|
|
|
let attribs = if writerIncremental opts
|
|
|
|
|
then [theclass "incremental"]
|
|
|
|
|
else []
|
|
|
|
|
return $ unordList ! attribs $ contents
|
|
|
|
|
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
|
2008-02-24 18:15:36 +00:00
|
|
|
|
then [thestyle $ "list-style-type: " ++ numstyle' ++ ";"]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else [])
|
|
|
|
|
return $ ordList ! attribs $ contents
|
|
|
|
|
blockToHtml opts (DefinitionList lst) = do
|
2009-12-07 08:26:53 +00:00
|
|
|
|
contents <- mapM (\(term, defs) ->
|
|
|
|
|
do term' <- liftM (dterm <<) $ inlineListToHtml opts term
|
|
|
|
|
defs' <- mapM (liftM (ddef <<) . blockListToHtml opts) defs
|
|
|
|
|
return $ term' : defs') lst
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let attribs = if writerIncremental opts
|
|
|
|
|
then [theclass "incremental"]
|
|
|
|
|
else []
|
2009-12-07 08:26:53 +00:00
|
|
|
|
return $ dlist ! attribs << concat contents
|
2008-07-13 17:08:55 +00:00
|
|
|
|
blockToHtml opts (Table capt aligns widths headers rows') = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let alignStrings = map alignmentToString aligns
|
|
|
|
|
captionDoc <- if null capt
|
|
|
|
|
then return noHtml
|
|
|
|
|
else inlineListToHtml opts capt >>= return . caption
|
|
|
|
|
colHeads <- colHeadsToHtml opts alignStrings
|
|
|
|
|
widths headers
|
2008-10-18 23:14:59 +00:00
|
|
|
|
rows'' <- zipWithM (tableRowToHtml opts alignStrings) (cycle ["odd", "even"]) rows'
|
2008-07-13 17:08:55 +00:00
|
|
|
|
return $ table $ captionDoc +++ colHeads +++ rows''
|
|
|
|
|
|
|
|
|
|
colHeadsToHtml :: WriterOptions
|
|
|
|
|
-> [[Char]]
|
2008-09-06 02:51:44 +00:00
|
|
|
|
-> [Double]
|
2008-07-13 17:08:55 +00:00
|
|
|
|
-> [[Block]]
|
|
|
|
|
-> State WriterState Html
|
2007-11-03 23:27:58 +00:00
|
|
|
|
colHeadsToHtml opts alignStrings widths headers = do
|
|
|
|
|
heads <- sequence $ zipWith3
|
2008-07-13 17:08:55 +00:00
|
|
|
|
(\alignment columnwidth item -> tableItemToHtml opts th alignment columnwidth item)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
alignStrings widths headers
|
2008-10-18 23:14:59 +00:00
|
|
|
|
return $ tr ! [theclass "header"] $ toHtmlFromList heads
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2008-07-13 17:08:55 +00:00
|
|
|
|
alignmentToString :: Alignment -> [Char]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
alignmentToString alignment = case alignment of
|
|
|
|
|
AlignLeft -> "left"
|
|
|
|
|
AlignRight -> "right"
|
|
|
|
|
AlignCenter -> "center"
|
|
|
|
|
AlignDefault -> "left"
|
|
|
|
|
|
2008-07-13 17:08:55 +00:00
|
|
|
|
tableRowToHtml :: WriterOptions
|
|
|
|
|
-> [[Char]]
|
2008-10-18 23:14:59 +00:00
|
|
|
|
-> String
|
2008-07-13 17:08:55 +00:00
|
|
|
|
-> [[Block]]
|
|
|
|
|
-> State WriterState Html
|
2008-10-18 23:14:59 +00:00
|
|
|
|
tableRowToHtml opts aligns rowclass columns =
|
2008-07-13 17:08:55 +00:00
|
|
|
|
(sequence $ zipWith3 (tableItemToHtml opts td) aligns (repeat 0) columns) >>=
|
2008-10-18 23:14:59 +00:00
|
|
|
|
return . (tr ! [theclass rowclass]) . toHtmlFromList
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2008-07-13 17:08:55 +00:00
|
|
|
|
tableItemToHtml :: WriterOptions
|
|
|
|
|
-> (Html -> Html)
|
|
|
|
|
-> [Char]
|
2008-09-06 02:51:44 +00:00
|
|
|
|
-> Double
|
2008-07-13 17:08:55 +00:00
|
|
|
|
-> [Block]
|
|
|
|
|
-> State WriterState Html
|
|
|
|
|
tableItemToHtml opts tag' align' width' item = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- blockListToHtml opts item
|
|
|
|
|
let attrib = [align align'] ++
|
2008-07-15 00:32:21 +00:00
|
|
|
|
if width' /= 0
|
|
|
|
|
then [thestyle ("width: " ++ (show (truncate (100 * width') :: Integer)) ++ "%;")]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else []
|
2008-07-13 17:08:55 +00:00
|
|
|
|
return $ tag' ! attrib $ contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
blockListToHtml :: WriterOptions -> [Block] -> State WriterState Html
|
|
|
|
|
blockListToHtml opts lst =
|
|
|
|
|
mapM (blockToHtml opts) lst >>= return . toHtmlFromList
|
|
|
|
|
|
|
|
|
|
-- | 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 str
|
|
|
|
|
(Space) -> return $ stringToHtml " "
|
2010-01-01 04:11:48 +00:00
|
|
|
|
(LineBreak) -> return br
|
|
|
|
|
(EmDash) -> return $ stringToHtml "—"
|
|
|
|
|
(EnDash) -> return $ stringToHtml "–"
|
|
|
|
|
(Ellipses) -> return $ stringToHtml "…"
|
|
|
|
|
(Apostrophe) -> return $ stringToHtml "’"
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(Emph lst) -> inlineListToHtml opts lst >>= return . emphasize
|
|
|
|
|
(Strong lst) -> inlineListToHtml opts lst >>= return . strong
|
|
|
|
|
(Code str) -> return $ thecode << str
|
2008-02-24 18:15:36 +00:00
|
|
|
|
(Strikeout lst) -> inlineListToHtml opts lst >>=
|
|
|
|
|
return . (thespan ! [thestyle "text-decoration: line-through;"])
|
2008-07-15 23:25:49 +00:00
|
|
|
|
(SmallCaps lst) -> inlineListToHtml opts lst >>=
|
|
|
|
|
return . (thespan ! [thestyle "font-variant: small-caps;"])
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(Superscript lst) -> inlineListToHtml opts lst >>= return . sup
|
|
|
|
|
(Subscript lst) -> inlineListToHtml opts lst >>= return . sub
|
|
|
|
|
(Quoted quoteType lst) ->
|
|
|
|
|
let (leftQuote, rightQuote) = case quoteType of
|
2010-01-01 04:11:48 +00:00
|
|
|
|
SingleQuote -> (stringToHtml "‘",
|
|
|
|
|
stringToHtml "’")
|
|
|
|
|
DoubleQuote -> (stringToHtml "“",
|
|
|
|
|
stringToHtml "”")
|
2007-11-03 23:27:58 +00:00
|
|
|
|
in do contents <- inlineListToHtml opts lst
|
|
|
|
|
return $ leftQuote +++ contents +++ rightQuote
|
2008-08-13 03:02:42 +00:00
|
|
|
|
(Math t str) ->
|
|
|
|
|
modify (\st -> st {stMath = True}) >>
|
2007-12-02 00:36:32 +00:00
|
|
|
|
(case writerHTMLMathMethod opts of
|
2008-08-13 03:02:42 +00:00
|
|
|
|
LaTeXMathML _ ->
|
2008-09-15 22:43:21 +00:00
|
|
|
|
-- 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"] $
|
|
|
|
|
if t == InlineMath
|
|
|
|
|
then primHtml ("$" ++ str ++ "$")
|
|
|
|
|
else primHtml ("$$" ++ str ++ "$$")
|
2008-10-28 21:54:50 +00:00
|
|
|
|
JsMath _ ->
|
|
|
|
|
return $ if t == InlineMath
|
|
|
|
|
then thespan ! [theclass "math"] $ primHtml str
|
|
|
|
|
else thediv ! [theclass "math"] $ primHtml str
|
2007-12-02 00:36:32 +00:00
|
|
|
|
MimeTeX url ->
|
|
|
|
|
return $ image ! [src (url ++ "?" ++ str),
|
2007-12-01 03:11:52 +00:00
|
|
|
|
alt str, title str]
|
2007-12-02 00:36:32 +00:00
|
|
|
|
GladTeX ->
|
2007-12-02 16:49:11 +00:00
|
|
|
|
return $ primHtml $ "<EQ>" ++ str ++ "</EQ>"
|
2007-12-02 00:36:32 +00:00
|
|
|
|
PlainMath ->
|
2007-12-02 02:50:41 +00:00
|
|
|
|
inlineListToHtml opts (readTeXMath str) >>=
|
2009-04-25 00:29:58 +00:00
|
|
|
|
return . (thespan ! [theclass "math"]) )
|
2008-08-13 03:02:42 +00:00
|
|
|
|
(TeX str) -> case writerHTMLMathMethod opts of
|
|
|
|
|
LaTeXMathML _ -> do modify (\st -> st {stMath = True})
|
|
|
|
|
return $ primHtml str
|
|
|
|
|
_ -> return noHtml
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(HtmlInline str) -> return $ primHtml str
|
2008-07-13 17:08:55 +00:00
|
|
|
|
(Link [Code str] (s,_)) | "mailto:" `isPrefixOf` s ->
|
|
|
|
|
return $ obfuscateLink opts str s
|
|
|
|
|
(Link txt (s,_)) | "mailto:" `isPrefixOf` s -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
linkText <- inlineListToHtml opts txt
|
2008-07-13 17:08:55 +00:00
|
|
|
|
return $ obfuscateLink opts (show linkText) s
|
|
|
|
|
(Link txt (s,tit)) -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
linkText <- inlineListToHtml opts txt
|
2008-07-13 17:08:55 +00:00
|
|
|
|
return $ anchor ! ([href s] ++
|
2007-11-03 23:27:58 +00:00
|
|
|
|
if null tit then [] else [title tit]) $
|
|
|
|
|
linkText
|
2008-07-13 17:08:55 +00:00
|
|
|
|
(Image txt (s,tit)) -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
alternate <- inlineListToHtml opts txt
|
|
|
|
|
let alternate' = renderFragment opts alternate
|
2008-07-13 17:08:55 +00:00
|
|
|
|
let attributes = [src s] ++
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(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
|
|
|
|
|
(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)}
|
2009-12-31 01:47:08 +00:00
|
|
|
|
return $ sup <<
|
|
|
|
|
anchor ! [href ("#" ++ writerIdentifierPrefix opts ++ "fn" ++ ref),
|
|
|
|
|
theclass "footnoteRef",
|
|
|
|
|
prefixedId opts ("fnref" ++ ref)] << ref
|
2008-08-04 03:15:12 +00:00
|
|
|
|
(Cite _ il) -> inlineListToHtml opts il
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
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.
|
2009-12-05 17:56:02 +00:00
|
|
|
|
let backlink = [HtmlInline $ " <a href=\"#" ++ writerIdentifierPrefix opts ++ "fnref" ++ ref ++
|
2007-11-03 23:27:58 +00:00
|
|
|
|
"\" class=\"footnoteBackLink\"" ++
|
2010-01-01 04:11:48 +00:00
|
|
|
|
" title=\"Jump back to footnote " ++ ref ++ "\">↩</a>"]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
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'
|
2009-12-05 17:56:02 +00:00
|
|
|
|
return $ li ! [prefixedId opts ("fn" ++ ref)] $ contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|