git.iko.soy/typeable/pandoc
on master help sign in
LaTeX reader: support `\num` from siunitx.
0a98648John MacFarlane
src/Text/Pandoc/Readers/LaTeX.hs+2−1
@@ -877,8 +877,9 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
877 , ("acfp", doAcronymPlural "full")
877 , ("acfp", doAcronymPlural "full")
878 , ("acsp", doAcronymPlural "abbrv")
878 , ("acsp", doAcronymPlural "abbrv")
879 -- siuntix
879 -- siuntix
880− , ("SI", dosiunitx tok)
880+ , ("SI", doSI tok)
881 , ("SIrange", doSIrange tok)
881 , ("SIrange", doSIrange tok)
882+ , ("num", doSInum)
882 -- hyphenat
883 -- hyphenat
883 , ("bshyp", lit "\\\173")
884 , ("bshyp", lit "\\\173")
884 , ("fshyp", lit "/\173")
885 , ("fshyp", lit "/\173")
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+38−5
@@ -1,23 +1,25 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2module Text.Pandoc.Readers.LaTeX.SIunitx
2module Text.Pandoc.Readers.LaTeX.SIunitx
3− ( dosiunitx
3+ ( doSI
4 , doSIrange
4 , doSIrange
5+ , doSInum
5 )
6 )
6where
7where
7import Text.Pandoc.Builder
8import Text.Pandoc.Builder
8import Text.Pandoc.Readers.LaTeX.Parsing
9import Text.Pandoc.Readers.LaTeX.Parsing
9import Text.Pandoc.Readers.LaTeX.Types
10import Text.Pandoc.Readers.LaTeX.Types
10import Text.Pandoc.Class
11import 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,
13 optional, space, spaces, withRaw, (<|>))
13 optional, space, spaces, withRaw, (<|>))
14import Control.Applicative ((<|>))
14import Control.Applicative ((<|>))
15import qualified Data.Map as M
15import qualified Data.Map as M
16+import Data.Char (isDigit)
16import Data.Text (Text)
17import Data.Text (Text)
18+import qualified Data.Text as T
17
19
18-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
20-- converts e.g. \SI{1}[\$]{} to "$ 1" or \SI{1}{\euro} to "1 €"
19−dosiunitx :: PandocMonad m => LP m Inlines -> LP m Inlines
21+doSI :: PandocMonad m => LP m Inlines -> LP m Inlines
20−dosiunitx tok = do
22+doSI tok = do
21 skipopts
23 skipopts
22 value <- tok
24 value <- tok
23 valueprefix <- option "" $ bracketed tok
25 valueprefix <- option "" $ bracketed tok
@@ -28,6 +30,37 @@ dosiunitx tok = do
28 emptyOr160 unit,
30 emptyOr160 unit,
29 unit]
31 unit]
30
32
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+
31-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
64-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
32doSIrange :: PandocMonad m => LP m Inlines -> LP m Inlines
65doSIrange :: PandocMonad m => LP m Inlines -> LP m Inlines
33doSIrange tok = do
66doSIrange tok = do