git.iko.soy/typeable/pandoc
on master help sign in
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.

commit02e68859ebe964002b42d76ecbd3eea51a3e1f33 authorAlexander Krotov <ilabdsf@gmail.com> dateSun 2 Sep 2018, 17:26 (8 years ago) parent1630a73
2 files changed+28−4
src/Text/Pandoc/Readers/Muse.hs+16−4 test/Tests/Readers/Muse.hs+12
src/Text/Pandoc/Readers/Muse.hs+16−4
@@ -123,7 +123,9 @@ instance HasLogMessages MuseState where
123123parseMuse :: PandocMonad m => MuseParser m Pandoc
124124parseMuse = 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
255258parseBlocks :: PandocMonad m
256259 => MuseParser m (F Blocks)
257260parseBlocks =
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+
274286parseBlocksTill :: PandocMonad m
275287 => MuseParser m a
276288 -> MuseParser m (F Blocks)
@@ -362,7 +374,7 @@ separator = try $ do
362374 return $ return B.horizontalRule
363375
364376headingStart :: 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+12
@@ -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" =: