@@ -123,6 +123,7 @@ defaultWriterEnv = WriterEnv{ envTextProperties = []
123
123
124
124
data WriterState = WriterState {
125
125
stFootnotes :: [Element ]
126
+ , stEndnotes :: [Element ]
126
127
, stComments :: [([(String ,String )], [Inline ])]
127
128
, stSectionIds :: Set. Set String
128
129
, stExternalLinks :: M. Map String String
@@ -140,6 +141,7 @@ data WriterState = WriterState{
140
141
defaultWriterState :: WriterState
141
142
defaultWriterState = WriterState {
142
143
stFootnotes = defaultFootnotes
144
+ , stEndnotes = []
143
145
, stComments = []
144
146
, stSectionIds = Set. empty
145
147
, stExternalLinks = M. empty
@@ -307,7 +309,7 @@ writeDocx opts doc@(Pandoc meta _) = do
307
309
}
308
310
309
311
310
- ((contents, footnotes, comments), st) <- runStateT
312
+ ((contents, footnotes, endnotes, comments), st) <- runStateT
311
313
(runReaderT
312
314
(writeOpenXML opts{writerWrapText = WrapNone } doc')
313
315
env)
@@ -376,6 +378,8 @@ writeDocx opts doc@(Pandoc meta _) = do
376
378
" application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml" )
377
379
,(" /word/footnotes.xml" ,
378
380
" application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml" )
381
+ ,(" /word/endnotes.xml" ,
382
+ " application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml" )
379
383
] ++
380
384
map (\ x -> (maybe " " (" /word/" ++ ) $ extractTarget x,
381
385
" application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml" )) headers ++
@@ -420,8 +424,11 @@ writeDocx opts doc@(Pandoc meta _) = do
420
424
,(" http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" ,
421
425
" rId7" ,
422
426
" footnotes.xml" )
423
- ,(" http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments " ,
427
+ ,(" http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes " ,
424
428
" rId8" ,
429
+ " endnotes.xml" )
430
+ ,(" http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" ,
431
+ " rId9" ,
425
432
" comments.xml" )
426
433
]
427
434
@@ -460,14 +467,23 @@ writeDocx opts doc@(Pandoc meta _) = do
460
467
$ renderXml docContents
461
468
462
469
-- footnotes
463
- let notes = mknode " w:footnotes" stdAttributes footnotes
464
- let footnotesEntry = toEntry " word/footnotes.xml" epochtime $ renderXml notes
470
+ let footnotesNode = mknode " w:footnotes" stdAttributes footnotes
471
+ let footnotesEntry = toEntry " word/footnotes.xml" epochtime $ renderXml footnotesNode
465
472
466
473
-- footnote rels
467
474
let footnoteRelEntry = toEntry " word/_rels/footnotes.xml.rels" epochtime
468
475
$ renderXml $ mknode " Relationships" [(" xmlns" ," http://schemas.openxmlformats.org/package/2006/relationships" )]
469
476
linkrels
470
477
478
+ -- endnotes
479
+ let endnotesNode = mknode " w:endnotes" stdAttributes endnotes
480
+ let endnotesEntry = toEntry " word/endnotes.xml" epochtime $ renderXml endnotesNode
481
+
482
+ -- endnote rels
483
+ let endnoteRelEntry = toEntry " word/_rels/endnotes.xml.rels" epochtime
484
+ $ renderXml $ mknode " Relationships" [(" xmlns" ," http://schemas.openxmlformats.org/package/2006/relationships" )]
485
+ linkrels
486
+
471
487
-- comments
472
488
let commentsEntry = toEntry " word/comments.xml" epochtime
473
489
$ renderXml $ mknode " w:comments" stdAttributes comments
@@ -568,15 +584,16 @@ writeDocx opts doc@(Pandoc meta _) = do
568
584
, " word/_rels/" `isPrefixOf` (eRelativePath e)
569
585
, " .xml.rels" `isSuffixOf` (eRelativePath e)
570
586
, eRelativePath e /= " word/_rels/document.xml.rels"
571
- , eRelativePath e /= " word/_rels/footnotes.xml.rels" ]
587
+ , eRelativePath e /= " word/_rels/footnotes.xml.rels"
588
+ , eRelativePath e /= " word/_rels/endnotes.xml.rels" ]
572
589
let otherMediaEntries = [ e | e <- zEntries refArchive
573
590
, " word/media/" `isPrefixOf` eRelativePath e ]
574
591
575
592
-- Create archive
576
593
let archive = foldr addEntryToArchive emptyArchive $
577
594
contentTypesEntry : relsEntry : contentEntry : relEntry :
578
- footnoteRelEntry : numEntry : styleEntry : footnotesEntry :
579
- commentsEntry :
595
+ footnoteRelEntry : endnoteRelEntry : numEntry : styleEntry :
596
+ footnotesEntry : endnotesEntry : commentsEntry :
580
597
docPropsEntry : docPropsAppEntry : themeEntry :
581
598
fontTableEntry : settingsEntry : webSettingsEntry :
582
599
imageEntries ++ headerFooterEntries ++
@@ -775,7 +792,7 @@ makeTOC _ = return []
775
792
776
793
-- | Convert Pandoc document to two lists of
777
794
-- OpenXML elements (the main document and footnotes).
778
- writeOpenXML :: (PandocMonad m ) => WriterOptions -> Pandoc -> WS m ([Element ], [Element ],[Element ])
795
+ writeOpenXML :: (PandocMonad m ) => WriterOptions -> Pandoc -> WS m ([Element ], [Element ], [ Element ], [Element ])
779
796
writeOpenXML opts (Pandoc meta blocks) = do
780
797
let tit = docTitle meta ++ case lookupMeta " subtitle" meta of
781
798
Just (MetaBlocks [Plain xs]) -> LineBreak : xs
@@ -804,7 +821,8 @@ writeOpenXML opts (Pandoc meta blocks) = do
804
821
convertSpace xs = xs
805
822
let blocks' = bottomUp convertSpace blocks
806
823
doc' <- setFirstPara >> blocksToOpenXML opts blocks'
807
- notes' <- reverse <$> gets stFootnotes
824
+ footnotes' <- reverse <$> gets stFootnotes
825
+ endnotes' <- reverse <$> gets stEndnotes
808
826
comments <- reverse <$> gets stComments
809
827
let toComment (kvs, ils) = do
810
828
annotation <- inlinesToOpenXML opts ils
@@ -824,7 +842,7 @@ writeOpenXML opts (Pandoc meta blocks) = do
824
842
comments' <- mapM toComment comments
825
843
toc <- makeTOC opts
826
844
let meta' = title ++ subtitle ++ authors ++ date ++ abstract ++ toc
827
- return (meta' ++ doc', notes ', comments')
845
+ return (meta' ++ doc', footnotes', endnotes ', comments')
828
846
829
847
-- | Convert a list of Pandoc blocks to OpenXML.
830
848
blocksToOpenXML :: (PandocMonad m ) => WriterOptions -> [Block ] -> WS m [Element ]
@@ -1250,8 +1268,8 @@ inlineToOpenXML' opts (Code attrs str) = do
1250
1268
Left msg -> do
1251
1269
unless (null msg) $ report $ CouldNotHighlight msg
1252
1270
unhighlighted
1253
- inlineToOpenXML' opts (Note _ bs) = do
1254
- notes <- gets stFootnotes
1271
+ inlineToOpenXML' opts (Note FootNote bs) = do
1272
+ footnotes <- gets stFootnotes
1255
1273
notenum <- (lift . lift) getUniqueId
1256
1274
footnoteStyle <- rStyleM " Footnote Reference"
1257
1275
let notemarker = mknode " w:r" []
@@ -1268,10 +1286,32 @@ inlineToOpenXML' opts (Note _ bs) = do
1268
1286
(withParaPropM (pStyleM " Footnote Text" ) $ blocksToOpenXML opts
1269
1287
$ insertNoteRef bs)
1270
1288
let newnote = mknode " w:footnote" [(" w:id" , notenum)] contents
1271
- modify $ \ s -> s{ stFootnotes = newnote : notes }
1289
+ modify $ \ s -> s{ stFootnotes = newnote : footnotes }
1272
1290
return [ mknode " w:r" []
1273
1291
[ mknode " w:rPr" [] footnoteStyle
1274
1292
, mknode " w:footnoteReference" [(" w:id" , notenum)] () ] ]
1293
+ inlineToOpenXML' opts (Note EndNote bs) = do
1294
+ endnotes <- gets stEndnotes
1295
+ notenum <- (lift . lift) getUniqueId
1296
+ endnoteStyle <- rStyleM " Endnote Reference"
1297
+ let notemarker = mknode " w:r" []
1298
+ [ mknode " w:rPr" [] endnoteStyle
1299
+ , mknode " w:endnoteRef" [] () ]
1300
+ let notemarkerXml = RawInline (Format " openxml" ) $ ppElement notemarker
1301
+ let insertNoteRef (Plain ils : xs) = Plain (notemarkerXml : Space : ils) : xs
1302
+ insertNoteRef (Para ils : xs) = Para (notemarkerXml : Space : ils) : xs
1303
+ insertNoteRef xs = Para [notemarkerXml] : xs
1304
+
1305
+ contents <- local (\ env -> env{ envListLevel = - 1
1306
+ , envParaProperties = []
1307
+ , envTextProperties = [] })
1308
+ (withParaPropM (pStyleM " Endnote Text" ) $ blocksToOpenXML opts
1309
+ $ insertNoteRef bs)
1310
+ let newnote = mknode " w:endnote" [(" w:id" , notenum)] contents
1311
+ modify $ \ s -> s{ stEndnotes = newnote : endnotes }
1312
+ return [ mknode " w:r" []
1313
+ [ mknode " w:rPr" [] endnoteStyle
1314
+ , mknode " w:endnoteReference" [(" w:id" , notenum)] () ] ]
1275
1315
-- internal link:
1276
1316
inlineToOpenXML' opts (Link _ txt (' #' : xs,_)) = do
1277
1317
contents <- withTextPropM (rStyleM " Hyperlink" ) $ inlinesToOpenXML opts txt
0 commit comments