git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: support `\num` from siunitx.
commit0a98648c1a64d382cdd577ecc1fc2245c6eb57e2 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 2 Sep 2020, 13:05 (6 years ago) parent529eb69
2 files changed+40−6
src/Text/Pandoc/Readers/LaTeX.hs+2−1 src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+38−5
src/Text/Pandoc/Readers/LaTeX.hs+2−1
@@ -877,8 +877,9 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
877877 , ("acfp", doAcronymPlural "full")
878878 , ("acsp", doAcronymPlural "abbrv")
879879 -- siuntix
880− , ("SI", dosiunitx tok)
880+ , ("SI", doSI tok)
881881 , ("SIrange", doSIrange tok)
882+ , ("num", doSInum)
882883 -- hyphenat
883884 , ("bshyp", lit "\\\173")
884885 , ("fshyp", lit "/\173")
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+38−5
@@ -1,23 +1,25 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Text.Pandoc.Readers.LaTeX.SIunitx
3− ( dosiunitx
3+ ( doSI
44 , doSIrange
5+ , doSInum
56 )
67where
78import Text.Pandoc.Builder
89import Text.Pandoc.Readers.LaTeX.Parsing
910import Text.Pandoc.Readers.LaTeX.Types
1011import Text.Pandoc.Class
11−import Control.Monad (mzero)
12−import Text.Pandoc.Parsing hiding (blankline, many, mathDisplay, mathInline,
12+import Text.Pandoc.Parsing hiding (blankline, mathDisplay, mathInline,
1313 optional, space, spaces, withRaw, (<|>))
1414import Control.Applicative ((<|>))
1515import qualified Data.Map as M
16+import Data.Char (isDigit)
1617import Data.Text (Text)
18+import qualified Data.Text as T
1719
1820-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
19−dosiunitx :: PandocMonad m => LP m Inlines -> LP m Inlines
20−dosiunitx tok = do
21+doSI :: PandocMonad m => LP m Inlines -> LP m Inlines
22+doSI tok = do
2123 skipopts
2224 value <- tok
2325 valueprefix <- option "" $ bracketed tok
@@ -28,6 +30,37 @@ dosiunitx tok = do
2830 emptyOr160 unit,
2931 unit]
3032
33+doSInum :: PandocMonad m => LP m Inlines
34+doSInum = do
35+ skipopts
36+ value <- untokenize <$> braced
37+ case runParser parseNum () "" value of
38+ Left _ -> return $ text value
39+ Right num -> return num
40+
41+parseNum :: Parser Text () Inlines
42+parseNum = mconcat <$> many parseNumPart
43+
44+parseNumPart :: Parser Text () Inlines
45+parseNumPart =
46+ parseDecimalNum <|>
47+ parseComma <|>
48+ parsePlusMinus <|>
49+ parseI <|>
50+ parseExp <|>
51+ parseX <|>
52+ parseSpace
53+ where
54+ parseDecimalNum =
55+ str . T.pack <$> many1 (satisfy (\c -> isDigit c || c == '.'))
56+ parseComma = str "." <$ char ','
57+ parsePlusMinus = str "\xa0\xb1\xa0" <$ try (string "+-")
58+ parseI = str "i" <$ char 'i'
59+ parseX = str "\xa0\xd7\xa0" <$ char 'x'
60+ parseExp = (\n -> str ("\xa0\xd7\xa0" <> "10") <> superscript n)
61+ <$> (char 'e' *> parseDecimalNum)
62+ parseSpace = mempty <$ skipMany1 (char ' ')
63+
3164-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
3265doSIrange :: PandocMonad m => LP m Inlines -> LP m Inlines
3366doSIrange tok = do