From f48960b75fe2d7ae532ec17a060109c92b893f57 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 17 Oct 2018 17:23:14 -0700 Subject: 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. --- src/Text/Pandoc/Writers/Ms.hs | 88 ++----------------------------------------- 1 file changed, 3 insertions(+), 85 deletions(-) (limited to 'src/Text/Pandoc/Writers/Ms.hs') 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 } -- cgit v1.2.3