git.iko.soy/typeable/pandoc
on master help sign in
Muse writer: escape ordered list markers

Also reduced amount of <verbatim> tags in output to avoid escaping every "-" and word that ends in a full stop.

commita3f659d2c0bccd01d2eceba8756c728f90d4f231 authorAlexander Krotov <ilabdsf@gmail.com> dateSun 25 Mar 2018, 18:11 (9 years ago) parentc24c9ef
3 files changed+59−20
src/Text/Pandoc/Writers/Muse.hs+52−14 test/Tests/Writers/Muse.hs+5−4 test/writer.muse+2−2
src/Text/Pandoc/Writers/Muse.hs+52−14
@@ -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
test/Tests/Writers/Muse.hs+5−4
@@ -312,16 +312,17 @@ tests = [ testGroup "block elements"
312312 -- We don't want colons to be escaped if they can't be confused
313313 -- with definition list item markers.
314314 , "do not escape colon" =: str ":" =?> ":"
315− , "escape - to avoid accidental unordered lists" =: text " - foo" =?> " <verbatim>-</verbatim> foo"
315+ , "escape - to avoid accidental unordered lists" =: text " - foo" =?> "<verbatim></verbatim> - foo"
316316 , "escape - inside a list to avoid accidental nested unordered lists" =:
317317 bulletList [ (para $ text "foo") <>
318318 (para $ text "- bar")
319319 ] =?>
320320 unlines [ " - foo"
321321 , ""
322− , " <verbatim>-</verbatim> bar"
322+ , " <verbatim></verbatim>- bar"
323323 ]
324− , "escape ; to avoid accidental comments" =: text "; foo" =?> "<verbatim>;</verbatim> foo"
324+ , "escape ; to avoid accidental comments" =: text "; foo" =?> "<verbatim></verbatim>; foo"
325+ , "escape ; after softbreak" =: text "foo" <> softbreak <> text "; bar" =?> "foo\n<verbatim></verbatim>; bar"
325326 ]
326327 , testGroup "emphasis"
327328 [ "emph" =: emph (text "foo") =?> "<em>foo</em>"
@@ -408,7 +409,7 @@ tests = [ testGroup "block elements"
408409 , "empty span with anchor" =: spanWith ("anchor", [], []) (mempty)
409410 =?> "#anchor"
410411 , "empty span without class and anchor" =: spanWith ("", [], []) (mempty)
411− =?> "<class><verbatim></verbatim></class>"
412+ =?> "<class></class>"
412413 , "span with class and anchor" =: spanWith ("anchor", ["foo"], []) (text "bar")
413414 =?> "#anchor <class name=\"foo\">bar</class>"
414415 , "adjacent spans" =: spanWith ("", ["syllable"], []) (str "wa") <>
test/writer.muse+2−2
@@ -245,7 +245,7 @@ Should not be a list item:
245245
246246M.A. 2007
247247
248−B. Williams
248+<verbatim></verbatim>B. Williams
249249
250250----
251251
@@ -594,7 +594,7 @@ Bang: !
594594
595595Plus: +
596596
597−Minus: <verbatim>-</verbatim>
597+Minus: -
598598
599599----
600600