Skip to content

Commit 02e6885

Browse files
author
Alexander Krotov
committed
Muse reader: autonumber sections in the correct order
Parsing now stops at each section header to ensure the header is registered before parsing of the next section starts.
1 parent 1630a73 commit 02e6885

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Text/Pandoc/Readers/Muse.hs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ instance HasLogMessages MuseState where
123123
parseMuse :: PandocMonad m => MuseParser m Pandoc
124124
parseMuse = do
125125
many directive
126-
blocks <- parseBlocks
126+
firstSection <- parseBlocks
127+
rest <- many parseSection
128+
let blocks = mconcat $ (firstSection : rest)
127129
st <- getState
128130
let doc = runF (do Pandoc _ bs <- B.doc <$> blocks
129131
meta <- museMeta st
@@ -252,17 +254,20 @@ directive = do
252254

253255
-- ** Block parsers
254256

257+
-- | Parse section contents until EOF or next header
255258
parseBlocks :: PandocMonad m
256259
=> MuseParser m (F Blocks)
257260
parseBlocks =
258261
try (parseEnd <|>
262+
nextSection <|>
259263
blockStart <|>
260264
listStart <|>
261265
paraStart)
262266
where
267+
nextSection = mempty <$ lookAhead headingStart
263268
parseEnd = mempty <$ eof
264-
blockStart = ((B.<>) <$> (emacsHeading <|> blockElements <|> emacsNoteBlock)
265-
<*> parseBlocks) <|> (uncurry (B.<>) <$> amuseHeadingUntil parseBlocks)
269+
blockStart = ((B.<>) <$> (blockElements <|> emacsNoteBlock)
270+
<*> parseBlocks)
266271
listStart = do
267272
updateState (\st -> st { museInPara = False })
268273
uncurry (B.<>) <$> (anyListUntil parseBlocks <|> amuseNoteBlockUntil parseBlocks)
@@ -271,6 +276,13 @@ parseBlocks =
271276
uncurry (B.<>) . first (p indent) <$> paraUntil parseBlocks
272277
where p indent = if indent >= 2 && indent < 6 then fmap B.blockQuote else id
273278

279+
-- | Parse section that starts with a header
280+
parseSection :: PandocMonad m
281+
=> MuseParser m (F Blocks)
282+
parseSection =
283+
((B.<>) <$> emacsHeading <*> parseBlocks) <|>
284+
((uncurry (B.<>)) <$> amuseHeadingUntil parseBlocks)
285+
274286
parseBlocksTill :: PandocMonad m
275287
=> MuseParser m a
276288
-> MuseParser m (F Blocks)
@@ -362,7 +374,7 @@ separator = try $ do
362374
return $ return B.horizontalRule
363375

364376
headingStart :: PandocMonad m => MuseParser m (String, Int)
365-
headingStart = do
377+
headingStart = try $ do
366378
anchorId <- option "" $ try (parseAnchor <* manyTill spaceChar eol)
367379
getPosition >>= \pos -> guard (sourceColumn pos == 1)
368380
level <- fmap length $ many1 $ char '*'

test/Tests/Readers/Muse.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,18 @@ tests =
625625
T.unlines [ "* Foo"
626626
, "bar"
627627
] =?> header 1 "Foo\nbar"
628+
, test (purely $ readMuse def { readerExtensions = extensionsFromList [Ext_amuse, Ext_auto_identifiers]})
629+
"Auto identifiers"
630+
(T.unlines [ "* foo"
631+
, "** Foo"
632+
, "* bar"
633+
, "** foo"
634+
, "* foo"
635+
] =?> headerWith ("foo",[],[]) 1 "foo" <>
636+
headerWith ("foo-1",[],[]) 2 "Foo" <>
637+
headerWith ("bar",[],[]) 1 "bar" <>
638+
headerWith ("foo-2",[],[]) 2 "foo" <>
639+
headerWith ("foo-3",[],[]) 1 "foo")
628640
]
629641
, testGroup "Directives"
630642
[ "Title" =:

0 commit comments

Comments
 (0)