git.iko.soy/typeable/pandoc
on master help sign in
Split out T.P.Readers.LaTeX.Inline.
commit2cbe949d8d5f24bf895b2ba4c857d26e03065d8c authorJohn MacFarlane <jgm@berkeley.edu> dateWed 3 Mar 2021, 10:05 (6 years ago) parent6fbdb48
3 files changed+419−341
pandoc.cabal+6−5 src/Text/Pandoc/Readers/LaTeX.hs+138−336 src/Text/Pandoc/Readers/LaTeX/Inline.hs+275
pandoc.cabal+6−5
@@ -626,15 +626,16 @@ library
626626 Text.Pandoc.Readers.HTML.Table,
627627 Text.Pandoc.Readers.HTML.TagCategories,
628628 Text.Pandoc.Readers.HTML.Types,
629− Text.Pandoc.Readers.LaTeX.Types,
630− Text.Pandoc.Readers.LaTeX.Parsing,
631− Text.Pandoc.Readers.LaTeX.Lang,
632− Text.Pandoc.Readers.LaTeX.SIunitx,
633629 Text.Pandoc.Readers.LaTeX.Accent,
630+ Text.Pandoc.Readers.LaTeX.Inline,
634631 Text.Pandoc.Readers.LaTeX.Citation,
632+ Text.Pandoc.Readers.LaTeX.Lang,
633+ Text.Pandoc.Readers.LaTeX.Macro,
635634 Text.Pandoc.Readers.LaTeX.Math,
635+ Text.Pandoc.Readers.LaTeX.Parsing,
636+ Text.Pandoc.Readers.LaTeX.SIunitx,
636637 Text.Pandoc.Readers.LaTeX.Table,
637− Text.Pandoc.Readers.LaTeX.Macro,
638+ Text.Pandoc.Readers.LaTeX.Types,
638639 Text.Pandoc.Readers.Odt.Base,
639640 Text.Pandoc.Readers.Odt.Namespaces,
640641 Text.Pandoc.Readers.Odt.StyleReader,
src/Text/Pandoc/Readers/LaTeX.hs+138−336
@@ -35,13 +35,13 @@ import Data.Text (Text)
3535import qualified Data.Text as T
3636import System.FilePath (addExtension, replaceExtension, takeExtension)
3737import Text.Pandoc.BCP47 (Lang (..), renderLang)
38−import Text.Pandoc.Builder
38+import Text.Pandoc.Builder as B
3939import Text.Pandoc.Class.PandocPure (PandocPure)
4040import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
41− readFileFromDirs, report, setResourcePath,
42− translateTerm)
41+ readFileFromDirs, report,
42+ setResourcePath)
4343import Text.Pandoc.Error (PandocError (PandocParseError, PandocParsecError))
44−import Text.Pandoc.Highlighting (fromListingsLanguage, languagesByExtension)
44+import Text.Pandoc.Highlighting (languagesByExtension)
4545import Text.Pandoc.ImageSize (numUnit, showFl)
4646import Text.Pandoc.Logging
4747import Text.Pandoc.Options
@@ -61,10 +61,12 @@ import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
6161import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
6262 babelLangToBCP47, setDefaultLanguage)
6363import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
64+import Text.Pandoc.Readers.LaTeX.Inline (acronymCommands, refCommands,
65+ nameCommands, charCommands,
66+ verbCommands, rawInlineOr,
67+ listingsLanguage)
6468import Text.Pandoc.Shared
65−import qualified Text.Pandoc.Translations as Translations
6669import Text.Pandoc.Walk
67−import qualified Text.Pandoc.Builder as B
6870import Safe
6971
7072-- for debugging:
@@ -317,76 +319,6 @@ blockquote cvariant mblang = do
317319 optional $ symbolIn (".:;?!" :: [Char]) -- currently ignored
318320 return $ blockQuote . langdiv $ (bs <> citepar)
319321
320−doAcronym :: PandocMonad m => Text -> LP m Inlines
321−doAcronym form = do
322− acro <- braced
323− return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
324− ("acronym-form", "singular+" <> form)])
325− $ str $ untokenize acro]
326−
327−doAcronymPlural :: PandocMonad m => Text -> LP m Inlines
328−doAcronymPlural form = do
329− acro <- braced
330− plural <- lit "s"
331− return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
332− ("acronym-form", "plural+" <> form)]) $
333− mconcat [str $ untokenize acro, plural]]
334−
335−doverb :: PandocMonad m => LP m Inlines
336−doverb = do
337− Tok _ Symbol t <- anySymbol
338− marker <- case T.uncons t of
339− Just (c, ts) | T.null ts -> return c
340− _ -> mzero
341− withVerbatimMode $
342− code . untokenize <$>
343− manyTill (notFollowedBy newlineTok >> verbTok marker) (symbol marker)
344−
345−verbTok :: PandocMonad m => Char -> LP m Tok
346−verbTok stopchar = do
347− t@(Tok pos toktype txt) <- anyTok
348− case T.findIndex (== stopchar) txt of
349− Nothing -> return t
350− Just i -> do
351− let (t1, t2) = T.splitAt i txt
352− inp <- getInput
353− setInput $ Tok (incSourceColumn pos i) Symbol (T.singleton stopchar)
354− : totoks (incSourceColumn pos (i + 1)) (T.drop 1 t2) ++ inp
355− return $ Tok pos toktype t1
356−
357−listingsLanguage :: [(Text, Text)] -> Maybe Text
358−listingsLanguage opts =
359− case lookup "language" opts of
360− Nothing -> Nothing
361− Just l -> fromListingsLanguage l `mplus` Just l
362−
363−dolstinline :: PandocMonad m => LP m Inlines
364−dolstinline = do
365− options <- option [] keyvals
366− let classes = maybeToList $ listingsLanguage options
367− doinlinecode classes
368−
369−domintinline :: PandocMonad m => LP m Inlines
370−domintinline = do
371− skipopts
372− cls <- untokenize <$> braced
373− doinlinecode [cls]
374−
375−doinlinecode :: PandocMonad m => [Text] -> LP m Inlines
376−doinlinecode classes = do
377− Tok _ Symbol t <- anySymbol
378− marker <- case T.uncons t of
379− Just (c, ts) | T.null ts -> return c
380− _ -> mzero
381− let stopchar = if marker == '{' then '}' else marker
382− withVerbatimMode $
383− codeWith ("",classes,[]) . T.map nlToSpace . untokenize <$>
384− manyTill (verbTok stopchar) (symbol stopchar)
385−
386−nlToSpace :: Char -> Char
387−nlToSpace '\n' = ' '
388−nlToSpace x = x
389−
390322inlineCommand' :: PandocMonad m => LP m Inlines
391323inlineCommand' = try $ do
392324 Tok _ (CtrlSeq name) cmd <- anyControlSeq
@@ -405,9 +337,6 @@ inlineCommand' = try $ do
405337tok :: PandocMonad m => LP m Inlines
406338tok = tokWith inline
407339
408−inBrackets :: Inlines -> Inlines
409−inBrackets x = str "[" <> x <> str "]"
410−
411340unescapeURL :: Text -> Text
412341unescapeURL = T.concat . go . T.splitOn "\\"
413342 where
@@ -420,234 +349,136 @@ unescapeURL = T.concat . go . T.splitOn "\\"
420349 | otherwise = "\\" <> t
421350
422351inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
423−inlineCommands =
424− M.union inlineLanguageCommands $
425− M.union (accentCommands tok) $
426− M.union (citationCommands inline) $
427− M.union (siunitxCommands tok) $
428− M.fromList
429− [ ("emph", extractSpaces emph <$> tok)
430− , ("textit", extractSpaces emph <$> tok)
431− , ("textsl", extractSpaces emph <$> tok)
432− , ("textsc", extractSpaces smallcaps <$> tok)
433− , ("textsf", extractSpaces (spanWith ("",["sans-serif"],[])) <$> tok)
434− , ("textmd", extractSpaces (spanWith ("",["medium"],[])) <$> tok)
435− , ("textrm", extractSpaces (spanWith ("",["roman"],[])) <$> tok)
436− , ("textup", extractSpaces (spanWith ("",["upright"],[])) <$> tok)
437− , ("texttt", ttfamily)
438− , ("sout", extractSpaces strikeout <$> tok)
439− , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
440− , ("lq", return (str "‘"))
441− , ("rq", return (str "’"))
442− , ("textquoteleft", return (str "‘"))
443− , ("textquoteright", return (str "’"))
444− , ("textquotedblleft", return (str "“"))
445− , ("textquotedblright", return (str "”"))
446− , ("textsuperscript", extractSpaces superscript <$> tok)
447− , ("textsubscript", extractSpaces subscript <$> tok)
448− , ("textbackslash", lit "\\")
449− , ("backslash", lit "\\")
450− , ("slash", lit "/")
451− , ("textbf", extractSpaces strong <$> tok)
452− , ("textnormal", extractSpaces (spanWith ("",["nodecor"],[])) <$> tok)
453− , ("underline", underline <$> tok)
454− , ("ldots", lit "…")
455− , ("vdots", lit "\8942")
456− , ("dots", lit "…")
457− , ("mdots", lit "…")
458− , ("sim", lit "~")
459− , ("sep", lit ",")
460− , ("label", rawInlineOr "label" dolabel)
461− , ("ref", rawInlineOr "ref" $ doref "ref")
462− , ("cref", rawInlineOr "cref" $ doref "ref") -- from cleveref.sty
463− , ("vref", rawInlineOr "vref" $ doref "ref+page") -- from varioref.sty
464− , ("eqref", rawInlineOr "eqref" $ doref "eqref") -- from amsmath.sty
465− , ("mbox", rawInlineOr "mbox" $ processHBox <$> tok)
466− , ("hbox", rawInlineOr "hbox" $ processHBox <$> tok)
467− , ("lettrine", rawInlineOr "lettrine" lettrine)
468− , ("(", mathInline . untokenize <$> manyTill anyTok (controlSeq ")"))
469− , ("[", mathDisplay . untokenize <$> manyTill anyTok (controlSeq "]"))
470− , ("ensuremath", mathInline . untokenize <$> braced)
471− , ("texorpdfstring", const <$> tok <*> tok)
472− , ("P", lit "¶")
473− , ("S", lit "§")
474− , ("$", lit "$")
475− , ("%", lit "%")
476− , ("&", lit "&")
477− , ("#", lit "#")
478− , ("_", lit "_")
479− , ("{", lit "{")
480− , ("}", lit "}")
481− , ("qed", lit "\a0\x25FB")
482− -- old TeX commands
483− , ("em", extractSpaces emph <$> inlines)
484− , ("it", extractSpaces emph <$> inlines)
485− , ("sl", extractSpaces emph <$> inlines)
486− , ("bf", extractSpaces strong <$> inlines)
487− , ("tt", code . stringify . toList <$> inlines)
488− , ("rm", inlines)
489− , ("itshape", extractSpaces emph <$> inlines)
490− , ("slshape", extractSpaces emph <$> inlines)
491− , ("scshape", extractSpaces smallcaps <$> inlines)
492− , ("bfseries", extractSpaces strong <$> inlines)
493− , ("MakeUppercase", makeUppercase <$> tok)
494− , ("MakeTextUppercase", makeUppercase <$> tok) -- textcase
495− , ("uppercase", makeUppercase <$> tok)
496− , ("MakeLowercase", makeLowercase <$> tok)
497− , ("MakeTextLowercase", makeLowercase <$> tok)
498− , ("lowercase", makeLowercase <$> tok)
499− , ("/", pure mempty) -- italic correction
500− , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
501− guard $ not inTableCell
502− optional rawopt
503− spaces))
504− , (",", lit "\8198")
505− , ("@", pure mempty)
506− , (" ", lit "\160")
507− , ("ps", pure $ str "PS." <> space)
508− , ("TeX", lit "TeX")
509− , ("LaTeX", lit "LaTeX")
510− , ("bar", lit "|")
511− , ("textless", lit "<")
512− , ("textgreater", lit ">")
513− , ("thanks", skipopts >> note <$> grouped block)
514− , ("footnote", skipopts >> note <$> grouped block)
515− , ("passthrough", tok) -- \passthrough macro used by latex writer
516− -- for listings
517− , ("verb", doverb)
518− , ("lstinline", dolstinline)
519− , ("mintinline", domintinline)
520− , ("Verb", doverb)
521− , ("url", (\url -> link url "" (str url)) . unescapeURL . untokenize <$>
522− bracedUrl)
523− , ("nolinkurl", code . unescapeURL . untokenize <$> bracedUrl)
524− , ("href", do url <- bracedUrl
525− sp
526− link (unescapeURL $ untokenize url) "" <$> tok)
527− , ("includegraphics", do options <- option [] keyvals
528− src <- braced
529− mkImage options . unescapeURL . removeDoubleQuotes $
530− untokenize src)
531− , ("enquote*", enquote True Nothing)
532− , ("enquote", enquote False Nothing)
533− -- foreignquote is supposed to use native quote marks
534− , ("foreignquote*", braced >>= enquote True . Just . untokenize)
535− , ("foreignquote", braced >>= enquote False . Just . untokenize)
536− -- hypehnquote uses regular quotes
537− , ("hyphenquote*", braced >>= enquote True . Just . untokenize)
538− , ("hyphenquote", braced >>= enquote False . Just . untokenize)
539− , ("figurename", doTerm Translations.Figure)
540− , ("prefacename", doTerm Translations.Preface)
541− , ("refname", doTerm Translations.References)
542− , ("bibname", doTerm Translations.Bibliography)
543− , ("chaptername", doTerm Translations.Chapter)
544− , ("partname", doTerm Translations.Part)
545− , ("contentsname", doTerm Translations.Contents)
546− , ("listfigurename", doTerm Translations.ListOfFigures)
547− , ("listtablename", doTerm Translations.ListOfTables)
548− , ("indexname", doTerm Translations.Index)
549− , ("abstractname", doTerm Translations.Abstract)
550− , ("tablename", doTerm Translations.Table)
551− , ("enclname", doTerm Translations.Encl)
552− , ("ccname", doTerm Translations.Cc)
553− , ("headtoname", doTerm Translations.To)
554− , ("pagename", doTerm Translations.Page)
555− , ("seename", doTerm Translations.See)
556− , ("seealsoname", doTerm Translations.SeeAlso)
557− , ("proofname", doTerm Translations.Proof)
558− , ("glossaryname", doTerm Translations.Glossary)
559− , ("lstlistingname", doTerm Translations.Listing)
560− , ("hyperlink", hyperlink)
561− , ("hypertarget", hypertargetInline)
562− -- glossaries package
563− , ("gls", doAcronym "short")
564− , ("Gls", doAcronym "short")
565− , ("glsdesc", doAcronym "long")
566− , ("Glsdesc", doAcronym "long")
567− , ("GLSdesc", doAcronym "long")
568− , ("acrlong", doAcronym "long")
569− , ("Acrlong", doAcronym "long")
570− , ("acrfull", doAcronym "full")
571− , ("Acrfull", doAcronym "full")
572− , ("acrshort", doAcronym "abbrv")
573− , ("Acrshort", doAcronym "abbrv")
574− , ("glspl", doAcronymPlural "short")
575− , ("Glspl", doAcronymPlural "short")
576− , ("glsdescplural", doAcronymPlural "long")
577− , ("Glsdescplural", doAcronymPlural "long")
578− , ("GLSdescplural", doAcronymPlural "long")
579− -- acronyms package
580− , ("ac", doAcronym "short")
581− , ("acf", doAcronym "full")
582− , ("acs", doAcronym "abbrv")
583− , ("acl", doAcronym "long")
584− , ("acp", doAcronymPlural "short")
585− , ("acfp", doAcronymPlural "full")
586− , ("acsp", doAcronymPlural "abbrv")
587− , ("aclp", doAcronymPlural "long")
588− , ("Ac", doAcronym "short")
589− , ("Acf", doAcronym "full")
590− , ("Acs", doAcronym "abbrv")
591− , ("Acl", doAcronym "long")
592− , ("Acp", doAcronymPlural "short")
593− , ("Acfp", doAcronymPlural "full")
594− , ("Acsp", doAcronymPlural "abbrv")
595− , ("Aclp", doAcronymPlural "long")
596− -- hyphenat
597− , ("bshyp", lit "\\\173")
598− , ("fshyp", lit "/\173")
599− , ("dothyp", lit ".\173")
600− , ("colonhyp", lit ":\173")
601− , ("hyp", lit "-")
602− , ("nohyphens", tok)
603− , ("textnhtt", ttfamily)
604− , ("nhttfamily", ttfamily)
605− -- LaTeX colors
606− , ("textcolor", coloredInline "color")
607− , ("colorbox", coloredInline "background-color")
608− -- fontawesome
609− , ("faCheck", lit "\10003")
610− , ("faClose", lit "\10007")
611− -- xspace
612− , ("xspace", doxspace)
613− -- etoolbox
614− , ("ifstrequal", ifstrequal)
615− , ("newtoggle", braced >>= newToggle)
616− , ("toggletrue", braced >>= setToggle True)
617− , ("togglefalse", braced >>= setToggle False)
618− , ("iftoggle", try $ ifToggle >> inline)
619− -- biblatex misc
620− , ("RN", romanNumeralUpper)
621− , ("Rn", romanNumeralLower)
622− -- babel
623− , ("foreignlanguage", foreignlanguage)
624− -- include
625− , ("input", rawInlineOr "input" $ include "input")
626− -- soul package
627− , ("ul", underline <$> tok)
628− -- ulem package
629− , ("uline", underline <$> tok)
630− -- plain tex stuff that should just be passed through as raw tex
631− , ("ifdim", ifdim)
632− -- bibtex
633− , ("mkbibquote", spanWith nullAttr . doubleQuoted <$> tok)
634− , ("mkbibemph", spanWith nullAttr . emph <$> tok)
635− , ("mkbibitalic", spanWith nullAttr . emph <$> tok)
636− , ("mkbibbold", spanWith nullAttr . strong <$> tok)
637− , ("mkbibparens",
638− spanWith nullAttr . (\x -> str "(" <> x <> str ")") <$> tok)
639− , ("mkbibbrackets",
640− spanWith nullAttr . (\x -> str "[" <> x <> str "]") <$> tok)
641− , ("autocap", spanWith nullAttr <$> tok)
642− , ("textnormal", spanWith ("",["nodecor"],[]) <$> tok)
643− , ("bibstring",
644− (\x -> spanWith ("",[],[("bibstring",x)]) (str x)) . untokenize
645− <$> braced)
646− , ("adddot", pure (str "."))
647− , ("adddotspace", pure (spanWith nullAttr (str "." <> space)))
648− , ("addabbrvspace", pure space)
649− , ("hyphen", pure (str "-"))
650− ]
352+inlineCommands = M.unions
353+ [ inlineLanguageCommands
354+ , accentCommands tok
355+ , citationCommands inline
356+ , siunitxCommands tok
357+ , acronymCommands
358+ , refCommands
359+ , nameCommands
360+ , verbCommands
361+ , charCommands
362+ , rest ]
363+ where
364+ rest = M.fromList
365+ [ ("emph", extractSpaces emph <$> tok)
366+ , ("textit", extractSpaces emph <$> tok)
367+ , ("textsl", extractSpaces emph <$> tok)
368+ , ("textsc", extractSpaces smallcaps <$> tok)
369+ , ("textsf", extractSpaces (spanWith ("",["sans-serif"],[])) <$> tok)
370+ , ("textmd", extractSpaces (spanWith ("",["medium"],[])) <$> tok)
371+ , ("textrm", extractSpaces (spanWith ("",["roman"],[])) <$> tok)
372+ , ("textup", extractSpaces (spanWith ("",["upright"],[])) <$> tok)
373+ , ("texttt", ttfamily)
374+ , ("sout", extractSpaces strikeout <$> tok)
375+ , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
376+ , ("lq", return (str "‘"))
377+ , ("rq", return (str "’"))
378+ , ("textquoteleft", return (str "‘"))
379+ , ("textquoteright", return (str "’"))
380+ , ("textquotedblleft", return (str "“"))
381+ , ("textquotedblright", return (str "”"))
382+ , ("textsuperscript", extractSpaces superscript <$> tok)
383+ , ("textsubscript", extractSpaces subscript <$> tok)
384+ , ("textbf", extractSpaces strong <$> tok)
385+ , ("textnormal", extractSpaces (spanWith ("",["nodecor"],[])) <$> tok)
386+ , ("underline", underline <$> tok)
387+ , ("mbox", rawInlineOr "mbox" $ processHBox <$> tok)
388+ , ("hbox", rawInlineOr "hbox" $ processHBox <$> tok)
389+ , ("lettrine", rawInlineOr "lettrine" lettrine)
390+ , ("(", mathInline . untokenize <$> manyTill anyTok (controlSeq ")"))
391+ , ("[", mathDisplay . untokenize <$> manyTill anyTok (controlSeq "]"))
392+ , ("ensuremath", mathInline . untokenize <$> braced)
393+ , ("texorpdfstring", const <$> tok <*> tok)
394+ -- old TeX commands
395+ , ("em", extractSpaces emph <$> inlines)
396+ , ("it", extractSpaces emph <$> inlines)
397+ , ("sl", extractSpaces emph <$> inlines)
398+ , ("bf", extractSpaces strong <$> inlines)
399+ , ("tt", code . stringify . toList <$> inlines)
400+ , ("rm", inlines)
401+ , ("itshape", extractSpaces emph <$> inlines)
402+ , ("slshape", extractSpaces emph <$> inlines)
403+ , ("scshape", extractSpaces smallcaps <$> inlines)
404+ , ("bfseries", extractSpaces strong <$> inlines)
405+ , ("MakeUppercase", makeUppercase <$> tok)
406+ , ("MakeTextUppercase", makeUppercase <$> tok) -- textcase
407+ , ("uppercase", makeUppercase <$> tok)
408+ , ("MakeLowercase", makeLowercase <$> tok)
409+ , ("MakeTextLowercase", makeLowercase <$> tok)
410+ , ("lowercase", makeLowercase <$> tok)
411+ , ("thanks", skipopts >> note <$> grouped block)
412+ , ("footnote", skipopts >> note <$> grouped block)
413+ , ("passthrough", tok) -- \passthrough macro used by latex writer
414+ -- for listings
415+ , ("url", (\url -> link url "" (str url)) . unescapeURL . untokenize <$>
416+ bracedUrl)
417+ , ("nolinkurl", code . unescapeURL . untokenize <$> bracedUrl)
418+ , ("href", do url <- bracedUrl
419+ sp
420+ link (unescapeURL $ untokenize url) "" <$> tok)
421+ , ("includegraphics", do options <- option [] keyvals
422+ src <- braced
423+ mkImage options . unescapeURL . removeDoubleQuotes $
424+ untokenize src)
425+ , ("enquote*", enquote True Nothing)
426+ , ("enquote", enquote False Nothing)
427+ -- foreignquote is supposed to use native quote marks
428+ , ("foreignquote*", braced >>= enquote True . Just . untokenize)
429+ , ("foreignquote", braced >>= enquote False . Just . untokenize)
430+ -- hypehnquote uses regular quotes
431+ , ("hyphenquote*", braced >>= enquote True . Just . untokenize)
432+ , ("hyphenquote", braced >>= enquote False . Just . untokenize)
433+ , ("hyperlink", hyperlink)
434+ , ("hypertarget", hypertargetInline)
435+ -- hyphenat
436+ , ("nohyphens", tok)
437+ , ("textnhtt", ttfamily)
438+ , ("nhttfamily", ttfamily)
439+ -- LaTeX colors
440+ , ("textcolor", coloredInline "color")
441+ , ("colorbox", coloredInline "background-color")
442+ -- xspace
443+ , ("xspace", doxspace)
444+ -- etoolbox
445+ , ("ifstrequal", ifstrequal)
446+ , ("newtoggle", braced >>= newToggle)
447+ , ("toggletrue", braced >>= setToggle True)
448+ , ("togglefalse", braced >>= setToggle False)
449+ , ("iftoggle", try $ ifToggle >> inline)
450+ -- biblatex misc
451+ , ("RN", romanNumeralUpper)
452+ , ("Rn", romanNumeralLower)
453+ -- babel
454+ , ("foreignlanguage", foreignlanguage)
455+ -- include
456+ , ("input", rawInlineOr "input" $ include "input")
457+ -- soul package
458+ , ("ul", underline <$> tok)
459+ -- ulem package
460+ , ("uline", underline <$> tok)
461+ -- plain tex stuff that should just be passed through as raw tex
462+ , ("ifdim", ifdim)
463+ -- bibtex
464+ , ("mkbibquote", spanWith nullAttr . doubleQuoted <$> tok)
465+ , ("mkbibemph", spanWith nullAttr . emph <$> tok)
466+ , ("mkbibitalic", spanWith nullAttr . emph <$> tok)
467+ , ("mkbibbold", spanWith nullAttr . strong <$> tok)
468+ , ("mkbibparens",
469+ spanWith nullAttr . (\x -> str "(" <> x <> str ")") <$> tok)
470+ , ("mkbibbrackets",
471+ spanWith nullAttr . (\x -> str "[" <> x <> str "]") <$> tok)
472+ , ("autocap", spanWith nullAttr <$> tok)
473+ , ("textnormal", spanWith ("",["nodecor"],[]) <$> tok)
474+ , ("bibstring",
475+ (\x -> spanWith ("",[],[("bibstring",x)]) (str x)) . untokenize
476+ <$> braced)
477+ , ("adddot", pure (str "."))
478+ , ("adddotspace", pure (spanWith nullAttr (str "." <> space)))
479+ , ("addabbrvspace", pure space)
480+ , ("hyphen", pure (str "-"))
481+ ]
651482
652483lettrine :: PandocMonad m => LP m Inlines
653484lettrine = do
@@ -766,9 +597,6 @@ ifToggle = do
766597 report $ UndefinedToggle name' pos
767598 return ()
768599
769−doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
770−doTerm term = str <$> translateTerm term
771−
772600ifstrequal :: (PandocMonad m, Monoid a) => LP m a
773601ifstrequal = do
774602 str1 <- tok
@@ -789,13 +617,6 @@ coloredInline stylename = do
789617ttfamily :: PandocMonad m => LP m Inlines
790618ttfamily = code . stringify . toList <$> tok
791619
792−rawInlineOr :: PandocMonad m => Text -> LP m Inlines -> LP m Inlines
793−rawInlineOr name' fallback = do
794− parseRaw <- extensionEnabled Ext_raw_tex <$> getOption readerExtensions
795− if parseRaw
796− then rawInline "latex" <$> getRawCommand name' ("\\" <> name')
797− else fallback
798−
799620processHBox :: Inlines -> Inlines
800621processHBox = walk convert
801622 where
@@ -846,25 +667,6 @@ treatAsInline = Set.fromList
846667 , "pagebreak"
847668 ]
848669
849−dolabel :: PandocMonad m => LP m Inlines
850−dolabel = do
851− v <- braced
852− let refstr = untokenize v
853− updateState $ \st ->
854− st{ sLastLabel = Just refstr }
855− return $ spanWith (refstr,[],[("label", refstr)])
856− $ inBrackets $ str $ untokenize v
857−
858−doref :: PandocMonad m => Text -> LP m Inlines
859−doref cls = do
860− v <- braced
861− let refstr = untokenize v
862− return $ linkWith ("",[],[ ("reference-type", cls)
863− , ("reference", refstr)])
864− ("#" <> refstr)
865− ""
866− (inBrackets $ str refstr)
867−
868670lookupListDefault :: (Ord k) => v -> [k] -> M.Map k v -> v
869671lookupListDefault d = (fromMaybe d .) . lookupList
870672 where lookupList l m = msum $ map (`M.lookup` m) l
src/Text/Pandoc/Readers/LaTeX/Inline.hsnew file+275
@@ -0,0 +1,275 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+{- |
3+ Module : Text.Pandoc.Readers.LaTeX.Inline
4+ Copyright : Copyright (C) 2006-2021 John MacFarlane
5+ License : GNU GPL, version 2 or above
6+
7+ Maintainer : John MacFarlane <jgm@berkeley.edu>
8+ Stability : alpha
9+ Portability : portable
10+-}
11+module Text.Pandoc.Readers.LaTeX.Inline
12+ ( acronymCommands
13+ , verbCommands
14+ , charCommands
15+ , nameCommands
16+ , refCommands
17+ , rawInlineOr
18+ , listingsLanguage
19+ )
20+where
21+
22+import qualified Data.Map as M
23+import Data.Text (Text)
24+import qualified Data.Text as T
25+import Text.Pandoc.Builder
26+import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
27+import Control.Applicative (optional)
28+import Control.Monad (guard, mzero, mplus)
29+import Text.Pandoc.Class.PandocMonad (PandocMonad (..), translateTerm)
30+import Text.Pandoc.Readers.LaTeX.Parsing
31+import Text.Pandoc.Extensions (extensionEnabled, Extension(..))
32+import Text.Pandoc.Parsing (getOption, updateState, getState, notFollowedBy,
33+ manyTill, getInput, setInput, incSourceColumn,
34+ option)
35+import Text.Pandoc.Highlighting (fromListingsLanguage,)
36+import Data.Maybe (maybeToList)
37+import Text.Pandoc.Options (ReaderOptions(..))
38+import qualified Text.Pandoc.Translations as Translations
39+
40+rawInlineOr :: PandocMonad m => Text -> LP m Inlines -> LP m Inlines
41+rawInlineOr name' fallback = do
42+ parseRaw <- extensionEnabled Ext_raw_tex <$> getOption readerExtensions
43+ if parseRaw
44+ then rawInline "latex" <$> getRawCommand name' ("\\" <> name')
45+ else fallback
46+
47+dolabel :: PandocMonad m => LP m Inlines
48+dolabel = do
49+ v <- braced
50+ let refstr = untokenize v
51+ updateState $ \st ->
52+ st{ sLastLabel = Just refstr }
53+ return $ spanWith (refstr,[],[("label", refstr)])
54+ $ inBrackets $ str $ untokenize v
55+
56+doref :: PandocMonad m => Text -> LP m Inlines
57+doref cls = do
58+ v <- braced
59+ let refstr = untokenize v
60+ return $ linkWith ("",[],[ ("reference-type", cls)
61+ , ("reference", refstr)])
62+ ("#" <> refstr)
63+ ""
64+ (inBrackets $ str refstr)
65+
66+inBrackets :: Inlines -> Inlines
67+inBrackets x = str "[" <> x <> str "]"
68+
69+doTerm :: PandocMonad m => Translations.Term -> LP m Inlines
70+doTerm term = str <$> translateTerm term
71+
72+lit :: Text -> LP m Inlines
73+lit = pure . str
74+
75+doverb :: PandocMonad m => LP m Inlines
76+doverb = do
77+ Tok _ Symbol t <- anySymbol
78+ marker <- case T.uncons t of
79+ Just (c, ts) | T.null ts -> return c
80+ _ -> mzero
81+ withVerbatimMode $
82+ code . untokenize <$>
83+ manyTill (notFollowedBy newlineTok >> verbTok marker) (symbol marker)
84+
85+verbTok :: PandocMonad m => Char -> LP m Tok
86+verbTok stopchar = do
87+ t@(Tok pos toktype txt) <- anyTok
88+ case T.findIndex (== stopchar) txt of
89+ Nothing -> return t
90+ Just i -> do
91+ let (t1, t2) = T.splitAt i txt
92+ inp <- getInput
93+ setInput $ Tok (incSourceColumn pos i) Symbol (T.singleton stopchar)
94+ : totoks (incSourceColumn pos (i + 1)) (T.drop 1 t2) ++ inp
95+ return $ Tok pos toktype t1
96+
97+listingsLanguage :: [(Text, Text)] -> Maybe Text
98+listingsLanguage opts =
99+ case lookup "language" opts of
100+ Nothing -> Nothing
101+ Just l -> fromListingsLanguage l `mplus` Just l
102+
103+dolstinline :: PandocMonad m => LP m Inlines
104+dolstinline = do
105+ options <- option [] keyvals
106+ let classes = maybeToList $ listingsLanguage options
107+ doinlinecode classes
108+
109+domintinline :: PandocMonad m => LP m Inlines
110+domintinline = do
111+ skipopts
112+ cls <- untokenize <$> braced
113+ doinlinecode [cls]
114+
115+doinlinecode :: PandocMonad m => [Text] -> LP m Inlines
116+doinlinecode classes = do
117+ Tok _ Symbol t <- anySymbol
118+ marker <- case T.uncons t of
119+ Just (c, ts) | T.null ts -> return c
120+ _ -> mzero
121+ let stopchar = if marker == '{' then '}' else marker
122+ withVerbatimMode $
123+ codeWith ("",classes,[]) . T.map nlToSpace . untokenize <$>
124+ manyTill (verbTok stopchar) (symbol stopchar)
125+
126+nlToSpace :: Char -> Char
127+nlToSpace '\n' = ' '
128+nlToSpace x = x
129+
130+
131+
132+verbCommands :: PandocMonad m => M.Map Text (LP m Inlines)
133+verbCommands = M.fromList
134+ [ ("verb", doverb)
135+ , ("lstinline", dolstinline)
136+ , ("mintinline", domintinline)
137+ , ("Verb", doverb)
138+ ]
139+
140+
141+
142+charCommands :: PandocMonad m => M.Map Text (LP m Inlines)
143+charCommands = M.fromList
144+ [ ("ldots", lit "…")
145+ , ("vdots", lit "\8942")
146+ , ("dots", lit "…")
147+ , ("mdots", lit "…")
148+ , ("sim", lit "~")
149+ , ("sep", lit ",")
150+ , ("P", lit "¶")
151+ , ("S", lit "§")
152+ , ("$", lit "$")
153+ , ("%", lit "%")
154+ , ("&", lit "&")
155+ , ("#", lit "#")
156+ , ("_", lit "_")
157+ , ("{", lit "{")
158+ , ("}", lit "}")
159+ , ("qed", lit "\a0\x25FB")
160+ , ("/", pure mempty) -- italic correction
161+ , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
162+ guard $ not inTableCell
163+ optional rawopt
164+ spaces))
165+ , (",", lit "\8198")
166+ , ("@", pure mempty)
167+ , (" ", lit "\160")
168+ , ("ps", pure $ str "PS." <> space)
169+ , ("TeX", lit "TeX")
170+ , ("LaTeX", lit "LaTeX")
171+ , ("bar", lit "|")
172+ , ("textless", lit "<")
173+ , ("textgreater", lit ">")
174+ , ("textbackslash", lit "\\")
175+ , ("backslash", lit "\\")
176+ , ("slash", lit "/")
177+ -- fontawesome
178+ , ("faCheck", lit "\10003")
179+ , ("faClose", lit "\10007")
180+ -- hyphenat
181+ , ("bshyp", lit "\\\173")
182+ , ("fshyp", lit "/\173")
183+ , ("dothyp", lit ".\173")
184+ , ("colonhyp", lit ":\173")
185+ , ("hyp", lit "-")
186+ ]
187+
188+nameCommands :: PandocMonad m => M.Map Text (LP m Inlines)
189+nameCommands = M.fromList
190+ [ ("figurename", doTerm Translations.Figure)
191+ , ("prefacename", doTerm Translations.Preface)
192+ , ("refname", doTerm Translations.References)
193+ , ("bibname", doTerm Translations.Bibliography)
194+ , ("chaptername", doTerm Translations.Chapter)
195+ , ("partname", doTerm Translations.Part)
196+ , ("contentsname", doTerm Translations.Contents)
197+ , ("listfigurename", doTerm Translations.ListOfFigures)
198+ , ("listtablename", doTerm Translations.ListOfTables)
199+ , ("indexname", doTerm Translations.Index)
200+ , ("abstractname", doTerm Translations.Abstract)
201+ , ("tablename", doTerm Translations.Table)
202+ , ("enclname", doTerm Translations.Encl)
203+ , ("ccname", doTerm Translations.Cc)
204+ , ("headtoname", doTerm Translations.To)
205+ , ("pagename", doTerm Translations.Page)
206+ , ("seename", doTerm Translations.See)
207+ , ("seealsoname", doTerm Translations.SeeAlso)
208+ , ("proofname", doTerm Translations.Proof)
209+ , ("glossaryname", doTerm Translations.Glossary)
210+ , ("lstlistingname", doTerm Translations.Listing)
211+ ]
212+
213+refCommands :: PandocMonad m => M.Map Text (LP m Inlines)
214+refCommands = M.fromList
215+ [ ("label", rawInlineOr "label" dolabel)
216+ , ("ref", rawInlineOr "ref" $ doref "ref")
217+ , ("cref", rawInlineOr "cref" $ doref "ref") -- from cleveref.sty
218+ , ("vref", rawInlineOr "vref" $ doref "ref+page") -- from varioref.sty
219+ , ("eqref", rawInlineOr "eqref" $ doref "eqref") -- from amsmath.sty
220+ ]
221+
222+acronymCommands :: PandocMonad m => M.Map Text (LP m Inlines)
223+acronymCommands = M.fromList
224+ -- glossaries package
225+ [ ("gls", doAcronym "short")
226+ , ("Gls", doAcronym "short")
227+ , ("glsdesc", doAcronym "long")
228+ , ("Glsdesc", doAcronym "long")
229+ , ("GLSdesc", doAcronym "long")
230+ , ("acrlong", doAcronym "long")
231+ , ("Acrlong", doAcronym "long")
232+ , ("acrfull", doAcronym "full")
233+ , ("Acrfull", doAcronym "full")
234+ , ("acrshort", doAcronym "abbrv")
235+ , ("Acrshort", doAcronym "abbrv")
236+ , ("glspl", doAcronymPlural "short")
237+ , ("Glspl", doAcronymPlural "short")
238+ , ("glsdescplural", doAcronymPlural "long")
239+ , ("Glsdescplural", doAcronymPlural "long")
240+ , ("GLSdescplural", doAcronymPlural "long")
241+ -- acronyms package
242+ , ("ac", doAcronym "short")
243+ , ("acf", doAcronym "full")
244+ , ("acs", doAcronym "abbrv")
245+ , ("acl", doAcronym "long")
246+ , ("acp", doAcronymPlural "short")
247+ , ("acfp", doAcronymPlural "full")
248+ , ("acsp", doAcronymPlural "abbrv")
249+ , ("aclp", doAcronymPlural "long")
250+ , ("Ac", doAcronym "short")
251+ , ("Acf", doAcronym "full")
252+ , ("Acs", doAcronym "abbrv")
253+ , ("Acl", doAcronym "long")
254+ , ("Acp", doAcronymPlural "short")
255+ , ("Acfp", doAcronymPlural "full")
256+ , ("Acsp", doAcronymPlural "abbrv")
257+ , ("Aclp", doAcronymPlural "long")
258+ ]
259+
260+doAcronym :: PandocMonad m => Text -> LP m Inlines
261+doAcronym form = do
262+ acro <- braced
263+ return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
264+ ("acronym-form", "singular+" <> form)])
265+ $ str $ untokenize acro]
266+
267+doAcronymPlural :: PandocMonad m => Text -> LP m Inlines
268+doAcronymPlural form = do
269+ acro <- braced
270+ let plural = str "s"
271+ return . mconcat $ [spanWith ("",[],[("acronym-label", untokenize acro),
272+ ("acronym-form", "plural+" <> form)]) $
273+ mconcat [str $ untokenize acro, plural]]
274+
275+