git.iko.soy/typeable/pandoc
on master help sign in
Support numrange, numlist for siunitx.

See #6658.

commita157e1a6e029166a090544d23501db5e68e0bbe9 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 2 Sep 2020, 17:00 (6 years ago) parent83f0aca
2 files changed+27−9
src/Text/Pandoc/Readers/LaTeX.hs+3−1 src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+24−8
src/Text/Pandoc/Readers/LaTeX.hs+3−1
@@ -879,7 +879,9 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
879879 -- siuntix
880880 , ("si", skipopts *> dosi tok)
881881 , ("SI", doSI tok)
882− , ("SIrange", doSIrange tok)
882+ , ("SIrange", doSIrange True tok)
883+ , ("numrange", doSIrange False tok)
884+ , ("numlist", doSInumlist)
883885 , ("num", doSInum)
884886 , ("ang", doSIang)
885887 -- hyphenat
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+24−8
@@ -4,6 +4,7 @@ module Text.Pandoc.Readers.LaTeX.SIunitx
44 , doSI
55 , doSIrange
66 , doSInum
7+ , doSInumlist
78 , doSIang
89 )
910where
@@ -18,6 +19,7 @@ import qualified Data.Map as M
1819import Data.Char (isDigit)
1920import Data.Text (Text)
2021import qualified Data.Text as T
22+import Data.List (intersperse)
2123
2224dosi :: PandocMonad m => LP m Inlines -> LP m Inlines
2325dosi tok = grouped (siUnit tok) <|> siUnit tok
@@ -36,12 +38,24 @@ doSI tok = do
3638 unit]
3739
3840doSInum :: PandocMonad m => LP m Inlines
39−doSInum = do
40− skipopts
41− value <- untokenize <$> braced
41+doSInum = skipopts *> (tonum . untokenize <$> braced)
42+
43+tonum :: Text -> Inlines
44+tonum value =
4245 case runParser parseNum () "" value of
43− Left _ -> return $ text value
44− Right num -> return num
46+ Left _ -> text value
47+ Right num -> num
48+
49+doSInumlist :: PandocMonad m => LP m Inlines
50+doSInumlist = do
51+ skipopts
52+ xs <- map tonum . T.splitOn ";" . untokenize <$> braced
53+ case xs of
54+ [] -> return mempty
55+ [x] -> return x
56+ _ -> return $
57+ mconcat (intersperse (str "," <> space) (init xs)) <>
58+ text ", & " <> last xs
4559
4660parseNum :: Parser Text () Inlines
4761parseNum = mconcat <$> many parseNumPart
@@ -78,14 +92,16 @@ doSIang = do
7892 _ -> return mempty
7993
8094-- converts e.g. \SIrange{100}{200}{\ms} to "100 ms--200 ms"
81−doSIrange :: PandocMonad m => LP m Inlines -> LP m Inlines
82−doSIrange tok = do
95+doSIrange :: PandocMonad m => Bool -> LP m Inlines -> LP m Inlines
96+doSIrange includeUnits tok = do
8397 skipopts
8498 startvalue <- doSInum
8599 startvalueprefix <- option "" $ bracketed tok
86100 stopvalue <- doSInum
87101 stopvalueprefix <- option "" $ bracketed tok
88− unit <- dosi tok
102+ unit <- if includeUnits
103+ then dosi tok
104+ else return mempty
89105 return . mconcat $ [startvalueprefix,
90106 emptyOr160 startvalueprefix,
91107 startvalue,