git.iko.soy/typeable/pandoc
on master help sign in
HTML writer: Use a record for state, instead of a tuple, for

easy extensibility. git-svn-id: https://pandoc.googlecode.com/svn/trunk@769 788f1e2b-df1e-0410-8736-df70ead52e1b

commit7006d9a044c01222a0a656e3b4b374e388f3dd78 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateSun 22 Jul 2007, 20:00 (19 years ago) parentb4d289c
1 file changed+32−24
src/Text/Pandoc/Writers/HTML.hs+32−24
src/Text/Pandoc/Writers/HTML.hs+32−24
@@ -38,15 +38,11 @@ import Data.List ( isPrefixOf, partition, intersperse )
3838import Control.Monad.State
3939import Text.XHtml.Strict
4040
41−type Notes = [Html]
42−type Ids = [String]
43−type ForHeader = [Html]
44−type WriterState = (Notes, Ids, ForHeader)
45−
46−-- | Add some Html to be included in the document header.
47−addToHeader :: Html -> State WriterState ()
48−addToHeader item = modify (\(notes, ids, forheader) ->
49− (notes, ids, item:forheader))
41+data WriterState = WriterState
42+ { stNotes :: [Html] -- ^ List of notes
43+ , stIds :: [String] -- ^ List of header identifiers
44+ , stHead :: [Html] -- ^ Html to include in header
45+ } deriving Show
5046
5147-- | Convert Pandoc document to Html string.
5248writeHtmlString :: WriterOptions -> Pandoc -> String
@@ -59,7 +55,8 @@ writeHtmlString opts =
5955writeHtml :: WriterOptions -> Pandoc -> Html
6056writeHtml opts (Pandoc (Meta tit authors date) blocks) =
6157 let titlePrefix = writerTitlePrefix opts
62− topTitle = evalState (inlineListToHtml opts tit) ([],[],[])
58+ topTitle = evalState (inlineListToHtml opts tit)
59+ (WriterState {stNotes = [], stIds = [], stHead = []})
6360 topTitle' = if null titlePrefix
6461 then topTitle
6562 else titlePrefix +++ " - " +++ topTitle
@@ -81,11 +78,12 @@ writeHtml opts (Pandoc (Meta tit authors date) blocks) =
8178 toc = if writerTableOfContents opts
8279 then tableOfContents opts headerBlocks ids
8380 else noHtml
84− (blocks', (revnotes,_,forhead)) =
85− runState (blockListToHtml opts blocks) ([],ids,[])
86− head = header $ metadata +++ toHtmlFromList forhead +++
81+ (blocks', newstate) =
82+ runState (blockListToHtml opts blocks)
83+ (WriterState {stNotes = [], stIds = ids, stHead = []})
84+ head = header $ metadata +++ toHtmlFromList (stHead newstate) +++
8785 primHtml (writerHeader opts)
88− notes = reverse revnotes
86+ notes = reverse (stNotes newstate)
8987 before = primHtml $ writerIncludeBefore opts
9088 after = primHtml $ writerIncludeAfter opts
9189 thebody = before +++ titleHeader +++ toc +++ blocks' +++
@@ -96,12 +94,12 @@ writeHtml opts (Pandoc (Meta tit authors date) blocks) =
9694
9795-- | Construct table of contents from list of header blocks and identifiers.
9896-- Assumes there are as many identifiers as header blocks.
99−tableOfContents :: WriterOptions -> [Block] -> Ids -> Html
97+tableOfContents :: WriterOptions -> [Block] -> [String] -> Html
10098tableOfContents opts headers ids =
10199 let opts' = opts { writerIgnoreNotes = True }
102100 contentsTree = hierarchicalize headers
103101 contents = evalState (mapM (elementToListItem opts') contentsTree)
104− ([],ids,[])
102+ (WriterState {stNotes= [], stIds = [], stHead = []})
105103 in thediv ! [identifier "toc"] $ unordList contents
106104
107105-- | Converts an Element to a list item for a table of contents,
@@ -109,11 +107,12 @@ tableOfContents opts headers ids =
109107elementToListItem :: WriterOptions -> Element -> State WriterState Html
110108elementToListItem opts (Blk _) = return noHtml
111109elementToListItem opts (Sec headerText subsecs) = do
112− (notes, ids, forhead) <- get
110+ st <- get
111+ let ids = stIds st
113112 let (id, rest) = if null ids
114− then ("",[])
113+ then ("", [])
115114 else (head ids, tail ids)
116− put (notes, rest, forhead)
115+ put $ st {stIds = rest}
117116 txt <- inlineListToHtml opts headerText
118117 subHeads <- mapM (elementToListItem opts) subsecs
119118 let subList = if null subHeads
@@ -123,7 +122,7 @@ elementToListItem opts (Sec headerText subsecs) = do
123122
124123-- | Convert list of Note blocks to a footnote <div>.
125124-- Assumes notes are sorted.
126−footnoteSection :: WriterOptions -> Notes -> Html
125+footnoteSection :: WriterOptions -> [Html] -> Html
127126footnoteSection opts notes =
128127 if null notes
129128 then noHtml
@@ -180,6 +179,13 @@ isPunctuation c =
180179 then True
181180 else False
182181
182+-- | Add Html to document header.
183+addToHeader :: Html -> State WriterState ()
184+addToHeader item = do
185+ st <- get
186+ let current = stHead st
187+ put $ st {stHead = (item:current)}
188+
183189-- | Convert Pandoc inline list to plain text identifier.
184190inlineListToIdentifier :: [Inline] -> String
185191inlineListToIdentifier [] = ""
@@ -243,11 +249,12 @@ blockToHtml opts block =
243249 (return . blockquote)
244250 else blockListToHtml opts blocks >>= (return . blockquote)
245251 (Header level lst) -> do contents <- inlineListToHtml opts lst
246− (notes, ids, forhead) <- get
252+ st <- get
253+ let ids = stIds st
247254 let (id, rest) = if null ids
248255 then ("", [])
249256 else (head ids, tail ids)
250− put (notes, rest, forhead)
257+ put $ st {stIds = rest}
251258 let attribs = [identifier id]
252259 let headerHtml = case level of
253260 1 -> h1 contents ! attribs
@@ -369,11 +376,12 @@ inlineToHtml opts inline =
369376 if null txt then [] else [alt alternate']
370377 return $ image ! attributes
371378 -- note: null title included, as in Markdown.pl
372− (Note contents) -> do (notes, ids, forhead) <- get
379+ (Note contents) -> do st <- get
380+ let notes = stNotes st
373381 let number = (length notes) + 1
374382 let ref = show number
375383 htmlContents <- blockListToNote opts ref contents
376− put (htmlContents:notes, ids, forhead) -- push contents onto front of notes
384+ put $ st {stNotes = (htmlContents:notes)} -- push contents onto front of notes
377385 return $ anchor ! [href ("#fn" ++ ref),
378386 theclass "footnoteRef",
379387 identifier ("fnref" ++ ref)] << sup << ref