Parse the SVG file output from latex.

This commit is contained in:
David 2019-02-16 10:59:46 +01:00
commit 3483413e07
2 changed files with 27 additions and 19 deletions

View file

@ -23,7 +23,7 @@ library
Reanimate.LaTeX
build-depends: base >=4.12 && <4.13,
lucid-svg, time, text, unix, lucid, filepath, process, directory,
containers
containers, svg-tree, xml, bytestring
executable reanimate-viewer
main-is: SvgViewer.hs
@ -35,6 +35,7 @@ executable reanimate-viewer
Reanimate.LaTeX
build-depends: base >=4.12 && <4.13, cairo >=0.13 && <0.14, gtk,
svgcairo, lucid-svg, time, text, unix, lucid, reanimate,
filepath, process, directory, containers
filepath, process, directory, containers, svg-tree, xml,
bytestring
hs-source-dirs: src
default-language: Haskell2010

View file

@ -1,18 +1,24 @@
{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Reanimate.LaTeX (latex) where
import Control.Exception
import qualified Data.ByteString as B
import Data.IORef
import qualified Data.Map as Map
import Data.Map (Map)
import System.Process
import qualified Data.Map as Map
import Lucid (toHtml, toHtmlRaw)
import Lucid.Svg (Svg, fill_, font_size_, text_)
import System.Directory
import System.Exit
import System.FilePath
import System.IO
import System.IO.Unsafe
import System.FilePath
import System.Directory
import Control.Exception
import Lucid (toHtmlRaw, toHtml)
import Lucid.Svg (Svg, text_, font_size_, fill_)
import System.Process
import Graphics.Svg (loadSvgFile, parseSvgFile,
xmlOfDocument)
import Text.XML.Light.Output (ppcElement, prettyConfigPP)
{-# NOINLINE cache #-}
cache :: IORef (Map String (Svg ()))
@ -43,9 +49,10 @@ latexToSVG tex = handle (\(e::SomeException) -> return (failedSvg tex)) $ do
, "--bbox=1,1" -- increase bbox size.
, "--no-fonts" -- use glyphs instead of fonts.
,"--verbosity=0", "-o",svg_file]
svg_data <- readFile svg_file
evaluate (length svg_data)
return $ toHtmlRaw $ unlines $ drop 1 $ lines svg_data
svg_data <- B.readFile svg_file
case parseSvgFile svg_file svg_data of
Nothing -> error "Malformed svg"
Just svg -> return $ toHtmlRaw $ ppcElement prettyConfigPP (xmlOfDocument svg)
failedSvg :: String -> Svg ()
failedSvg tex =