aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Writers/Ms.hs
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2018-10-17 17:23:14 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2018-10-17 17:26:37 -0700
commitf48960b75fe2d7ae532ec17a060109c92b893f57 (patch)
treea63b379a0072f6727ce7d07310dd8fffee5c43d0 /src/Text/Pandoc/Writers/Ms.hs
parentb3feaba6af38d621d331afb83a266e2a2ccf6ea4 (diff)
downloadpandoc-f48960b75fe2d7ae532ec17a060109c92b893f57.tar.gz
Move common groff functions to Text.Pandoc.Writers.Groff
(unexported module). These are used in both the man and ms writers. Moved groffEscape out of Text.Pandoc.Writers.Shared [cancels earlier API change from adding it, which was after last release]. This fixes strong/code combination on man (should be `\f[CB]` not `\f[BC]`), mentioned in #4973. Updated tests. Closes #4975.
Diffstat (limited to 'src/Text/Pandoc/Writers/Ms.hs')
-rw-r--r--src/Text/Pandoc/Writers/Ms.hs88
1 files changed, 3 insertions, 85 deletions
diff --git a/src/Text/Pandoc/Writers/Ms.hs b/src/Text/Pandoc/Writers/Ms.hs
index 9a35a9693..cdca24702 100644
--- a/src/Text/Pandoc/Writers/Ms.hs
+++ b/src/Text/Pandoc/Writers/Ms.hs
@@ -60,36 +60,10 @@ import Text.Pandoc.Shared
import Text.Pandoc.Templates
import Text.Pandoc.Writers.Math
import Text.Pandoc.Writers.Shared
+import Text.Pandoc.Writers.Groff
import Text.Printf (printf)
import Text.TeXMath (writeEqn)
-data WriterState = WriterState { stHasInlineMath :: Bool
- , stFirstPara :: Bool
- , stNotes :: [Note]
- , stSmallCaps :: Bool
- , stHighlighting :: Bool
- , stInHeader :: Bool
- , stFontFeatures :: Map.Map Char Bool
- }
-
-defaultWriterState :: WriterState
-defaultWriterState = WriterState{ stHasInlineMath = False
- , stFirstPara = True
- , stNotes = []
- , stSmallCaps = False
- , stHighlighting = False
- , stInHeader = False
- , stFontFeatures = Map.fromList [
- ('I',False)
- , ('B',False)
- , ('C',False)
- ]
- }
-
-type Note = [Block]
-
-type MS = StateT WriterState
-
-- | Convert Pandoc to Ms.
writeMs :: PandocMonad m => WriterOptions -> Pandoc -> m Text
writeMs opts document =
@@ -132,24 +106,8 @@ pandocToMs opts (Pandoc meta blocks) = do
Nothing -> return main
Just tpl -> renderTemplate' tpl context
--- | Association list of characters to escape.
-msEscapes :: Map.Map Char String
-msEscapes = Map.fromList
- [ ('\160', "\\~")
- , ('\'', "\\[aq]")
- , ('`', "\\`")
- , ('"', "\\[dq]")
- , ('\x2014', "\\[em]")
- , ('\x2013', "\\[en]")
- , ('\x2026', "\\&...")
- , ('~', "\\[ti]")
- , ('^', "\\[ha]")
- , ('@', "\\@")
- , ('\\', "\\\\")
- ]
-
-escapeChar :: Char -> String
-escapeChar c = fromMaybe [c] (Map.lookup c msEscapes)
+escapeUri :: String -> String
+escapeUri = escapeURIString (\c -> c /= '@' && isAllowedInURI c)
-- | Escape | character, used to mark inline math, inside math.
escapeBar :: String -> String
@@ -157,13 +115,6 @@ escapeBar = concatMap go
where go '|' = "\\[u007C]"
go c = [c]
--- | Escape special characters for Ms.
-escapeString :: String -> String
-escapeString = concatMap escapeChar
-
-escapeUri :: String -> String
-escapeUri = escapeURIString (\c -> c /= '@' && isAllowedInURI c)
-
toSmallCaps :: String -> String
toSmallCaps [] = []
toSmallCaps (c:cs)
@@ -174,17 +125,6 @@ toSmallCaps (c:cs)
in escapeString uppers ++ toSmallCaps rest
| otherwise = escapeChar c ++ toSmallCaps cs
--- | Escape a literal (code) section for Ms.
-escapeCode :: String -> String
-escapeCode = intercalate "\n" . map escapeLine . lines
- where escapeCodeChar ' ' = "\\ "
- escapeCodeChar '\t' = "\\\t"
- escapeCodeChar c = escapeChar c
- escapeLine codeline =
- case concatMap escapeCodeChar codeline of
- a@('.':_) -> "\\&" ++ a
- b -> b
-
-- We split inline lists into sentences, and print one sentence per
-- line. groff/troff treats the line-ending period differently.
-- See http://code.google.com/p/pandoc/issues/detail?id=148.
@@ -535,28 +475,6 @@ handleNote opts bs = do
contents <- blockListToMs opts bs'
return $ cr <> text ".FS" $$ contents $$ text ".FE" <> cr
-fontChange :: PandocMonad m => MS m Doc
-fontChange = do
- features <- gets stFontFeatures
- inHeader <- gets stInHeader
- let filling = ['C' | fromMaybe False $ Map.lookup 'C' features] ++
- ['B' | inHeader ||
- fromMaybe False (Map.lookup 'B' features)] ++
- ['I' | fromMaybe False $ Map.lookup 'I' features]
- return $
- if null filling
- then text "\\f[R]"
- else text $ "\\f[" ++ filling ++ "]"
-
-withFontFeature :: PandocMonad m => Char -> MS m Doc -> MS m Doc
-withFontFeature c action = do
- modify $ \st -> st{ stFontFeatures = Map.adjust not c $ stFontFeatures st }
- begin <- fontChange
- d <- action
- modify $ \st -> st{ stFontFeatures = Map.adjust not c $ stFontFeatures st }
- end <- fontChange
- return $ begin <> d <> end
-
setFirstPara :: PandocMonad m => MS m ()
setFirstPara = modify $ \st -> st{ stFirstPara = True }