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

View file

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