Skip to content

Commit 8380f26

Browse files
author
Alexander Krotov
committed
Muse reader: add support for endnotes
1 parent 4335bf2 commit 8380f26

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

src/Text/Pandoc/Readers/Muse.hs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -335,21 +335,31 @@ para = do
335335
endOfPara = try $ blankline >> skipMany1 blankline
336336
newBlockElement = try $ blankline >> void blockElements
337337

338-
noteMarker :: PandocMonad m => MuseParser m String
339-
noteMarker = try $ do
340-
char '['
338+
noteBrackets :: NoteType -> (Char, Char)
339+
noteBrackets nt =
340+
case nt of
341+
EndNote -> ('{', '}')
342+
_ -> ('[', ']')
343+
344+
noteMarker :: PandocMonad m => NoteType -> MuseParser m (NoteType, String)
345+
noteMarker nt = try $ do
346+
char l
341347
first <- oneOf "123456789"
342-
rest <- manyTill digit (char ']')
343-
return $ first:rest
348+
rest <- manyTill digit (char r)
349+
return (nt, [l] ++ (first:rest) ++ [r])
350+
where (l, r) = noteBrackets nt
351+
352+
anyNoteMarker :: PandocMonad m => MuseParser m (NoteType, String)
353+
anyNoteMarker = noteMarker FootNote <|> noteMarker EndNote
344354

345355
-- Amusewiki version of note
346356
-- Parsing is similar to list item, except that note marker is used instead of list marker
347357
amuseNoteBlock :: PandocMonad m => MuseParser m (F Blocks)
348358
amuseNoteBlock = try $ do
349359
guardEnabled Ext_amuse
350360
pos <- getPosition
351-
ref <- noteMarker <* spaceChar
352-
content <- listItemContents $ 3 + length ref
361+
(_, ref) <- anyNoteMarker <* spaceChar
362+
content <- listItemContents $ 1 + length ref
353363
oldnotes <- stateNotes' <$> getState
354364
case M.lookup ref oldnotes of
355365
Just _ -> logMessage $ DuplicateNoteReference ref pos
@@ -363,7 +373,7 @@ emacsNoteBlock :: PandocMonad m => MuseParser m (F Blocks)
363373
emacsNoteBlock = try $ do
364374
guardDisabled Ext_amuse
365375
pos <- getPosition
366-
ref <- noteMarker <* skipSpaces
376+
(_, ref) <- anyNoteMarker <* skipSpaces
367377
content <- mconcat <$> blocksTillNote
368378
oldnotes <- stateNotes' <$> getState
369379
case M.lookup ref oldnotes of
@@ -373,7 +383,7 @@ emacsNoteBlock = try $ do
373383
return mempty
374384
where
375385
blocksTillNote =
376-
many1Till block (eof <|> () <$ lookAhead noteMarker)
386+
many1Till block (eof <|> () <$ lookAhead anyNoteMarker)
377387

378388
--
379389
-- Verse markup
@@ -647,15 +657,15 @@ anchor = try $ do
647657

648658
footnote :: PandocMonad m => MuseParser m (F Inlines)
649659
footnote = try $ do
650-
ref <- noteMarker
660+
(notetype, ref) <- anyNoteMarker
651661
return $ do
652662
notes <- asksF stateNotes'
653663
case M.lookup ref notes of
654-
Nothing -> return $ B.str $ "[" ++ ref ++ "]"
664+
Nothing -> return $ B.str ref
655665
Just (_pos, contents) -> do
656666
st <- askF
657667
let contents' = runF contents st { stateNotes' = M.empty }
658-
return $ B.note contents'
668+
return $ B.singleton $ Note notetype $ B.toList contents'
659669

660670
whitespace :: PandocMonad m => MuseParser m (F Inlines)
661671
whitespace = fmap return (lb <|> regsp)

test/Tests/Readers/Muse.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,22 @@ tests =
509509
para (text "Here is a footnote" <>
510510
note (para "Footnote contents") <>
511511
str ".")
512+
, "Simple endnote" =:
513+
T.unlines [ "Here is an endnote{1}."
514+
, ""
515+
, "{1} Endnote contents"
516+
] =?>
517+
para (text "Here is an endnote" <>
518+
endNote (para "Endnote contents") <>
519+
str ".")
520+
, "Missing footnote" =: "Foo[1]" =?> para "Foo[1]"
521+
, "Missing endnote" =: "Foo{1}" =?> para "Foo{1}"
522+
, "Wrong note type" =:
523+
T.unlines [ "Here is an endnote{1}."
524+
, ""
525+
, "[1] Footnote contents"
526+
] =?>
527+
para "Here is an endnote{1}."
512528
, "Recursive footnote" =:
513529
T.unlines [ "Start recursion here[1]"
514530
, ""

0 commit comments

Comments
 (0)