git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: don't break headers, line blocks and tables with line breaks
52803e2Alexander Krotov
src/Text/Pandoc/Writers/Muse.hs+10−5
@@ -73,6 +73,7 @@ data WriterEnv =
73 , envInlineStart :: Bool
73 , envInlineStart :: Bool
74 , envInsideLinkDescription :: Bool -- ^ Escape ] if True
74 , envInsideLinkDescription :: Bool -- ^ Escape ] if True
75 , envAfterSpace :: Bool
75 , envAfterSpace :: Bool
76+ , envOneLine :: Bool -- ^ True if newlines are not allowed
76 }
77 }
77
78
78data WriterState =
79data WriterState =
@@ -101,6 +102,7 @@ writeMuse opts document =
101 , envInlineStart = True
102 , envInlineStart = True
102 , envInsideLinkDescription = False
103 , envInsideLinkDescription = False
103 , envAfterSpace = True
104 , envAfterSpace = True
105+ , envOneLine = False
104 }
106 }
105
107
106-- | Return Muse representation of document.
108-- | Return Muse representation of document.
@@ -173,7 +175,7 @@ blockToMuse (Para inlines) = do
173 contents <- inlineListToMuse' inlines
175 contents <- inlineListToMuse' inlines
174 return $ contents <> blankline
176 return $ contents <> blankline
175blockToMuse (LineBlock lns) = do
177blockToMuse (LineBlock lns) = do
176− lns' <- mapM inlineListToMuse lns
178+ lns' <- local (\env -> env { envOneLine = True }) $ mapM inlineListToMuse lns
177 return $ nowrap $ vcat (map (text "> " <>) lns') <> blankline
179 return $ nowrap $ vcat (map (text "> " <>) lns') <> blankline
178blockToMuse (CodeBlock (_,_,_) str) =
180blockToMuse (CodeBlock (_,_,_) str) =
179 return $ "<example>" $$ text str $$ "</example>" $$ blankline
181 return $ "<example>" $$ text str $$ "</example>" $$ blankline
@@ -221,7 +223,7 @@ blockToMuse (DefinitionList items) = do
221 => ([Inline], [[Block]])
223 => ([Inline], [[Block]])
222 -> Muse m Doc
224 -> Muse m Doc
223 definitionListItemToMuse (label, defs) = do
225 definitionListItemToMuse (label, defs) = do
224− label' <- inlineListToMuse' label
226+ label' <- local (\env -> env { envOneLine = True }) $ inlineListToMuse' label
225 contents <- vcat <$> mapM descriptionToMuse defs
227 contents <- vcat <$> mapM descriptionToMuse defs
226 let ind = offset label'
228 let ind = offset label'
227 return $ hang ind label' contents
229 return $ hang ind label' contents
@@ -231,7 +233,7 @@ blockToMuse (DefinitionList items) = do
231 descriptionToMuse desc = hang 4 " :: " <$> blockListToMuse desc
233 descriptionToMuse desc = hang 4 " :: " <$> blockListToMuse desc
232blockToMuse (Header level (ident,_,_) inlines) = do
234blockToMuse (Header level (ident,_,_) inlines) = do
233 opts <- asks envOptions
235 opts <- asks envOptions
234− contents <- inlineListToMuse' inlines
236+ contents <- local (\env -> env { envOneLine = True }) $ inlineListToMuse' inlines
235 ids <- gets stIds
237 ids <- gets stIds
236 let autoId = uniqueIdent inlines ids
238 let autoId = uniqueIdent inlines ids
237 modify $ \st -> st{ stIds = Set.insert autoId ids }
239 modify $ \st -> st{ stIds = Set.insert autoId ids }
@@ -486,11 +488,14 @@ inlineToMuse Math{} =
486 fail "Math should be expanded before normalization"
488 fail "Math should be expanded before normalization"
487inlineToMuse (RawInline (Format f) str) =
489inlineToMuse (RawInline (Format f) str) =
488 return $ "<literal style=\"" <> text f <> "\">" <> text str <> "</literal>"
490 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
490inlineToMuse Space = return space
494inlineToMuse Space = return space
491inlineToMuse SoftBreak = do
495inlineToMuse SoftBreak = do
496+ oneline <- asks envOneLine
492 wrapText <- asks $ writerWrapText . envOptions
497 wrapText <- asks $ writerWrapText . envOptions
493− return $ if wrapText == WrapPreserve then cr else space
498+ return $ if not oneline && wrapText == WrapPreserve then cr else space
494inlineToMuse (Link _ txt (src, _)) =
499inlineToMuse (Link _ txt (src, _)) =
495 case txt of
500 case txt of
496 [Str x] | escapeURI x == src ->
501 [Str x] | escapeURI x == src ->
test/Tests/Writers/Muse.hs+2
@@ -360,6 +360,8 @@ tests = [ testGroup "block elements"
360 "remove soft break" $ text "a" <> softbreak <> text "b"
360 "remove soft break" $ text "a" <> softbreak <> text "b"
361 =?> "a b"
361 =?> "a b"
362 , "line break" =: text "a" <> linebreak <> text "b" =?> "a<br>\nb"
362 , "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"
363 ]
365 ]
364 , testGroup "math"
366 , testGroup "math"
365 [ "inline math" =: math "2^3" =?> "2<sup>3</sup>"
367 [ "inline math" =: math "2^3" =?> "2<sup>3</sup>"