latex-formulae-pandoc (empty) → 0.1.0.0
raw patch · 5 files changed
+206/−0 lines, 5 filesdep +JuicyPixelsdep +basedep +base64-bytestringsetup-changed
Dependencies added: JuicyPixels, base, base64-bytestring, bytestring, directory, filepath, latex-formulae-image, latex-formulae-pandoc, pandoc-types
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- filter/Main.hs +9/−0
- latex-formulae-pandoc.cabal +46/−0
- src/Image/LaTeX/Render/Pandoc.hs +119/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 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,9 @@+import Image.LaTeX.Render.Pandoc+import Image.LaTeX.Render+import Text.Pandoc.JSON++main :: IO ()+main = toJSONFilter $ convertFormulaDataURI 2 defaultEnv eqopts+ where eqopts InlineMath = math+ eqopts DisplayMath = displaymath+
+ latex-formulae-pandoc.cabal view
@@ -0,0 +1,46 @@+name: latex-formulae-pandoc+version: 0.1.0.0+synopsis: Render LaTeX formulae in pandoc documents to images with an actual LaTeX installation+description: This library provides utility functions to convert LaTeX equations to images in Pandoc documents using the @latex-formulae-image@ package.+ It requires an actual LaTeX installation to work (@latex@,@dvips@), along with ImageMagick's @convert@ (which needs to understand PostScript)+ .+ 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.+homepage: http://github.com/liamoc/latex-formulae#readme+license: BSD3+license-file: LICENSE+author: Liam O'Connor+maintainer: liamoc@cse.unsw.edu.au+copyright: Liam O'Connor, 2015+category: Image+build-type: Simple+cabal-version: >=1.10+source-repository head+ type: git+ location: https://github.com/liamoc/latex-formulae++library+ exposed-modules: Image.LaTeX.Render.Pandoc+ build-depends: base >=4.7 && <5+ , pandoc-types >= 1.12 && < 1.13 + , latex-formulae-image >= 0.1 && < 0.2+ , JuicyPixels >= 3.2 && < 3.3+ , directory >= 1.2 && < 1.3+ , filepath >= 1.3 && < 1.5+ , base64-bytestring >= 1.0 && < 1.1+ , bytestring >= 0.10 && <0.11+ hs-source-dirs: src+ ghc-options: -Wall+ default-language: Haskell2010++executable latex-formulae-filter+ hs-source-dirs: filter+ main-is: Main.hs+ ghc-options: -Wall+ build-depends: base >=4.7 && <5, latex-formulae-pandoc+ , pandoc-types >= 1.12 && < 1.13+ , latex-formulae-image >= 0.1 && < 0.2+ default-language: Haskell2010+ ++
+ src/Image/LaTeX/Render/Pandoc.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE LambdaCase #-}+module Image.LaTeX.Render.Pandoc+ ( -- * Data URIs+ convertFormulaDataURI+ , convertAllFormulaeDataURI+ -- * Separate Files+ , convertFormulaFiles+ , convertAllFormulaeFiles+ -- ** Name Supplies+ , NameSupply+ , newNameSupply+ -- * Scaling+ , ShrinkSize+ ) where++import Text.Pandoc.Definition+import Text.Pandoc.Walk+import Image.LaTeX.Render+import Codec.Picture+import Control.Applicative+import Data.IORef+import System.FilePath++import qualified Data.ByteString.Base64.Lazy as B64+import qualified Data.ByteString.Lazy.Char8 as BS++dims :: Image a -> (Int,Int)+dims = liftA2 (,) imageWidth imageHeight++dimensions :: DynamicImage -> (Int,Int)+dimensions (ImageRGB8 i) = dims i+dimensions (ImageRGBA8 i) = dims i+dimensions (ImageRGB16 i) = dims i+dimensions (ImageRGBA16 i) = dims i+dimensions (ImageY8 i) = dims i+dimensions (ImageY16 i) = dims i+dimensions (ImageYA8 i) = dims i+dimensions (ImageYA16 i) = dims i+dimensions _ = error "Unsupported image format somehow!"++-- | 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.+convertFormulaDataURI+ :: ShrinkSize -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.+ -> EnvironmentOptions -- ^ System environment settings+ -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+ -> Inline -> IO Inline+convertFormulaDataURI sh o1 o2 (Math t s) = imageForFormula o1 (o2 t) s >>= \case+ Left e -> return $ Str (show e)+ Right (b,i) -> let+ Right bs = encodeDynamicPng i+ dataUri = "data:image/png;base64," ++ BS.unpack (B64.encode bs)+ (ow,oh) = dimensions i+ (w,h) = (ow `div` sh, oh `div` sh)+ in return $ RawInline (Format "html") $+ "<img width=" ++ show w +++ " height=" ++ show h +++ " src=\"" ++ dataUri ++ "\"" +++ " class=" ++ (case t of InlineMath -> "inline-math"; _ -> "display-math") +++ " style=\"margin:0; vertical-align:-" ++ show (b `div` sh) ++ "px;\"/>"+convertFormulaDataURI sh o1 o2 x = return x++-- | Convert all formulae in a pandoc document to images, embedding the images into the HTML using Data URIs.+convertAllFormulaeDataURI+ :: ShrinkSize -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.+ -> EnvironmentOptions -- ^ System environment settings+ -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+ -> Pandoc -> IO Pandoc+convertAllFormulaeDataURI a b c = walkM $ convertFormulaDataURI a b c +++-- | 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.+type NameSupply = IORef Int++-- | Create a new name supply.+newNameSupply :: IO NameSupply+newNameSupply = newIORef 0++-- | Convert a formula in a pandoc document to an image, embedding the image into the HTML using Data URIs.+convertFormulaFiles+ :: 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+ -> ShrinkSize -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.+ -> EnvironmentOptions -- ^ System environment settings+ -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+ -> Inline -> IO Inline+convertFormulaFiles ns bn sh o1 o2 (Math t s) = imageForFormula o1 (o2 t) s >>= \case+ Left e -> return $ Str (show e)+ Right (b,i) -> do+ fn <- readIORef ns+ modifyIORef ns (+1)+ let uri = bn </> show fn <.> "png"+ (ow,oh) = dimensions i+ (w,h) = (ow `div` sh, oh `div` sh)+ _ <- writeDynamicPng uri i+ return $ RawInline (Format "html") $+ "<img width=" ++ show w +++ " height=" ++ show h +++ " src=\"" ++ uri ++ "\"" +++ " class=" ++ (case t of InlineMath -> "inline-math"; _ -> "display-math") +++ " style=\"margin:0; vertical-align:-" ++ show (b `div` sh) ++ "px;\"/>"+convertFormulaFiles _ _ _ _ _ x = return x++convertAllFormulaeFiles+ :: 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+ -> ShrinkSize -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.+ -> EnvironmentOptions -- ^ System environment settings+ -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+ -> Pandoc -> IO Pandoc+convertAllFormulaeFiles x y a b c = walkM $ convertFormulaFiles x y a b c