Skip to content

Commit

Permalink
Docx writer: support --number-sections.
Browse files Browse the repository at this point in the history
Closes #1413.
  • Loading branch information
jgm committed Jul 22, 2020
1 parent 3bccc08 commit 1e84178
Show file tree
Hide file tree
Showing 31 changed files with 23 additions and 5 deletions.
3 changes: 2 additions & 1 deletion MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1125,6 +1125,7 @@ header when requesting a document from a URL:
- Verbatim Char
- Footnote Reference
- Hyperlink
- Section Number

Table style:

Expand Down
4 changes: 4 additions & 0 deletions data/docx/word/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
21 changes: 17 additions & 4 deletions src/Text/Pandoc/Writers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Binary file modified test/docx/golden/block_quotes.docx
Binary file not shown.
Binary file modified test/docx/golden/codeblock.docx
Binary file not shown.
Binary file modified test/docx/golden/comments.docx
Binary file not shown.
Binary file modified test/docx/golden/custom_style_no_reference.docx
Binary file not shown.
Binary file modified test/docx/golden/custom_style_preserve.docx
Binary file not shown.
Binary file modified test/docx/golden/definition_list.docx
Binary file not shown.
Binary file modified test/docx/golden/document-properties-short-desc.docx
Binary file not shown.
Binary file modified test/docx/golden/document-properties.docx
Binary file not shown.
Binary file modified test/docx/golden/headers.docx
Binary file not shown.
Binary file modified test/docx/golden/image.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_code.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_formatting.docx
Binary file not shown.
Binary file modified test/docx/golden/inline_images.docx
Binary file not shown.
Binary file modified test/docx/golden/link_in_notes.docx
Binary file not shown.
Binary file modified test/docx/golden/links.docx
Binary file not shown.
Binary file modified test/docx/golden/lists.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_continuing.docx
Binary file not shown.
Binary file modified test/docx/golden/lists_restarting.docx
Binary file not shown.
Binary file modified test/docx/golden/nested_anchors_in_header.docx
Binary file not shown.
Binary file modified test/docx/golden/notes.docx
Binary file not shown.
Binary file modified test/docx/golden/table_one_row.docx
Binary file not shown.
Binary file modified test/docx/golden/table_with_list_cell.docx
Binary file not shown.
Binary file modified test/docx/golden/tables.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_deletion.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_insertion.docx
Binary file not shown.
Binary file modified test/docx/golden/track_changes_move.docx
Binary file not shown.
Binary file modified test/docx/golden/unicode.docx
Binary file not shown.
Binary file modified test/docx/golden/verbatim_subsuper.docx
Binary file not shown.

0 comments on commit 1e84178

Please sign in to comment.