texmath 0.12.9 → 0.12.10
raw patch · 8 files changed
+89/−32 lines, 8 filesdep +wai-extra
Dependencies added: wai-extra
Files
- README.md +9/−8
- changelog.md +16/−0
- server/Main.hs +25/−16
- server/texmath.html +1/−1
- src/Text/TeXMath/Readers/TeX.hs +19/−6
- test/regression/258.test +13/−0
- test/regression/259.test +4/−0
- texmath.cabal +2/−1
README.md view
@@ -42,8 +42,6 @@ `texmath` will behave as a CGI script when called under the name `texmath-cgi` (e.g. through a symbolic link).-The file `cgi/texmath.html` contains an example of how it can-be used. But it is also possible to compile a full webserver with a JSON API. To do this, set the `server` cabal flag, e.g.@@ -57,7 +55,7 @@ Sample of use, with `httpie`: ```-% http --verbose localhost:3000/convert text='2^2' from=tex to=mathml display:=false Accept:'text/plain'+% http --verbose localhost:3000 text='2^2' from=tex to=mathml display:=false Accept:'text/plain' POST /convert HTTP/1.1 Accept: text/plain Accept-Encoding: gzip, deflate@@ -90,12 +88,15 @@ ``` Possible values for `from` are `tex`, `mathml`, and `omml`.-Possible values for `to` are `tex`, `mathml`, `omml`, `eqn`, and-`pandoc` (JSON-encoded Pandoc).+Possible values for `to` are `tex`, `mathml`, `omml`, `eqn`,+`typst` and `pandoc` (JSON-encoded Pandoc). -Alternatively, you can use the `convert-batch` endpoint to pass-in a JSON-encoded list of conversions and get back a JSON-encoded-list of results.+Alternatively, you can use the `/batch` endpoint, passing+in in a JSON-encoded list of conversions and getting back a+JSON-encoded list of results.++If you rename `pandoc-server` to `pandoc-server.cgi`, it will+function as a CGI program that accepts POST requests. # Generating lookup tables
changelog.md view
@@ -1,3 +1,19 @@+texmath (0.12.10)++ * texmath-server:++ + Change endpoints: `/convert` to root, and `/convert-batch` to `/batch`.+ + Allow running as CGI if renamed pandoc-server.cgi. In this mode it+ accepts JSON content with POST requests or parameters with GET requests,+ just like pandoc-server itself.++ * TeX reader:++ + Fix parsing bug with comment at beginning of braced (#258).+ + Support negative numbers in `\hspace` (#259).+ + Allow decimals in `\hspace` (#259).+ + Support `\quad` in `\text` (#260).+ texmath (0.12.9) * Better handling of primes in eqn, typst, and tex writers.
server/Main.hs view
@@ -6,8 +6,10 @@ {-# LANGUAGE OverloadedStrings #-} module Main where -import Network.Wai.Handler.Warp+import qualified Network.Wai.Handler.Warp as Warp import Network.Wai.Logger (withStdoutLogger)+import qualified Network.Wai.Handler.CGI as CGI+import Network.Wai.Middleware.Timeout (timeout) import Data.Aeson import Data.Aeson.TH import Data.Maybe (fromMaybe)@@ -19,6 +21,7 @@ import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy.Encoding as TLE import Text.XML.Light (ppElement)+import System.Environment (getProgName) import Options.Applicative import Safe (readMay) @@ -76,25 +79,32 @@ main :: IO () main = do- let options = info (optsSpec <**> helper)- ( fullDesc- <> progDesc "Run a server for texmath"- <> header "texmath-server - an HTTP server for texmath" )- opts <- execParser options- putStrLn $ "Starting server on port " <> show (port opts)- withStdoutLogger $ \logger -> do- let settings = setPort (port opts) $ setLogger logger defaultSettings- runSettings settings app--- This is the API. The "/convert" endpoint takes a request body+ prg <- getProgName+ case prg of+ "texmath-server.cgi" -> CGI.run (timeout 2 app)+ _ -> do+ let options = info (optsSpec <**> helper)+ ( fullDesc+ <> progDesc "Run a server for texmath"+ <> header "texmath-server - an HTTP server for texmath" )+ opts <- execParser options+ putStrLn $ "Starting server on port " <> show (port opts)+ withStdoutLogger $ \logger -> do+ let settings = Warp.setPort (port opts) $ Warp.setLogger logger Warp.defaultSettings+ Warp.runSettings settings app++-- This is the API. The root endpoint takes a request body -- consisting of a JSON-encoded Params structure and responds to -- Get requests with either plain text or JSON, depending on the--- Accept header.+-- Accept header. Alternatively, the "/batch" endpoint may be+-- used, accepting a JSON-encoded array of Params and returning+-- an array of results. type API =- "convert" :> ReqBody '[JSON] Params :> Post '[PlainText, JSON] Text+ ReqBody '[JSON] Params :> Post '[PlainText, JSON] Text :<|>- "convert" :> QueryParam "text" Text :> QueryParam "from" Format :> QueryParam "to" Format :> QueryFlag "display" :> Get '[PlainText] Text+ QueryParam "text" Text :> QueryParam "from" Format :> QueryParam "to" Format :> QueryFlag "display" :> Get '[PlainText] Text :<|>- "convert-batch" :> ReqBody '[JSON] [Params] :> Post '[JSON] [Text]+ "batch" :> ReqBody '[JSON] [Params] :> Post '[JSON] [Text] app :: Application app = serve api server@@ -133,4 +143,3 @@ handleErr (Right t) = return t handleErr (Left err) = throwError $ err500 { errBody = TLE.encodeUtf8 $ TL.fromStrict err }-
server/texmath.html view
@@ -10,7 +10,7 @@ <script> var postConvert = function(body, onSuccess, onError) { var xhr = new XMLHttpRequest();- xhr.open('POST', '/texmath/convert', true);+ xhr.open('POST', 'https://texmath.johnmacfarlane.net/', true); xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () {
src/Text/TeXMath/Readers/TeX.hs view
@@ -31,6 +31,7 @@ import qualified Data.Map as M import qualified Data.Text as T import Data.Text (Text)+import Data.Ratio ((%)) import Data.Maybe (catMaybes, fromJust, mapMaybe) import Text.Parsec hiding (label) import Text.Parsec.Error@@ -358,7 +359,7 @@ many1Exp = manyExp' True inbraces :: TP Exp-inbraces = braces (manyExp expr)+inbraces = braces (ignorable *> manyExp expr) texToken :: TP Exp texToken = texSymbol <|> inbraces <|> texChar@@ -718,17 +719,28 @@ _ -> mzero xspace "\\hspace" = do braces $ do- len <- many1 digit- scaleFactor <-+ neg <- option 1 $ (-1) <$ char '-'+ as <- option "" $ many1 digit+ bs <- option "" $ char '.' *> many1 digit+ let denominator = 10^(length bs)+ as' <- if null as then pure 0 else stringToInteger as+ bs' <- if null bs then pure 0 else stringToInteger bs+ let numerator = (as' * denominator) + bs'+ let n = neg * (numerator % denominator)+ scaleFactor <- lexeme $ 1 <$ (string "em") <|> (1/12) <$ (string "pt") <|> 6 <$ (string "in") <|> (50/21) <$ (string "cm")- case reads len of- ((n :: Integer,[]):_) -> return $ ESpace (fromIntegral n * scaleFactor)- _ -> mzero+ return $ ESpace (n * scaleFactor) xspace _ = mzero +stringToInteger :: String -> TP Integer+stringToInteger s =+ case reads s of+ ((n :: Integer, []):_) -> pure n+ _ -> fail $ "Could not read " <> s <> " as Integer."+ mathop :: Text -> TP Exp mathop c = case c of@@ -993,6 +1005,7 @@ , ("\\v", option "v" $ try $ hacek <$> tok) , ("\\u", option "u" $ try $ breve <$> tok) , ("\\ ", return " ")+ , ("\\quad", return "\x2000") ] parseC :: TP Text
+ test/regression/258.test view
@@ -0,0 +1,13 @@+<<< tex+e^{+ % below is the complex part:+ \sum_i^n i^2 }+>>> native+[ ESuper+ (EIdentifier "e")+ (EGrouped+ [ EUnderover+ True (ESymbol Op "\8721") (EIdentifier "i") (EIdentifier "n")+ , ESuper (EIdentifier "i") (ENumber "2")+ ])+]
+ test/regression/259.test view
@@ -0,0 +1,4 @@+<<< tex+\hspace{-1.2em}+>>> native+[ ESpace ((-6) % 5) ]
texmath.cabal view
@@ -1,5 +1,5 @@ Name: texmath-Version: 0.12.9+Version: 0.12.10 Cabal-Version: >= 1.10 Build-type: Simple Synopsis: Conversion between math formats.@@ -155,6 +155,7 @@ , servant-server , wai , wai-logger+ , wai-extra , warp , optparse-applicative , safe