git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #3159 from jkr/refs

Specify location for footnotes (and reference links) in MD output

commit901045b0bbc6c1028d1347878363607bb4716d92 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 12 Oct 2016, 11:11 (10 years ago) parentbb48f2eafbeba9
5 files changed+313−87
MANUAL.txt+7 pandoc.hs+17 src/Text/Pandoc/Options.hs+9 src/Text/Pandoc/Writers/Markdown.hs+142−85 tests/Tests/Writers/Markdown.hs+138−2
MANUAL.txt+7
@@ -645,6 +645,13 @@ Options affecting specific writers
645645: Use reference-style links, rather than inline links, in writing Markdown
646646 or reStructuredText. By default inline links are used.
647647
648+`--reference-location = block` | `section` | `document`
649+
650+: Specify whether footnotes (and references, if `reference-links` is
651+ set) are placed at the end of the current (top-level) block, the
652+ current section, or the document. The default is
653+ `document`. Currently only affects the markdown writer.
654+
648655`--atx-headers`
649656
650657: Use ATX-style headers in Markdown and asciidoc output. The default is
pandoc.hs+17
@@ -196,6 +196,7 @@ data Opt = Opt
196196 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
197197 , optVerbose :: Bool -- ^ Verbose diagnostic output
198198 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
199+ , optReferenceLocation :: ReferenceLocation -- ^ location for footnotes and link references in markdown output
199200 , optDpi :: Int -- ^ Dpi
200201 , optWrapText :: WrapOption -- ^ Options for wrapping text
201202 , optColumns :: Int -- ^ Line length in characters
@@ -260,6 +261,7 @@ defaultOpts = Opt
260261 , optIgnoreArgs = False
261262 , optVerbose = False
262263 , optReferenceLinks = False
264+ , optReferenceLocation = EndOfDocument
263265 , optDpi = 96
264266 , optWrapText = WrapAuto
265267 , optColumns = 72
@@ -584,6 +586,19 @@ options =
584586 (\opt -> return opt { optReferenceLinks = True } ))
585587 "" -- "Use reference links in parsing HTML"
586588
589+ , Option "" ["reference-location"]
590+ (ReqArg
591+ (\arg opt -> do
592+ action <- case arg of
593+ "block" -> return EndOfBlock
594+ "section" -> return EndOfSection
595+ "document" -> return EndOfDocument
596+ _ -> err 6
597+ ("Unknown option for reference-location: " ++ arg)
598+ return opt { optReferenceLocation = action })
599+ "block|section|document")
600+ "" -- "Accepting or reject MS Word track-changes.""
601+
587602 , Option "" ["atx-headers"]
588603 (NoArg
589604 (\opt -> return opt { optSetextHeaders = False } ))
@@ -1120,6 +1135,7 @@ convertWithOpts opts args = do
11201135 , optIgnoreArgs = ignoreArgs
11211136 , optVerbose = verbose
11221137 , optReferenceLinks = referenceLinks
1138+ , optReferenceLocation = referenceLocation
11231139 , optDpi = dpi
11241140 , optWrapText = wrap
11251141 , optColumns = columns
@@ -1360,6 +1376,7 @@ convertWithOpts opts args = do
13601376 writerNumberOffset = numberFrom,
13611377 writerSectionDivs = sectionDivs,
13621378 writerReferenceLinks = referenceLinks,
1379+ writerReferenceLocation = referenceLocation,
13631380 writerDpi = dpi,
13641381 writerWrapText = wrap,
13651382 writerColumns = columns,
src/Text/Pandoc/Options.hs+9
@@ -45,6 +45,7 @@ module Text.Pandoc.Options ( Extension(..)
4545 , WrapOption (..)
4646 , WriterOptions (..)
4747 , TrackChanges (..)
48+ , ReferenceLocation (..)
4849 , def
4950 , isEnabled
5051 ) where
@@ -336,6 +337,12 @@ data WrapOption = WrapAuto -- ^ Automatically wrap to width
336337 | WrapPreserve -- ^ Preserve wrapping of input source
337338 deriving (Show, Read, Eq, Data, Typeable, Generic)
338339
340+-- | Locations for footnotes and references in markdown output
341+data ReferenceLocation = EndOfBlock -- ^ End of block
342+ | EndOfSection -- ^ prior to next section header (or end of document)
343+ | EndOfDocument -- ^ at end of document
344+ deriving (Show, Read, Eq, Data, Typeable, Generic)
345+
339346-- | Options for writers
340347data WriterOptions = WriterOptions
341348 { writerStandalone :: Bool -- ^ Include header and footer
@@ -383,6 +390,7 @@ data WriterOptions = WriterOptions
383390 , writerMediaBag :: MediaBag -- ^ Media collected by docx or epub reader
384391 , writerVerbose :: Bool -- ^ Verbose debugging output
385392 , writerLaTeXArgs :: [String] -- ^ Flags to pass to latex-engine
393+ , writerReferenceLocation :: ReferenceLocation -- ^ Location of footnotes and references for writing markdown
386394 } deriving (Show, Data, Typeable, Generic)
387395
388396instance Default WriterOptions where
@@ -430,6 +438,7 @@ instance Default WriterOptions where
430438 , writerMediaBag = mempty
431439 , writerVerbose = False
432440 , writerLaTeXArgs = []
441+ , writerReferenceLocation = EndOfDocument
433442 }
434443
435444-- | Returns True if the given extension is enabled.
src/Text/Pandoc/Writers/Markdown.hs+142−85
@@ -44,6 +44,7 @@ import Data.List ( group, stripPrefix, find, intersperse, transpose, sortBy )
4444import Data.Char ( isSpace, isPunctuation, ord, chr )
4545import Data.Ord ( comparing )
4646import Text.Pandoc.Pretty
47+import Control.Monad.Reader
4748import Control.Monad.State
4849import Text.Pandoc.Writers.HTML (writeHtmlString)
4950import Text.Pandoc.Readers.TeXMath (texMathToInlines)
@@ -60,30 +61,52 @@ import Network.HTTP ( urlEncode )
6061type Notes = [[Block]]
6162type Ref = ([Inline], Target, Attr)
6263type Refs = [Ref]
63−data WriterState = WriterState { stNotes :: Notes
64− , stRefs :: Refs
65− , stRefShortcutable :: Bool
66− , stInList :: Bool
67− , stIds :: Set.Set String
68− , stPlain :: Bool }
64+
65+type MD = ReaderT WriterEnv (State WriterState)
66+
67+evalMD :: MD a -> WriterEnv -> WriterState -> a
68+evalMD md env st = evalState (runReaderT md env) st
69+
70+data WriterEnv = WriterEnv { envInList :: Bool
71+ , envPlain :: Bool
72+ , envRefShortcutable :: Bool
73+ , envBlockLevel :: Int
74+ }
75+
76+instance Default WriterEnv
77+ where def = WriterEnv { envInList = False
78+ , envPlain = False
79+ , envRefShortcutable = True
80+ , envBlockLevel = 0
81+ }
82+
83+data WriterState = WriterState { stNotes :: Notes
84+ , stRefs :: Refs
85+ , stIds :: Set.Set String
86+ , stNoteNum :: Int
87+ }
88+
6989instance Default WriterState
70− where def = WriterState{ stNotes = [], stRefs = [], stRefShortcutable = True,
71− stInList = False, stIds = Set.empty, stPlain = False }
90+ where def = WriterState{ stNotes = []
91+ , stRefs = []
92+ , stIds = Set.empty
93+ , stNoteNum = 1
94+ }
7295
7396-- | Convert Pandoc to Markdown.
7497writeMarkdown :: WriterOptions -> Pandoc -> String
7598writeMarkdown opts document =
76− evalState (pandocToMarkdown opts{
77− writerWrapText = if isEnabled Ext_hard_line_breaks opts
78− then WrapNone
79− else writerWrapText opts }
80− document) def
99+ evalMD (pandocToMarkdown opts{
100+ writerWrapText = if isEnabled Ext_hard_line_breaks opts
101+ then WrapNone
102+ else writerWrapText opts }
103+ document) def def
81104
82105-- | Convert Pandoc to plain text (like markdown, but without links,
83106-- pictures, or inline formatting).
84107writePlain :: WriterOptions -> Pandoc -> String
85108writePlain opts document =
86− evalState (pandocToMarkdown opts document) def{ stPlain = True }
109+ evalMD (pandocToMarkdown opts document) def{ envPlain = True } def
87110
88111pandocTitleBlock :: Doc -> [Doc] -> Doc -> Doc
89112pandocTitleBlock tit auths dat =
@@ -146,12 +169,12 @@ jsonToYaml (Number n) = text $ show n
146169jsonToYaml _ = empty
147170
148171-- | Return markdown representation of document.
149−pandocToMarkdown :: WriterOptions -> Pandoc -> State WriterState String
172+pandocToMarkdown :: WriterOptions -> Pandoc -> MD String
150173pandocToMarkdown opts (Pandoc meta blocks) = do
151174 let colwidth = if writerWrapText opts == WrapAuto
152175 then Just $ writerColumns opts
153176 else Nothing
154− isPlain <- gets stPlain
177+ isPlain <- asks envPlain
155178 metadata <- metaToJSON opts
156179 (fmap (render colwidth) . blockListToMarkdown opts)
157180 (fmap (render colwidth) . inlineListToMarkdown opts)
@@ -201,13 +224,13 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
201224 else return main
202225
203226-- | Return markdown representation of reference key table.
204−refsToMarkdown :: WriterOptions -> Refs -> State WriterState Doc
227+refsToMarkdown :: WriterOptions -> Refs -> MD Doc
205228refsToMarkdown opts refs = mapM (keyToMarkdown opts) refs >>= return . vcat
206229
207230-- | Return markdown representation of a reference key.
208231keyToMarkdown :: WriterOptions
209232 -> Ref
210− -> State WriterState Doc
233+ -> MD Doc
211234keyToMarkdown opts (label, (src, tit), attr) = do
212235 label' <- inlineListToMarkdown opts label
213236 let tit' = if null tit
@@ -218,13 +241,15 @@ keyToMarkdown opts (label, (src, tit), attr) = do
218241 <> linkAttributes opts attr
219242
220243-- | Return markdown representation of notes.
221−notesToMarkdown :: WriterOptions -> [[Block]] -> State WriterState Doc
222−notesToMarkdown opts notes =
223− mapM (\(num, note) -> noteToMarkdown opts num note) (zip [1..] notes) >>=
224− return . vsep
244+notesToMarkdown :: WriterOptions -> [[Block]] -> MD Doc
245+notesToMarkdown opts notes = do
246+ n <- gets stNoteNum
247+ notes' <- mapM (\(num, note) -> noteToMarkdown opts num note) (zip [n..] notes)
248+ modify $ \st -> st { stNoteNum = stNoteNum st + length notes }
249+ return $ vsep notes'
225250
226251-- | Return markdown representation of a note.
227−noteToMarkdown :: WriterOptions -> Int -> [Block] -> State WriterState Doc
252+noteToMarkdown :: WriterOptions -> Int -> [Block] -> MD Doc
228253noteToMarkdown opts num blocks = do
229254 contents <- blockListToMarkdown opts blocks
230255 let num' = text $ writerIdentifierPrefix opts ++ show num
@@ -261,7 +286,7 @@ tableOfContents :: WriterOptions -> [Block] -> Doc
261286tableOfContents opts headers =
262287 let opts' = opts { writerIgnoreNotes = True }
263288 contents = BulletList $ map (elementToListItem opts) $ hierarchicalize headers
264− in evalState (blockToMarkdown opts' contents) def
289+ in evalMD (blockToMarkdown opts' contents) def def
265290
266291-- | Converts an Element to a list item for a table of contents,
267292elementToListItem :: WriterOptions -> Element -> [Block]
@@ -315,19 +340,39 @@ beginsWithOrderedListMarker str =
315340-- | Convert Pandoc block element to markdown.
316341blockToMarkdown :: WriterOptions -- ^ Options
317342 -> Block -- ^ Block element
318− -> State WriterState Doc
319−blockToMarkdown _ Null = return empty
320−blockToMarkdown opts (Div attrs ils) = do
343+ -> MD Doc
344+blockToMarkdown opts blk =
345+ local (\env -> env {envBlockLevel = envBlockLevel env + 1}) $
346+ do doc <- blockToMarkdown' opts blk
347+ blkLevel <- asks envBlockLevel
348+ if writerReferenceLocation opts == EndOfBlock && blkLevel == 1
349+ then do st <- get
350+ notes' <- notesToMarkdown opts (reverse $ stNotes st)
351+ modify $ \s -> s { stNotes = [] }
352+ st' <- get -- note that the notes may contain refs
353+ refs' <- refsToMarkdown opts (reverse $ stRefs st')
354+ modify $ \s -> s { stRefs = [] }
355+ return $ doc <>
356+ (if isEmpty notes' then empty else blankline <> notes') <>
357+ (if isEmpty refs' then empty else blankline <> refs') <>
358+ (if (isEmpty notes' && isEmpty refs') then empty else blankline)
359+ else return doc
360+
361+blockToMarkdown' :: WriterOptions -- ^ Options
362+ -> Block -- ^ Block element
363+ -> MD Doc
364+blockToMarkdown' _ Null = return empty
365+blockToMarkdown' opts (Div attrs ils) = do
321366 contents <- blockListToMarkdown opts ils
322367 return $ if isEnabled Ext_raw_html opts &&
323368 isEnabled Ext_markdown_in_html_blocks opts
324369 then tagWithAttrs "div" attrs <> blankline <>
325370 contents <> blankline <> "</div>" <> blankline
326371 else contents <> blankline
327−blockToMarkdown opts (Plain inlines) = do
372+blockToMarkdown' opts (Plain inlines) = do
328373 contents <- inlineListToMarkdown opts inlines
329374 -- escape if para starts with ordered list marker
330− st <- get
375+ isPlain <- asks envPlain
331376 let colwidth = if writerWrapText opts == WrapAuto
332377 then Just $ writerColumns opts
333378 else Nothing
@@ -336,34 +381,50 @@ blockToMarkdown opts (Plain inlines) = do
336381 | otherwise = x : escapeDelimiter xs
337382 escapeDelimiter [] = []
338383 let contents' = if isEnabled Ext_all_symbols_escapable opts &&
339− not (stPlain st) && beginsWithOrderedListMarker rendered
384+ not isPlain && beginsWithOrderedListMarker rendered
340385 then text $ escapeDelimiter rendered
341386 else contents
342387 return $ contents' <> cr
343388-- title beginning with fig: indicates figure
344−blockToMarkdown opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) =
389+blockToMarkdown' opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) =
345390 blockToMarkdown opts (Para [Image attr alt (src,tit)])
346−blockToMarkdown opts (Para inlines) =
391+blockToMarkdown' opts (Para inlines) =
347392 (<> blankline) `fmap` blockToMarkdown opts (Plain inlines)
348−blockToMarkdown opts (RawBlock f str)
393+blockToMarkdown' opts (RawBlock f str)
349394 | f == "markdown" = return $ text str <> text "\n"
350395 | f == "html" && isEnabled Ext_raw_html opts = do
351− plain <- gets stPlain
396+ plain <- asks envPlain
352397 return $ if plain
353398 then empty
354399 else if isEnabled Ext_markdown_attribute opts
355400 then text (addMarkdownAttribute str) <> text "\n"
356401 else text str <> text "\n"
357402 | f `elem` ["latex", "tex"] && isEnabled Ext_raw_tex opts = do
358− plain <- gets stPlain
403+ plain <- asks envPlain
359404 return $ if plain
360405 then empty
361406 else text str <> text "\n"
362407 | otherwise = return empty
363−blockToMarkdown opts HorizontalRule = do
408+blockToMarkdown' opts HorizontalRule = do
364409 return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline
365−blockToMarkdown opts (Header level attr inlines) = do
366− plain <- gets stPlain
410+blockToMarkdown' opts (Header level attr inlines) = do
411+ -- first, if we're putting references at the end of a section, we
412+ -- put them here.
413+ blkLevel <- asks envBlockLevel
414+ refs <- if writerReferenceLocation opts == EndOfSection && blkLevel == 1
415+ then do st <- get
416+ notes' <- notesToMarkdown opts (reverse $ stNotes st)
417+ modify $ \s -> s { stNotes = [] }
418+ st' <- get -- note that the notes may contain refs
419+ refs' <- refsToMarkdown opts (reverse $ stRefs st')
420+ modify $ \s -> s { stRefs = [] }
421+ return $
422+ (if isEmpty notes' then empty else blankline <> notes') <>
423+ (if isEmpty refs' then empty else blankline <> refs') <>
424+ (if (isEmpty notes' && isEmpty refs') then empty else blankline)
425+ else return empty
426+
427+ plain <- asks envPlain
367428 -- we calculate the id that would be used by auto_identifiers
368429 -- so we know whether to print an explicit identifier
369430 ids <- gets stIds
@@ -383,8 +444,7 @@ blockToMarkdown opts (Header level attr inlines) = do
383444 then capitalize inlines
384445 else inlines
385446 let setext = writerSetextHeaders opts
386− return $ nowrap
387− $ case level of
447+ hdr = nowrap $ case level of
388448 1 | plain -> blanklines 3 <> contents <> blanklines 2
389449 | setext ->
390450 contents <> attr' <> cr <> text (replicate (offset contents) '=') <>
@@ -397,11 +457,13 @@ blockToMarkdown opts (Header level attr inlines) = do
397457 _ | plain || isEnabled Ext_literate_haskell opts ->
398458 contents <> blankline
399459 _ -> text (replicate level '#') <> space <> contents <> attr' <> blankline
400−blockToMarkdown opts (CodeBlock (_,classes,_) str)
460+
461+ return $ refs <> hdr
462+blockToMarkdown' opts (CodeBlock (_,classes,_) str)
401463 | "haskell" `elem` classes && "literate" `elem` classes &&
402464 isEnabled Ext_literate_haskell opts =
403465 return $ prefixed "> " (text str) <> blankline
404−blockToMarkdown opts (CodeBlock attribs str) = return $
466+blockToMarkdown' opts (CodeBlock attribs str) = return $
405467 case attribs == nullAttr of
406468 False | isEnabled Ext_backtick_code_blocks opts ->
407469 backticks <> attrs <> cr <> text str <> cr <> backticks <> blankline
@@ -423,8 +485,8 @@ blockToMarkdown opts (CodeBlock attribs str) = return $
423485 else case attribs of
424486 (_,(cls:_),_) -> " " <> text cls
425487 _ -> empty
426−blockToMarkdown opts (BlockQuote blocks) = do
427− plain <- gets stPlain
488+blockToMarkdown' opts (BlockQuote blocks) = do
489+ plain <- asks envPlain
428490 -- if we're writing literate haskell, put a space before the bird tracks
429491 -- so they won't be interpreted as lhs...
430492 let leader = if isEnabled Ext_literate_haskell opts
@@ -432,7 +494,7 @@ blockToMarkdown opts (BlockQuote blocks) = do
432494 else if plain then " " else "> "
433495 contents <- blockListToMarkdown opts blocks
434496 return $ (prefixed leader contents) <> blankline
435−blockToMarkdown opts t@(Table caption aligns widths headers rows) = do
497+blockToMarkdown' opts t@(Table caption aligns widths headers rows) = do
436498 caption' <- inlineListToMarkdown opts caption
437499 let caption'' = if null caption || not (isEnabled Ext_table_captions opts)
438500 then empty
@@ -466,10 +528,10 @@ blockToMarkdown opts t@(Table caption aligns widths headers rows) = do
466528 $ Pandoc nullMeta [t]
467529 | otherwise -> return $ (id, text "[TABLE]")
468530 return $ nst $ tbl $$ blankline $$ caption'' $$ blankline
469−blockToMarkdown opts (BulletList items) = do
531+blockToMarkdown' opts (BulletList items) = do
470532 contents <- inList $ mapM (bulletListItemToMarkdown opts) items
471533 return $ cat contents <> blankline
472−blockToMarkdown opts (OrderedList (start,sty,delim) items) = do
534+blockToMarkdown' opts (OrderedList (start,sty,delim) items) = do
473535 let start' = if isEnabled Ext_startnum opts then start else 1
474536 let sty' = if isEnabled Ext_fancy_lists opts then sty else DefaultStyle
475537 let delim' = if isEnabled Ext_fancy_lists opts then delim else DefaultDelim
@@ -482,17 +544,12 @@ blockToMarkdown opts (OrderedList (start,sty,delim) items) = do
482544 mapM (\(item, num) -> orderedListItemToMarkdown opts item num) $
483545 zip markers' items
484546 return $ cat contents <> blankline
485−blockToMarkdown opts (DefinitionList items) = do
547+blockToMarkdown' opts (DefinitionList items) = do
486548 contents <- inList $ mapM (definitionListItemToMarkdown opts) items
487549 return $ cat contents <> blankline
488550
489−inList :: State WriterState a -> State WriterState a
490−inList p = do
491− oldInList <- gets stInList
492− modify $ \st -> st{ stInList = True }
493− res <- p
494− modify $ \st -> st{ stInList = oldInList }
495− return res
551+inList :: MD a -> MD a
552+inList p = local (\env -> env {envInList = True}) p
496553
497554addMarkdownAttribute :: String -> String
498555addMarkdownAttribute s =
@@ -503,7 +560,7 @@ addMarkdownAttribute s =
503560 x /= "markdown"]
504561 _ -> s
505562
506−pipeTable :: Bool -> [Alignment] -> [Doc] -> [[Doc]] -> State WriterState Doc
563+pipeTable :: Bool -> [Alignment] -> [Doc] -> [[Doc]] -> MD Doc
507564pipeTable headless aligns rawHeaders rawRows = do
508565 let sp = text " "
509566 let blockFor AlignLeft x y = lblock (x + 2) (sp <> y) <> lblock 0 empty
@@ -532,7 +589,7 @@ pipeTable headless aligns rawHeaders rawRows = do
532589 return $ header $$ border $$ body
533590
534591pandocTable :: WriterOptions -> Bool -> [Alignment] -> [Double]
535− -> [Doc] -> [[Doc]] -> State WriterState Doc
592+ -> [Doc] -> [[Doc]] -> MD Doc
536593pandocTable opts headless aligns widths rawHeaders rawRows = do
537594 let isSimple = all (==0) widths
538595 let alignHeader alignment = case alignment of
@@ -572,7 +629,7 @@ pandocTable opts headless aligns widths rawHeaders rawRows = do
572629 return $ head'' $$ underline $$ body $$ bottom
573630
574631gridTable :: WriterOptions -> Bool -> [Alignment] -> [Double]
575− -> [Doc] -> [[Doc]] -> State WriterState Doc
632+ -> [Doc] -> [[Doc]] -> MD Doc
576633gridTable opts headless _aligns widths headers' rawRows = do
577634 let numcols = length headers'
578635 let widths' = if all (==0) widths
@@ -599,7 +656,7 @@ gridTable opts headless _aligns widths headers' rawRows = do
599656 return $ border '-' $$ head'' $$ body $$ border '-'
600657
601658-- | Convert bullet list item (list of blocks) to markdown.
602−bulletListItemToMarkdown :: WriterOptions -> [Block] -> State WriterState Doc
659+bulletListItemToMarkdown :: WriterOptions -> [Block] -> MD Doc
603660bulletListItemToMarkdown opts items = do
604661 contents <- blockListToMarkdown opts items
605662 let sps = replicate (writerTabStop opts - 2) ' '
@@ -617,7 +674,7 @@ bulletListItemToMarkdown opts items = do
617674orderedListItemToMarkdown :: WriterOptions -- ^ options
618675 -> String -- ^ list item marker
619676 -> [Block] -- ^ list item (list of blocks)
620− -> State WriterState Doc
677+ -> MD Doc
621678orderedListItemToMarkdown opts marker items = do
622679 contents <- blockListToMarkdown opts items
623680 let sps = case length marker - writerTabStop opts of
@@ -629,15 +686,15 @@ orderedListItemToMarkdown opts marker items = do
629686-- | Convert definition list item (label, list of blocks) to markdown.
630687definitionListItemToMarkdown :: WriterOptions
631688 -> ([Inline],[[Block]])
632− -> State WriterState Doc
689+ -> MD Doc
633690definitionListItemToMarkdown opts (label, defs) = do
634691 labelText <- inlineListToMarkdown opts label
635692 defs' <- mapM (mapM (blockToMarkdown opts)) defs
636693 if isEnabled Ext_definition_lists opts
637694 then do
638695 let tabStop = writerTabStop opts
639− st <- get
640− let leader = if stPlain st then " " else ": "
696+ isPlain <- asks envPlain
697+ let leader = if isPlain then " " else ": "
641698 let sps = case writerTabStop opts - 3 of
642699 n | n > 0 -> text $ replicate n ' '
643700 _ -> text " "
@@ -661,7 +718,7 @@ definitionListItemToMarkdown opts (label, defs) = do
661718-- | Convert list of Pandoc block elements to markdown.
662719blockListToMarkdown :: WriterOptions -- ^ Options
663720 -> [Block] -- ^ List of block elements
664− -> State WriterState Doc
721+ -> MD Doc
665722blockListToMarkdown opts blocks =
666723 mapM (blockToMarkdown opts) (fixBlocks blocks) >>= return . cat
667724 -- insert comment between list and indented code block, or the
@@ -688,7 +745,7 @@ blockListToMarkdown opts blocks =
688745
689746-- | Get reference for target; if none exists, create unique one and return.
690747-- Prefer label if possible; otherwise, generate a unique key.
691−getReference :: Attr -> [Inline] -> Target -> State WriterState [Inline]
748+getReference :: Attr -> [Inline] -> Target -> MD [Inline]
692749getReference attr label target = do
693750 st <- get
694751 case find (\(_,t,a) -> t == target && a == attr) (stRefs st) of
@@ -706,9 +763,9 @@ getReference attr label target = do
706763 return label'
707764
708765-- | Convert list of Pandoc inline elements to markdown.
709−inlineListToMarkdown :: WriterOptions -> [Inline] -> State WriterState Doc
766+inlineListToMarkdown :: WriterOptions -> [Inline] -> MD Doc
710767inlineListToMarkdown opts lst = do
711− inlist <- gets stInList
768+ inlist <- asks envInList
712769 go (if inlist then avoidBadWrapsInList lst else lst)
713770 where go [] = return empty
714771 go (i:is) = case i of
@@ -731,9 +788,9 @@ inlineListToMarkdown opts lst = do
731788 _ -> shortcutable
732789 where shortcutable = liftM2 (<>) (inlineToMarkdown opts i) (go is)
733790 unshortcutable = do
734− iMark <- withState (\s -> s { stRefShortcutable = False })
735− (inlineToMarkdown opts i)
736− modify (\s -> s {stRefShortcutable = True })
791+ iMark <- local
792+ (\env -> env { envRefShortcutable = False })
793+ (inlineToMarkdown opts i)
737794 fmap (iMark <>) (go is)
738795
739796isSp :: Inline -> Bool
@@ -773,22 +830,22 @@ escapeSpaces SoftBreak = Str "\\ "
773830escapeSpaces x = x
774831
775832-- | Convert Pandoc inline element to markdown.
776−inlineToMarkdown :: WriterOptions -> Inline -> State WriterState Doc
833+inlineToMarkdown :: WriterOptions -> Inline -> MD Doc
777834inlineToMarkdown opts (Span attrs ils) = do
778− plain <- gets stPlain
835+ plain <- asks envPlain
779836 contents <- inlineListToMarkdown opts ils
780837 return $ if not plain &&
781838 (isEnabled Ext_raw_html opts || isEnabled Ext_native_spans opts)
782839 then tagWithAttrs "span" attrs <> contents <> text "</span>"
783840 else contents
784841inlineToMarkdown opts (Emph lst) = do
785− plain <- gets stPlain
842+ plain <- asks envPlain
786843 contents <- inlineListToMarkdown opts lst
787844 return $ if plain
788845 then "_" <> contents <> "_"
789846 else "*" <> contents <> "*"
790847inlineToMarkdown opts (Strong lst) = do
791− plain <- gets stPlain
848+ plain <- asks envPlain
792849 if plain
793850 then inlineListToMarkdown opts $ capitalize lst
794851 else do
@@ -827,7 +884,7 @@ inlineToMarkdown opts (Subscript lst) = do
827884 _ -> contents
828885 where toSubscript c = chr (0x2080 + (ord c - 48))
829886inlineToMarkdown opts (SmallCaps lst) = do
830− plain <- gets stPlain
887+ plain <- asks envPlain
831888 if not plain &&
832889 (isEnabled Ext_raw_html opts || isEnabled Ext_native_spans opts)
833890 then do
@@ -852,13 +909,13 @@ inlineToMarkdown opts (Code attr str) = do
852909 let attrs = if isEnabled Ext_inline_code_attributes opts && attr /= nullAttr
853910 then attrsToMarkdown attr
854911 else empty
855− plain <- gets stPlain
912+ plain <- asks envPlain
856913 if plain
857914 then return $ text str
858915 else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
859916inlineToMarkdown opts (Str str) = do
860− st <- get
861− if stPlain st
917+ isPlain <- asks envPlain
918+ if isPlain
862919 then return $ text str
863920 else return $ text $ escapeString opts str
864921inlineToMarkdown opts (Math InlineMath str) =
@@ -873,7 +930,7 @@ inlineToMarkdown opts (Math InlineMath str) =
873930 | isEnabled Ext_tex_math_double_backslash opts ->
874931 return $ "\\\\(" <> text str <> "\\\\)"
875932 | otherwise -> do
876− plain <- gets stPlain
933+ plain <- asks envPlain
877934 inlineListToMarkdown opts $
878935 (if plain then makeMathPlainer else id) $
879936 texMathToInlines InlineMath str
@@ -887,7 +944,7 @@ inlineToMarkdown opts (Math DisplayMath str)
887944 | otherwise = (\x -> cr <> x <> cr) `fmap`
888945 inlineListToMarkdown opts (texMathToInlines DisplayMath str)
889946inlineToMarkdown opts (RawInline f str) = do
890− plain <- gets stPlain
947+ plain <- asks envPlain
891948 if not plain &&
892949 ( f == "markdown" ||
893950 (isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) ||
@@ -895,7 +952,7 @@ inlineToMarkdown opts (RawInline f str) = do
895952 then return $ text str
896953 else return empty
897954inlineToMarkdown opts (LineBreak) = do
898− plain <- gets stPlain
955+ plain <- asks envPlain
899956 if plain || isEnabled Ext_hard_line_breaks opts
900957 then return cr
901958 else return $
@@ -944,7 +1001,7 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit))
9441001 attr /= nullAttr = -- use raw HTML
9451002 return $ text $ trim $ writeHtmlString def $ Pandoc nullMeta [Plain [lnk]]
9461003 | otherwise = do
947− plain <- gets stPlain
1004+ plain <- asks envPlain
9481005 linktext <- inlineListToMarkdown opts txt
9491006 let linktitle = if null tit
9501007 then empty
@@ -955,7 +1012,7 @@ inlineToMarkdown opts lnk@(Link attr txt (src, tit))
9551012 [Str s] | escapeURI s == srcSuffix -> True
9561013 _ -> False
9571014 let useRefLinks = writerReferenceLinks opts && not useAuto
958− shortcutable <- gets stRefShortcutable
1015+ shortcutable <- asks envRefShortcutable
9591016 let useShortcutRefLinks = shortcutable &&
9601017 isEnabled Ext_shortcut_reference_links opts
9611018 ref <- if useRefLinks then getReference attr txt (src, tit) else return []
@@ -983,7 +1040,7 @@ inlineToMarkdown opts img@(Image attr alternate (source, tit))
9831040 attr /= nullAttr = -- use raw HTML
9841041 return $ text $ trim $ writeHtmlString def $ Pandoc nullMeta [Plain [img]]
9851042 | otherwise = do
986− plain <- gets stPlain
1043+ plain <- asks envPlain
9871044 let txt = if null alternate || alternate == [Str source]
9881045 -- to prevent autolinks
9891046 then [Str ""]
@@ -995,7 +1052,7 @@ inlineToMarkdown opts img@(Image attr alternate (source, tit))
9951052inlineToMarkdown opts (Note contents) = do
9961053 modify (\st -> st{ stNotes = contents : stNotes st })
9971054 st <- get
998− let ref = text $ writerIdentifierPrefix opts ++ show (length $ stNotes st)
1055+ let ref = text $ writerIdentifierPrefix opts ++ show (stNoteNum st + (length $ stNotes st) - 1)
9991056 if isEnabled Ext_footnotes opts
10001057 then return $ "[^" <> ref <> "]"
10011058 else return $ "[" <> ref <> "]"
tests/Tests/Writers/Markdown.hs+138−2
@@ -11,6 +11,9 @@ import Tests.Arbitrary()
1111markdown :: (ToPandoc a) => a -> String
1212markdown = writeMarkdown def . toPandoc
1313
14+markdownWithOpts :: (ToPandoc a) => WriterOptions -> a -> String
15+markdownWithOpts opts x = writeMarkdown opts $ toPandoc x
16+
1417{-
1518 "my test" =: X =?> Y
1619
@@ -36,13 +39,146 @@ tests = [ "indented code after list"
3639 =: bulletList [ plain "foo" <> bulletList [ plain "bar" ],
3740 plain "baz" ]
3841 =?> "- foo\n - bar\n- baz\n"
39− ] ++ [shortcutLinkRefsTests]
42+ ] ++ [noteTests] ++ [shortcutLinkRefsTests]
43+
44+{-
45+
46+Testing with the following text:
47+
48+First Header
49+============
50+
51+This is a footnote.[^1] And this is a [link](https://www.google.com).
52+
53+> A note inside a block quote.[^2]
54+>
55+> A second paragraph.
56+
57+Second Header
58+=============
59+
60+Some more text.
61+
62+
63+[^1]: Down here.
64+
65+[^2]: The second note.
66+
67+-}
68+
69+noteTestDoc :: Blocks
70+noteTestDoc =
71+ header 1 "First Header" <>
72+ para ("This is a footnote." <>
73+ note (para "Down here.") <>
74+ " And this is a " <>
75+ link "https://www.google.com" "" "link" <>
76+ ".") <>
77+ blockQuote (para ("A note inside a block quote." <>
78+ note (para "The second note.")) <>
79+ para ("A second paragraph.")) <>
80+ header 1 "Second Header" <>
81+ para "Some more text."
82+
83+
84+
85+noteTests :: Test
86+noteTests = testGroup "note and reference location"
87+ [ test (markdownWithOpts def)
88+ "footnotes at the end of a document" $
89+ noteTestDoc =?>
90+ (unlines $ [ "First Header"
91+ , "============"
92+ , ""
93+ , "This is a footnote.[^1] And this is a [link](https://www.google.com)."
94+ , ""
95+ , "> A note inside a block quote.[^2]"
96+ , ">"
97+ , "> A second paragraph."
98+ , ""
99+ , "Second Header"
100+ , "============="
101+ , ""
102+ , "Some more text."
103+ , ""
104+ , "[^1]: Down here."
105+ , ""
106+ , "[^2]: The second note."
107+ ])
108+ , test (markdownWithOpts def{writerReferenceLocation=EndOfBlock})
109+ "footnotes at the end of blocks" $
110+ noteTestDoc =?>
111+ (unlines $ [ "First Header"
112+ , "============"
113+ , ""
114+ , "This is a footnote.[^1] And this is a [link](https://www.google.com)."
115+ , ""
116+ , "[^1]: Down here."
117+ , ""
118+ , "> A note inside a block quote.[^2]"
119+ , ">"
120+ , "> A second paragraph."
121+ , ""
122+ , "[^2]: The second note."
123+ , ""
124+ , "Second Header"
125+ , "============="
126+ , ""
127+ , "Some more text."
128+ ])
129+ , test (markdownWithOpts def{writerReferenceLocation=EndOfBlock, writerReferenceLinks=True})
130+ "footnotes and reference links at the end of blocks" $
131+ noteTestDoc =?>
132+ (unlines $ [ "First Header"
133+ , "============"
134+ , ""
135+ , "This is a footnote.[^1] And this is a [link]."
136+ , ""
137+ , "[^1]: Down here."
138+ , ""
139+ , " [link]: https://www.google.com"
140+ , ""
141+ , "> A note inside a block quote.[^2]"
142+ , ">"
143+ , "> A second paragraph."
144+ , ""
145+ , "[^2]: The second note."
146+ , ""
147+ , "Second Header"
148+ , "============="
149+ , ""
150+ , "Some more text."
151+ ])
152+ , test (markdownWithOpts def{writerReferenceLocation=EndOfSection})
153+ "footnotes at the end of section" $
154+ noteTestDoc =?>
155+ (unlines $ [ "First Header"
156+ , "============"
157+ , ""
158+ , "This is a footnote.[^1] And this is a [link](https://www.google.com)."
159+ , ""
160+ , "> A note inside a block quote.[^2]"
161+ , ">"
162+ , "> A second paragraph."
163+ , ""
164+ , "[^1]: Down here."
165+ , ""
166+ , "[^2]: The second note."
167+ , ""
168+ , "Second Header"
169+ , "============="
170+ , ""
171+ , "Some more text."
172+ ])
173+
174+ ]
40175
41176shortcutLinkRefsTests :: Test
42177shortcutLinkRefsTests =
43178 let infix 4 =:
44179 (=:) :: (ToString a, ToPandoc a)
45− => String -> (a, String) -> Test
180+
181+ => String -> (a, String) -> Test
46182 (=:) = test (writeMarkdown (def {writerReferenceLinks = True}) . toPandoc)
47183 in testGroup "Shortcut reference links"
48184 [ "Simple link (shortcutable)"