@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121-}
2222
2323{- |
24− Module : Text.Pandoc.Readers.Groff
24+ Module : Text.Pandoc.Readers.Roff
2525 Copyright : Copyright (C) 2018 Yan Pashkovsky and John MacFarlane
2626 License : GNU GPL, version 2 or above
2727
@@ -31,7 +31,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3131
3232Tokenizer for groff formats (man, ms).
3333-}
34−module Text.Pandoc.Readers.Groff
34+module Text.Pandoc.Readers.Roff
3535 ( MacroKind
3636 , FontSpec(..)
3737 , defaultFontSpec
@@ -40,10 +40,10 @@ module Text.Pandoc.Readers.Groff
4040 , TableOption
4141 , CellFormat(..)
4242 , TableRow
43− , GroffToken(..)
44− , GroffTokens(..)
43+ , RoffToken(..)
44+ , RoffTokens(..)
4545 , linePartsToString
46− , lexGroff
46+ , lexRoff
4747 )
4848where
4949
@@ -64,7 +64,7 @@ import Text.Pandoc.Parsing
6464import Text.Pandoc.Shared (safeRead)
6565import Text.Parsec hiding (tokenPrim)
6666import qualified Text.Parsec as Parsec
67−import Text.Pandoc.GroffChar (characterCodes, combiningAccents)
67+import Text.Pandoc.RoffChar (characterCodes, combiningAccents)
6868import qualified Data.Sequence as Seq
6969import qualified Data.Foldable as Foldable
7070import qualified Data.Text.Normalize as Normalize
@@ -102,22 +102,22 @@ data CellFormat =
102102 , columnSuffixes :: [String]
103103 } deriving (Show, Eq, Ord)
104104
105−type TableRow = ([CellFormat], [GroffTokens])
105+type TableRow = ([CellFormat], [RoffTokens])
106106
107107-- TODO parse tables (see man tbl)
108−data GroffToken = MLine [LinePart]
108+data RoffToken = MLine [LinePart]
109109 | MEmptyLine
110110 | MMacro MacroKind [Arg] SourcePos
111111 | MTable [TableOption] [TableRow] SourcePos
112112 deriving Show
113113
114−newtype GroffTokens = GroffTokens { unGroffTokens :: Seq.Seq GroffToken }
114+newtype RoffTokens = RoffTokens { unRoffTokens :: Seq.Seq RoffToken }
115115 deriving (Show, Semigroup, Monoid)
116116
117−singleTok :: GroffToken -> GroffTokens
118−singleTok t = GroffTokens (Seq.singleton t)
117+singleTok :: RoffToken -> RoffTokens
118+singleTok t = RoffTokens (Seq.singleton t)
119119
120−data RoffState = RoffState { customMacros :: M.Map String GroffTokens
120+data RoffState = RoffState { customMacros :: M.Map String RoffTokens
121121 , prevFont :: FontSpec
122122 , currentFont :: FontSpec
123123 , tableTabChar :: Char
@@ -137,10 +137,10 @@ instance Default RoffState where
137137 , tableTabChar = '\t'
138138 }
139139
140−type GroffLexer m = ParserT [Char] RoffState m
140+type RoffLexer m = ParserT [Char] RoffState m
141141
142142--
143−-- Lexer: String -> GroffToken
143+-- Lexer: String -> RoffToken
144144--
145145
146146eofline :: Stream s m Char => ParsecT s u m ()
@@ -157,7 +157,7 @@ combiningAccentsMap :: M.Map String Char
157157combiningAccentsMap =
158158 M.fromList $ map (\(x,y) -> (y,x)) combiningAccents
159159
160−escape :: PandocMonad m => GroffLexer m [LinePart]
160+escape :: PandocMonad m => RoffLexer m [LinePart]
161161escape = do
162162 char '\\'
163163 c <- anyChar
@@ -239,14 +239,14 @@ escape = do
239239 Nothing -> mzero
240240 Just c -> return c
241241
242− escUnknown :: PandocMonad m => String -> GroffLexer m [LinePart]
242+ escUnknown :: PandocMonad m => String -> RoffLexer m [LinePart]
243243 escUnknown s = do
244244 pos <- getPosition
245245 report $ SkippedContent ("Unknown escape sequence " ++ s) pos
246246 return [RoffStr "\xFFFD"]
247247
248248-- \s-1 \s0
249−escFontSize :: PandocMonad m => GroffLexer m [LinePart]
249+escFontSize :: PandocMonad m => RoffLexer m [LinePart]
250250escFontSize = do
251251 let sign = option "" $ ("-" <$ char '-' <|> "" <$ char '+')
252252 let toFontSize xs =
@@ -268,7 +268,7 @@ escFontSize = do
268268 toFontSize (s ++ ds)
269269 ]
270270
271−escFont :: PandocMonad m => GroffLexer m [LinePart]
271+escFont :: PandocMonad m => RoffLexer m [LinePart]
272272escFont = do
273273 font <- choice
274274 [ char 'S' >> return defaultFontSpec
@@ -282,7 +282,7 @@ escFont = do
282282 , currentFont = font }
283283 return [Font font]
284284
285−lettersFont :: PandocMonad m => GroffLexer m FontSpec
285+lettersFont :: PandocMonad m => RoffLexer m FontSpec
286286lettersFont = try $ do
287287 char '['
288288 fs <- many letterFontKind
@@ -292,7 +292,7 @@ lettersFont = try $ do
292292 then prevFont <$> getState
293293 else return $ foldr ($) defaultFontSpec fs
294294
295−letterFontKind :: PandocMonad m => GroffLexer m (FontSpec -> FontSpec)
295+letterFontKind :: PandocMonad m => RoffLexer m (FontSpec -> FontSpec)
296296letterFontKind = choice [
297297 oneOf ['B','b'] >> return (\fs -> fs{ fontBold = True })
298298 , oneOf ['I','i'] >> return (\fs -> fs { fontItalic = True })
@@ -303,7 +303,7 @@ letterFontKind = choice [
303303
304304-- separate function from lexMacro since real man files sometimes do not
305305-- follow the rules
306−lexComment :: PandocMonad m => GroffLexer m GroffTokens
306+lexComment :: PandocMonad m => RoffLexer m RoffTokens
307307lexComment = do
308308 try $ string ".\\\""
309309 many Parsec.space
@@ -311,7 +311,7 @@ lexComment = do
311311 char '\n'
312312 return mempty
313313
314−lexMacro :: PandocMonad m => GroffLexer m GroffTokens
314+lexMacro :: PandocMonad m => RoffLexer m RoffTokens
315315lexMacro = do
316316 pos <- getPosition
317317 char '.' <|> char '\''
@@ -338,7 +338,7 @@ lexMacro = do
338338 "so" -> lexIncludeFile args
339339 _ -> resolveMacro macroName args pos
340340
341−lexTable :: PandocMonad m => SourcePos -> GroffLexer m GroffTokens
341+lexTable :: PandocMonad m => SourcePos -> RoffLexer m RoffTokens
342342lexTable pos = do
343343 skipMany lexComment
344344 spaces
@@ -360,7 +360,7 @@ lexTable pos = do
360360 eofline
361361 return $ singleTok $ MTable opts (rows ++ concat morerows) pos
362362
363−lexTableRows :: PandocMonad m => GroffLexer m [TableRow]
363+lexTableRows :: PandocMonad m => RoffLexer m [TableRow]
364364lexTableRows = do
365365 aligns <- tableFormatSpec
366366 spaces
@@ -370,8 +370,8 @@ lexTableRows = do
370370 tableRow)
371371 return $ zip aligns rows
372372
373−tableCell :: PandocMonad m => GroffLexer m GroffTokens
374−tableCell = (enclosedCell <|> simpleCell) >>= lexGroff . T.pack
373+tableCell :: PandocMonad m => RoffLexer m RoffTokens
374+tableCell = (enclosedCell <|> simpleCell) >>= lexRoff . T.pack
375375 where
376376 enclosedCell = do
377377 try (string "T{")
@@ -380,7 +380,7 @@ tableCell = (enclosedCell <|> simpleCell) >>= lexGroff . T.pack
380380 tabChar <- tableTabChar <$> getState
381381 many (notFollowedBy (char tabChar <|> newline) >> anyChar)
382382
383−tableRow :: PandocMonad m => GroffLexer m [GroffTokens]
383+tableRow :: PandocMonad m => RoffLexer m [RoffTokens]
384384tableRow = do
385385 tabChar <- tableTabChar <$> getState
386386 c <- tableCell
@@ -390,10 +390,10 @@ tableRow = do
390390 skipMany lexComment
391391 return (c:cs)
392392
393−tableOptions :: PandocMonad m => GroffLexer m [TableOption]
393+tableOptions :: PandocMonad m => RoffLexer m [TableOption]
394394tableOptions = try $ many1 tableOption <* spaces <* char ';'
395395
396−tableOption :: PandocMonad m => GroffLexer m TableOption
396+tableOption :: PandocMonad m => RoffLexer m TableOption
397397tableOption = do
398398 k <- many1 letter
399399 v <- option "" $ do
@@ -404,18 +404,18 @@ tableOption = do
404404 skipMany spacetab
405405 return (k,v)
406406
407−tableFormatSpec :: PandocMonad m => GroffLexer m [[CellFormat]]
407+tableFormatSpec :: PandocMonad m => RoffLexer m [[CellFormat]]
408408tableFormatSpec = do
409409 speclines <- tableFormatSpecLine `sepBy1` (newline <|> char ',')
410410 skipMany spacetab
411411 char '.'
412412 return $ speclines ++ repeat (lastDef [] speclines) -- last line is default
413413
414−tableFormatSpecLine :: PandocMonad m => GroffLexer m [CellFormat]
414+tableFormatSpecLine :: PandocMonad m => RoffLexer m [CellFormat]
415415tableFormatSpecLine =
416416 many1 $ try $ skipMany spacetab >> tableColFormat
417417
418−tableColFormat :: PandocMonad m => GroffLexer m CellFormat
418+tableColFormat :: PandocMonad m => RoffLexer m CellFormat
419419tableColFormat = do
420420 pipePrefix' <- option False
421421 $ True <$ (try $ string "|" <* notFollowedBy spacetab)
@@ -442,19 +442,19 @@ tableColFormat = do
442442-- We don't fully handle the conditional. But we do
443443-- include everything under '.ie n', which occurs commonly
444444-- in man pages. We always skip the '.el' part.
445−lexConditional :: PandocMonad m => GroffLexer m GroffTokens
445+lexConditional :: PandocMonad m => RoffLexer m RoffTokens
446446lexConditional = do
447447 skipMany spacetab
448448 lexNCond <|> skipConditional
449449
450450-- n means nroff mode
451−lexNCond :: PandocMonad m => GroffLexer m GroffTokens
451+lexNCond :: PandocMonad m => RoffLexer m RoffTokens
452452lexNCond = do
453453 char '\n'
454454 many1 spacetab
455455 lexGroup <|> manToken
456456
457−lexGroup :: PandocMonad m => GroffLexer m GroffTokens
457+lexGroup :: PandocMonad m => RoffLexer m RoffTokens
458458lexGroup = do
459459 groupstart
460460 mconcat <$> manyTill manToken groupend
@@ -462,14 +462,14 @@ lexGroup = do
462462 groupstart = try $ string "\\{\\" >> newline
463463 groupend = try $ string "\\}" >> eofline
464464
465−skipConditional :: PandocMonad m => GroffLexer m GroffTokens
465+skipConditional :: PandocMonad m => RoffLexer m RoffTokens
466466skipConditional = do
467467 rest <- anyLine
468468 when ("\\{\\" `isSuffixOf` rest) $
469469 void $ manyTill anyChar (try (string "\\}"))
470470 return mempty
471471
472−lexIncludeFile :: PandocMonad m => [Arg] -> GroffLexer m GroffTokens
472+lexIncludeFile :: PandocMonad m => [Arg] -> RoffLexer m RoffTokens
473473lexIncludeFile args = do
474474 pos <- getPosition
475475 case args of
@@ -484,7 +484,7 @@ lexIncludeFile args = do
484484 [] -> return mempty
485485
486486resolveMacro :: PandocMonad m
487− => String -> [Arg] -> SourcePos -> GroffLexer m GroffTokens
487+ => String -> [Arg] -> SourcePos -> RoffLexer m RoffTokens
488488resolveMacro macroName args pos = do
489489 macros <- customMacros <$> getState
490490 case M.lookup macroName macros of
@@ -498,9 +498,9 @@ resolveMacro macroName args pos = do
498498 let fillMacroArg (MLine lineparts) =
499499 MLine (foldr fillLP [] lineparts)
500500 fillMacroArg x = x
501− return $ GroffTokens . fmap fillMacroArg . unGroffTokens $ ts
501+ return $ RoffTokens . fmap fillMacroArg . unRoffTokens $ ts
502502
503−lexStringDef :: PandocMonad m => [Arg] -> GroffLexer m GroffTokens
503+lexStringDef :: PandocMonad m => [Arg] -> RoffLexer m RoffTokens
504504lexStringDef args = do -- string definition
505505 case args of
506506 [] -> fail "No argument to .ds"
@@ -511,7 +511,7 @@ lexStringDef args = do -- string definition
511511 st{ customMacros = M.insert stringName ts (customMacros st) }
512512 return mempty
513513
514−lexMacroDef :: PandocMonad m => [Arg] -> GroffLexer m GroffTokens
514+lexMacroDef :: PandocMonad m => [Arg] -> RoffLexer m RoffTokens
515515lexMacroDef args = do -- macro definition
516516 (macroName, stopMacro) <-
517517 case args of
@@ -530,7 +530,7 @@ lexMacroDef args = do -- macro definition
530530 st{ customMacros = M.insert macroName ts (customMacros st) }
531531 return mempty
532532
533−lexArgs :: PandocMonad m => GroffLexer m [Arg]
533+lexArgs :: PandocMonad m => RoffLexer m [Arg]
534534lexArgs = do
535535 args <- many $ try oneArg
536536 skipMany spacetab
@@ -539,20 +539,20 @@ lexArgs = do
539539
540540 where
541541
542− oneArg :: PandocMonad m => GroffLexer m [LinePart]
542+ oneArg :: PandocMonad m => RoffLexer m [LinePart]
543543 oneArg = do
544544 skipMany $ try $ string "\\\n" -- continuation line
545545 try quotedArg <|> plainArg
546546 -- try, because there are some erroneous files, e.g. linux/bpf.2
547547
548− plainArg :: PandocMonad m => GroffLexer m [LinePart]
548+ plainArg :: PandocMonad m => RoffLexer m [LinePart]
549549 plainArg = do
550550 skipMany spacetab
551551 mconcat <$> many1 (macroArg <|> escape <|> regularText <|> unescapedQuote)
552552 where
553553 unescapedQuote = char '"' >> return [RoffStr "\""]
554554
555− quotedArg :: PandocMonad m => GroffLexer m [LinePart]
555+ quotedArg :: PandocMonad m => RoffLexer m [LinePart]
556556 quotedArg = do
557557 skipMany spacetab
558558 char '"'
@@ -567,7 +567,7 @@ lexArgs = do
567567 char '"'
568568 return [RoffStr "\""]
569569
570−escStar :: PandocMonad m => GroffLexer m [LinePart]
570+escStar :: PandocMonad m => RoffLexer m [LinePart]
571571escStar = try $ do
572572 pos <- getPosition
573573 c <- anyChar
@@ -586,14 +586,14 @@ escStar = try $ do
586586
587587 -- strings and macros share namespace
588588 resolveString stringname pos = do
589− GroffTokens ts <- resolveMacro stringname [] pos
589+ RoffTokens ts <- resolveMacro stringname [] pos
590590 case Foldable.toList ts of
591591 [MLine xs] -> return xs
592592 _ -> do
593593 report $ SkippedContent ("unknown string " ++ stringname) pos
594594 return mempty
595595
596−lexLine :: PandocMonad m => GroffLexer m GroffTokens
596+lexLine :: PandocMonad m => RoffLexer m RoffTokens
597597lexLine = do
598598 lnparts <- mconcat <$> many1 linePart
599599 eofline
@@ -604,35 +604,35 @@ lexLine = do
604604 go (RoffStr "" : xs) = go xs
605605 go xs = return $ singleTok $ MLine xs
606606
607−linePart :: PandocMonad m => GroffLexer m [LinePart]
607+linePart :: PandocMonad m => RoffLexer m [LinePart]
608608linePart = macroArg <|> escape <|>
609609 regularText <|> quoteChar <|> spaceTabChar
610610
611−macroArg :: PandocMonad m => GroffLexer m [LinePart]
611+macroArg :: PandocMonad m => RoffLexer m [LinePart]
612612macroArg = try $ do
613613 string "\\\\$"
614614 x <- digit
615615 return [MacroArg $ ord x - ord '0']
616616
617−regularText :: PandocMonad m => GroffLexer m [LinePart]
617+regularText :: PandocMonad m => RoffLexer m [LinePart]
618618regularText = do
619619 s <- many1 $ noneOf "\n\r\t \\\""
620620 return [RoffStr s]
621621
622−quoteChar :: PandocMonad m => GroffLexer m [LinePart]
622+quoteChar :: PandocMonad m => RoffLexer m [LinePart]
623623quoteChar = do
624624 char '"'
625625 return [RoffStr "\""]
626626
627−spaceTabChar :: PandocMonad m => GroffLexer m [LinePart]
627+spaceTabChar :: PandocMonad m => RoffLexer m [LinePart]
628628spaceTabChar = do
629629 c <- spacetab
630630 return [RoffStr [c]]
631631
632−lexEmptyLine :: PandocMonad m => GroffLexer m GroffTokens
632+lexEmptyLine :: PandocMonad m => RoffLexer m RoffTokens
633633lexEmptyLine = char '\n' >> return (singleTok MEmptyLine)
634634
635−manToken :: PandocMonad m => GroffLexer m GroffTokens
635+manToken :: PandocMonad m => RoffLexer m RoffTokens
636636manToken = lexComment <|> lexMacro <|> lexLine <|> lexEmptyLine
637637
638638linePartsToString :: [LinePart] -> String
@@ -642,8 +642,8 @@ linePartsToString = mconcat . map go
642642 go _ = mempty
643643
644644-- | Tokenize a string as a sequence of groff tokens.
645−lexGroff :: PandocMonad m => T.Text -> m GroffTokens
646−lexGroff txt = do
645+lexRoff :: PandocMonad m => T.Text -> m RoffTokens
646+lexRoff txt = do
647647 eithertokens <- readWithM (mconcat <$> many manToken) def (T.unpack txt)
648648 case eithertokens of
649649 Left e -> throwError e