aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2018-10-28 17:19:52 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2018-10-28 17:23:09 -0700
commit53d89d7eab98186ea947d2380ccee52f85f5f7bd (patch)
treee88a1a4107d9c7d0df739c3274d631fd1c27c68c /src/Text/Pandoc/Readers
parent3cb8a3f397469edb2e29d17209195081d556035a (diff)
downloadpandoc-53d89d7eab98186ea947d2380ccee52f85f5f7bd.tar.gz
Roff tokenizer: abstract out patterns: escapeArg, quoteArg.
Diffstat (limited to 'src/Text/Pandoc/Readers')
-rw-r--r--src/Text/Pandoc/Readers/Roff.hs54
1 files changed, 27 insertions, 27 deletions
diff --git a/src/Text/Pandoc/Readers/Roff.hs b/src/Text/Pandoc/Readers/Roff.hs
index 58bafb07a..e1af58c9f 100644
--- a/src/Text/Pandoc/Readers/Roff.hs
+++ b/src/Text/Pandoc/Readers/Roff.hs
@@ -53,7 +53,8 @@ import Control.Monad (void, mzero, guard, when)
import Control.Monad.Except (throwError)
import Text.Pandoc.Class
(getResourcePath, readFileFromDirs, PandocMonad(..), report)
-import Data.Char (toLower, isHexDigit, chr, ord, isAscii, isAlphaNum, isSpace)
+import Data.Char (isLower, toLower, toUpper, isHexDigit, chr, ord,
+ isAscii, isAlphaNum, isSpace)
import Data.Default (Default)
import qualified Data.Map as M
import Data.List (intercalate, isSuffixOf)
@@ -267,35 +268,34 @@ escFontSize = do
toFontSize (s ++ ds)
]
+-- Parses: [..], (.., or . (single character).
+escapeArg :: PandocMonad m => RoffLexer m String
+escapeArg = choice
+ [ char '[' *> manyTill (noneOf ['\n',']']) (char ']')
+ , char ')' *> count 2 anyChar
+ , count 1 anyChar
+ ]
+
+-- Parses: '..'
+quoteArg :: PandocMonad m => RoffLexer m String
+quoteArg = char '\'' *> manyTill (noneOf ['\n','\'']) (char '\'')
+
escFont :: PandocMonad m => RoffLexer m [LinePart]
escFont = do
- font <- choice
- [ digit >> return defaultFontSpec
- , digit >> return defaultFontSpec
- , ($ defaultFontSpec) <$> letterFontKind
- , lettersFont
- ]
+ font <- escapeArg
+ font' <- if null font
+ then prevFont <$> getState
+ else return $ foldr processFontLetter defaultFontSpec font
modifyState $ \st -> st{ prevFont = currentFont st
- , currentFont = font }
- return [Font font]
-
-lettersFont :: PandocMonad m => RoffLexer m FontSpec
-lettersFont = try $ do
- fs <- (char '[' *> many letterFontKind <* char ']')
- <|> (char '(' *> count 2 letterFontKind)
- if null fs
- then prevFont <$> getState
- else return $ foldr ($) defaultFontSpec fs
-
-letterFontKind :: PandocMonad m => RoffLexer m (FontSpec -> FontSpec)
-letterFontKind = choice [
- oneOf ['B','b'] >> return (\fs -> fs{ fontBold = True })
- , oneOf ['I','i'] >> return (\fs -> fs { fontItalic = True })
- , oneOf ['C','c'] >> return (\fs -> fs { fontMonospace = True })
- , oneOf ['P','p','R','r'] >> return id
- , letter >> return id -- L, S, etc.
- ]
-
+ , currentFont = font' }
+ return [Font font']
+ where
+ processFontLetter c fs
+ | isLower c = processFontLetter (toUpper c) fs
+ processFontLetter 'B' fs = fs{ fontBold = True }
+ processFontLetter 'I' fs = fs{ fontItalic = True }
+ processFontLetter 'C' fs = fs{ fontMonospace = True }
+ processFontLetter _ fs = fs -- do nothing
-- separate function from lexMacro since real man files sometimes do not
-- follow the rules