git.iko.soy/typeable/pandoc
on master help sign in
Man reader: Fix .B, .I, .BR, etc.
f202279John MacFarlane
src/Text/Pandoc/Readers/Man.hs+36−18
@@ -41,7 +41,7 @@ import Data.Default (Default)
41import Data.Maybe (catMaybes)
41import Data.Maybe (catMaybes)
42import qualified Data.Map as M
42import qualified Data.Map as M
43import Data.Set (Set, singleton)
43import Data.Set (Set, singleton)
44−import qualified Data.Set as S (fromList, toList, union)
44+import qualified Data.Set as S (fromList, toList, insert)
45import Data.List (intersperse, intercalate)
45import Data.List (intersperse, intercalate)
46import qualified Data.Text as T
46import qualified Data.Text as T
47import Text.Pandoc.Class (PandocMonad(..), report)
47import Text.Pandoc.Class (PandocMonad(..), report)
@@ -56,6 +56,8 @@ import qualified Text.Parsec as Parsec
56import Text.Parsec.Pos (updatePosString)
56import Text.Parsec.Pos (updatePosString)
57import Text.Pandoc.GroffChar (characterCodes, combiningAccents)
57import Text.Pandoc.GroffChar (characterCodes, combiningAccents)
58
58
59+import Debug.Trace (traceShowId)
60+
59--
61--
60-- Data Types
62-- Data Types
61--
63--
@@ -280,25 +282,41 @@ lexMacro = do
280 many spacetab
282 many spacetab
281 macroName <- many (letter <|> oneOf ['\\', '"', '&', '.'])
283 macroName <- many (letter <|> oneOf ['\\', '"', '&', '.'])
282 args <- lexArgs
284 args <- lexArgs
283− let addFonts fs = map (addFontsToRoffStr fs)
285+ let addFont f = map (addFontToRoffStr f)
284− addFontsToRoffStr fs (RoffStr (s, fs')) = RoffStr (s, fs `S.union` fs')
286+ addFontToRoffStr f (RoffStr (s, fs)) = RoffStr (s, S.insert f fs)
285− addFontsToRoffStr _ x = x
287+ addFontToRoffStr _ x = x
286−
288+
287− tok = case macroName of
289+ case macroName of
288− "" -> MComment
290+ "" -> return MComment
291+ "\\\"" -> return MComment
289− x | x `elem` ["\\\"", "\\#"] -> MComment
292+ "\\#" -> return MComment
293+ "B" -> do
294+ args' <- argsOrFromNextLine args
290− "B" -> MLine $ concatMap (addFonts (singleton Bold)) args
295+ return $ MLine $ concatMap (addFont Bold) args'
291− "BR" -> MLine $ concat args -- TODO
296+ "I" -> do
297+ args' <- argsOrFromNextLine args
298+ return $ MLine $ concatMap (addFont Italic) args'
292− x | x `elem` ["BI", "IB"] -> MLine $ -- TODO FIXME!
299+ x | x `elem` ["BI", "IB", "RI", "IR", "BR", "RB"] -> do
293− concatMap (addFonts (S.fromList [Italic, Bold])) args
300+ let toFont 'I' = Italic
294− x | x `elem` ["I", "IR", "RI"] -> MLine $
301+ toFont 'R' = Regular
295− concatMap (addFonts (singleton Italic)) args
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
296− x | x `elem` [ "P", "PP", "LP", "sp"] -> MEmptyLine
307+ x | x `elem` [ "P", "PP", "LP", "sp"] -> return MEmptyLine
297− _ -> MMacro macroName args
308+ _ -> return $ MMacro macroName args
298− return tok
299
309
300 where
310 where
301
311
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+
302 lexArgs :: PandocMonad m => ManLexer m [[LinePart]]
320 lexArgs :: PandocMonad m => ManLexer m [[LinePart]]
303 lexArgs = do
321 lexArgs = do
304 args <- many $ try oneArg
322 args <- many $ try oneArg
@@ -439,7 +457,7 @@ parseSkippedContent :: PandocMonad m => ManParser m Blocks
439parseSkippedContent = mempty <$ (mcomment <|> memptyLine)
457parseSkippedContent = mempty <$ (mcomment <|> memptyLine)
440
458
441linePartsToInlines :: [LinePart] -> Inlines
459linePartsToInlines :: [LinePart] -> Inlines
442−linePartsToInlines = mconcat . map go
460+linePartsToInlines = mconcat . traceShowId . map go . traceShowId
443 where
461 where
444 go (RoffStr (s, fonts)) = inner (S.toList fonts) s
462 go (RoffStr (s, fonts)) = inner (S.toList fonts) s
445 go _ = mempty
463 go _ = mempty
test/Tests/Readers/Man.hs+4−1
@@ -40,12 +40,15 @@ tests = [
40 , "Macro args" =:
40 , "Macro args" =:
41 ".B \"single arg with \"\"Q\"\"\""
41 ".B \"single arg with \"\"Q\"\"\""
42 =?> (para $ strong $ text "single arg with \"Q\"")
42 =?> (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\"")
43 , "comment" =:
46 , "comment" =:
44 ".\\\"bla\naaa"
47 ".\\\"bla\naaa"
45 =?> (para $ str "aaa")
48 =?> (para $ str "aaa")
46 , "link" =:
49 , "link" =:
47 ".BR aa (1)"
50 ".BR aa (1)"
48− =?> para (text "aa(1)")
51+ =?> para (strong (str "aa") <> str "(1)")
49 ],
52 ],
50 testGroup "Escapes" [
53 testGroup "Escapes" [
51 "fonts" =:
54 "fonts" =: