git.iko.soy/typeable/pandoc
on master help sign in
Muse reader: parse anchors immediately after headings as IDs
0405c5bAlexander Krotov
src/Text/Pandoc/Readers/Muse.hs+9−5
@@ -31,7 +31,6 @@ Conversion of Muse text to 'Pandoc' document.
31{-
31{-
32TODO:
32TODO:
33- Page breaks (five "*")
33- Page breaks (five "*")
34−- Headings with anchors (make it round trip with Muse writer)
35- Org tables
34- Org tables
36- table.el tables
35- table.el tables
37- Images with attributes (floating and width)
36- Images with attributes (floating and width)
@@ -241,7 +240,8 @@ header = try $ do
241 guard $ level <= 5
240 guard $ level <= 5
242 spaceChar
241 spaceChar
243 content <- trimInlinesF . mconcat <$> manyTill inline eol
242 content <- trimInlinesF . mconcat <$> manyTill inline eol
243+ anchorId <- option "" parseAnchor
244− attr <- registerHeader ("", [], []) (runF content defaultParserState)
244+ attr <- registerHeader (anchorId, [], []) (runF content defaultParserState)
245 return $ B.headerWith attr level <$> content
245 return $ B.headerWith attr level <$> content
246
246
247example :: PandocMonad m => MuseParser m (F Blocks)
247example :: PandocMonad m => MuseParser m (F Blocks)
@@ -629,14 +629,18 @@ endline = try $ do
629 notFollowedBy blankline
629 notFollowedBy blankline
630 returnF B.softbreak
630 returnF B.softbreak
631
631
632−anchor :: PandocMonad m => MuseParser m (F Inlines)
632+parseAnchor :: PandocMonad m => MuseParser m String
633−anchor = try $ do
633+parseAnchor = try $ do
634 getPosition >>= \pos -> guard (sourceColumn pos == 1)
634 getPosition >>= \pos -> guard (sourceColumn pos == 1)
635 char '#'
635 char '#'
636 first <- letter
636 first <- letter
637 rest <- many (letter <|> digit)
637 rest <- many (letter <|> digit)
638 skipMany spaceChar <|> void newline
638 skipMany spaceChar <|> void newline
639− let anchorId = first:rest
639+ return $ first:rest
640+
641+anchor :: PandocMonad m => MuseParser m (F Inlines)
642+anchor = try $ do
643+ anchorId <- parseAnchor
640 return $ return $ B.spanWith (anchorId, [], []) mempty
644 return $ return $ B.spanWith (anchorId, [], []) mempty
641
645
642footnote :: PandocMonad m => MuseParser m (F Inlines)
646footnote :: PandocMonad m => MuseParser m (F Inlines)
src/Text/Pandoc/Writers/Muse.hs+1−1
@@ -229,7 +229,7 @@ blockToMuse (Header level (ident,_,_) inlines) = do
229 else "#" <> text ident <> cr
229 else "#" <> text ident <> cr
230 let header' = text $ replicate level '*'
230 let header' = text $ replicate level '*'
231 return $ blankline <> nowrap (header' <> space <> contents)
231 return $ blankline <> nowrap (header' <> space <> contents)
232− <> blankline <> attr'
232+ $$ attr' <> blankline
233-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
233-- https://www.gnu.org/software/emacs-muse/manual/muse.html#Horizontal-Rules-and-Anchors
234blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
234blockToMuse HorizontalRule = return $ blankline $$ "----" $$ blankline
235blockToMuse (Table caption _ _ headers rows) = do
235blockToMuse (Table caption _ _ headers rows) = do
test/Tests/Readers/Muse.hs+12
@@ -455,6 +455,18 @@ tests =
455 , "</quote>"
455 , "</quote>"
456 ] =?>
456 ] =?>
457 blockQuote (para "* Hi")
457 blockQuote (para "* Hi")
458+ , "Headers consume anchors" =:
459+ T.unlines [ "** Foo"
460+ , "#bar"
461+ ] =?>
462+ headerWith ("bar",[],[]) 2 "Foo"
463+ , "Headers don't consume anchors separated with a blankline" =:
464+ T.unlines [ "** Foo"
465+ , ""
466+ , "#bar"
467+ ] =?>
468+ header 2 "Foo" <>
469+ para (spanWith ("bar", [], []) mempty)
458 ]
470 ]
459 , testGroup "Directives"
471 , testGroup "Directives"
460 [ "Title" =:
472 [ "Title" =:
test/Tests/Writers/Muse.hs+5
@@ -234,6 +234,11 @@ tests = [ testGroup "block elements"
234 , ""
234 , ""
235 , "*** Third level"
235 , "*** Third level"
236 ]
236 ]
237+ , "heading with ID" =:
238+ headerWith ("bar", [], []) 2 (text "Foo") =?>
239+ unlines [ "** Foo"
240+ , "#bar"
241+ ]
237 ]
242 ]
238 , "horizontal rule" =: horizontalRule =?> "----"
243 , "horizontal rule" =: horizontalRule =?> "----"
239 , "escape horizontal rule" =: para (text "----") =?> "<verbatim>----</verbatim>"
244 , "escape horizontal rule" =: para (text "----") =?> "<verbatim>----</verbatim>"