latex-svg-pandoc (empty) → 0.1
raw patch · 5 files changed
+288/−0 lines, 5 filesdep +basedep +bytestringdep +directorysetup-changed
Dependencies added: base, bytestring, directory, filepath, latex-svg-image, latex-svg-pandoc, pandoc-types, text
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- filter/Main.hs +7/−0
- latex-svg-pandoc.cabal +57/−0
- src/Image/LaTeX/Render/Pandoc.hs +192/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2020 Oleg Grenrus, 2015 Liam O'Connor++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Liam O'Connor nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ filter/Main.hs view
@@ -0,0 +1,7 @@+import Image.LaTeX.Render.Pandoc+import Image.LaTeX.Render+import Text.Pandoc.JSON++main :: IO ()+main = toJSONFilter $ convertFormulaSVG defaultEnv defaultPandocFormulaOptions+
+ latex-svg-pandoc.cabal view
@@ -0,0 +1,57 @@+cabal-version: 2.2+name: latex-svg-pandoc+version: 0.1+synopsis:+ Render LaTeX formulae in pandoc documents to images with an actual LaTeX++description:+ This library provides utility functions to convert LaTeX equations to images+ in Pandoc documents using the @latex-svg-image@ package. It requires an+ actual LaTeX installation to work (@latex@, @dvisvgm@).+ .+ An executable is also provided which can be used as a Pandoc filter from the+ commandline, which allows this technique to be used even for standalone Pandoc+ jobs.+ .+ This is a fork of https://github.com/liamoc/latex-formulae++homepage: http://github.com/phadej/latex-svg#readme+license: BSD-3-Clause+license-file: LICENSE+author: Oleg Grenrus, Liam O'Connor+maintainer: Oleg Grenrus <oleg.grenrus@iki.fi>+copyright: 2020 Oleg Grenrus, 2015-2019 Liam O'Connor+category: Image+build-type: Simple+tested-with: GHC ==8.2.2 || ==8.4.4 || ==8.6.5 || ==8.8.3++source-repository head+ type: git+ location: https://github.com/phadej/latex-svg+ subdir: latex-svg-pandoc++library+ default-language: Haskell2010+ hs-source-dirs: src+ ghc-options: -Wall+ exposed-modules: Image.LaTeX.Render.Pandoc+ build-depends:+ , base >=4.10 && <4.14+ , bytestring ^>=0.10+ , directory ^>=1.3.0.1+ , filepath ^>=1.4.1.2+ , latex-svg-image ^>=0.1+ , pandoc-types ^>=1.17.6.1 || ^>=1.20+ , text ^>=1.2.3.0++executable latex-svg-filter+ hs-source-dirs: filter+ main-is: Main.hs+ ghc-options: -Wall+ build-depends:+ , base+ , latex-svg-image+ , latex-svg-pandoc+ , pandoc-types++ default-language: Haskell2010
+ src/Image/LaTeX/Render/Pandoc.hs view
@@ -0,0 +1,192 @@+{-# LANGUAGE GADTs, OverloadedStrings #-}+module Image.LaTeX.Render.Pandoc (+ -- * SVG+ convertFormulaSVG,+ convertAllFormulaeSVG,+ -- * Separate Files+ convertFormulaFiles,+ convertAllFormulaeFiles,+ -- ** Name Supplies+ NameSupply,+ newNameSupply,+ -- * Options+ PandocFormulaOptions(..),+ ShrinkSize,+ defaultPandocFormulaOptions,+ -- ** Error display functions+ hideError,+ displayError,+ -- * Generalised versions+ -- ** SVG+ convertFormulaSVGWith,+ convertAllFormulaeSVGWith,+ -- ** Files+ convertFormulaFilesWith,+ convertAllFormulaeFilesWith,+ ) where++import Data.IORef (IORef, modifyIORef, newIORef, readIORef)+import Data.String (IsString (..))+import Numeric (showFFloat)+import System.FilePath ((<.>), (</>))+import Text.Pandoc.Definition (Format (..), Inline (..), MathType (..), Pandoc, nullAttr)+import Text.Pandoc.Walk (walkM)++import qualified Data.Text as T++import Image.LaTeX.Render++-- | All options pertaining to the actual display of formulae.+data PandocFormulaOptions = PandocFormulaOptions+ { errorDisplay :: RenderError -> Inline+ -- ^ How to display various errors (such as LaTeX errors). Usually this can just be @displayError@ but you may wish @hideError@+ -- to avoid putting potentially secure information into the output page.+ , formulaOptions :: MathType -> FormulaOptions+ -- ^ LaTeX environment settings, including the preamble, for each equation type (display and inline)+ }++-- | A set of sensible defaults for formula options.+defaultPandocFormulaOptions :: PandocFormulaOptions+defaultPandocFormulaOptions = PandocFormulaOptions+ { errorDisplay = displayError+ , formulaOptions = fopts+ }+ where+ fopts DisplayMath = displaymath+ fopts InlineMath = math++-- | Denominator for various dimensions. For high DPI displays, it can be useful to use values of 2 or 4, so that the dimensions+-- of the image are a fraction of the actual image size, and the image appears more crisp. Otherwise, a value of 1 will always+-- produce sensible, if somewhat pixelated results.+type ShrinkSize = Int++-- | Convert a formula in a pandoc document to an image, embedding the image into the HTML using Data URIs.+convertFormulaSVG+ :: EnvironmentOptions -- ^ System environment settings+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Inline -> IO Inline+convertFormulaSVG = convertFormulaSVGWith . imageForFormula++-- | Convert all formulae in a pandoc document to images, embedding the images into the HTML using Data URIs.+convertAllFormulaeSVG+ :: EnvironmentOptions -- ^ System environment settings+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Pandoc -> IO Pandoc+convertAllFormulaeSVG e = walkM . convertFormulaSVG e++-- | A generalisation of 'convertFormulaSVG' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertFormulaSVGWith+ :: (FormulaOptions -> Formula -> IO (Either RenderError SVG))+ -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Inline -> IO Inline+convertFormulaSVGWith f o (Math t s) = do+ res <- f (formulaOptions o t) (toString s)+ case res of+ Left e -> return $ errorDisplay o e+ Right svg -> return $ RawInline (Format "html") $ fromString $ alterForHTML svg+convertFormulaSVGWith _ _ x = return x++-- | A generalisation of 'convertAllFormulaeSVG' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertAllFormulaeSVGWith+ :: (FormulaOptions -> Formula -> IO (Either RenderError SVG))+ -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Pandoc -> IO Pandoc+convertAllFormulaeSVGWith f = walkM . convertFormulaSVGWith f++-- | If we use files for the images, we need some way of naming the image files we produce+-- A NameSupply provides us with a source of unique names via an ever-increasing integer.+-- It's important that any invocation of 'convertFormulaFiles' or 'convertAllFormulaeFiles'+-- that shares the same image storage directory will also use the same name supply, or they+-- will overwrite each others images.+--+-- TODO: remove+type NameSupply = IORef Int++-- | Create a new name supply.+newNameSupply :: IO NameSupply+newNameSupply = newIORef 0++-- | A generalisation of 'convertFormulaFiles' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertFormulaFilesWith+ :: (FormulaOptions -> Formula -> IO (Either RenderError SVG))+ -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@+ -> NameSupply -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+ -> FilePath -- ^ Name of image directory where images will be stored+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Inline -> IO Inline+convertFormulaFilesWith f ns bn o (Math t s) = f (formulaOptions o t) (toString s) >>= \res -> case res of+ Left e -> return $ errorDisplay o e+ Right svg -> do+ let baseline = getBaseline svg+ fn <- readIORef ns+ modifyIORef ns (+1)+ let uri = bn </> show fn <.> "svg"+ writeFile uri svg+ return $ RawInline (Format "html") $ fromString $+ "<img src=\"" ++ uri ++ "\"" +++ " class=" ++ (case t of InlineMath -> "inline-math"; DisplayMath -> "display-math") +++ " style=\"margin:0; vertical-align:-" ++ showFFloat (Just 6) baseline "" ++ "pt;\"/>"++convertFormulaFilesWith _ _ _ _ x = return x++-- | Convert a formula in a pandoc document to an image, storing the images in a separate directory.+convertFormulaFiles+ :: EnvironmentOptions -- ^ System environment settings+ -> NameSupply -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+ -> FilePath -- ^ Name of image directory where images will be stored+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Inline -> IO Inline+convertFormulaFiles = convertFormulaFilesWith . imageForFormula++-- | Convert every formula in a pandoc document to an image, storing the images in a separate directory.+convertAllFormulaeFiles+ :: EnvironmentOptions -- ^ System environment settings+ -> NameSupply -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+ -> FilePath -- ^ Name of image directory where images will be stored+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Pandoc -> IO Pandoc+convertAllFormulaeFiles eo ns fp = walkM . convertFormulaFiles eo ns fp++-- | A generalisation of 'convertAllFormulaeFiles' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertAllFormulaeFilesWith+ :: (FormulaOptions -> Formula -> IO (Either RenderError SVG))+ -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@+ -> NameSupply -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+ -> FilePath -- ^ Name of image directory where images will be stored+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Pandoc -> IO Pandoc+convertAllFormulaeFilesWith x y a = walkM . convertFormulaFilesWith x y a++-- | Render all errors simply as "Error"+hideError :: RenderError -> Inline+hideError = const $ Str blank+ where+ blank = "Error"++-- | Render errors nicely, in order to show any problems clearly, with all information intact.+displayError :: RenderError -> Inline+displayError (LaTeXFailure str) = pandocError [Str "LaTeX failed:", LineBreak, Code nullAttr $ fromString str]+displayError (DVISVGMFailure str) = pandocError [Str "DVIPS failed:", LineBreak, Code nullAttr $ fromString str]+displayError (IOException e) = pandocError [Str "IO Exception:", LineBreak, Code nullAttr $ fromString $ show e]++pandocError :: [Inline] -> Inline+pandocError = Strong . (Emph [Str "Error:"] :)++-------------------------------------------------------------------------------+-- compat+-------------------------------------------------------------------------------++class IsString s => ToString s where+ toString :: s -> String++instance Char ~ c => ToString [c] where+ toString = id++instance ToString T.Text where+ toString = T.unpack