git.iko.soy/typeable/pandoc
on master help sign in
Moved more into T.P.Readers.LaTeX.Lang.
commit953cbb7f6ac42bde750546ef88e57841656ff8b9 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 3 Mar 2021, 11:08 (6 years ago) parent2cbe949
3 files changed+97−82
src/Text/Pandoc/Readers/LaTeX.hs+6−78 src/Text/Pandoc/Readers/LaTeX/Inline.hs+62−3 src/Text/Pandoc/Readers/LaTeX/Lang.hs+29−1
src/Text/Pandoc/Readers/LaTeX.hs+6−78
@@ -34,7 +34,7 @@ import qualified Data.Set as Set
3434import Data.Text (Text)
3535import qualified Data.Text as T
3636import System.FilePath (addExtension, replaceExtension, takeExtension)
37−import Text.Pandoc.BCP47 (Lang (..), renderLang)
37+import Text.Pandoc.BCP47 (renderLang)
3838import Text.Pandoc.Builder as B
3939import Text.Pandoc.Class.PandocPure (PandocPure)
4040import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
@@ -58,11 +58,12 @@ import Text.Pandoc.Readers.LaTeX.Math (dollarsMath, inlineEnvironments,
5858 theoremEnvironment)
5959import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
6060import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
61−import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
61+import Text.Pandoc.Readers.LaTeX.Lang (inlineLanguageCommands,
6262 babelLangToBCP47, setDefaultLanguage)
6363import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
6464import Text.Pandoc.Readers.LaTeX.Inline (acronymCommands, refCommands,
6565 nameCommands, charCommands,
66+ biblatexInlineCommands,
6667 verbCommands, rawInlineOr,
6768 listingsLanguage)
6869import Text.Pandoc.Shared
@@ -350,8 +351,7 @@ unescapeURL = T.concat . go . T.splitOn "\\"
350351
351352inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
352353inlineCommands = M.unions
353− [ inlineLanguageCommands
354− , accentCommands tok
354+ [ accentCommands tok
355355 , citationCommands inline
356356 , siunitxCommands tok
357357 , acronymCommands
@@ -359,6 +359,8 @@ inlineCommands = M.unions
359359 , nameCommands
360360 , verbCommands
361361 , charCommands
362+ , inlineLanguageCommands tok
363+ , biblatexInlineCommands tok
362364 , rest ]
363365 where
364366 rest = M.fromList
@@ -373,12 +375,6 @@ inlineCommands = M.unions
373375 , ("texttt", ttfamily)
374376 , ("sout", extractSpaces strikeout <$> tok)
375377 , ("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 "”"))
382378 , ("textsuperscript", extractSpaces superscript <$> tok)
383379 , ("textsubscript", extractSpaces subscript <$> tok)
384380 , ("textbf", extractSpaces strong <$> tok)
@@ -447,11 +443,6 @@ inlineCommands = M.unions
447443 , ("toggletrue", braced >>= setToggle True)
448444 , ("togglefalse", braced >>= setToggle False)
449445 , ("iftoggle", try $ ifToggle >> inline)
450− -- biblatex misc
451− , ("RN", romanNumeralUpper)
452− , ("Rn", romanNumeralLower)
453− -- babel
454− , ("foreignlanguage", foreignlanguage)
455446 -- include
456447 , ("input", rawInlineOr "input" $ include "input")
457448 -- soul package
@@ -460,24 +451,6 @@ inlineCommands = M.unions
460451 , ("uline", underline <$> tok)
461452 -- plain tex stuff that should just be passed through as raw tex
462453 , ("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 "-"))
481454 ]
482455
483456lettrine :: PandocMonad m => LP m Inlines
@@ -502,26 +475,6 @@ alterStr :: (Text -> Text) -> Inline -> Inline
502475alterStr f (Str xs) = Str (f xs)
503476alterStr _ x = x
504477
505−foreignlanguage :: PandocMonad m => LP m Inlines
506−foreignlanguage = do
507− babelLang <- untokenize <$> braced
508− case babelLangToBCP47 babelLang of
509− Just lang -> spanWith ("", [], [("lang", renderLang lang)]) <$> tok
510− _ -> tok
511−
512−inlineLanguageCommands :: PandocMonad m => M.Map Text (LP m Inlines)
513−inlineLanguageCommands = M.fromList $ mk <$> M.toList polyglossiaLangToBCP47
514− where
515− mk (polyglossia, bcp47Func) =
516− ("text" <> polyglossia, inlineLanguage bcp47Func)
517−
518−inlineLanguage :: PandocMonad m => (Text -> Lang) -> LP m Inlines
519−inlineLanguage bcp47Func = do
520− o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
521− <$> rawopt
522− let lang = renderLang $ bcp47Func o
523− extractSpaces (spanWith ("", [], [("lang", lang)])) <$> tok
524−
525478hyperlink :: PandocMonad m => LP m Inlines
526479hyperlink = try $ do
527480 src <- untokenize <$> braced
@@ -542,31 +495,6 @@ hypertargetInline = try $ do
542495 ils <- grouped inline
543496 return $ spanWith (ref, [], []) ils
544497
545−romanNumeralUpper :: (PandocMonad m) => LP m Inlines
546−romanNumeralUpper =
547− str . toRomanNumeral <$> romanNumeralArg
548−
549−romanNumeralLower :: (PandocMonad m) => LP m Inlines
550−romanNumeralLower =
551− str . T.toLower . toRomanNumeral <$> romanNumeralArg
552−
553−romanNumeralArg :: (PandocMonad m) => LP m Int
554−romanNumeralArg = spaces *> (parser <|> inBraces)
555− where
556− inBraces = do
557− symbol '{'
558− spaces
559− res <- parser
560− spaces
561− symbol '}'
562− return res
563− parser = do
564− s <- untokenize <$> many1 (satisfyTok isWordTok)
565− let (digits, rest) = T.span isDigit s
566− unless (T.null rest) $
567− Prelude.fail "Non-digits in argument to \\Rn or \\RN"
568− safeRead digits
569−
570498newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
571499newToggle name = do
572500 updateState $ \st ->
src/Text/Pandoc/Readers/LaTeX/Inline.hs+62−3
@@ -13,6 +13,7 @@ module Text.Pandoc.Readers.LaTeX.Inline
1313 , verbCommands
1414 , charCommands
1515 , nameCommands
16+ , biblatexInlineCommands
1617 , refCommands
1718 , rawInlineOr
1819 , listingsLanguage
@@ -23,15 +24,17 @@ import qualified Data.Map as M
2324import Data.Text (Text)
2425import qualified Data.Text as T
2526import Text.Pandoc.Builder
27+import Text.Pandoc.Shared (toRomanNumeral, safeRead)
2628import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
27−import Control.Applicative (optional)
28−import Control.Monad (guard, mzero, mplus)
29+import Control.Applicative (optional, (<|>))
30+import Control.Monad (guard, mzero, mplus, unless)
2931import Text.Pandoc.Class.PandocMonad (PandocMonad (..), translateTerm)
3032import Text.Pandoc.Readers.LaTeX.Parsing
3133import Text.Pandoc.Extensions (extensionEnabled, Extension(..))
3234import Text.Pandoc.Parsing (getOption, updateState, getState, notFollowedBy,
3335 manyTill, getInput, setInput, incSourceColumn,
34− option)
36+ option, many1)
37+import Data.Char (isDigit)
3538import Text.Pandoc.Highlighting (fromListingsLanguage,)
3639import Data.Maybe (maybeToList)
3740import Text.Pandoc.Options (ReaderOptions(..))
@@ -127,6 +130,31 @@ nlToSpace :: Char -> Char
127130nlToSpace '\n' = ' '
128131nlToSpace x = x
129132
133+romanNumeralUpper :: (PandocMonad m) => LP m Inlines
134+romanNumeralUpper =
135+ str . toRomanNumeral <$> romanNumeralArg
136+
137+romanNumeralLower :: (PandocMonad m) => LP m Inlines
138+romanNumeralLower =
139+ str . T.toLower . toRomanNumeral <$> romanNumeralArg
140+
141+romanNumeralArg :: (PandocMonad m) => LP m Int
142+romanNumeralArg = spaces *> (parser <|> inBraces)
143+ where
144+ inBraces = do
145+ symbol '{'
146+ spaces
147+ res <- parser
148+ spaces
149+ symbol '}'
150+ return res
151+ parser = do
152+ s <- untokenize <$> many1 (satisfyTok isWordTok)
153+ let (digits, rest) = T.span isDigit s
154+ unless (T.null rest) $
155+ Prelude.fail "Non-digits in argument to \\Rn or \\RN"
156+ safeRead digits
157+
130158
131159
132160verbCommands :: PandocMonad m => M.Map Text (LP m Inlines)
@@ -157,6 +185,12 @@ charCommands = M.fromList
157185 , ("{", lit "{")
158186 , ("}", lit "}")
159187 , ("qed", lit "\a0\x25FB")
188+ , ("lq", return (str "‘"))
189+ , ("rq", return (str "’"))
190+ , ("textquoteleft", return (str "‘"))
191+ , ("textquoteright", return (str "’"))
192+ , ("textquotedblleft", return (str "“"))
193+ , ("textquotedblright", return (str "”"))
160194 , ("/", pure mempty) -- italic correction
161195 , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
162196 guard $ not inTableCell
@@ -185,6 +219,31 @@ charCommands = M.fromList
185219 , ("hyp", lit "-")
186220 ]
187221
222+biblatexInlineCommands :: PandocMonad m
223+ => LP m Inlines -> M.Map Text (LP m Inlines)
224+biblatexInlineCommands tok = M.fromList
225+ -- biblatex misc
226+ [ ("RN", romanNumeralUpper)
227+ , ("Rn", romanNumeralLower)
228+ , ("mkbibquote", spanWith nullAttr . doubleQuoted <$> tok)
229+ , ("mkbibemph", spanWith nullAttr . emph <$> tok)
230+ , ("mkbibitalic", spanWith nullAttr . emph <$> tok)
231+ , ("mkbibbold", spanWith nullAttr . strong <$> tok)
232+ , ("mkbibparens",
233+ spanWith nullAttr . (\x -> str "(" <> x <> str ")") <$> tok)
234+ , ("mkbibbrackets",
235+ spanWith nullAttr . (\x -> str "[" <> x <> str "]") <$> tok)
236+ , ("autocap", spanWith nullAttr <$> tok)
237+ , ("textnormal", spanWith ("",["nodecor"],[]) <$> tok)
238+ , ("bibstring",
239+ (\x -> spanWith ("",[],[("bibstring",x)]) (str x)) . untokenize
240+ <$> braced)
241+ , ("adddot", pure (str "."))
242+ , ("adddotspace", pure (spanWith nullAttr (str "." <> space)))
243+ , ("addabbrvspace", pure space)
244+ , ("hyphen", pure (str "-"))
245+ ]
246+
188247nameCommands :: PandocMonad m => M.Map Text (LP m Inlines)
189248nameCommands = M.fromList
190249 [ ("figurename", doTerm Translations.Figure)
src/Text/Pandoc/Readers/LaTeX/Lang.hs+29−1
@@ -15,15 +15,43 @@ module Text.Pandoc.Readers.LaTeX.Lang
1515 ( setDefaultLanguage
1616 , polyglossiaLangToBCP47
1717 , babelLangToBCP47
18+ , inlineLanguageCommands
1819 )
1920where
2021import qualified Data.Map as M
22+import Data.Text (Text)
2123import qualified Data.Text as T
24+import Text.Pandoc.Shared (extractSpaces)
2225import Text.Pandoc.BCP47 (Lang(..), renderLang)
2326import Text.Pandoc.Class (PandocMonad(..), setTranslations)
2427import Text.Pandoc.Readers.LaTeX.Parsing
2528import Text.Pandoc.Parsing (updateState, option)
26−import Text.Pandoc.Builder (Blocks, setMeta, str)
29+import Text.Pandoc.Builder (Blocks, Inlines, setMeta, str, spanWith)
30+
31+foreignlanguage :: PandocMonad m => LP m Inlines -> LP m Inlines
32+foreignlanguage tok = do
33+ babelLang <- untokenize <$> braced
34+ case babelLangToBCP47 babelLang of
35+ Just lang -> spanWith ("", [], [("lang", renderLang lang)]) <$> tok
36+ _ -> tok
37+
38+inlineLanguageCommands :: PandocMonad m
39+ => LP m Inlines -> M.Map Text (LP m Inlines)
40+inlineLanguageCommands tok =
41+ M.fromList $
42+ ("foreignlanguage", foreignlanguage tok) :
43+ (mk <$> M.toList polyglossiaLangToBCP47)
44+ where
45+ mk (polyglossia, bcp47Func) =
46+ ("text" <> polyglossia, inlineLanguage tok bcp47Func)
47+
48+inlineLanguage :: PandocMonad m
49+ => LP m Inlines -> (Text -> Lang) -> LP m Inlines
50+inlineLanguage tok bcp47Func = do
51+ o <- option "" $ T.filter (\c -> c /= '[' && c /= ']')
52+ <$> rawopt
53+ let lang = renderLang $ bcp47Func o
54+ extractSpaces (spanWith ("", [], [("lang", lang)])) <$> tok
2755
2856setDefaultLanguage :: PandocMonad m => LP m Blocks
2957setDefaultLanguage = do