texmath 0.6.5.2 → 0.6.6
raw patch · 8 files changed
+151/−34 lines, 8 files
Files
- README.markdown +37/−0
- Text/TeXMath/Macros.hs +29/−4
- Text/TeXMath/Pandoc.hs +1/−1
- Text/TeXMath/Parser.hs +50/−16
- changelog +8/−0
- tests/macros.omml +5/−1
- tests/macros.xhtml +17/−10
- texmath.cabal +4/−2
+ README.markdown view
@@ -0,0 +1,37 @@+texmath+=======++texmath is a Haskell library for converting LaTeX math to+MathML and OMML (the math format used in Microsoft Office).+It is used by [pandoc] and [gitit].++[pandoc]: http://github.com/jgm/pandoc+[gitit]: http://gitit.net++You can [try it out online here](http://johnmacfarlane.net/texmath.html).+(Note that the math it produces will be rendered correctly only+if your browser supports MathML. Firefox does; Safari and Chrome do not.)++By default, only the Haskell library is installed. To install a+test program, `texmath`, use the `test` Cabal flag:++ cabal install -ftest++`texmath` reads a LaTeX formula from stdin and writes a+standalone xhtml file to stdout. You can run the test suite thus:++ cd tests+ sh runtests.sh++Macro definitions may be included before the formula.++The `cgi` Cabal flag will cause a CGI binary, `texmath-cgi`, to be+produced:++ cabal install -fcgi++The file `cgi/texmath.html` contains an example of how it can+be used.++Thanks to John Lenz for many contributions.+
Text/TeXMath/Macros.hs view
@@ -58,7 +58,7 @@ -- | Parses a @\\newcommand@ or @\\renewcommand@ macro definition and -- returns a 'Macro'. pMacroDefinition :: GenParser Char st Macro-pMacroDefinition = newcommand+pMacroDefinition = newcommand <|> declareMathOperator -- | Skip whitespace and comments. pSkipSpaceComments :: GenParser Char st ()@@ -103,8 +103,10 @@ newcommand :: GenParser Char st Macro newcommand = try $ do char '\\'- optional $ try $ string "re"- string "newcommand"+ -- we ignore differences between these so far:+ try (string "newcommand")+ <|> try (string "renewcommand")+ <|> string "providecommand" pSkipSpaceComments name <- inbraces <|> ctrlseq guard (take 1 name == "\\")@@ -138,7 +140,30 @@ let args' = case opt of Just x -> x : args Nothing -> args- return $ apply args' body+ return $ apply args' $ "{" ++ body ++ "}"++-- | Parser for \DeclareMathOperator(*) command.+declareMathOperator :: GenParser Char st Macro+declareMathOperator = try $ do+ string "\\DeclareMathOperator"+ pSkipSpaceComments+ star <- option "" (string "*")+ pSkipSpaceComments+ name <- inbraces <|> ctrlseq+ guard (take 1 name == "\\")+ let name' = drop 1 name+ pSkipSpaceComments+ body <- inbraces <|> ctrlseq+ let defn = "\\DeclareMathOperator" ++ star ++ "{" ++ name ++ "}" +++ "{" ++ body ++ "}"+ return $ Macro defn $ try $ do+ char '\\'+ string name'+ when (all isLetter name') $+ notFollowedBy letter+ pSkipSpaceComments+ return $ "\\operatorname" ++ star ++ "{" ++ body ++ "}"+ apply :: [String] -> String -> String apply args ('#':d:xs) | isDigit d =
Text/TeXMath/Pandoc.hs view
@@ -43,7 +43,7 @@ expToInlines (EIdentifier s) = Just [Emph [Str s]] expToInlines (EMathOperator s) = Just [Str s] expToInlines (ESymbol t s) = Just $ addSpace t (Str s)- where addSpace Op x = [x, thinspace]+ where addSpace Op x = [x] addSpace Bin x = [medspace, x, medspace] addSpace Rel x = [widespace, x, widespace] addSpace Pun x = [x, thinspace]
Text/TeXMath/Parser.hs view
@@ -85,13 +85,44 @@ expr :: TP Exp expr = do optional (try $ symbol "\\displaystyle")- a <- expr1+ (a, convertible) <- try (braces operatorname) -- needed because macros add {}+ <|> (expr1 >>= \e -> return (e, False))+ <|> operatorname limits <- limitsIndicator- subSup limits a <|> superOrSubscripted limits a <|> return a+ subSup limits convertible a <|> superOrSubscripted limits convertible a <|> return a +-- | Parser for \operatorname command.+-- Returns a tuple of EMathOperator name and Bool depending on the flavor+-- of the command:+--+-- - True for convertible operator (\operator*)+--+-- - False otherwise+operatorname :: TP (Exp, Bool)+operatorname = try $ do+ symbol "\\operatorname"+ convertible <- (char '*' >> spaces >> return True) <|> return False+ op <- liftM expToOperatorName texToken+ maybe pzero (\s -> return (EMathOperator s, convertible)) op++-- | Converts identifiers, symbols and numbers to a flat string.+-- Returns Nothing if the expression contains anything else.+expToOperatorName :: Exp -> Maybe String+expToOperatorName e = case e of+ EGrouped xs -> liftM concat $ mapM fl xs+ _ -> fl e+ where fl f = case f of+ EIdentifier s -> Just s+ -- handle special characters+ ESymbol _ "\x2212" -> Just "-"+ ESymbol _ "\x02B9" -> Just "'"+ ESymbol _ s -> Just s+ ENumber s -> Just s+ _ -> Nothing+ bareSubSup :: TP Exp-bareSubSup = subSup Nothing (EIdentifier "")- <|> superOrSubscripted Nothing (EIdentifier "")+bareSubSup = subSup Nothing False (EIdentifier "")+ <|> superOrSubscripted Nothing False (EIdentifier "") limitsIndicator :: TP (Maybe Bool) limitsIndicator =@@ -100,7 +131,7 @@ <|> return Nothing inbraces :: TP Exp-inbraces = liftM EGrouped (braces $ many $ notFollowedBy (char '}') >> expr)+inbraces = liftM EGrouped $ braces $ many $ notFollowedBy (char '}') >> expr texToken :: TP Exp texToken = texSymbol <|> inbraces <|> inbrackets <|>@@ -165,6 +196,7 @@ endLine = try $ do symbol "\\\\" optional inbrackets -- can contain e.g. [1.0in] for a line height, not yet supported+ optional $ try $ symbol "\\hline" -- we don't represent the line, but it shouldn't crash parsing return '\n' arrayLine :: TP ArrayLine@@ -271,32 +303,32 @@ isUnderover (EUnder _ (ESymbol Accent "\x23B5")) = True -- \underbracket isUnderover _ = False -subSup :: Maybe Bool -> Exp -> TP Exp-subSup limits a = try $ do+subSup :: Maybe Bool -> Bool -> Exp -> TP Exp+subSup limits convertible a = try $ do let sub1 = symbol "_" >> expr1 let sup1 = symbol "^" >> expr1 (b,c) <- try (do {m <- sub1; n <- sup1; return (m,n)}) <|> (do {n <- sup1; m <- sub1; return (m,n)}) return $ case limits of Just True -> EUnderover a b c- Nothing | isConvertible a -> EDownup a b c+ Nothing | convertible || isConvertible a -> EDownup a b c | isUnderover a -> EUnderover a b c _ -> ESubsup a b c -superOrSubscripted :: Maybe Bool -> Exp -> TP Exp-superOrSubscripted limits a = try $ do+superOrSubscripted :: Maybe Bool -> Bool -> Exp -> TP Exp+superOrSubscripted limits convertible a = try $ do c <- oneOf "^_" spaces b <- expr case c of '^' -> return $ case limits of Just True -> EOver a b- Nothing | isConvertible a -> EUp a b+ Nothing | convertible || isConvertible a -> EUp a b | isUnderover a -> EOver a b _ -> ESuper a b '_' -> return $ case limits of Just True -> EUnder a b- Nothing | isConvertible a -> EDown a b+ Nothing | convertible || isConvertible a -> EDown a b | isUnderover a -> EUnder a b _ -> ESub a b _ -> pzero@@ -533,11 +565,13 @@ ("+", ESymbol Bin "+") , ("-", ESymbol Bin "\x2212") , ("*", ESymbol Bin "*")+ , ("@", ESymbol Ord "@") , (",", ESymbol Pun ",")- , (".", ESymbol Pun ".")+ , (".", ESymbol Ord ".") , (";", ESymbol Pun ";")- , (":", ESymbol Pun ":")- , ("?", ESymbol Pun "?")+ , (":", ESymbol Rel ":")+ , ("\\colon", ESymbol Pun ":")+ , ("?", ESymbol Ord "?") , (">", ESymbol Rel ">") , ("<", ESymbol Rel "<") , ("!", ESymbol Ord "!")@@ -550,7 +584,7 @@ , ("\\mid", ESymbol Bin "\x2223") , ("\\parallel", ESymbol Rel "\x2225") , ("\\backslash", ESymbol Bin "\x2216")- , ("/", ESymbol Bin "/")+ , ("/", ESymbol Ord "/") , ("\\setminus", ESymbol Bin "\\") , ("\\times", ESymbol Bin "\x00D7") , ("\\alpha", EIdentifier "\x03B1")
+ changelog view
@@ -0,0 +1,8 @@+texmath (0.6.6)++ * Insert braces around macro expansions to prevent breakage (#7).+ * Support \operatorname and \DeclareMathOperator (rekka) (#17).+ * Support \providecommand (#15).+ * Fixed spacing bugs in pandoc rendering (#24).+ * Ignore \hline at end of array row instead of failing (#19).+
tests/macros.omml view
@@ -6,7 +6,11 @@ <m:oMath> <m:r> <m:rPr />- <m:t>25</m:t>+ <m:t>2</m:t>+ </m:r>+ <m:r>+ <m:rPr />+ <m:t>5</m:t> </m:r> <m:r> <m:rPr />
tests/macros.xhtml view
@@ -6,16 +6,23 @@ <body> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> <mrow>- <mn>25</mn>- <mi>y</mi>- <mo>+</mo>- <mn>3</mn>- <mi>x</mi>- <mo>+</mo>- <mn>3</mn>- <mi>x</mi>- <mo>+</mo>- <mi>α</mi>+ <mn>2</mn>+ <mn>5</mn>+ <mrow>+ <mi>y</mi>+ <mo>+</mo>+ <mn>3</mn>+ </mrow>+ <mrow>+ <mi>x</mi>+ <mo>+</mo>+ <mn>3</mn>+ </mrow>+ <mrow>+ <mi>x</mi>+ <mo>+</mo>+ <mi>α</mi>+ </mrow> </mrow> </math> </body>
texmath.cabal view
@@ -1,5 +1,5 @@ Name: texmath-Version: 0.6.5.2+Version: 0.6.6 Cabal-Version: >= 1.6 Build-type: Custom Synopsis: Conversion of LaTeX math formulas to MathML or OMML.@@ -23,7 +23,9 @@ Author: John MacFarlane Maintainer: jgm@berkeley.edu Homepage: http://github.com/jgm/texmath-Data-Files: cgi/texmath.xhtml,+Extra-source-files: README.markdown,+ changelog,+ cgi/texmath.xhtml, tests/runtests.sh, tests/01.tex, tests/01.xhtml, tests/01.omml, tests/02.tex, tests/02.xhtml, tests/02.omml,