Fix #4499: add mbox and hbox handling to LaTeX reader (#5586)

When `+raw_tex` is enabled, these are passed through literally.
Otherwise, they are handled in a way that emulates LaTeX's behavior.
This commit is contained in:
Vasily Alferov 2019-07-14 02:55:41 +03:00 committed by John MacFarlane
parent 7bc9eab846
commit f6c92c7523
2 changed files with 98 additions and 1 deletions

View file

@ -29,7 +29,7 @@ import Prelude
import Control.Applicative (many, optional, (<|>))
import Control.Monad
import Control.Monad.Except (throwError)
import Data.Char (isDigit, isLetter, toLower, toUpper)
import Data.Char (isDigit, isLetter, toLower, toUpper, chr)
import Data.Default
import Data.List (intercalate, isPrefixOf)
import qualified Data.Map as M
@ -893,6 +893,8 @@ inlineCommands = M.union inlineLanguageCommands $ M.fromList
, ("cref", rawInlineOr "cref" $ doref "ref") -- from cleveref.sty
, ("vref", rawInlineOr "vref" $ doref "ref+page") -- from varioref.sty
, ("eqref", rawInlineOr "eqref" $ doref "eqref") -- from amsmath.sty
, ("mbox", rawInlineOr "mbox" $ processHBox <$> tok)
, ("hbox", rawInlineOr "hbox" $ processHBox <$> tok)
, ("lettrine", optional opt >> extractSpaces (spanWith ("",["lettrine"],[])) <$> tok)
, ("(", mathInline . toksToString <$> manyTill anyTok (controlSeq ")"))
, ("[", mathDisplay . toksToString <$> manyTill anyTok (controlSeq "]"))
@ -1287,6 +1289,14 @@ rawInlineOr name' fallback = do
then rawInline "latex" <$> getRawCommand name' ("\\" <> name')
else fallback
processHBox :: Inlines -> Inlines
processHBox = walk convert
where
convert Space = Str [chr 160] -- non-breakable space
convert SoftBreak = Str [chr 160] -- non-breakable space
convert LineBreak = Str ""
convert x = x
getRawCommand :: PandocMonad m => Text -> Text -> LP m String
getRawCommand name txt = do
(_, rawargs) <- withRaw $

87
test/command/4499.md Normal file
View file

@ -0,0 +1,87 @@
```
% pandoc -f latex -t html
\mbox{abc def} ghi
^D
<p>abc def ghi</p>
```
```
% pandoc -f latex+raw_tex -t native
\mbox{abc def}
^D
[Para [RawInline (Format "latex") "\\mbox{abc def}"]]
```
```
% pandoc -f latex -t html
abc \mbox{\textit{def ghi} jkl} mno
^D
<p>abc <em>def ghi</em> jkl mno</p>
```
```
% pandoc -f latex -t html
abc \mbox{def \\ ghi} jkl
^D
<p>abc defghi jkl</p>
```
```
% pandoc -f latex -t html
abc \mbox{def
ghi}
^D
<p>abc def ghi</p>
```
```
% pandoc -f latex -t html
abc \mbox{def \textit{ghi \\ jkl}
mno} pqr
^D
<p>abc def <em>ghijkl</em> mno pqr</p>
```
```
% pandoc -f latex -t html
\hbox{abc def} ghi
^D
<p>abc def ghi</p>
```
```
% pandoc -f latex+raw_tex -t native
\hbox{abc def}
^D
[Para [RawInline (Format "latex") "\\hbox{abc def}"]]
```
```
% pandoc -f latex -t html
abc \hbox{\textit{def ghi} jkl} mno
^D
<p>abc <em>def ghi</em> jkl mno</p>
```
```
% pandoc -f latex -t html
abc \hbox{def \\ ghi} jkl
^D
<p>abc defghi jkl</p>
```
```
% pandoc -f latex -t html
abc \hbox{def
ghi}
^D
<p>abc def ghi</p>
```
```
% pandoc -f latex -t html
abc \hbox{def \textit{ghi \\ jkl}
mno} pqr
^D
<p>abc def <em>ghijkl</em> mno pqr</p>
```