git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #3065 from tarleb/org-verse-indent

Org reader: preserve indentation of verse lines

commit13424a2bd7ffbf049a6f3b3303fcb486f9385511 authorJohn MacFarlane <jgm@berkeley.edu> dateTue 9 Aug 2016, 21:33 (10 years ago) parent3a6e15a13280a8
2 files changed+18−1
src/Text/Pandoc/Readers/Org/Blocks.hs+10−1 tests/Tests/Readers/Org.hs+8
src/Text/Pandoc/Readers/Org/Blocks.hs+10−1
@@ -422,7 +422,16 @@ verseBlock blockType = try $ do
422422 ignHeaders
423423 content <- rawBlockContent blockType
424424 fmap B.para . mconcat . intersperse (pure B.linebreak)
425− <$> mapM (parseFromString inlines) (map (++ "\n") . lines $ content)
425+ <$> mapM parseVerseLine (lines content)
426+ where
427+ -- replace initial spaces with nonbreaking spaces to preserve
428+ -- indentation, parse the rest as normal inline
429+ parseVerseLine :: String -> OrgParser (F Inlines)
430+ parseVerseLine cs = do
431+ let (initialSpaces, indentedLine) = span isSpace cs
432+ let nbspIndent = B.str $ map (const '\160') initialSpaces
433+ line <- parseFromString inlines (indentedLine ++ "\n")
434+ return (pure nbspIndent <> line)
426435
427436-- | Read a code block and the associated results block if present. Which of
428437-- boths blocks is included in the output is determined using the "exports"
tests/Tests/Readers/Org.hs+8
@@ -1440,6 +1440,14 @@ tests =
14401440 ] =?>
14411441 para ("foo" <> linebreak <> linebreak <> "bar")
14421442
1443+ , "Verse block with varying indentation" =:
1444+ unlines [ "#+BEGIN_VERSE"
1445+ , " hello darkness"
1446+ , "my old friend"
1447+ , "#+END_VERSE"
1448+ ] =?>
1449+ para ("\160\160hello darkness" <> linebreak <> "my old friend")
1450+
14431451 , "Raw block LaTeX" =:
14441452 unlines [ "#+BEGIN_LaTeX"
14451453 , "The category $\\cat{Set}$ is adhesive."