Skip to content

Commit

Permalink
Muse reader: add support for endnotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krotov committed Jan 19, 2018
1 parent 711d1ed commit adb9625
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
32 changes: 21 additions & 11 deletions src/Text/Pandoc/Readers/Muse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -346,20 +346,30 @@ para = do
endOfPara = try $ blankline >> skipMany1 blankline
newBlockElement = try $ blankline >> void blockElements

noteMarker :: PandocMonad m => MuseParser m String
noteMarker = try $ do
char '['
noteBrackets :: NoteType -> (Char, Char)
noteBrackets nt =
case nt of
EndNote -> ('{', '}')
_ -> ('[', ']')

noteMarker :: PandocMonad m => NoteType -> MuseParser m (NoteType, String)
noteMarker nt = try $ do
char l
first <- oneOf "123456789"
rest <- manyTill digit (char ']')
return $ first:rest
rest <- manyTill digit (char r)
return (nt, [l] ++ (first:rest) ++ [r])
where (l, r) = noteBrackets nt

anyNoteMarker :: PandocMonad m => MuseParser m (NoteType, String)
anyNoteMarker = noteMarker FootNote <|> noteMarker EndNote

-- Amusewiki version of note
-- Parsing is similar to list item, except that note marker is used instead of list marker
amuseNoteBlock :: PandocMonad m => MuseParser m (F Blocks)
amuseNoteBlock = try $ do
guardEnabled Ext_amuse
pos <- getPosition
ref <- noteMarker <* spaceChar
(_, ref) <- anyNoteMarker <* spaceChar
content <- listItemContents
oldnotes <- stateNotes' <$> getState
case M.lookup ref oldnotes of
Expand All @@ -374,7 +384,7 @@ emacsNoteBlock :: PandocMonad m => MuseParser m (F Blocks)
emacsNoteBlock = try $ do
guardDisabled Ext_amuse
pos <- getPosition
ref <- noteMarker <* skipSpaces
(_, ref) <- anyNoteMarker <* skipSpaces
content <- mconcat <$> blocksTillNote
oldnotes <- stateNotes' <$> getState
case M.lookup ref oldnotes of
Expand All @@ -384,7 +394,7 @@ emacsNoteBlock = try $ do
return mempty
where
blocksTillNote =
many1Till block (eof <|> () <$ lookAhead noteMarker)
many1Till block (eof <|> () <$ lookAhead anyNoteMarker)

--
-- Verse markup
Expand Down Expand Up @@ -629,15 +639,15 @@ anchor = try $ do

footnote :: PandocMonad m => MuseParser m (F Inlines)
footnote = try $ do
ref <- noteMarker
(notetype, ref) <- anyNoteMarker
return $ do
notes <- asksF stateNotes'
case M.lookup ref notes of
Nothing -> return $ B.str $ "[" ++ ref ++ "]"
Nothing -> return $ B.str ref
Just (_pos, contents) -> do
st <- askF
let contents' = runF contents st { stateNotes' = M.empty }
return $ B.note contents'
return $ B.singleton $ Note notetype $ B.toList contents'

linebreak :: PandocMonad m => MuseParser m (F Inlines)
linebreak = try $ do
Expand Down
16 changes: 16 additions & 0 deletions test/Tests/Readers/Muse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,22 @@ tests =
para (text "Here is a footnote" <>
note (para "Footnote contents") <>
str ".")
, "Simple endnote" =:
T.unlines [ "Here is an endnote{1}."
, ""
, "{1} Endnote contents"
] =?>
para (text "Here is an endnote" <>
endNote (para "Endnote contents") <>
str ".")
, "Missing footnote" =: "Foo[1]" =?> para "Foo[1]"
, "Missing endnote" =: "Foo{1}" =?> para "Foo{1}"
, "Wrong note type" =:
T.unlines [ "Here is an endnote{1}."
, ""
, "[1] Footnote contents"
] =?>
para "Here is an endnote{1}."
, "Recursive footnote" =:
T.unlines [ "Start recursion here[1]"
, ""
Expand Down

0 comments on commit adb9625

Please sign in to comment.