custom ordered lists

This commit is contained in:
Yan Pas 2018-05-27 14:09:34 +03:00
parent 9030c5ae46
commit c2ae72aa6c

View file

@ -39,7 +39,7 @@ import Control.Monad.Except (throwError)
import Data.Char (isDigit, isUpper, isLower) import Data.Char (isDigit, isUpper, isLower)
import Data.Default (Default) import Data.Default (Default)
import Data.Map (insert) import Data.Map (insert)
import Data.Maybe (catMaybes) import Data.Maybe (catMaybes, fromMaybe, isNothing)
import Data.List (intersperse, intercalate) import Data.List (intersperse, intercalate)
import qualified Data.Text as T import qualified Data.Text as T
@ -153,7 +153,7 @@ lexMan = many (lexMacro <|> lexLine <|> lexEmptyLine)
parseMan :: PandocMonad m => ManParser m Pandoc parseMan :: PandocMonad m => ManParser m Pandoc
parseMan = do parseMan = do
let parsers = [ try parseBulletList, parseTitle, parsePara, parseSkippedContent let parsers = [ try parseList, parseTitle, parsePara, parseSkippedContent
, parseCodeBlock, parseHeader, parseSkipMacro] , parseCodeBlock, parseHeader, parseSkipMacro]
blocks <- many $ choice parsers blocks <- many $ choice parsers
parserst <- getState parserst <- getState
@ -210,7 +210,7 @@ escapeLexer = do
letterFont = choice [ letterFont = choice [
char 'B' >> return Bold char 'B' >> return Bold
, char 'I' >> return Italic , char 'I' >> return Italic
, char 'P' >> return Regular , (char 'P' <|> char 'R') >> return Regular
] ]
currentFont :: PandocMonad m => ManLexer m FontKind currentFont :: PandocMonad m => ManLexer m FontKind
@ -248,6 +248,7 @@ lexMacro = do
where where
-- TODO rework args
lexArgs :: PandocMonad m => ManLexer m [RoffStr] lexArgs :: PandocMonad m => ManLexer m [RoffStr]
lexArgs = do lexArgs = do
args <- many oneArg args <- many oneArg
@ -478,38 +479,47 @@ parseHeader = do
(MHeader lvl ss) <- mheader (MHeader lvl ss) <- mheader
return $ Header lvl nullAttr $ intersperse Space $ strToInline <$> ss return $ Header lvl nullAttr $ intersperse Space $ strToInline <$> ss
parseBulletList :: PandocMonad m => ManParser m Block type ListBuilder = [[Block]] -> Block
parseBulletList = BulletList <$> many1 paras where
parseList :: PandocMonad m => ManParser m Block
parseList = do
xx <- many1 paras
let bls = map snd xx
let bldr = fst $ head xx
return $ bldr bls
where
macroIPInl :: [RoffStr] -> [Inline] macroIPInl :: [RoffStr] -> [Inline]
macroIPInl (x:_:[]) = [strToInline x, Space] macroIPInl (x:_:[]) = [strToInline x, Space]
macroIPInl _ = [] macroIPInl _ = []
listKind :: [RoffStr] -> Maybe ([[Block]] -> Block) listKind :: [RoffStr] -> Maybe ListBuilder
listKind (((c:_), _):_:[]) = listKind (((c:_), _):_:[]) =
let params style = OrderedList (1, style, DefaultDelim) let params style = OrderedList (1, style, DefaultDelim)
in Just $ case c of in case c of
_ | isDigit c -> params Decimal _ | isDigit c -> Just $ params Decimal
_ | isUpper c -> params UpperAlpha _ | isUpper c -> Just $ params UpperAlpha
_ | isLower c -> params LowerAlpha _ | isLower c -> Just $ params LowerAlpha
_ -> BulletList _ -> Nothing
listKind _ = Nothing listKind _ = Nothing
paras :: PandocMonad m => ManParser m [Block] paras :: PandocMonad m => ManParser m (ListBuilder, [Block])
paras = do paras = do
(MMacro _ args) <- mmacro KTab (MMacro _ args) <- mmacro KTab
let lk = listKind args let lbuilderOpt = listKind args
let lbuilder = fromMaybe BulletList lbuilderOpt
inls <- parseInlines inls <- parseInlines
let macroinl = macroIPInl args let macroinl = macroIPInl args
let para = Plain $ macroinl ++ inls let parainls = if isNothing lbuilderOpt then macroinl ++ inls else inls
subls <- many sublist subls <- many sublist
return $ para : subls return $ (lbuilder, (Plain parainls) : subls)
sublist :: PandocMonad m => ManParser m Block sublist :: PandocMonad m => ManParser m Block
sublist = do sublist = do
mmacro KSubTab mmacro KSubTab
bl <- parseBulletList bl <- parseList
mmacro KTabEnd mmacro KTabEnd
return bl return bl