git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #2081 from lierdakil/docx-content-title

Docx Writer: Take TOC title from toc-title metadata field

commit81fa3ef2ee2c4e918cb4d01d324dc288e7439363 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 14 Apr 2015, 09:39 (12 years ago) parent693bc5f2337ef6
1 file changed+15−1
src/Text/Pandoc/Writers/Docx.hs+15−1
src/Text/Pandoc/Writers/Docx.hs+15−1
@@ -110,6 +110,7 @@ data WriterState = WriterState{
110110 , stPrintWidth :: Integer
111111 , stStyleMaps :: StyleMaps
112112 , stFirstPara :: Bool
113+ , stTocTitle :: [Inline]
113114 }
114115
115116defaultWriterState :: WriterState
@@ -131,6 +132,7 @@ defaultWriterState = WriterState{
131132 , stPrintWidth = 1
132133 , stStyleMaps = defaultStyleMaps
133134 , stFirstPara = False
135+ , stTocTitle = normalizeInlines [Str "Table of Contents"]
134136 }
135137
136138type WS a = StateT WriterState IO a
@@ -193,6 +195,13 @@ isValidChar (ord -> c)
193195 | 0x10000 <= c && c <= 0x10FFFF = True
194196 | otherwise = False
195197
198+metaValueToInlines :: MetaValue -> [Inline]
199+metaValueToInlines (MetaString s) = normalizeInlines [Str s]
200+metaValueToInlines (MetaInlines ils) = ils
201+metaValueToInlines (MetaBlocks bs) = query return bs
202+metaValueToInlines (MetaBool b) = [Str $ show b]
203+metaValueToInlines _ = []
204+
196205-- | Produce an Docx file from a Pandoc document.
197206writeDocx :: WriterOptions -- ^ Writer options
198207 -> Pandoc -- ^ Document to convert
@@ -235,11 +244,15 @@ writeDocx opts doc@(Pandoc meta _) = do
235244 -- parse styledoc for heading styles
236245 let styleMaps = getStyleMaps styledoc
237246
247+ let tocTitle = fromMaybe (stTocTitle defaultWriterState) $
248+ metaValueToInlines <$> lookupMeta "toc-title" meta
249+
238250 ((contents, footnotes), st) <- runStateT (writeOpenXML opts{writerWrapText = False} doc')
239251 defaultWriterState{ stChangesAuthor = fromMaybe "unknown" username
240252 , stChangesDate = formatTime defaultTimeLocale "%FT%XZ" utctime
241253 , stPrintWidth = (maybe 420 (\x -> quot x 20) pgContentWidth)
242254 , stStyleMaps = styleMaps
255+ , stTocTitle = tocTitle
243256 }
244257 let epochtime = floor $ utcTimeToPOSIXSeconds utctime
245258 let imgs = M.elems $ stImages st
@@ -626,7 +639,8 @@ makeTOC :: WriterOptions -> WS [Element]
626639makeTOC opts | writerTableOfContents opts = do
627640 let depth = "1-"++(show (writerTOCDepth opts))
628641 let tocCmd = "TOC \\o \""++depth++"\" \\h \\z \\u"
629− title <- withParaPropM (pStyleM "TOC Heading") (blocksToOpenXML opts [Para [Str "Table of Contents"]])
642+ tocTitle <- gets stTocTitle
643+ title <- withParaPropM (pStyleM "TOC Heading") (blocksToOpenXML opts [Para tocTitle])
630644 return $
631645 [mknode "w:sdt" [] ([
632646 mknode "w:sdtPr" [] (