git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: don't break headers, line blocks and tables with line breaks
commit52803e2960c3520f8b2159f9076cb454c03988f8 authorAlexander Krotov <ilabdsf@gmail.com> dateMon 9 Apr 2018, 03:16 (9 years ago) parent79b67de
2 files changed+12−5
src/Text/Pandoc/Writers/Muse.hs+10−5 test/Tests/Writers/Muse.hs+2
src/Text/Pandoc/Writers/Muse.hs+10−5
@@ -73,6 +73,7 @@ data WriterEnv =
7373 , envInlineStart :: Bool
7474 , envInsideLinkDescription :: Bool -- ^ Escape ] if True
7575 , envAfterSpace :: Bool
76+ , envOneLine :: Bool -- ^ True if newlines are not allowed
7677 }
7778
7879data WriterState =
@@ -101,6 +102,7 @@ writeMuse opts document =
101102 , envInlineStart = True
102103 , envInsideLinkDescription = False
103104 , envAfterSpace = True
105+ , envOneLine = False
104106 }
105107
106108-- | Return Muse representation of document.
@@ -173,7 +175,7 @@ blockToMuse (Para inlines) = do
173175 contents <- inlineListToMuse' inlines
174176 return $ contents <> blankline
175177blockToMuse (LineBlock lns) = do
176− lns' <- mapM inlineListToMuse lns
178+ lns' <- local (\env -> env { envOneLine = True }) $ mapM inlineListToMuse lns
177179 return $ nowrap $ vcat (map (text "> " <>) lns') <> blankline
178180blockToMuse (CodeBlock (_,_,_) str) =
179181 return $ "<example>" $$ text str $$ "</example>" $$ blankline
@@ -221,7 +223,7 @@ blockToMuse (DefinitionList items) = do
221223 => ([Inline], [[Block]])
222224 -> Muse m Doc
223225 definitionListItemToMuse (label, defs) = do
224− label' <- inlineListToMuse' label
226+ label' <- local (\env -> env { envOneLine = True }) $ inlineListToMuse' label
225227 contents <- vcat <$> mapM descriptionToMuse defs
226228 let ind = offset label'
227229 return $ hang ind label' contents
@@ -231,7 +233,7 @@ blockToMuse (DefinitionList items) = do
231233 descriptionToMuse desc = hang 4 " :: " <$> blockListToMuse desc
232234blockToMuse (Header level (ident,_,_) inlines) = do
233235 opts <- asks envOptions
234− contents <- inlineListToMuse' inlines
236+ contents <- local (\env -> env { envOneLine = True }) $ inlineListToMuse' inlines
235237 ids <- gets stIds
236238 let autoId = uniqueIdent inlines ids
237239 modify $ \st -> st{ stIds = Set.insert autoId ids }
@@ -486,11 +488,14 @@ inlineToMuse Math{} =
486488 fail "Math should be expanded before normalization"
487489inlineToMuse (RawInline (Format f) str) =
488490 return $ "<literal style=\"" <> text f <> "\">" <> text str <> "</literal>"
489−inlineToMuse LineBreak = return $ "<br>" <> cr
491+inlineToMuse LineBreak = do
492+ oneline <- asks envOneLine
493+ return $ if oneline then "<br>" else "<br>" <> cr
490494inlineToMuse Space = return space
491495inlineToMuse SoftBreak = do
496+ oneline <- asks envOneLine
492497 wrapText <- asks $ writerWrapText . envOptions
493− return $ if wrapText == WrapPreserve then cr else space
498+ return $ if not oneline && wrapText == WrapPreserve then cr else space
494499inlineToMuse (Link _ txt (src, _)) =
495500 case txt of
496501 [Str x] | escapeURI x == src ->
test/Tests/Writers/Muse.hs+2
@@ -360,6 +360,8 @@ tests = [ testGroup "block elements"
360360 "remove soft break" $ text "a" <> softbreak <> text "b"
361361 =?> "a b"
362362 , "line break" =: text "a" <> linebreak <> text "b" =?> "a<br>\nb"
363+ , "no newline after line break in header" =: header 1 (text "a" <> linebreak <> text "b") =?> "* a<br>b"
364+ , "no softbreak in header" =: header 1 (text "a" <> softbreak <> text "b") =?> "* a b"
363365 ]
364366 , testGroup "math"
365367 [ "inline math" =: math "2^3" =?> "2<sup>3</sup>"