git.iko.soy/typeable/pandoc
on master help sign in
Man reader: Fix .B, .I, .BR, etc.
commitf202279902da34dfa4f22e4e53cb0bf93d519d1e authorJohn MacFarlane <jgm@berkeley.edu> dateSat 20 Oct 2018, 16:40 (8 years ago) parenta9fc711
2 files changed+40−19
src/Text/Pandoc/Readers/Man.hs+36−18 test/Tests/Readers/Man.hs+4−1
src/Text/Pandoc/Readers/Man.hs+36−18
@@ -41,7 +41,7 @@ import Data.Default (Default)
4141import Data.Maybe (catMaybes)
4242import qualified Data.Map as M
4343import Data.Set (Set, singleton)
44−import qualified Data.Set as S (fromList, toList, union)
44+import qualified Data.Set as S (fromList, toList, insert)
4545import Data.List (intersperse, intercalate)
4646import qualified Data.Text as T
4747import Text.Pandoc.Class (PandocMonad(..), report)
@@ -56,6 +56,8 @@ import qualified Text.Parsec as Parsec
5656import Text.Parsec.Pos (updatePosString)
5757import Text.Pandoc.GroffChar (characterCodes, combiningAccents)
5858
59+import Debug.Trace (traceShowId)
60+
5961--
6062-- Data Types
6163--
@@ -280,25 +282,41 @@ lexMacro = do
280282 many spacetab
281283 macroName <- many (letter <|> oneOf ['\\', '"', '&', '.'])
282284 args <- lexArgs
283− let addFonts fs = map (addFontsToRoffStr fs)
284− addFontsToRoffStr fs (RoffStr (s, fs')) = RoffStr (s, fs `S.union` fs')
285− addFontsToRoffStr _ x = x
286−
287− tok = case macroName of
288− "" -> MComment
289− x | x `elem` ["\\\"", "\\#"] -> MComment
290− "B" -> MLine $ concatMap (addFonts (singleton Bold)) args
291− "BR" -> MLine $ concat args -- TODO
292− x | x `elem` ["BI", "IB"] -> MLine $ -- TODO FIXME!
293− concatMap (addFonts (S.fromList [Italic, Bold])) args
294− x | x `elem` ["I", "IR", "RI"] -> MLine $
295− concatMap (addFonts (singleton Italic)) args
296− x | x `elem` [ "P", "PP", "LP", "sp"] -> MEmptyLine
297− _ -> MMacro macroName args
298− return tok
285+ let addFont f = map (addFontToRoffStr f)
286+ addFontToRoffStr f (RoffStr (s, fs)) = RoffStr (s, S.insert f fs)
287+ addFontToRoffStr _ x = x
288+
289+ case macroName of
290+ "" -> return MComment
291+ "\\\"" -> return MComment
292+ "\\#" -> return MComment
293+ "B" -> do
294+ args' <- argsOrFromNextLine args
295+ return $ MLine $ concatMap (addFont Bold) args'
296+ "I" -> do
297+ args' <- argsOrFromNextLine args
298+ return $ MLine $ concatMap (addFont Italic) args'
299+ x | x `elem` ["BI", "IB", "RI", "IR", "BR", "RB"] -> do
300+ let toFont 'I' = Italic
301+ toFont 'R' = Regular
302+ toFont 'B' = Bold
303+ toFont 'M' = Monospace
304+ toFont _ = Regular
305+ let fontlist = map toFont x
306+ return $ MLine $ concat $ zipWith addFont (cycle fontlist) args
307+ x | x `elem` [ "P", "PP", "LP", "sp"] -> return MEmptyLine
308+ _ -> return $ MMacro macroName args
299309
300310 where
301311
312+ argsOrFromNextLine :: PandocMonad m => [[LinePart]] -> ManLexer m [[LinePart]]
313+ argsOrFromNextLine args =
314+ if null args
315+ then do
316+ MLine lps <- lexLine
317+ return [lps]
318+ else return args
319+
302320 lexArgs :: PandocMonad m => ManLexer m [[LinePart]]
303321 lexArgs = do
304322 args <- many $ try oneArg
@@ -439,7 +457,7 @@ parseSkippedContent :: PandocMonad m => ManParser m Blocks
439457parseSkippedContent = mempty <$ (mcomment <|> memptyLine)
440458
441459linePartsToInlines :: [LinePart] -> Inlines
442−linePartsToInlines = mconcat . map go
460+linePartsToInlines = mconcat . traceShowId . map go . traceShowId
443461 where
444462 go (RoffStr (s, fonts)) = inner (S.toList fonts) s
445463 go _ = mempty
test/Tests/Readers/Man.hs+4−1
@@ -40,12 +40,15 @@ tests = [
4040 , "Macro args" =:
4141 ".B \"single arg with \"\"Q\"\"\""
4242 =?> (para $ strong $ text "single arg with \"Q\"")
43+ , "Argument from next line" =:
44+ ".B\nsingle arg with \"Q\""
45+ =?> (para $ strong $ text "single arg with \"Q\"")
4346 , "comment" =:
4447 ".\\\"bla\naaa"
4548 =?> (para $ str "aaa")
4649 , "link" =:
4750 ".BR aa (1)"
48− =?> para (text "aa(1)")
51+ =?> para (strong (str "aa") <> str "(1)")
4952 ],
5053 testGroup "Escapes" [
5154 "fonts" =: