TexMath: Export readTeXMath', which attends to display/inline.

Deprecate readTeXMath, and use readTeXMath' in all the writers.
Require texmath >= 0.6.5.
This commit is contained in:
John MacFarlane 2013-11-01 14:27:22 -07:00
parent ab0ffe6549
commit 0d95c15e83
14 changed files with 38 additions and 23 deletions

View file

@ -215,7 +215,7 @@ Library
old-locale >= 1 && < 1.1, old-locale >= 1 && < 1.1,
time >= 1.2 && < 1.5, time >= 1.2 && < 1.5,
HTTP >= 4000.0.5 && < 4000.3, HTTP >= 4000.0.5 && < 4000.3,
texmath >= 0.6.4 && < 0.7, texmath >= 0.6.5 && < 0.7,
xml >= 1.3.12 && < 1.4, xml >= 1.3.12 && < 1.4,
random >= 1 && < 1.1, random >= 1 && < 1.1,
extensible-exceptions >= 0.1 && < 0.2, extensible-exceptions >= 0.1 && < 0.2,

View file

@ -27,16 +27,30 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Conversion of TeX math to a list of 'Pandoc' inline elements. Conversion of TeX math to a list of 'Pandoc' inline elements.
-} -}
module Text.Pandoc.Readers.TeXMath ( readTeXMath ) where module Text.Pandoc.Readers.TeXMath ( readTeXMath, readTeXMath' ) where
import Text.Pandoc.Definition import Text.Pandoc.Definition
import Text.TeXMath import Text.TeXMath
-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines. -- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
-- Defaults to raw formula between @$@ characters if entire formula -- Defaults to raw formula between @$@ or @$$@ characters if entire formula
-- can't be converted. -- can't be converted.
readTeXMath' :: MathType
-> String -- ^ String to parse (assumes @'\n'@ line endings)
-> [Inline]
readTeXMath' mt inp = case texMathToPandoc dt inp of
Left _ -> [Str (delim ++ inp ++ delim)]
Right res -> res
where (dt, delim) = case mt of
DisplayMath -> (DisplayBlock, "$$")
InlineMath -> (DisplayInline, "$")
{-# DEPRECATED readTeXMath "Use readTeXMath' from Text.Pandoc.JSON instead" #-}
-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
-- Defaults to raw formula between @$@ characters if entire formula
-- can't be converted. (This is provided for backwards compatibility;
-- it is better to use @readTeXMath'@, which properly distinguishes
-- between display and inline math.)
readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings) readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings)
-> [Inline] -> [Inline]
readTeXMath inp = case texMathToPandoc DisplayInline inp of readTeXMath = readTeXMath' InlineMath
Left _ -> [Str ("$" ++ inp ++ "$")]
Right res -> res

View file

@ -281,8 +281,8 @@ inlineToDocbook opts (Math t str)
$ fixNS $ fixNS
$ removeAttr r $ removeAttr r
Left _ -> inlinesToDocbook opts Left _ -> inlinesToDocbook opts
$ readTeXMath str $ readTeXMath' t str
| otherwise = inlinesToDocbook opts $ readTeXMath str | otherwise = inlinesToDocbook opts $ readTeXMath' t str
where (dt, tagtype) = case t of where (dt, tagtype) = case t of
InlineMath -> (DisplayInline,"inlineequation") InlineMath -> (DisplayInline,"inlineequation")
DisplayMath -> (DisplayBlock,"informalequation") DisplayMath -> (DisplayBlock,"informalequation")

View file

