git.iko.soy/typeable/pandoc
on master help sign in
T.P.R.LaTeX.Parsing: moved more functions.
8f5cd94John MacFarlane
src/Text/Pandoc/Readers/LaTeX/Parsing.hs+64−57
@@ -398,63 +398,70 @@ doMacros n = do
398 Tok spos (CtrlSeq name) _ : ts
398 Tok spos (CtrlSeq name) _ : ts
399 -> handleMacros spos name ts
399 -> handleMacros spos name ts
400 _ -> return ()
400 _ -> return ()
401+
402+ where
401− where combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
403+ combineTok (Tok spos (CtrlSeq name) x) (Tok _ Word w : ts)
402− | T.all isLetterOrAt w =
404+ | T.all isLetterOrAt w =
403− Tok spos (CtrlSeq (name <> w)) (x1 <> w <> x2) : ts
405+ Tok spos (CtrlSeq (name <> w)) (x1 <> w <> x2) : ts
404− where (x1, x2) = T.break isSpaceOrTab x
406+ where (x1, x2) = T.break isSpaceOrTab x
405− combineTok t ts = t:ts
407+ combineTok t ts = t:ts
408+
406− matchTok (Tok _ toktype txt) =
409+ matchTok (Tok _ toktype txt) =
407− satisfyTok (\(Tok _ toktype' txt') ->
410+ satisfyTok (\(Tok _ toktype' txt') ->
408− toktype == toktype' &&
411+ toktype == toktype' &&
409− txt == txt')
412+ txt == txt')
413+
410− matchPattern toks = try $ mapM_ matchTok toks
414+ matchPattern toks = try $ mapM_ matchTok toks
415+
411− getargs argmap [] = return argmap
416+ getargs argmap [] = return argmap
412− getargs argmap (Pattern toks : rest) = try $ do
417+ getargs argmap (Pattern toks : rest) = try $ do
413− matchPattern toks
418+ matchPattern toks
414− getargs argmap rest
419+ getargs argmap rest
415− getargs argmap (ArgNum i : Pattern toks : rest) =
420+ getargs argmap (ArgNum i : Pattern toks : rest) =
416− try $ do
421+ try $ do
417− x <- mconcat <$> manyTill
422+ x <- mconcat <$> manyTill
418− (braced <|> ((:[]) <$> anyTok))
423+ (braced <|> ((:[]) <$> anyTok))
419− (matchPattern toks)
424+ (matchPattern toks)
420− getargs (M.insert i x argmap) rest
425+ getargs (M.insert i x argmap) rest
421− getargs argmap (ArgNum i : rest) = do
426+ getargs argmap (ArgNum i : rest) = do
422− x <- try $ spaces >> bracedOrToken
427+ x <- try $ spaces >> bracedOrToken
423− getargs (M.insert i x argmap) rest
428+ getargs (M.insert i x argmap) rest
429+
430+ addTok False args spos (Tok _ (Arg i) _) acc =
431+ case M.lookup i args of
432+ Nothing -> mzero
433+ Just xs -> foldr (addTok True args spos) acc xs
434+ -- see #4007
435+ addTok _ _ spos (Tok _ (CtrlSeq x) txt)
436+ acc@(Tok _ Word _ : _)
437+ | not (T.null txt)
438+ , isLetter (T.last txt) =
439+ Tok spos (CtrlSeq x) (txt <> " ") : acc
440+ addTok _ _ spos t acc = setpos spos t : acc
441+
424− handleMacros spos name ts = do
442+ handleMacros spos name ts = do
425− macros <- sMacros <$> getState
443+ macros <- sMacros <$> getState
426− case M.lookup name macros of
444+ case M.lookup name macros of
427− Nothing -> return ()
445+ Nothing -> return ()
428− Just (Macro expansionPoint argspecs optarg newtoks) -> do
446+ Just (Macro expansionPoint argspecs optarg newtoks) -> do
429− setInput ts
447+ setInput ts
430− args <- case optarg of
448+ args <- case optarg of
431− Nothing -> getargs M.empty argspecs
449+ Nothing -> getargs M.empty argspecs
432− Just o -> do
450+ Just o -> do
433− x <- option o bracketedToks
451+ x <- option o bracketedToks
434− getargs (M.singleton 1 x) argspecs
452+ getargs (M.singleton 1 x) argspecs
435− -- first boolean param is true if we're tokenizing
453+ -- first boolean param is true if we're tokenizing
436− -- an argument (in which case we don't want to
454+ -- an argument (in which case we don't want to
437− -- expand #1 etc.)
455+ -- expand #1 etc.)
438− let addTok False (Tok _ (Arg i) _) acc =
439− case M.lookup i args of
440− Nothing -> mzero
441− Just xs -> foldr (addTok True) acc xs
442− -- see #4007
443− addTok _ (Tok _ (CtrlSeq x) txt)
444− acc@(Tok _ Word _ : _)
445− | not (T.null txt)
446− , isLetter (T.last txt) =
447− Tok spos (CtrlSeq x) (txt <> " ") : acc
448− addTok _ t acc = setpos spos t : acc
449− ts' <- getInput
456+ ts' <- getInput
450− setInput $ foldr (addTok False) ts' newtoks
457+ setInput $ foldr (addTok False args spos) ts' newtoks
451−
458+
452− case expansionPoint of
459+ case expansionPoint of
453− ExpandWhenUsed ->
460+ ExpandWhenUsed ->
454− if n > 20 -- detect macro expansion loops
461+ if n > 20 -- detect macro expansion loops
455− then throwError $ PandocMacroLoop (T.unpack name)
462+ then throwError $ PandocMacroLoop (T.unpack name)
456− else doMacros (n + 1)
463+ else doMacros (n + 1)
457− ExpandWhenDefined -> return ()
464+ ExpandWhenDefined -> return ()
458
465
459
466
460setpos :: SourcePos -> Tok -> Tok
467setpos :: SourcePos -> Tok -> Tok