2011-12-15 21:17:32 -08:00
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2008-08-01 21:00:50 +00:00
|
|
|
|
{-# OPTIONS_GHC -fno-warn-deprecations #-}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
{-
|
2010-03-23 13:31:09 -07:00
|
|
|
|
Copyright (C) 2006-2010 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
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
{- |
|
2011-04-16 10:37:47 -07:00
|
|
|
|
Module : Text.Pandoc.Writers.HTML
|
2010-03-23 13:31:09 -07:00
|
|
|
|
Copyright : Copyright (C) 2006-2010 John MacFarlane
|
2011-04-16 10:37:47 -07:00
|
|
|
|
License : GNU GPL, version 2 or above
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
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
|
2010-07-16 00:23:13 -07:00
|
|
|
|
import Text.Pandoc.Highlighting ( highlightHtml, defaultHighlightingCss )
|
2011-12-17 23:58:15 -08:00
|
|
|
|
import Text.Pandoc.XML (stripTags, escapeStringForXML)
|
2010-07-15 19:01:00 -07:00
|
|
|
|
import Network.HTTP ( urlEncode )
|
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 )
|
2011-12-15 21:17:32 -08:00
|
|
|
|
import Data.String ( fromString )
|
2009-04-25 00:29:58 +00:00
|
|
|
|
import Data.Maybe ( catMaybes )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Control.Monad.State
|
2011-12-15 21:17:32 -08:00
|
|
|
|
import Text.Blaze
|
2011-12-20 11:25:26 -08:00
|
|
|
|
import qualified Text.Blaze.Html5 as H5
|
|
|
|
|
import qualified Text.Blaze.XHtml1.Transitional as H
|
|
|
|
|
import qualified Text.Blaze.XHtml1.Transitional.Attributes as A
|
2011-12-15 21:17:32 -08:00
|
|
|
|
import Text.Blaze.Renderer.String (renderHtml)
|
2010-03-18 06:45:56 +00:00
|
|
|
|
import Text.TeXMath
|
|
|
|
|
import Text.XML.Light.Output
|
2011-07-16 10:11:04 -07:00
|
|
|
|
import System.FilePath (takeExtension)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
import Data.Monoid (mempty, mconcat)
|
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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-12-15 21:17:32 -08:00
|
|
|
|
strToHtml :: String -> Html
|
2011-12-17 23:58:15 -08:00
|
|
|
|
strToHtml = preEscapedString . escapeStringForXML
|
|
|
|
|
-- strToHtml = toHtml
|
2008-07-27 03:54:07 +00:00
|
|
|
|
|
2011-02-03 17:30:38 -08:00
|
|
|
|
-- | Hard linebreak.
|
2011-02-04 19:27:53 -08:00
|
|
|
|
nl :: WriterOptions -> Html
|
|
|
|
|
nl opts = if writerWrapText opts
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then preEscapedString "\n"
|
|
|
|
|
else mempty
|
2011-02-03 17:30:38 -08: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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else renderHtml 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
|
2010-03-20 17:14:18 +00:00
|
|
|
|
-> State WriterState (Html, [Html], Html, Maybe Html, Html, [(String,String)])
|
2009-12-31 01:13:26 +00:00
|
|
|
|
pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
|
|
|
|
|
let standalone = writerStandalone opts
|
|
|
|
|
tit <- if standalone
|
|
|
|
|
then inlineListToHtml opts title'
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else return mempty
|
2009-12-31 01:13:26 +00:00
|
|
|
|
auths <- if standalone
|
|
|
|
|
then mapM (inlineListToHtml opts) authors'
|
2011-04-16 10:37:47 -07:00
|
|
|
|
else return []
|
2009-12-31 01:13:26 +00:00
|
|
|
|
date <- if standalone
|
|
|
|
|
then inlineListToHtml opts date'
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else return mempty
|
2011-10-02 11:49:56 -07:00
|
|
|
|
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
|
2011-04-16 12:16:24 -07:00
|
|
|
|
let sects = hierarchicalize $
|
|
|
|
|
if writerSlideVariant opts == NoSlides
|
|
|
|
|
then blocks
|
2011-10-02 11:49:56 -07:00
|
|
|
|
else ensureStartWithH1 $ splitHrule blocks
|
2011-04-13 18:36:21 -07:00
|
|
|
|
toc <- if writerTableOfContents opts
|
|
|
|
|
then tableOfContents opts sects
|
|
|
|
|
else return Nothing
|
2011-12-15 21:17:32 -08:00
|
|
|
|
blocks' <- liftM (mconcat . intersperse (nl opts)) $
|
2011-04-13 18:36:21 -07:00
|
|
|
|
mapM (elementToHtml opts) sects
|
2009-12-31 01:13:26 +00:00
|
|
|
|
st <- get
|
|
|
|
|
let notes = reverse (stNotes st)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let thebody = blocks' >> footnoteSection opts notes
|
2009-12-31 01:13:26 +00:00
|
|
|
|
let math = if stMath st
|
|
|
|
|
then case writerHTMLMathMethod opts of
|
|
|
|
|
LaTeXMathML (Just url) ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.script ! A.src (toValue url)
|
|
|
|
|
! A.type_ "text/javascript"
|
|
|
|
|
$ mempty
|
2010-03-18 06:45:56 +00:00
|
|
|
|
MathML (Just url) ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.script ! A.src (toValue url)
|
|
|
|
|
! A.type_ "text/javascript"
|
|
|
|
|
$ mempty
|
2010-10-26 21:06:51 -07:00
|
|
|
|
MathJax url ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.script ! A.src (toValue url)
|
|
|
|
|
! A.type_ "text/javascript"
|
|
|
|
|
$ mempty
|
2009-12-31 01:13:26 +00:00
|
|
|
|
JsMath (Just url) ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.script ! A.src (toValue url)
|
|
|
|
|
! A.type_ "text/javascript"
|
|
|
|
|
$ mempty
|
2010-03-18 06:45:56 +00:00
|
|
|
|
_ -> case lookup "mathml-script" (writerVariables opts) of
|
2011-04-16 10:37:47 -07:00
|
|
|
|
Just s ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.script ! A.type_ "text/javascript"
|
|
|
|
|
$ preEscapedString
|
|
|
|
|
("/*<![CDATA[*/\n" ++ s ++ "/*]]>*/\n")
|
|
|
|
|
Nothing -> mempty
|
|
|
|
|
else mempty
|
2010-07-16 00:23:13 -07:00
|
|
|
|
let newvars = [("highlighting-css", defaultHighlightingCss) |
|
|
|
|
|
stHighlighting st] ++
|
2011-12-15 21:17:32 -08:00
|
|
|
|
[("math", renderHtml math) | stMath st]
|
2009-12-31 01:13:26 +00:00
|
|
|
|
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
|
2010-03-20 17:14:18 +00:00
|
|
|
|
-> Maybe Html
|
2009-12-31 01:13:26 +00:00
|
|
|
|
-> 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 =
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let title' = renderHtml tit
|
|
|
|
|
authors = map renderHtml auths
|
|
|
|
|
date' = renderHtml date
|
2009-12-31 01:13:26 +00:00
|
|
|
|
variables = writerVariables opts ++ newvars
|
|
|
|
|
context = variables ++
|
2011-12-15 21:17:32 -08:00
|
|
|
|
[ ("body", dropWhile (=='\n') $ renderHtml body')
|
2011-11-29 13:31:12 -08:00
|
|
|
|
, ("pagetitle", stripTags title')
|
|
|
|
|
, ("title", title')
|
2011-07-23 12:04:31 -07:00
|
|
|
|
, ("date", date')
|
2011-11-29 13:31:12 -08:00
|
|
|
|
, ("date-meta", stripTags date')
|
2011-07-23 12:04:31 -07:00
|
|
|
|
, ("idprefix", writerIdentifierPrefix opts)
|
2011-07-28 17:35:26 -07:00
|
|
|
|
, ("slidy-url", "http://www.w3.org/Talks/Tools/Slidy2")
|
2011-11-20 12:11:38 -08:00
|
|
|
|
, ("s5-url", "s5/default") ] ++
|
2011-01-11 20:37:06 -08:00
|
|
|
|
[ ("html5","true") | writerHtml5 opts ] ++
|
2010-03-20 17:14:18 +00:00
|
|
|
|
(case toc of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
Just t -> [ ("toc", renderHtml t)]
|
2010-03-20 17:14:18 +00:00
|
|
|
|
Nothing -> []) ++
|
2011-11-29 13:31:12 -08:00
|
|
|
|
[ ("author", a) | a <- authors ] ++
|
|
|
|
|
[ ("author-meta", stripTags 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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
prefixedId :: WriterOptions -> String -> Attribute
|
|
|
|
|
prefixedId opts s = A.id $ toValue $ writerIdentifierPrefix opts ++ s
|
2009-12-05 17:56:02 +00:00
|
|
|
|
|
2011-02-04 19:27:53 -08:00
|
|
|
|
-- | Replacement for Text.XHtml's unordList.
|
|
|
|
|
unordList :: WriterOptions -> ([Html] -> Html)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
unordList opts items = H.ul $ mconcat $ toListItems opts items
|
2011-02-04 19:27:53 -08:00
|
|
|
|
|
|
|
|
|
-- | Replacement for Text.XHtml's ordList.
|
|
|
|
|
ordList :: WriterOptions -> ([Html] -> Html)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
ordList opts items = H.ol $ mconcat $ toListItems opts items
|
2011-02-04 19:27:53 -08:00
|
|
|
|
|
2009-04-25 00:29:58 +00:00
|
|
|
|
-- | Construct table of contents from list of elements.
|
2010-03-20 17:14:18 +00:00
|
|
|
|
tableOfContents :: WriterOptions -> [Element] -> State WriterState (Maybe Html)
|
|
|
|
|
tableOfContents _ [] = return Nothing
|
2009-04-25 00:29:58 +00:00
|
|
|
|
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
|
2010-01-09 03:13:08 +00:00
|
|
|
|
let tocList = catMaybes contents
|
2010-03-20 17:14:18 +00:00
|
|
|
|
return $ if null tocList
|
|
|
|
|
then Nothing
|
2011-07-23 12:04:31 -07:00
|
|
|
|
else Just $ unordList opts tocList
|
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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then (H.span ! A.class_ "toc-section-number" $ toHtml $ showSecNum num) >>
|
|
|
|
|
preEscapedString " "
|
|
|
|
|
else mempty
|
|
|
|
|
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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then mempty
|
2011-02-04 19:27:53 -08:00
|
|
|
|
else unordList opts subHeads
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ Just $ (H.a ! A.href (toValue $ "#" ++ writerIdentifierPrefix opts ++ id')
|
|
|
|
|
$ toHtml txt) >> subList
|
2009-04-25 00:29:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert an Element to Html.
|
|
|
|
|
elementToHtml :: WriterOptions -> Element -> State WriterState Html
|
2011-04-16 10:37:47 -07:00
|
|
|
|
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-12-09 04:58:29 +00:00
|
|
|
|
modify $ \st -> st{stSecNum = num} -- update section number
|
|
|
|
|
header' <- blockToHtml opts (Header level title')
|
2011-03-28 16:54:37 -07:00
|
|
|
|
innerContents <- mapM (elementToHtml opts) elements
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let header'' = if (writerStrictMarkdown opts ||
|
|
|
|
|
writerSectionDivs opts ||
|
|
|
|
|
writerSlideVariant opts == S5Slides)
|
|
|
|
|
then header'
|
|
|
|
|
else header' ! prefixedId opts id'
|
2010-07-15 19:01:00 -07:00
|
|
|
|
let stuff = header'' : innerContents
|
2011-04-16 12:16:24 -07:00
|
|
|
|
let slide = writerSlideVariant opts /= NoSlides && level == 1
|
2011-10-02 11:49:56 -07:00
|
|
|
|
let titleSlide = slide && null elements
|
2011-12-18 13:09:58 -08:00
|
|
|
|
let classes = ["titleslide" | titleSlide] ++ ["slide" | slide] ++
|
|
|
|
|
["level" ++ show level]
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let inNl x = mconcat $ nl opts : intersperse (nl opts) x ++ [nl opts]
|
2011-12-18 13:09:58 -08:00
|
|
|
|
let secttag = if writerHtml5 opts
|
2011-12-20 11:25:26 -08:00
|
|
|
|
then H5.section ! A.class_ (toValue $ unwords classes)
|
2011-12-18 13:09:58 -08:00
|
|
|
|
else H.div ! A.class_ (toValue $ unwords ("section":classes))
|
2011-10-02 11:49:56 -07:00
|
|
|
|
return $ if writerSectionDivs opts || slide
|
2011-12-18 13:09:58 -08:00
|
|
|
|
then secttag ! prefixedId opts id' $ inNl stuff
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else mconcat $ intersperse (nl opts) stuff
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Note blocks to a footnote <div>.
|
|
|
|
|
-- Assumes notes are sorted.
|
2011-02-04 19:27:53 -08:00
|
|
|
|
footnoteSection :: WriterOptions -> [Html] -> Html
|
|
|
|
|
footnoteSection opts notes =
|
2011-04-16 10:37:47 -07:00
|
|
|
|
if null notes
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then mempty
|
|
|
|
|
else nl opts >> (container
|
|
|
|
|
$ nl opts >> H.hr >> nl opts >>
|
2011-12-20 11:36:51 -08:00
|
|
|
|
H.ol (mconcat notes >> nl opts) >> nl opts)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
where container x = if writerHtml5 opts
|
2011-12-20 11:25:26 -08:00
|
|
|
|
then H5.section ! A.class_ "footnotes" $ x
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else if writerSlideVariant opts /= NoSlides
|
|
|
|
|
then H.div ! A.class_ "footnotes slide" $ x
|
|
|
|
|
else H.div ! A.class_ "footnotes" $ x
|
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)
|
2011-04-16 10:37:47 -07:00
|
|
|
|
parseMailto _ = Nothing
|
2008-02-09 03:21:04 +00:00
|
|
|
|
|
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 =
|
2011-12-15 21:17:32 -08:00
|
|
|
|
H.a ! A.href (toValue s) $ toHtml 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
|
2011-04-16 10:37:47 -07:00
|
|
|
|
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 '@'
|
2011-04-16 10:37:47 -07:00
|
|
|
|
(linkText, altText) =
|
2008-07-13 17:08:55 +00:00
|
|
|
|
if txt == drop 7 s' -- autolink
|
|
|
|
|
then ("'<code>'+e+'</code>'", name' ++ " at " ++ domain')
|
2011-04-16 10:37:47 -07:00
|
|
|
|
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 ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
-- need to use preEscapedString or &'s are escaped to & in URL
|
|
|
|
|
preEscapedString $ "<a href=\"" ++ (obfuscateString s')
|
2008-07-13 17:08:55 +00:00
|
|
|
|
++ "\">" ++ (obfuscateString txt) ++ "</a>"
|
2009-01-24 19:58:48 +00:00
|
|
|
|
JavascriptObfuscation ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
(H.script ! A.type_ "text/javascript" $
|
|
|
|
|
preEscapedString ("\n<!--\nh='" ++
|
2011-04-16 10:37:47 -07:00
|
|
|
|
obfuscateString domain ++ "';a='" ++ at' ++ "';n='" ++
|
2008-07-13 17:08:55 +00:00
|
|
|
|
obfuscateString name' ++ "';e=n+a+h;\n" ++
|
2011-04-16 10:37:47 -07:00
|
|
|
|
"document.write('<a h'+'ref'+'=\"ma'+'ilto'+':'+e+'\">'+" ++
|
2011-12-15 21:17:32 -08:00
|
|
|
|
linkText ++ "+'<\\/'+'a'+'>');\n// -->\n")) >>
|
|
|
|
|
H.noscript (preEscapedString $ obfuscateString altText)
|
2009-01-24 19:58:48 +00:00
|
|
|
|
_ -> error $ "Unknown obfuscation method: " ++ show meth
|
2011-12-15 21:17:32 -08:00
|
|
|
|
_ -> H.a ! A.href (toValue s) $ toHtml txt -- malformed email
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Obfuscate character as entity.
|
|
|
|
|
obfuscateChar :: Char -> String
|
2011-04-16 10:37:47 -07:00
|
|
|
|
obfuscateChar char =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
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
|
|
|
|
|
|
2011-12-15 21:17:32 -08:00
|
|
|
|
attrsToHtml :: WriterOptions -> Attr -> [Attribute]
|
2011-01-26 20:44:25 -08:00
|
|
|
|
attrsToHtml opts (id',classes',keyvals) =
|
2011-12-15 21:17:32 -08:00
|
|
|
|
[A.class_ (toValue $ unwords classes') | not (null classes')] ++
|
2011-01-26 20:44:25 -08:00
|
|
|
|
[prefixedId opts id' | not (null id')] ++
|
2011-12-15 21:17:32 -08:00
|
|
|
|
map (\(x,y) -> customAttribute (fromString x) (toValue y)) keyvals
|
2011-01-26 20:44:25 -08:00
|
|
|
|
|
2011-07-16 10:11:04 -07:00
|
|
|
|
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
|
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Convert Pandoc block element to HTML.
|
|
|
|
|
blockToHtml :: WriterOptions -> Block -> State WriterState Html
|
2011-12-15 21:17:32 -08:00
|
|
|
|
blockToHtml _ Null = return mempty
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (Plain lst) = inlineListToHtml opts lst
|
|
|
|
|
blockToHtml opts (Para [Image txt (s,tit)]) = do
|
2010-03-16 04:06:25 +00:00
|
|
|
|
img <- inlineToHtml opts (Image txt (s,tit))
|
|
|
|
|
capt <- inlineListToHtml opts txt
|
2011-01-11 22:25:57 -08:00
|
|
|
|
return $ if writerHtml5 opts
|
2011-12-20 11:25:26 -08:00
|
|
|
|
then H5.figure $ mconcat
|
|
|
|
|
[nl opts, img, H5.figcaption capt, nl opts]
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else H.div ! A.class_ "figure" $ mconcat
|
|
|
|
|
[nl opts, img, H.p ! A.class_ "caption" $ capt,
|
2011-02-04 19:27:53 -08:00
|
|
|
|
nl opts]
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (Para lst) = do
|
2011-02-03 17:30:38 -08:00
|
|
|
|
contents <- inlineListToHtml opts lst
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.p contents
|
|
|
|
|
blockToHtml _ (RawBlock "html" str) = return $ preEscapedString str
|
|
|
|
|
blockToHtml _ (RawBlock _ _) = return mempty
|
|
|
|
|
blockToHtml _ (HorizontalRule) = return H.hr
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (CodeBlock (id',classes,keyvals) rawCode) = do
|
2009-11-21 04:40:59 +00:00
|
|
|
|
let classes' = if writerLiterateHaskell opts
|
|
|
|
|
then classes
|
|
|
|
|
else filter (/= "literate") classes
|
2011-01-29 16:04:07 -08:00
|
|
|
|
case highlightHtml False (id',classes',keyvals) rawCode of
|
2011-12-22 00:33:38 -08:00
|
|
|
|
Nothing -> let attrs = attrsToHtml opts (id', classes', keyvals)
|
2010-07-07 10:28:25 -07:00
|
|
|
|
addBird = if "literate" `elem` classes'
|
2010-06-28 19:47:29 -07:00
|
|
|
|
then unlines . map ("> " ++) . lines
|
|
|
|
|
else unlines . lines
|
2011-12-15 21:17:32 -08:00
|
|
|
|
in return $ foldl (!) H.pre attrs $ H.code
|
|
|
|
|
$ toHtml $ addBird rawCode
|
2011-12-22 00:33:38 -08:00
|
|
|
|
Just h -> modify (\st -> st{ stHighlighting = True }) >>
|
2011-02-04 19:27:53 -08:00
|
|
|
|
return h
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (BlockQuote blocks) =
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- in S5, treat list in blockquote specially
|
2011-04-16 10:37:47 -07:00
|
|
|
|
-- if default is incremental, make it nonincremental;
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- otherwise incremental
|
2010-07-13 20:44:56 -07:00
|
|
|
|
if writerSlideVariant opts /= NoSlides
|
2007-11-03 23:27:58 +00:00
|
|
|
|
then let inc = not (writerIncremental opts) in
|
2011-04-16 10:37:47 -07:00
|
|
|
|
case blocks of
|
2007-11-03 23:27:58 +00:00
|
|
|
|
[BulletList lst] -> blockToHtml (opts {writerIncremental = inc})
|
|
|
|
|
(BulletList lst)
|
2011-04-16 10:37:47 -07:00
|
|
|
|
[OrderedList attribs lst] ->
|
2007-11-03 23:27:58 +00:00
|
|
|
|
blockToHtml (opts {writerIncremental = inc})
|
|
|
|
|
(OrderedList attribs lst)
|
2011-02-04 23:25:28 -08:00
|
|
|
|
_ -> do contents <- blockListToHtml opts blocks
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.blockquote
|
|
|
|
|
$ nl opts >> contents >> nl opts
|
2011-02-03 17:30:38 -08:00
|
|
|
|
else do
|
|
|
|
|
contents <- blockListToHtml opts blocks
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.blockquote $ nl opts >> contents >> nl opts
|
2011-04-16 10:37:47 -07:00
|
|
|
|
blockToHtml opts (Header level lst) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- inlineListToHtml opts lst
|
2009-12-09 04:58:29 +00:00
|
|
|
|
secnum <- liftM stSecNum get
|
|
|
|
|
let contents' = if writerNumberSections opts
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then (H.span ! A.class_ "header-section-number" $ toHtml $ showSecNum secnum) >>
|
|
|
|
|
strToHtml " " >> contents
|
2009-12-09 04:58:29 +00:00
|
|
|
|
else contents
|
|
|
|
|
let contents'' = if writerTableOfContents opts
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then H.a ! A.href (toValue $ "#" ++ writerIdentifierPrefix opts ++ "TOC") $ contents'
|
2009-12-09 04:58:29 +00:00
|
|
|
|
else contents'
|
2011-02-03 17:30:38 -08:00
|
|
|
|
return $ (case level of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
1 -> H.h1 contents''
|
|
|
|
|
2 -> H.h2 contents''
|
|
|
|
|
3 -> H.h3 contents''
|
|
|
|
|
4 -> H.h4 contents''
|
|
|
|
|
5 -> H.h5 contents''
|
|
|
|
|
6 -> H.h6 contents''
|
|
|
|
|
_ -> H.p contents'')
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (BulletList lst) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- mapM (blockListToHtml opts) lst
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let lst' = unordList opts contents
|
|
|
|
|
let lst'' = if writerIncremental opts
|
|
|
|
|
then lst' ! A.class_ "incremental"
|
|
|
|
|
else lst'
|
|
|
|
|
return lst''
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (OrderedList (startnum, numstyle, _) lst) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- mapM (blockListToHtml opts) lst
|
|
|
|
|
let numstyle' = camelCaseToHyphenated $ show numstyle
|
|
|
|
|
let attribs = (if writerIncremental opts
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then [A.class_ "incremental"]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else []) ++
|
|
|
|
|
(if startnum /= 1
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then [A.start $ toValue startnum]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else []) ++
|
|
|
|
|
(if numstyle /= DefaultStyle
|
2011-01-11 22:25:57 -08:00
|
|
|
|
then if writerHtml5 opts
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then [A.type_ $
|
2011-01-11 22:25:57 -08:00
|
|
|
|
case numstyle of
|
|
|
|
|
Decimal -> "1"
|
|
|
|
|
LowerAlpha -> "a"
|
|
|
|
|
UpperAlpha -> "A"
|
|
|
|
|
LowerRoman -> "i"
|
|
|
|
|
UpperRoman -> "I"
|
|
|
|
|
_ -> "1"]
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else [A.style $ toValue $ "list-style-type: " ++
|
2011-01-11 22:25:57 -08:00
|
|
|
|
numstyle']
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else [])
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ foldl (!) (ordList opts contents) attribs
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (DefinitionList lst) = do
|
2009-12-07 08:26:53 +00:00
|
|
|
|
contents <- mapM (\(term, defs) ->
|
2011-12-15 21:17:32 -08:00
|
|
|
|
do term' <- liftM (H.dt) $ inlineListToHtml opts term
|
|
|
|
|
defs' <- mapM ((liftM (\x -> H.dd $ (x >> nl opts))) .
|
2011-02-04 19:27:53 -08:00
|
|
|
|
blockListToHtml opts) defs
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ mconcat $ nl opts : term' : nl opts : defs') lst
|
|
|
|
|
let lst' = H.dl $ mconcat contents >> nl opts
|
|
|
|
|
let lst'' = if writerIncremental opts
|
|
|
|
|
then lst' ! A.class_ "incremental"
|
|
|
|
|
else lst'
|
|
|
|
|
return lst''
|
2011-02-04 23:03:38 -08:00
|
|
|
|
blockToHtml opts (Table capt aligns widths headers rows') = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
captionDoc <- if null capt
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then return mempty
|
2011-02-04 23:25:28 -08:00
|
|
|
|
else do
|
|
|
|
|
cs <- inlineListToHtml opts capt
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.caption cs >> nl opts
|
2010-03-10 06:19:53 +00:00
|
|
|
|
let percent w = show (truncate (100*w) :: Integer) ++ "%"
|
|
|
|
|
let coltags = if all (== 0.0) widths
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then mempty
|
|
|
|
|
else mconcat $ map (\w ->
|
|
|
|
|
if writerHtml5 opts
|
|
|
|
|
then H.col ! A.style (toValue $ "width: " ++ percent w)
|
|
|
|
|
else H.col ! A.width (toValue $ percent w) >> nl opts)
|
|
|
|
|
widths
|
2010-03-10 06:19:53 +00:00
|
|
|
|
head' <- if all null headers
|
2011-12-15 21:17:32 -08:00
|
|
|
|
then return mempty
|
2011-02-04 23:25:28 -08:00
|
|
|
|
else do
|
|
|
|
|
contents <- tableRowToHtml opts aligns 0 headers
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.thead (nl opts >> contents) >> nl opts
|
|
|
|
|
body' <- liftM (\x -> H.tbody (nl opts >> mconcat x)) $
|
2011-01-11 22:25:57 -08:00
|
|
|
|
zipWithM (tableRowToHtml opts aligns) [1..] rows'
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.table $ nl opts >> captionDoc >> coltags >> head' >>
|
|
|
|
|
body' >> nl opts
|
2008-07-13 17:08:55 +00:00
|
|
|
|
|
2010-03-07 19:35:14 +00:00
|
|
|
|
tableRowToHtml :: WriterOptions
|
2011-01-11 22:25:57 -08:00
|
|
|
|
-> [Alignment]
|
2010-03-10 06:19:53 +00:00
|
|
|
|
-> Int
|
|
|
|
|
-> [[Block]]
|
2008-07-13 17:08:55 +00:00
|
|
|
|
-> State WriterState Html
|
2011-01-11 22:25:57 -08:00
|
|
|
|
tableRowToHtml opts aligns rownum cols' = do
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let mkcell = if rownum == 0 then H.th else H.td
|
2010-03-07 19:35:14 +00:00
|
|
|
|
let rowclass = case rownum of
|
|
|
|
|
0 -> "header"
|
|
|
|
|
x | x `rem` 2 == 1 -> "odd"
|
|
|
|
|
_ -> "even"
|
2011-04-16 10:37:47 -07:00
|
|
|
|
cols'' <- sequence $ zipWith
|
|
|
|
|
(\alignment item -> tableItemToHtml opts mkcell alignment item)
|
2011-01-11 22:25:57 -08:00
|
|
|
|
aligns cols'
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ (H.tr ! A.class_ rowclass $ nl opts >> mconcat cols'')
|
|
|
|
|
>> nl opts
|
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
|
|
|
|
tableItemToHtml :: WriterOptions
|
|
|
|
|
-> (Html -> Html)
|
2011-01-11 22:25:57 -08:00
|
|
|
|
-> Alignment
|
2008-07-13 17:08:55 +00:00
|
|
|
|
-> [Block]
|
|
|
|
|
-> State WriterState Html
|
2010-03-10 06:19:53 +00:00
|
|
|
|
tableItemToHtml opts tag' align' item = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
contents <- blockListToHtml opts item
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let alignStr = alignmentToString align'
|
|
|
|
|
let attribs = if writerHtml5 opts
|
|
|
|
|
then A.style (toValue $ "text-align: " ++ alignStr ++ ";")
|
2011-12-20 11:25:26 -08:00
|
|
|
|
else A.align (toValue alignStr)
|
2011-12-17 23:52:59 -08:00
|
|
|
|
return $ (tag' ! attribs $ contents) >> nl opts
|
2011-02-04 19:27:53 -08:00
|
|
|
|
|
|
|
|
|
toListItems :: WriterOptions -> [Html] -> [Html]
|
|
|
|
|
toListItems opts items = map (toListItem opts) items ++ [nl opts]
|
|
|
|
|
|
|
|
|
|
toListItem :: WriterOptions -> Html -> Html
|
2011-12-15 21:17:32 -08:00
|
|
|
|
toListItem opts item = nl opts >> H.li item
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
blockListToHtml :: WriterOptions -> [Block] -> State WriterState Html
|
2011-02-04 19:27:53 -08:00
|
|
|
|
blockListToHtml opts lst =
|
|
|
|
|
mapM (blockToHtml opts) lst >>=
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return . mconcat . intersperse (nl opts)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Pandoc inline elements to HTML.
|
|
|
|
|
inlineListToHtml :: WriterOptions -> [Inline] -> State WriterState Html
|
2011-04-16 10:37:47 -07:00
|
|
|
|
inlineListToHtml opts lst =
|
2011-12-15 21:17:32 -08:00
|
|
|
|
mapM (inlineToHtml opts) lst >>= return . mconcat
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc inline element to HTML.
|
|
|
|
|
inlineToHtml :: WriterOptions -> Inline -> State WriterState Html
|
|
|
|
|
inlineToHtml opts inline =
|
2011-04-16 10:37:47 -07:00
|
|
|
|
case inline of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
(Str str) -> return $ strToHtml str
|
|
|
|
|
(Space) -> return $ strToHtml " "
|
|
|
|
|
(LineBreak) -> return H.br
|
|
|
|
|
(EmDash) -> return $ strToHtml "—"
|
|
|
|
|
(EnDash) -> return $ strToHtml "–"
|
|
|
|
|
(Ellipses) -> return $ strToHtml "…"
|
|
|
|
|
(Apostrophe) -> return $ strToHtml "’"
|
|
|
|
|
(Emph lst) -> inlineListToHtml opts lst >>= return . H.em
|
|
|
|
|
(Strong lst) -> inlineListToHtml opts lst >>= return . H.strong
|
2011-01-29 16:11:16 -08:00
|
|
|
|
(Code attr str) -> case highlightHtml True attr str of
|
2011-12-22 00:33:38 -08:00
|
|
|
|
Nothing -> return
|
2011-12-15 21:17:32 -08:00
|
|
|
|
$ foldl (!) H.code (attrsToHtml opts attr)
|
|
|
|
|
$ strToHtml str
|
2011-12-22 00:33:38 -08:00
|
|
|
|
Just h -> return h
|
2008-02-24 18:15:36 +00:00
|
|
|
|
(Strikeout lst) -> inlineListToHtml opts lst >>=
|
2011-12-18 11:08:04 -08:00
|
|
|
|
return . H.del
|
2008-07-15 23:25:49 +00:00
|
|
|
|
(SmallCaps lst) -> inlineListToHtml opts lst >>=
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return . (H.span ! A.style "font-variant: small-caps;")
|
|
|
|
|
(Superscript lst) -> inlineListToHtml opts lst >>= return . H.sup
|
|
|
|
|
(Subscript lst) -> inlineListToHtml opts lst >>= return . H.sub
|
2007-11-03 23:27:58 +00:00
|
|
|
|
(Quoted quoteType lst) ->
|
|
|
|
|
let (leftQuote, rightQuote) = case quoteType of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
SingleQuote -> (strToHtml "‘",
|
|
|
|
|
strToHtml "’")
|
|
|
|
|
DoubleQuote -> (strToHtml "“",
|
|
|
|
|
strToHtml "”")
|
2007-11-03 23:27:58 +00:00
|
|
|
|
in do contents <- inlineListToHtml opts lst
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ leftQuote >> contents >> rightQuote
|
2011-04-16 10:37:47 -07:00
|
|
|
|
(Math t str) -> modify (\st -> st {stMath = True}) >>
|
2007-12-02 00:36:32 +00:00
|
|
|
|
(case writerHTMLMathMethod opts of
|
2011-04-16 10:37:47 -07: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
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.span ! A.class_ "LaTeX" $
|
2010-07-15 19:01:00 -07:00
|
|
|
|
case t of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
InlineMath -> toHtml ("$" ++ str ++ "$")
|
|
|
|
|
DisplayMath -> toHtml ("$$" ++ str ++ "$$")
|
2010-07-15 19:01:00 -07:00
|
|
|
|
JsMath _ -> do
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let m = preEscapedString str
|
2010-07-15 19:01:00 -07:00
|
|
|
|
return $ case t of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
InlineMath -> H.span ! A.class_ "math" $ m
|
|
|
|
|
DisplayMath -> H.div ! A.class_ "math" $ m
|
2010-07-15 19:01:00 -07:00
|
|
|
|
WebTeX url -> do
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let m = H.img ! A.style "vertical-align:middle"
|
|
|
|
|
! A.src (toValue $ url ++ urlEncode str)
|
|
|
|
|
! A.alt (toValue str)
|
|
|
|
|
! A.title (toValue str)
|
2010-07-15 19:01:00 -07:00
|
|
|
|
return $ case t of
|
|
|
|
|
InlineMath -> m
|
2011-12-15 21:17:32 -08:00
|
|
|
|
DisplayMath -> H.br >> m >> H.br
|
2007-12-02 00:36:32 +00:00
|
|
|
|
GladTeX ->
|
2010-08-01 08:30:04 -07:00
|
|
|
|
return $ case t of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
InlineMath -> preEscapedString "<EQ ENV=\"math\">" >> toHtml str >> preEscapedString "</EQ>"
|
|
|
|
|
DisplayMath -> preEscapedString "<EQ ENV=\"displaymath\">" >> toHtml str >> preEscapedString "</EQ>"
|
2010-03-18 06:45:56 +00:00
|
|
|
|
MathML _ -> do
|
|
|
|
|
let dt = if t == InlineMath
|
|
|
|
|
then DisplayInline
|
|
|
|
|
else DisplayBlock
|
|
|
|
|
let conf = useShortEmptyTags (const False)
|
|
|
|
|
defaultConfigPP
|
|
|
|
|
case texMathToMathML dt str of
|
2011-12-15 21:17:32 -08:00
|
|
|
|
Right r -> return $ preEscapedString $
|
2010-03-18 06:45:56 +00:00
|
|
|
|
ppcElement conf r
|
2010-03-21 22:48:47 -07:00
|
|
|
|
Left _ -> inlineListToHtml opts
|
2010-07-15 19:01:00 -07:00
|
|
|
|
(readTeXMath str) >>= return .
|
2011-12-15 21:17:32 -08:00
|
|
|
|
(H.span ! A.class_ "math")
|
|
|
|
|
MathJax _ -> return $ toHtml $
|
2010-10-31 18:55:02 -07:00
|
|
|
|
case t of
|
|
|
|
|
InlineMath -> "\\(" ++ str ++ "\\)"
|
|
|
|
|
DisplayMath -> "\\[" ++ str ++ "\\]"
|
2010-07-15 19:01:00 -07:00
|
|
|
|
PlainMath -> do
|
|
|
|
|
x <- inlineListToHtml opts (readTeXMath str)
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let m = H.span ! A.class_ "math" $ x
|
2010-07-15 19:01:00 -07:00
|
|
|
|
return $ case t of
|
|
|
|
|
InlineMath -> m
|
2011-12-15 21:17:32 -08:00
|
|
|
|
DisplayMath -> H.br >> m >> H.br )
|
2011-01-23 10:55:56 -08:00
|
|
|
|
(RawInline "latex" str) -> case writerHTMLMathMethod opts of
|
|
|
|
|
LaTeXMathML _ -> do modify (\st -> st {stMath = True})
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ toHtml str
|
|
|
|
|
_ -> return mempty
|
|
|
|
|
(RawInline "html" str) -> return $ preEscapedString str
|
|
|
|
|
(RawInline _ _) -> return mempty
|
2011-01-26 20:44:25 -08:00
|
|
|
|
(Link [Code _ str] (s,_)) | "mailto:" `isPrefixOf` s ->
|
2008-07-13 17:08:55 +00:00
|
|
|
|
return $ obfuscateLink opts str s
|
|
|
|
|
(Link txt (s,_)) | "mailto:" `isPrefixOf` s -> do
|
2011-04-16 10:37:47 -07:00
|
|
|
|
linkText <- inlineListToHtml opts txt
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ obfuscateLink opts (renderHtml linkText) s
|
2008-07-13 17:08:55 +00:00
|
|
|
|
(Link txt (s,tit)) -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
linkText <- inlineListToHtml opts txt
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let link = H.a ! A.href (toValue s) $ linkText
|
|
|
|
|
return $ if null tit
|
|
|
|
|
then link
|
|
|
|
|
else link ! A.title (toValue tit)
|
2011-07-16 10:11:04 -07:00
|
|
|
|
(Image txt (s,tit)) | treatAsImage s -> do
|
2011-02-05 08:16:34 -08:00
|
|
|
|
let alternate' = stringify txt
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let attributes = [A.src $ toValue s] ++
|
2011-04-16 10:37:47 -07:00
|
|
|
|
(if null tit
|
|
|
|
|
then []
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else [A.title $ toValue tit]) ++
|
2011-04-16 10:37:47 -07:00
|
|
|
|
if null txt
|
|
|
|
|
then []
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else [A.alt $ toValue alternate']
|
|
|
|
|
return $ foldl (!) H.img attributes
|
2011-04-16 10:37:47 -07:00
|
|
|
|
-- note: null title included, as in Markdown.pl
|
2011-07-16 10:11:04 -07:00
|
|
|
|
(Image _ (s,tit)) -> do
|
2011-12-15 21:17:32 -08:00
|
|
|
|
let attributes = [A.src $ toValue s] ++
|
2011-07-16 10:11:04 -07:00
|
|
|
|
(if null tit
|
|
|
|
|
then []
|
2011-12-15 21:17:32 -08:00
|
|
|
|
else [A.title $ toValue tit])
|
2011-12-20 11:25:26 -08:00
|
|
|
|
return $ foldl (!) H5.embed attributes
|
2011-07-16 10:11:04 -07:00
|
|
|
|
-- note: null title included, as in Markdown.pl
|
2011-04-16 10:37:47 -07:00
|
|
|
|
(Note contents) -> do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
st <- get
|
|
|
|
|
let notes = stNotes st
|
|
|
|
|
let number = (length notes) + 1
|
|
|
|
|
let ref = show number
|
2011-04-16 10:37:47 -07:00
|
|
|
|
htmlContents <- blockListToNote opts ref contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- push contents onto front of notes
|
2011-04-16 10:37:47 -07:00
|
|
|
|
put $ st {stNotes = (htmlContents:notes)}
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.sup $
|
|
|
|
|
H.a ! A.href (toValue $ "#" ++ writerIdentifierPrefix opts ++ "fn" ++ ref)
|
|
|
|
|
! A.class_ "footnoteRef"
|
|
|
|
|
! prefixedId opts ("fnref" ++ ref)
|
|
|
|
|
$ toHtml ref
|
2011-11-29 12:49:39 -08:00
|
|
|
|
(Cite _ il) -> do contents <- inlineListToHtml opts il
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ H.span ! A.class_ "citation" $ contents
|
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.
|
2011-04-16 10:37:47 -07:00
|
|
|
|
let backlink = [RawInline "html" $ " <a href=\"#" ++ writerIdentifierPrefix opts ++ "fnref" ++ ref ++
|
2011-12-15 21:17:32 -08:00
|
|
|
|
"\" class=\"footnoteBackLink\">↩</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'
|
2011-12-15 21:17:32 -08:00
|
|
|
|
return $ nl opts >> (H.li ! (prefixedId opts ("fn" ++ ref)) $ contents)
|