From 3483413e07aeb2128dccba95b7960bae65bcdbdd Mon Sep 17 00:00:00 2001 From: David Date: Sat, 16 Feb 2019 10:59:46 +0100 Subject: [PATCH] Parse the SVG file output from latex. --- reanimate.cabal | 5 +++-- src/Reanimate/LaTeX.hs | 41 ++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/reanimate.cabal b/reanimate.cabal index d3c3a5c..902b0bb 100644 --- a/reanimate.cabal +++ b/reanimate.cabal @@ -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 diff --git a/src/Reanimate/LaTeX.hs b/src/Reanimate/LaTeX.hs index 22b7698..7d059b8 100644 --- a/src/Reanimate/LaTeX.hs +++ b/src/Reanimate/LaTeX.hs @@ -1,18 +1,24 @@ -{-# LANGUAGE ScopedTypeVariables, OverloadedStrings #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ScopedTypeVariables #-} module Reanimate.LaTeX (latex) where -import Data.IORef -import qualified Data.Map as Map -import Data.Map (Map) -import System.Process -import System.Exit -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 Control.Exception +import qualified Data.ByteString as B +import Data.IORef +import Data.Map (Map) +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.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 = @@ -82,7 +89,7 @@ requireExecutable :: String -> IO FilePath requireExecutable exec = do mbPath <- findExecutable exec case mbPath of - Nothing -> error $ "Couldn't find executable: " ++ exec + Nothing -> error $ "Couldn't find executable: " ++ exec Just path -> return path