@@ -45,6 +45,7 @@ even though it is supported only in Emacs Muse.
4545module Text.Pandoc.Writers.Muse (writeMuse) where
4646import Prelude
4747import Control.Monad.State.Strict
48+import Data.Char (isSpace, isDigit, isAsciiUpper, isAsciiLower)
4849import Data.Text (Text)
4950import Data.List (intersperse, transpose, isInfixOf)
5051import System.FilePath (takeExtension)
@@ -153,9 +154,9 @@ blockListToMuse blocks = do
153154blockToMuse :: PandocMonad m
154155 => Block -- ^ Block element
155156 -> StateT WriterState m Doc
156−blockToMuse (Plain inlines) = inlineListToMuse inlines
157+blockToMuse (Plain inlines) = inlineListToMuse' inlines
157158blockToMuse (Para inlines) = do
158− contents <- inlineListToMuse inlines
159+ contents <- inlineListToMuse' inlines
159160 return $ contents <> blankline
160161blockToMuse (LineBlock lns) = do
161162 lns' <- mapM inlineListToMuse lns
@@ -206,7 +207,7 @@ blockToMuse (DefinitionList items) = do
206207 => ([Inline], [[Block]])
207208 -> StateT WriterState m Doc
208209 definitionListItemToMuse (label, defs) = do
209− label' <- inlineListToMuse label
210+ label' <- inlineListToMuse' label
210211 contents <- liftM vcat $ mapM descriptionToMuse defs
211212 let ind = offset label'
212213 return $ hang ind label' contents
@@ -280,15 +281,23 @@ escapeString s =
280281 substitute "</verbatim>" "<</verbatim><verbatim>/verbatim>" s ++
281282 "</verbatim>"
282283
284+startsWithMarker :: (Char -> Bool) -> String -> Bool
285+startsWithMarker f (' ':xs) = startsWithMarker f xs
286+startsWithMarker f (x:xs) =
287+ f x && (startsWithMarker f xs || startsWithDot xs)
288+ where
289+ startsWithDot ('.':[]) = True
290+ startsWithDot ('.':c:_) = isSpace c
291+ startsWithDot _ = False
292+startsWithMarker _ [] = False
293+
283294-- | Escape special characters for Muse if needed.
284295conditionalEscapeString :: String -> String
285296conditionalEscapeString s =
286297 if any (`elem` ("#*<=>[]|" :: String)) s ||
287298 "::" `isInfixOf` s ||
288299 "----" `isInfixOf` s ||
289− "~~" `isInfixOf` s ||
290− "-" == s ||
291− ";" == s
300+ "~~" `isInfixOf` s
292301 then escapeString s
293302 else s
294303
@@ -354,15 +363,44 @@ urlEscapeBrackets (']':xs) = '%':'5':'D':urlEscapeBrackets xs
354363urlEscapeBrackets (x:xs) = x:urlEscapeBrackets xs
355364urlEscapeBrackets [] = []
356365
357−-- | Convert list of Pandoc inline elements to Muse.
358−inlineListToMuse :: PandocMonad m
359− => [Inline]
366+fixOrEscape :: Inline -> Bool
367+fixOrEscape (Str "-") = True -- TODO: " - " should be escaped too
368+fixOrEscape (Str ";") = True
369+fixOrEscape (Str s) = startsWithMarker isDigit s ||
370+ startsWithMarker isAsciiLower s ||
371+ startsWithMarker isAsciiUpper s
372+fixOrEscape (Space) = True
373+fixOrEscape (SoftBreak) = True
374+fixOrEscape _ = False
375+
376+-- | Convert list of Pandoc inline elements to Muse
377+renderInlineList :: PandocMonad m
378+ => Bool
379+ -> [Inline]
360380 -> StateT WriterState m Doc
361−inlineListToMuse lst = do
362− lst' <- normalizeInlineList <$> preprocessInlineList (map (removeKeyValues . replaceSmallCaps) lst)
363− if null lst'
364− then pure "<verbatim></verbatim>"
365− else hcat <$> mapM inlineToMuse (fixNotes lst')
381+renderInlineList True [] = pure "<verbatim></verbatim>"
382+renderInlineList False [] = pure ""
383+renderInlineList start lst@(x:xs) = do r <- inlineToMuse x
384+ opts <- gets stOptions
385+ lst' <- renderInlineList (x == SoftBreak && writerWrapText opts == WrapPreserve) xs --hcat <$> mapM inlineToMuse xs
386+ if start && fixOrEscape x
387+ then pure ((text "<verbatim></verbatim>") <> r <> lst')
388+ else pure (r <> lst')
389+
390+-- | Normalize and convert list of Pandoc inline elements to Muse.
391+inlineListToMuse'' :: PandocMonad m
392+ => Bool
393+ -> [Inline]
394+ -> StateT WriterState m Doc
395+inlineListToMuse'' start lst = do
396+ lst' <- (normalizeInlineList . fixNotes) <$> preprocessInlineList (map (removeKeyValues . replaceSmallCaps) lst)
397+ renderInlineList start lst'
398+
399+inlineListToMuse' :: PandocMonad m => [Inline] -> StateT WriterState m Doc
400+inlineListToMuse' = inlineListToMuse'' True
401+
402+inlineListToMuse :: PandocMonad m => [Inline] -> StateT WriterState m Doc
403+inlineListToMuse = inlineListToMuse'' False
366404
367405-- | Convert Pandoc inline element to Muse.
368406inlineToMuse :: PandocMonad m