@@ -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