git.iko.soy/typeable/pandoc
on master help sign in
LaTeX/siunitx: fix parsing of `\cubic` etc. See #6658.
commit4e990a8cf9207f2315d6a55a45c93c2857663316 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 20 May 2021, 10:12 (5 years ago) parentbc50582
2 files changed+53−35
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+50−35 test/command/6658.md+3
src/Text/Pandoc/Readers/LaTeX/SIunitx.hs+50−35
@@ -1,3 +1,4 @@
1+{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE OverloadedStrings #-}
23module Text.Pandoc.Readers.LaTeX.SIunitx
34 ( siunitxCommands )
@@ -154,40 +155,55 @@ doSIrange includeUnits tok = do
154155emptyOr160 :: Inlines -> Inlines
155156emptyOr160 x = if x == mempty then x else str "\160"
156157
157−siUnit :: PandocMonad m => LP m Inlines -> LP m Inlines
158−siUnit tok = try (do
159− Tok _ (CtrlSeq name) _ <- anyControlSeq
160− case name of
161− "square" -> do
162− unit <- siUnit tok
163− return $ unit <> superscript "2"
164− "cubic" -> do
165− unit <- siUnit tok
166− return $ unit <> superscript "3"
167− "raisetothe" -> do
168− n <- tok
169− unit <- siUnit tok
170− return $ unit <> superscript n
171− _ ->
172− case M.lookup name siUnitMap of
173− Just il ->
174− option il $
175− choice
176− [ (il <> superscript "2") <$ controlSeq "squared"
177− , (il <> superscript "3") <$ controlSeq "cubed"
178− , (\n -> il <> superscript n) <$> (controlSeq "tothe" *> tok)
179− ]
180− Nothing -> fail "not an siunit unit command")
181− <|> (lookAhead anyControlSeq >> tok)
182− <|> (do Tok _ Word t <- satisfyTok isWordTok
183− return $ str t)
184− <|> (symbol '^' *> (superscript <$> tok))
185− <|> (symbol '_' *> (subscript <$> tok))
186− <|> ("\xa0" <$ symbol '.')
187− <|> ("\xa0" <$ symbol '~')
188− <|> tok
189− <|> (do Tok _ _ t <- anyTok
190− return (str t))
158+siUnit :: forall m. PandocMonad m => LP m Inlines -> LP m Inlines
159+siUnit tok = mconcat <$> many1 siUnitPart
160+ where
161+ siUnitPart :: LP m Inlines
162+ siUnitPart =
163+ (siPrefix <*> siUnitPart)
164+ <|> (do u <- siBase <|> tok
165+ option u $ siSuffix <*> pure u)
166+ siPrefix :: LP m (Inlines -> Inlines)
167+ siPrefix =
168+ (do _ <- controlSeq "per"
169+ skipopts -- TODO handle option
170+ return (str "/" <>))
171+ <|> (do _ <- controlSeq "square"
172+ skipopts
173+ return (<> superscript "2"))
174+ <|> (do _ <- controlSeq "cubic"
175+ skipopts
176+ return (<> superscript "3"))
177+ <|> (do _ <- controlSeq "raisetothe"
178+ skipopts
179+ n <- tok
180+ return (<> superscript n))
181+ siSuffix :: LP m (Inlines -> Inlines)
182+ siSuffix =
183+ (do _ <- controlSeq "squared"
184+ skipopts
185+ return (<> superscript "2"))
186+ <|> (do _ <- controlSeq "cubed"
187+ skipopts
188+ return (<> superscript "3"))
189+ <|> (do _ <- controlSeq "tothe"
190+ skipopts
191+ n <- tok
192+ return (<> superscript n))
193+ siBase :: LP m Inlines
194+ siBase = mconcat <$> many1
195+ ((try
196+ (do Tok _ (CtrlSeq name) _ <- anyControlSeq
197+ case M.lookup name siUnitMap of
198+ Just il -> pure il
199+ Nothing -> fail "not a unit command"))
200+ <|> (do Tok _ Word t <- satisfyTok isWordTok
201+ return $ str t)
202+ <|> (symbol '^' *> (superscript <$> tok))
203+ <|> (symbol '_' *> (subscript <$> tok))
204+ <|> (str "\xa0" <$ symbol '.')
205+ <|> (str "\xa0" <$ symbol '~')
206+ )
191207
192208siUnitMap :: M.Map Text Inlines
193209siUnitMap = M.fromList
@@ -347,7 +363,6 @@ siUnitMap = M.fromList
347363 , ("Pa", str "Pa")
348364 , ("pascal", str "Pa")
349365 , ("percent", str "%")
350− , ("per", str "/")
351366 , ("peta", str "P")
352367 , ("pico", str "p")
353368 , ("planckbar", emph (str "\x210f"))
test/command/6658.md+3
@@ -5,8 +5,11 @@ pandoc -f latex
55\num{.3e45}
66
77\ang{+10;+3;}
8+
9+\si{\gram\per\cubic\centi\metre}
810^D
911<p>10.0 ± 3.3 ms</p>
1012<p>0.3 × 10<sup>45</sup></p>
1113<p>10°3′</p>
14+<p>g/cm<sup>3</sup></p>
1215```