@ -669,7 +669,7 @@ inlineToOpenXML opts (Math mathType str) = do
else DisplayInline else DisplayInline
case texMathToOMML displayType str of case texMathToOMML displayType str of
Right r -> return [r] Right r -> return [r]
Left _ -> inlinesToOpenXML opts (readTeXMath str) Left _ -> inlinesToOpenXML opts (readTeXMath' mathType str)
inlineToOpenXML opts (Cite _ lst) = inlinesToOpenXML opts lst inlineToOpenXML opts (Cite _ lst) = inlinesToOpenXML opts lst
inlineToOpenXML opts (Code attrs str) = inlineToOpenXML opts (Code attrs str) =
withTextProp (rStyle "VerbatimChar") withTextProp (rStyle "VerbatimChar")

View file

@ -685,14 +685,14 @@ inlineToHtml opts inline =
Right r -> return $ preEscapedString $ Right r -> return $ preEscapedString $
ppcElement conf r ppcElement conf r
Left _ -> inlineListToHtml opts Left _ -> inlineListToHtml opts
(readTeXMath str) >>= return . (readTeXMath' t str) >>= return .
(H.span ! A.class_ "math") (H.span ! A.class_ "math")
MathJax _ -> return $ H.span ! A.class_ "math" $ toHtml $ MathJax _ -> return $ H.span ! A.class_ "math" $ toHtml $
case t of case t of
InlineMath -> "\\(" ++ str ++ "\\)" InlineMath -> "\\(" ++ str ++ "\\)"
DisplayMath -> "\\[" ++ str ++ "\\]" DisplayMath -> "\\[" ++ str ++ "\\]"
PlainMath -> do PlainMath -> do
x <- inlineListToHtml opts (readTeXMath str) x <- inlineListToHtml opts (readTeXMath' t str)
let m = H.span ! A.class_ "math" $ x let m = H.span ! A.class_ "math" $ x
let brtag = if writerHtml5 opts then H5.br else H.br let brtag = if writerHtml5 opts then H5.br else H.br
return $ case t of return $ case t of

View file

@ -330,9 +330,10 @@ inlineToMan opts (Cite _ lst) =
inlineToMan _ (Code _ str) = inlineToMan _ (Code _ str) =
return $ text $ "\\f[C]" ++ escapeCode str ++ "\\f[]" return $ text $ "\\f[C]" ++ escapeCode str ++ "\\f[]"
inlineToMan _ (Str str) = return $ text $ escapeString str inlineToMan _ (Str str) = return $ text $ escapeString str
inlineToMan opts (Math InlineMath str) = inlineListToMan opts $ readTeXMath str inlineToMan opts (Math InlineMath str) =
inlineListToMan opts $ readTeXMath' InlineMath str
inlineToMan opts (Math DisplayMath str) = do inlineToMan opts (Math DisplayMath str) = do
contents <- inlineListToMan opts $ readTeXMath str contents <- inlineListToMan opts $ readTeXMath' DisplayMath str
return $ cr <> text ".RS" $$ contents $$ text ".RE" return $ cr <> text ".RS" $$ contents $$ text ".RE"
inlineToMan _ (RawInline f str) inlineToMan _ (RawInline f str)
| f == Format "man" = return $ text str | f == Format "man" = return $ text str

View file

@ -45,7 +45,7 @@ import Text.Pandoc.Pretty
import Control.Monad.State import Control.Monad.State
import qualified Data.Set as Set import qualified Data.Set as Set
import Text.Pandoc.Writers.HTML (writeHtmlString) import Text.Pandoc.Writers.HTML (writeHtmlString)
import Text.Pandoc.Readers.TeXMath (readTeXMath) import Text.Pandoc.Readers.TeXMath (readTeXMath')
import Text.HTML.TagSoup (renderTags, parseTags, isTagText, Tag(..)) import Text.HTML.TagSoup (renderTags, parseTags, isTagText, Tag(..))
import Network.URI (isURI) import Network.URI (isURI)
import Data.Default import Data.Default
@ -697,7 +697,7 @@ inlineToMarkdown opts (Math InlineMath str)
return $ "\\(" <> text str <> "\\)" return $ "\\(" <> text str <> "\\)"
| isEnabled Ext_tex_math_double_backslash opts = | isEnabled Ext_tex_math_double_backslash opts =
return $ "\\\\(" <> text str <> "\\\\)" return $ "\\\\(" <> text str <> "\\\\)"
| otherwise = inlineListToMarkdown opts $ readTeXMath str | otherwise = inlineListToMarkdown opts $ readTeXMath' InlineMath str
inlineToMarkdown opts (Math DisplayMath str) inlineToMarkdown opts (Math DisplayMath str)
| isEnabled Ext_tex_math_dollars opts = | isEnabled Ext_tex_math_dollars opts =
return $ "$$" <> text str <> "$$" return $ "$$" <> text str <> "$$"
@ -706,7 +706,7 @@ inlineToMarkdown opts (Math DisplayMath str)
| isEnabled Ext_tex_math_double_backslash opts = | isEnabled Ext_tex_math_double_backslash opts =
return $ "\\\\[" <> text str <> "\\\\]" return $ "\\\\[" <> text str <> "\\\\]"
| otherwise = (\x -> cr <> x <> cr) `fmap` | otherwise = (\x -> cr <> x <> cr) `fmap`
inlineListToMarkdown opts (readTeXMath str) inlineListToMarkdown opts (readTeXMath' DisplayMath str)
inlineToMarkdown opts (RawInline f str) inlineToMarkdown opts (RawInline f str)
| f == "html" || f == "markdown" || | f == "html" || f == "markdown" ||
(isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) = (isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) =

View file

@ -374,7 +374,7 @@ inlineToOpenDocument o ils
| SmallCaps l <- ils = withTextStyle SmallC $ inlinesToOpenDocument o l | SmallCaps l <- ils = withTextStyle SmallC $ inlinesToOpenDocument o l
| Quoted t l <- ils = inQuotes t <$> inlinesToOpenDocument o l | Quoted t l <- ils = inQuotes t <$> inlinesToOpenDocument o l
| Code _ s <- ils = withTextStyle Pre $ inTextStyle $ preformatted s | Code _ s <- ils = withTextStyle Pre $ inTextStyle $ preformatted s
| Math _ s <- ils = inlinesToOpenDocument o (readTeXMath s) | Math t s <- ils = inlinesToOpenDocument o (readTeXMath' t s)
| Cite _ l <- ils = inlinesToOpenDocument o l | Cite _ l <- ils = inlinesToOpenDocument o l
| RawInline f s <- ils = if f == "opendocument" || f == "html" | RawInline f s <- ils = if f == "opendocument" || f == "html"
then withTextStyle Pre $ inTextStyle $ preformatted s then withTextStyle Pre $ inTextStyle $ preformatted s

View file

@ -324,7 +324,7 @@ inlineToRTF (Quoted DoubleQuote lst) =
"\\u8220\"" ++ (inlineListToRTF lst) ++ "\\u8221\"" "\\u8220\"" ++ (inlineListToRTF lst) ++ "\\u8221\""
inlineToRTF (Code _ str) = "{\\f1 " ++ (codeStringToRTF str) ++ "}" inlineToRTF (Code _ str) = "{\\f1 " ++ (codeStringToRTF str) ++ "}"
inlineToRTF (Str str) = stringToRTF str inlineToRTF (Str str) = stringToRTF str
inlineToRTF (Math _ str) = inlineListToRTF $ readTeXMath str inlineToRTF (Math t str) = inlineListToRTF $ readTeXMath' t str
inlineToRTF (Cite _ lst) = inlineListToRTF lst inlineToRTF (Cite _ lst) = inlineListToRTF lst
inlineToRTF (RawInline f str) inlineToRTF (RawInline f str)
| f == Format "rtf" = str | f == Format "rtf" = str

View file

@ -1084,7 +1084,7 @@ These should not be escaped: \$ \\ \&gt; \[ \{
<listitem> <listitem>
<para> <para>
Heres some display math: Heres some display math:
$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$ $$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$
</para> </para>
</listitem> </listitem>
<listitem> <listitem>

View file

@ -439,7 +439,7 @@ Blah
<li><span class="math"><em>α</em> ∧ <em>ω</em></span></li> <li><span class="math"><em>α</em> ∧ <em>ω</em></span></li>
<li><span class="math">223</span></li> <li><span class="math">223</span></li>
<li><span class="math"><em>p</em></span>-Tree</li> <li><span class="math"><em>p</em></span>-Tree</li>
<li>Heres some display math: <br /><span class="math">$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$</span><br /></li> <li>Heres some display math: <br /><span class="math">$$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$</span><br /></li>
<li>Heres one that has a line break in it: <span class="math"><em>α</em>+<em>ω</em>×<em>x</em><sup>2</sup></span>.</li> <li>Heres one that has a line break in it: <span class="math"><em>α</em>+<em>ω</em>×<em>x</em><sup>2</sup></span>.</li>
</ul> </ul>
<p>These shouldnt be math:</p> <p>These shouldnt be math:</p>

View file

@ -572,7 +572,7 @@ Ellipses\&...and\&...and\&....
.IP \[bu] 2 .IP \[bu] 2
Here's some display math: Here's some display math:
.RS .RS
$\\frac{d}{dx}f(x)=\\lim_{h\\to 0}\\frac{f(x+h)\-f(x)}{h}$ $$\\frac{d}{dx}f(x)=\\lim_{h\\to 0}\\frac{f(x+h)\-f(x)}{h}$$
.RE .RE
.IP \[bu] 2 .IP \[bu] 2
Here's one that has a line break in it: Here's one that has a line break in it:

View file

@ -1418,7 +1418,7 @@ five.</text:p>
</text:list-item> </text:list-item>
<text:list-item> <text:list-item>
<text:p text:style-name="P51">Heres some display math: <text:p text:style-name="P51">Heres some display math:
$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$</text:p> $$\frac{d}{dx}f(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h}$$</text:p>
</text:list-item> </text:list-item>
<text:list-item> <text:list-item>
<text:p text:style-name="P51">Heres one that has a line break in it: <text:p text:style-name="P51">Heres one that has a line break in it:

View file

@ -269,7 +269,7 @@ quoted link
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\i \u945?}\u8197?\u8743?\u8197?{\i \u969?}\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\i \u945?}\u8197?\u8743?\u8197?{\i \u969?}\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab 223\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab 223\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\i p}-Tree\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab {\i p}-Tree\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Here\u8217's some display math: $\\frac\{d\}\{dx\}f(x)=\\lim_\{h\\to 0\}\\frac\{f(x+h)-f(x)\}\{h\}$\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Here\u8217's some display math: $$\\frac\{d\}\{dx\}f(x)=\\lim_\{h\\to 0\}\\frac\{f(x+h)-f(x)\}\{h\}$$\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Here\u8217's one that has a line break in it: {\i \u945?}\u8197?+\u8197?{\i \u969?}\u8197?\u215?\u8197?{\i x}{\super 2}.\sa180\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab Here\u8217's one that has a line break in it: {\i \u945?}\u8197?+\u8197?{\i \u969?}\u8197?\u215?\u8197?{\i x}{\super 2}.\sa180\par}
{\pard \ql \f0 \sa180 \li0 \fi0 These shouldn\u8217't be math:\par} {\pard \ql \f0 \sa180 \li0 \fi0 These shouldn\u8217't be math:\par}
{\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab To get the famous equation, write {\f1 $e = mc^2$}.\par} {\pard \ql \f0 \sa0 \li360 \fi-360 \bullet \tx360\tab To get the famous equation, write {\f1 $e = mc^2$}.\par}