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 Data.IORef import Control.Exception
import qualified Data.Map as Map import qualified Data.ByteString as B
import Data.Map (Map) import Data.IORef
import System.Process import Data.Map (Map)
import System.Exit import qualified Data.Map as Map
import System.IO import Lucid (toHtml, toHtmlRaw)
import System.IO.Unsafe import Lucid.Svg (Svg, fill_, font_size_, text_)
import System.FilePath import System.Directory
import System.Directory import System.Exit
import Control.Exception import System.FilePath
import Lucid (toHtmlRaw, toHtml) import System.IO
import Lucid.Svg (Svg, text_, font_size_, fill_) import System.IO.Unsafe
import System.Process
import Graphics.Svg (loadSvgFile, parseSvgFile,
xmlOfDocument)
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 =
@ -82,7 +89,7 @@ requireExecutable :: String -> IO FilePath
requireExecutable exec = do requireExecutable exec = do
mbPath <- findExecutable exec mbPath <- findExecutable exec
case mbPath of case mbPath of
Nothing -> error $ "Couldn't find executable: " ++ exec Nothing -> error $ "Couldn't find executable: " ++ exec
Just path -> return path Just path -> return path