diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-07-22 10:33:33 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2020-07-22 11:53:31 -0700 |
commit | 1e84178431b3fc18de92b86c7f09f4908d955a92 (patch) | |
tree | 9426fe16b140310aa5a367d5d2b6c9fb45306e1d | |
parent | 3bccc08f6c6ac5c3973d67a4e62266c26507033d (diff) | |
download | pandoc-1e84178431b3fc18de92b86c7f09f4908d955a92.tar.gz |
Docx writer: support --number-sections.
Closes #1413.
31 files changed, 23 insertions, 5 deletions
diff --git a/MANUAL.txt b/MANUAL.txt index 7ed69b09d..5f110dc5f 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -982,7 +982,7 @@ header when requesting a document from a URL: `-N`, `--number-sections` -: Number section headings in LaTeX, ConTeXt, HTML, or EPUB output. +: Number section headings in LaTeX, ConTeXt, HTML, Docx, or EPUB output. By default, sections are not numbered. Sections with class `unnumbered` will never be numbered, even if `--number-sections` is specified. @@ -1125,6 +1125,7 @@ header when requesting a document from a URL: - Verbatim Char - Footnote Reference - Hyperlink + - Section Number Table style: diff --git a/data/docx/word/styles.xml b/data/docx/word/styles.xml index 583596266..6bb5a3f52 100644 --- a/data/docx/word/styles.xml +++ b/data/docx/word/styles.xml @@ -414,6 +414,10 @@ <w:sz w:val="22" /> </w:rPr> </w:style> + <w:style w:type="character" w:customStyle="1" w:styleId="SectionNumber"> + <w:name w:val="Section Number" /> + <w:basedOn w:val="BodyTextChar" /> + </w:style> <w:style w:type="character" w:styleId="FootnoteReference"> <w:name w:val="Footnote Reference" /> <w:basedOn w:val="BodyTextChar" /> diff --git a/src/Text/Pandoc/Writers/Docx.hs b/src/Text/Pandoc/Writers/Docx.hs index 89f100720..f448c4ce2 100644 --- a/src/Text/Pandoc/Writers/Docx.hs +++ b/src/Text/Pandoc/Writers/Docx.hs @@ -210,8 +210,11 @@ writeDocx :: (PandocMonad m) => WriterOptions -- ^ Writer options -> Pandoc -- ^ Document to convert -> m BL.ByteString -writeDocx opts doc@(Pandoc meta _) = do - let doc' = walk fixDisplayMath doc +writeDocx opts doc = do + let Pandoc meta blocks = walk fixDisplayMath doc + let blocks' = makeSections True Nothing blocks + let doc' = Pandoc meta blocks' + username <- P.lookupEnv "USERNAME" utctime <- P.getCurrentTime oldUserDataDir <- P.getUserDataDir @@ -898,11 +901,21 @@ blockToOpenXML' opts (Div (ident,_classes,kvs) bs) = do header <- dirmod $ stylemod $ blocksToOpenXML opts hs contents <- dirmod $ bibmod $ stylemod $ blocksToOpenXML opts bs' wrapBookmark ident $ header <> contents -blockToOpenXML' opts (Header lev (ident,_,_) lst) = do +blockToOpenXML' opts (Header lev (ident,_,kvs) lst) = do setFirstPara paraProps <- withParaPropM (pStyleM (fromString $ "Heading "++show lev)) $ getParaProps False - contents <- inlinesToOpenXML opts lst + number <- + if writerNumberSections opts + then + case lookup "number" kvs of + Just n -> do + num <- withTextPropM (rStyleM "SectionNumber") + (inlineToOpenXML opts (Str n)) + return $ num ++ [mknode "w:r" [] [mknode "w:tab" [] ()]] + Nothing -> return [] + else return [] + contents <- (number ++) <$> inlinesToOpenXML opts lst if T.null ident then return [mknode "w:p" [] (paraProps ++ contents)] else do diff --git a/test/docx/golden/block_quotes.docx b/test/docx/golden/block_quotes.docx Binary files differindex d478369f7..94eb8e811 100644 --- a/test/docx/golden/block_quotes.docx +++ b/test/docx/golden/block_quotes.docx diff --git a/test/docx/golden/codeblock.docx b/test/docx/golden/codeblock.docx Binary files differindex f986b0055..ee796a1fb 100644 --- a/test/docx/golden/codeblock.docx +++ b/test/docx/golden/codeblock.docx diff --git a/test/docx/golden/comments.docx b/test/docx/golden/comments.docx Binary files differindex fbb919260..a19f77278 100644 --- a/test/docx/golden/comments.docx +++ b/test/docx/golden/comments.docx diff --git a/test/docx/golden/custom_style_no_reference.docx b/test/docx/golden/custom_style_no_reference.docx Binary files differindex f14537def..c6195737f 100644 --- a/test/docx/golden/custom_style_no_reference.docx +++ b/test/docx/golden/custom_style_no_reference.docx diff --git a/test/docx/golden/custom_style_preserve.docx b/test/docx/golden/custom_style_preserve.docx Binary files differindex 64e42e9d3..9997672ba 100644 --- a/test/docx/golden/custom_style_preserve.docx +++ b/test/docx/golden/custom_style_preserve.docx diff --git a/test/docx/golden/definition_list.docx b/test/docx/golden/definition_list.docx Binary files differindex 1afb20e6c..a4abcd463 100644 --- a/test/docx/golden/definition_list.docx +++ b/test/docx/golden/definition_list.docx diff --git a/test/docx/golden/document-properties-short-desc.docx b/test/docx/golden/document-properties-short-desc.docx Binary files differindex ba7cc8df0..d18c110b6 100644 --- a/test/docx/golden/document-properties-short-desc.docx +++ b/test/docx/golden/document-properties-short-desc.docx diff --git a/test/docx/golden/document-properties.docx b/test/docx/golden/document-properties.docx Binary files differindex 426a908bb..39d87a31a 100644 --- a/test/docx/golden/document-properties.docx +++ b/test/docx/golden/document-properties.docx diff --git a/test/docx/golden/headers.docx b/test/docx/golden/headers.docx Binary files differindex 63e754b5c..6458ce105 100644 --- a/test/docx/golden/headers.docx +++ b/test/docx/golden/headers.docx diff --git a/test/docx/golden/image.docx b/test/docx/golden/image.docx Binary files differindex 04a6f2041..90362a709 100644 --- a/test/docx/golden/image.docx +++ b/test/docx/golden/image.docx diff --git a/test/docx/golden/inline_code.docx b/test/docx/golden/inline_code.docx Binary files differindex ae415a3a1..3002a2b7d 100644 --- a/test/docx/golden/inline_code.docx +++ b/test/docx/golden/inline_code.docx diff --git a/test/docx/golden/inline_formatting.docx b/test/docx/golden/inline_formatting.docx Binary files differindex ddfd45280..c5f6ce186 100644 --- a/test/docx/golden/inline_formatting.docx +++ b/test/docx/golden/inline_formatting.docx diff --git a/test/docx/golden/inline_images.docx b/test/docx/golden/inline_images.docx Binary files differindex 00a4e77c7..99e1e5e79 100644 --- a/test/docx/golden/inline_images.docx +++ b/test/docx/golden/inline_images.docx diff --git a/test/docx/golden/link_in_notes.docx b/test/docx/golden/link_in_notes.docx Binary files differindex 18c7ec8c4..a1003b47e 100644 --- a/test/docx/golden/link_in_notes.docx +++ b/test/docx/golden/link_in_notes.docx diff --git a/test/docx/golden/links.docx b/test/docx/golden/links.docx Binary files differindex 3e99c48f3..86f0230ca 100644 --- a/test/docx/golden/links.docx +++ b/test/docx/golden/links.docx diff --git a/test/docx/golden/lists.docx b/test/docx/golden/lists.docx Binary files differindex 84cb1b277..c64cbf46b 100644 --- a/test/docx/golden/lists.docx +++ b/test/docx/golden/lists.docx diff --git a/test/docx/golden/lists_continuing.docx b/test/docx/golden/lists_continuing.docx Binary files differindex ee1cb3d49..8fbd3190c 100644 --- a/test/docx/golden/lists_continuing.docx +++ b/test/docx/golden/lists_continuing.docx diff --git a/test/docx/golden/lists_restarting.docx b/test/docx/golden/lists_restarting.docx Binary files differindex 8725ffe82..e4d75764d 100644 --- a/test/docx/golden/lists_restarting.docx +++ b/test/docx/golden/lists_restarting.docx diff --git a/test/docx/golden/nested_anchors_in_header.docx b/test/docx/golden/nested_anchors_in_header.docx Binary files differindex 96b999707..3a1286e31 100644 --- a/test/docx/golden/nested_anchors_in_header.docx +++ b/test/docx/golden/nested_anchors_in_header.docx diff --git a/test/docx/golden/notes.docx b/test/docx/golden/notes.docx Binary files differindex 17dfa8c54..de39a4305 100644 --- a/test/docx/golden/notes.docx +++ b/test/docx/golden/notes.docx diff --git a/test/docx/golden/table_one_row.docx b/test/docx/golden/table_one_row.docx Binary files differindex 1a7ddf476..b3cf7b27d 100644 --- a/test/docx/golden/table_one_row.docx +++ b/test/docx/golden/table_one_row.docx diff --git a/test/docx/golden/table_with_list_cell.docx b/test/docx/golden/table_with_list_cell.docx Binary files differindex e200c8cf5..64b5f1d59 100644 --- a/test/docx/golden/table_with_list_cell.docx +++ b/test/docx/golden/table_with_list_cell.docx diff --git a/test/docx/golden/tables.docx b/test/docx/golden/tables.docx Binary files differindex 5b0a728e3..38b4d2391 100644 --- a/test/docx/golden/tables.docx +++ b/test/docx/golden/tables.docx diff --git a/test/docx/golden/track_changes_deletion.docx b/test/docx/golden/track_changes_deletion.docx Binary files differindex 1ab4f1d0e..97c060915 100644 --- a/test/docx/golden/track_changes_deletion.docx +++ b/test/docx/golden/track_changes_deletion.docx diff --git a/test/docx/golden/track_changes_insertion.docx b/test/docx/golden/track_changes_insertion.docx Binary files differindex c9f3c8a88..a1f141043 100644 --- a/test/docx/golden/track_changes_insertion.docx +++ b/test/docx/golden/track_changes_insertion.docx diff --git a/test/docx/golden/track_changes_move.docx b/test/docx/golden/track_changes_move.docx Binary files differindex acf11e7f7..fbfd8ec59 100644 --- a/test/docx/golden/track_changes_move.docx +++ b/test/docx/golden/track_changes_move.docx diff --git a/test/docx/golden/unicode.docx b/test/docx/golden/unicode.docx Binary files differindex 97ca3e7e5..4daf635e9 100644 --- a/test/docx/golden/unicode.docx +++ b/test/docx/golden/unicode.docx diff --git a/test/docx/golden/verbatim_subsuper.docx b/test/docx/golden/verbatim_subsuper.docx Binary files differindex 279b11279..984f16abd 100644 --- a/test/docx/golden/verbatim_subsuper.docx +++ b/test/docx/golden/verbatim_subsuper.docx |