git.iko.soy/typeable/pandoc
on master help sign in
Moved more into T.P.Readers.LaTeX.Lang.
044bc44John MacFarlane
src/Text/Pandoc/Readers/LaTeX.hs+6−78
@@ -34,7 +34,7 @@ import qualified Data.Set as Set
34import Data.Text (Text)
34import Data.Text (Text)
35import qualified Data.Text as T
35import qualified Data.Text as T
36import System.FilePath (addExtension, replaceExtension, takeExtension)
36import System.FilePath (addExtension, replaceExtension, takeExtension)
37−import Text.Pandoc.BCP47 (Lang (..), renderLang)
37+import Text.Pandoc.BCP47 (renderLang)
38import Text.Pandoc.Builder as B
38import Text.Pandoc.Builder as B
39import Text.Pandoc.Class.PandocPure (PandocPure)
39import Text.Pandoc.Class.PandocPure (PandocPure)
40import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
40import Text.Pandoc.Class.PandocMonad (PandocMonad (..), getResourcePath,
@@ -58,11 +58,12 @@ import Text.Pandoc.Readers.LaTeX.Math (dollarsMath, inlineEnvironments,
58 theoremEnvironment)
58 theoremEnvironment)
59import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
59import Text.Pandoc.Readers.LaTeX.Table (tableEnvironments)
60import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
60import Text.Pandoc.Readers.LaTeX.Macro (macroDef)
61−import Text.Pandoc.Readers.LaTeX.Lang (polyglossiaLangToBCP47,
61+import Text.Pandoc.Readers.LaTeX.Lang (inlineLanguageCommands,
62 babelLangToBCP47, setDefaultLanguage)
62 babelLangToBCP47, setDefaultLanguage)
63import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
63import Text.Pandoc.Readers.LaTeX.SIunitx (siunitxCommands)
64import Text.Pandoc.Readers.LaTeX.Inline (acronymCommands, refCommands,
64import Text.Pandoc.Readers.LaTeX.Inline (acronymCommands, refCommands,
65 nameCommands, charCommands,
65 nameCommands, charCommands,
66+ biblatexInlineCommands,
66 verbCommands, rawInlineOr,
67 verbCommands, rawInlineOr,
67 listingsLanguage)
68 listingsLanguage)
68import Text.Pandoc.Shared
69import Text.Pandoc.Shared
@@ -350,8 +351,7 @@ unescapeURL = T.concat . go . T.splitOn "\\"
350
351
351inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
352inlineCommands :: PandocMonad m => M.Map Text (LP m Inlines)
352inlineCommands = M.unions
353inlineCommands = M.unions
353− [ inlineLanguageCommands
354− , accentCommands tok
354+ [ accentCommands tok
355 , citationCommands inline
355 , citationCommands inline
356 , siunitxCommands tok
356 , siunitxCommands tok
357 , acronymCommands
357 , acronymCommands
@@ -359,6 +359,8 @@ inlineCommands = M.unions
359 , nameCommands
359 , nameCommands
360 , verbCommands
360 , verbCommands
361 , charCommands
361 , charCommands
362+ , inlineLanguageCommands tok
363+ , biblatexInlineCommands tok
362 , rest ]
364 , rest ]
363 where
365 where
364 rest = M.fromList
366 rest = M.fromList
@@ -373,12 +375,6 @@ inlineCommands = M.unions
373 , ("texttt", ttfamily)
375 , ("texttt", ttfamily)
374 , ("sout", extractSpaces strikeout <$> tok)
376 , ("sout", extractSpaces strikeout <$> tok)
375 , ("alert", skipopts >> spanWith ("",["alert"],[]) <$> tok) -- beamer
377 , ("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)
378 , ("textsuperscript", extractSpaces superscript <$> tok)
383 , ("textsubscript", extractSpaces subscript <$> tok)
379 , ("textsubscript", extractSpaces subscript <$> tok)
384 , ("textbf", extractSpaces strong <$> tok)
380 , ("textbf", extractSpaces strong <$> tok)
@@ -447,11 +443,6 @@ inlineCommands = M.unions
447 , ("toggletrue", braced >>= setToggle True)
443 , ("toggletrue", braced >>= setToggle True)
448 , ("togglefalse", braced >>= setToggle False)
444 , ("togglefalse", braced >>= setToggle False)
449 , ("iftoggle", try $ ifToggle >> inline)
445 , ("iftoggle", try $ ifToggle >> inline)
450− -- biblatex misc
451− , ("RN", romanNumeralUpper)
452− , ("Rn", romanNumeralLower)
453− -- babel
454− , ("foreignlanguage", foreignlanguage)
455 -- include
446 -- include
456 , ("input", rawInlineOr "input" $ include "input")
447 , ("input", rawInlineOr "input" $ include "input")
457 -- soul package
448 -- soul package
@@ -460,24 +451,6 @@ inlineCommands = M.unions
460 , ("uline", underline <$> tok)
451 , ("uline", underline <$> tok)
461 -- plain tex stuff that should just be passed through as raw tex
452 -- plain tex stuff that should just be passed through as raw tex
462 , ("ifdim", ifdim)
453 , ("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 ]
454 ]
482
455
483lettrine :: PandocMonad m => LP m Inlines
456lettrine :: PandocMonad m => LP m Inlines
@@ -502,26 +475,6 @@ alterStr :: (Text -> Text) -> Inline -> Inline
502alterStr f (Str xs) = Str (f xs)
475alterStr f (Str xs) = Str (f xs)
503alterStr _ x = x
476alterStr _ x = x
504
477
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−
525hyperlink :: PandocMonad m => LP m Inlines
478hyperlink :: PandocMonad m => LP m Inlines
526hyperlink = try $ do
479hyperlink = try $ do
527 src <- untokenize <$> braced
480 src <- untokenize <$> braced
@@ -542,31 +495,6 @@ hypertargetInline = try $ do
542 ils <- grouped inline
495 ils <- grouped inline
543 return $ spanWith (ref, [], []) ils
496 return $ spanWith (ref, [], []) ils
544
497
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−
570newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
498newToggle :: (Monoid a, PandocMonad m) => [Tok] -> LP m a
571newToggle name = do
499newToggle name = do
572 updateState $ \st ->
500 updateState $ \st ->
src/Text/Pandoc/Readers/LaTeX/Inline.hs+62−3
@@ -13,6 +13,7 @@ module Text.Pandoc.Readers.LaTeX.Inline
13 , verbCommands
13 , verbCommands
14 , charCommands
14 , charCommands
15 , nameCommands
15 , nameCommands
16+ , biblatexInlineCommands
16 , refCommands
17 , refCommands
17 , rawInlineOr
18 , rawInlineOr
18 , listingsLanguage
19 , listingsLanguage
@@ -23,15 +24,17 @@ import qualified Data.Map as M
23import Data.Text (Text)
24import Data.Text (Text)
24import qualified Data.Text as T
25import qualified Data.Text as T
25import Text.Pandoc.Builder
26import Text.Pandoc.Builder
27+import Text.Pandoc.Shared (toRomanNumeral, safeRead)
26import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
28import Text.Pandoc.Readers.LaTeX.Types (Tok (..), TokType (..))
27−import Control.Applicative (optional)
29+import Control.Applicative (optional, (<|>))
28−import Control.Monad (guard, mzero, mplus)
30+import Control.Monad (guard, mzero, mplus, unless)
29import Text.Pandoc.Class.PandocMonad (PandocMonad (..), translateTerm)
31import Text.Pandoc.Class.PandocMonad (PandocMonad (..), translateTerm)
30import Text.Pandoc.Readers.LaTeX.Parsing
32import Text.Pandoc.Readers.LaTeX.Parsing
31import Text.Pandoc.Extensions (extensionEnabled, Extension(..))
33import Text.Pandoc.Extensions (extensionEnabled, Extension(..))
32import Text.Pandoc.Parsing (getOption, updateState, getState, notFollowedBy,
34import Text.Pandoc.Parsing (getOption, updateState, getState, notFollowedBy,
33 manyTill, getInput, setInput, incSourceColumn,
35 manyTill, getInput, setInput, incSourceColumn,
34− option)
36+ option, many1)
37+import Data.Char (isDigit)
35import Text.Pandoc.Highlighting (fromListingsLanguage,)
38import Text.Pandoc.Highlighting (fromListingsLanguage,)
36import Data.Maybe (maybeToList)
39import Data.Maybe (maybeToList)
37import Text.Pandoc.Options (ReaderOptions(..))
40import Text.Pandoc.Options (ReaderOptions(..))
@@ -127,6 +130,31 @@ nlToSpace :: Char -> Char
127nlToSpace '\n' = ' '
130nlToSpace '\n' = ' '
128nlToSpace x = x
131nlToSpace x = x
129
132
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+
130
158
131
159
132verbCommands :: PandocMonad m => M.Map Text (LP m Inlines)
160verbCommands :: PandocMonad m => M.Map Text (LP m Inlines)
@@ -157,6 +185,12 @@ charCommands = M.fromList
157 , ("{", lit "{")
185 , ("{", lit "{")
158 , ("}", lit "}")
186 , ("}", lit "}")
159 , ("qed", lit "\a0\x25FB")
187 , ("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 "”"))
160 , ("/", pure mempty) -- italic correction
194 , ("/", pure mempty) -- italic correction
161 , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
195 , ("\\", linebreak <$ (do inTableCell <- sInTableCell <$> getState
162 guard $ not inTableCell
196 guard $ not inTableCell
@@ -185,6 +219,31 @@ charCommands = M.fromList
185 , ("hyp", lit "-")
219 , ("hyp", lit "-")
186 ]
220 ]
187
221
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+
188nameCommands :: PandocMonad m => M.Map Text (LP m Inlines)
247nameCommands :: PandocMonad m => M.Map Text (LP m Inlines)
189nameCommands = M.fromList
248nameCommands = M.fromList
190 [ ("figurename", doTerm Translations.Figure)
249 [ ("figurename", doTerm Translations.Figure)
src/Text/Pandoc/Readers/LaTeX/Lang.hs+29−1
@@ -15,15 +15,43 @@ module Text.Pandoc.Readers.LaTeX.Lang
15 ( setDefaultLanguage
15 ( setDefaultLanguage
16 , polyglossiaLangToBCP47
16 , polyglossiaLangToBCP47
17 , babelLangToBCP47
17 , babelLangToBCP47
18+ , inlineLanguageCommands
18 )
19 )
19where
20where
20import qualified Data.Map as M
21import qualified Data.Map as M
22+import Data.Text (Text)
21import qualified Data.Text as T
23import qualified Data.Text as T
24+import Text.Pandoc.Shared (extractSpaces)
22import Text.Pandoc.BCP47 (Lang(..), renderLang)
25import Text.Pandoc.BCP47 (Lang(..), renderLang)
23import Text.Pandoc.Class (PandocMonad(..), setTranslations)
26import Text.Pandoc.Class (PandocMonad(..), setTranslations)
24import Text.Pandoc.Readers.LaTeX.Parsing
27import Text.Pandoc.Readers.LaTeX.Parsing
25import Text.Pandoc.Parsing (updateState, option)
28import 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
27
55
28setDefaultLanguage :: PandocMonad m => LP m Blocks
56setDefaultLanguage :: PandocMonad m => LP m Blocks
29setDefaultLanguage = do
57setDefaultLanguage = do