Skip to content

Commit

Permalink
Muse reader: add endnote support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Krotov committed Dec 15, 2017
1 parent de649bf commit f7913d7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Text/Pandoc/Readers/Muse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -334,19 +334,30 @@ para = do
endOfPara = try $ blankline >> skipMany1 blankline
newBlockElement = try $ blankline >> void blockElements

noteMarker :: PandocMonad m => MuseParser m String
noteMarker = try $ do
char '['
many1Till digit $ 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
ref <- many1Till digit $ char r
return (nt, [l] ++ ref ++ [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
content <- listItemContents $ 3 + length ref
(_, ref) <- anyNoteMarker <* spaceChar
content <- listItemContents $ 1 + length ref
oldnotes <- stateNotes' <$> getState
case M.lookup ref oldnotes of
Just _ -> logMessage $ DuplicateNoteReference ref pos
Expand All @@ -360,7 +371,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 @@ -370,7 +381,7 @@ emacsNoteBlock = try $ do
return mempty
where
blocksTillNote =
many1Till block (eof <|> () <$ lookAhead noteMarker)
many1Till block (eof <|> () <$ lookAhead anyNoteMarker)

--
-- Verse markup
Expand Down Expand Up @@ -640,15 +651,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'

whitespace :: PandocMonad m => MuseParser m (F Inlines)
whitespace = fmap return (lb <|> regsp)
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 @@ -496,6 +496,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 f7913d7

Please sign in to comment.