latex-svg-pandoc 0.1 → 0.2
raw patch · 3 files changed
+133/−54 lines, 3 filesdep ~latex-svg-imagePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: latex-svg-image
API changes (from Hackage documentation)
- Image.LaTeX.Render.Pandoc: convertAllFormulaeFiles :: EnvironmentOptions -> NameSupply -> FilePath -> PandocFormulaOptions -> Pandoc -> IO Pandoc
- Image.LaTeX.Render.Pandoc: convertAllFormulaeFilesWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> NameSupply -> FilePath -> PandocFormulaOptions -> Pandoc -> IO Pandoc
- Image.LaTeX.Render.Pandoc: convertAllFormulaeSVG :: EnvironmentOptions -> PandocFormulaOptions -> Pandoc -> IO Pandoc
- Image.LaTeX.Render.Pandoc: convertAllFormulaeSVGWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> PandocFormulaOptions -> Pandoc -> IO Pandoc
- Image.LaTeX.Render.Pandoc: convertFormulaFiles :: EnvironmentOptions -> NameSupply -> FilePath -> PandocFormulaOptions -> Inline -> IO Inline
- Image.LaTeX.Render.Pandoc: convertFormulaFilesWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> NameSupply -> FilePath -> PandocFormulaOptions -> Inline -> IO Inline
- Image.LaTeX.Render.Pandoc: convertFormulaSVG :: EnvironmentOptions -> PandocFormulaOptions -> Inline -> IO Inline
- Image.LaTeX.Render.Pandoc: convertFormulaSVGWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> PandocFormulaOptions -> Inline -> IO Inline
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesBlock :: EnvironmentOptions -> NameSupply -> FilePath -> PandocFormulaOptions -> Block -> IO Block
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesBlockWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> NameSupply -> FilePath -> PandocFormulaOptions -> Block -> IO Block
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesInline :: EnvironmentOptions -> NameSupply -> FilePath -> PandocFormulaOptions -> Inline -> IO Inline
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesInlineWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> NameSupply -> FilePath -> PandocFormulaOptions -> Inline -> IO Inline
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesPandoc :: EnvironmentOptions -> NameSupply -> FilePath -> PandocFormulaOptions -> Pandoc -> IO Pandoc
+ Image.LaTeX.Render.Pandoc: convertFormulaFilesPandocWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> NameSupply -> FilePath -> PandocFormulaOptions -> Pandoc -> IO Pandoc
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgBlock :: EnvironmentOptions -> PandocFormulaOptions -> Block -> IO Block
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgBlockWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> PandocFormulaOptions -> Block -> IO Block
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgInline :: EnvironmentOptions -> PandocFormulaOptions -> Inline -> IO Inline
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgInlineWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> PandocFormulaOptions -> Inline -> IO Inline
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgPandoc :: EnvironmentOptions -> PandocFormulaOptions -> Pandoc -> IO Pandoc
+ Image.LaTeX.Render.Pandoc: convertFormulaSvgPandocWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -> PandocFormulaOptions -> Pandoc -> IO Pandoc
- Image.LaTeX.Render.Pandoc: PandocFormulaOptions :: (RenderError -> Inline) -> (MathType -> FormulaOptions) -> PandocFormulaOptions
+ Image.LaTeX.Render.Pandoc: PandocFormulaOptions :: (RenderError -> Inline) -> (Maybe MathType -> FormulaOptions) -> PandocFormulaOptions
- Image.LaTeX.Render.Pandoc: [formulaOptions] :: PandocFormulaOptions -> MathType -> FormulaOptions
+ Image.LaTeX.Render.Pandoc: [formulaOptions] :: PandocFormulaOptions -> Maybe MathType -> FormulaOptions
Files
- filter/Main.hs +15/−2
- latex-svg-pandoc.cabal +2/−2
- src/Image/LaTeX/Render/Pandoc.hs +116/−50
filter/Main.hs view
@@ -3,5 +3,18 @@ import Text.Pandoc.JSON main :: IO ()-main = toJSONFilter $ convertFormulaSVG defaultEnv defaultPandocFormulaOptions-+main = toJSONFilter $ convertFormulaSvgPandoc env fopts+ where+ env = defaultEnv { latexFontSize = 14 }+ fopts = defaultPandocFormulaOptions+ { formulaOptions = \mt -> (formulaOptions defaultPandocFormulaOptions mt)+ { preamble = unlines+ [ "\\usepackage{amsmath}"+ , "\\usepackage{amsfonts}"+ , "\\usepackage[T1]{fontenc}"+ , "\\usepackage{lmodern}"+ , "\\usepackage{xcolor}"+ , "\\usepackage{tikz-cd}"+ ]+ }+ }
latex-svg-pandoc.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: latex-svg-pandoc-version: 0.1+version: 0.2 synopsis: Render LaTeX formulae in pandoc documents to images with an actual LaTeX @@ -40,7 +40,7 @@ , bytestring ^>=0.10 , directory ^>=1.3.0.1 , filepath ^>=1.4.1.2- , latex-svg-image ^>=0.1+ , latex-svg-image ^>=0.2 , pandoc-types ^>=1.17.6.1 || ^>=1.20 , text ^>=1.2.3.0
src/Image/LaTeX/Render/Pandoc.hs view
@@ -1,11 +1,14 @@-{-# LANGUAGE GADTs, OverloadedStrings #-}+{-# LANGUAGE GADTs #-}+{-# LANGUAGE OverloadedStrings #-} module Image.LaTeX.Render.Pandoc ( -- * SVG- convertFormulaSVG,- convertAllFormulaeSVG,+ convertFormulaSvgInline,+ convertFormulaSvgBlock,+ convertFormulaSvgPandoc, -- * Separate Files- convertFormulaFiles,- convertAllFormulaeFiles,+ convertFormulaFilesInline,+ convertFormulaFilesBlock,+ convertFormulaFilesPandoc, -- ** Name Supplies NameSupply, newNameSupply,@@ -18,18 +21,20 @@ displayError, -- * Generalised versions -- ** SVG- convertFormulaSVGWith,- convertAllFormulaeSVGWith,+ convertFormulaSvgInlineWith,+ convertFormulaSvgBlockWith,+ convertFormulaSvgPandocWith, -- ** Files- convertFormulaFilesWith,- convertAllFormulaeFilesWith,+ convertFormulaFilesInlineWith,+ convertFormulaFilesBlockWith,+ convertFormulaFilesPandocWith, ) 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.Definition (Block (..), Format (..), Inline (..), MathType (..), Pandoc, nullAttr) import Text.Pandoc.Walk (walkM) import qualified Data.Text as T@@ -41,7 +46,7 @@ { 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+ , formulaOptions :: Maybe MathType -> FormulaOptions -- ^ LaTeX environment settings, including the preamble, for each equation type (display and inline) } @@ -52,8 +57,9 @@ , formulaOptions = fopts } where- fopts DisplayMath = displaymath- fopts InlineMath = math+ fopts (Just DisplayMath) = displaymath+ fopts (Just InlineMath) = math+ fopts Nothing = defaultFormulaOptions -- | 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@@ -61,45 +67,68 @@ type ShrinkSize = Int -- | Convert a formula in a pandoc document to an image, embedding the image into the HTML using Data URIs.-convertFormulaSVG+convertFormulaSvgInline :: EnvironmentOptions -- ^ System environment settings -> PandocFormulaOptions -- ^ Formula display settings -> Inline -> IO Inline-convertFormulaSVG = convertFormulaSVGWith . imageForFormula+convertFormulaSvgInline = convertFormulaSvgInlineWith . imageForFormula+--+-- | Convert all formulae in a pandoc block to images, embedding the images into the HTML using Data URIs.+convertFormulaSvgBlock+ :: EnvironmentOptions -- ^ System environment settings+ -> PandocFormulaOptions -- ^ Formula display settings+ -> Block -> IO Block+convertFormulaSvgBlock = convertFormulaSvgBlockWith . imageForFormula -- | Convert all formulae in a pandoc document to images, embedding the images into the HTML using Data URIs.-convertAllFormulaeSVG+convertFormulaSvgPandoc :: EnvironmentOptions -- ^ System environment settings -> PandocFormulaOptions -- ^ Formula display settings -> Pandoc -> IO Pandoc-convertAllFormulaeSVG e = walkM . convertFormulaSVG e+convertFormulaSvgPandoc e = walkM . convertFormulaSvgBlock e --- | A generalisation of 'convertFormulaSVG' which allows the actual image rendering+-- | A generalisation of 'convertFormulaSvgBlock' which allows the actual image rendering -- function to be customised, so that (e.g) caching can be added or other image processing.-convertFormulaSVGWith+convertFormulaSvgBlockWith :: (FormulaOptions -> Formula -> IO (Either RenderError SVG)) -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@ -> PandocFormulaOptions -- ^ Formula display settings+ -> Block -> IO Block+convertFormulaSvgBlockWith f o (RawBlock format s)+ | format == Format "tex"+ = do+ res <- f (formulaOptions o Nothing) (toString s)+ return $ Para $ singleton $ case res of+ Left e -> errorDisplay o e+ Right svg -> RawInline (Format "html") $ fromString $ alterForHTML svg+convertFormulaSvgBlockWith f o b = walkM (convertFormulaSvgInlineWith f o) b++-- | A generalisation of 'convertFormulaSvgInline' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertFormulaSvgInlineWith+ :: (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+convertFormulaSvgInlineWith f o (Math t s) = do+ res <- f (formulaOptions o (Just t)) (toString s)+ return $ case res of+ Left e -> errorDisplay o e+ Right svg -> RawInline (Format "html") $ fromString $ alterForHTML svg+convertFormulaSvgInlineWith _ _ x = return x --- | A generalisation of 'convertAllFormulaeSVG' which allows the actual image rendering+-- | A generalisation of 'convertFormulaSvgPandoc' which allows the actual image rendering -- function to be customised, so that (e.g) caching can be added or other image processing.-convertAllFormulaeSVGWith+convertFormulaSvgPandocWith :: (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+convertFormulaSvgPandocWith f = walkM . convertFormulaSvgBlockWith 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'+-- It's important that any invocation of 'convertFormulaFiles' or 'convertFormulaFiles' -- that shares the same image storage directory will also use the same name supply, or they -- will overwrite each others images. --@@ -110,58 +139,92 @@ newNameSupply :: IO NameSupply newNameSupply = newIORef 0 --- | A generalisation of 'convertFormulaFiles' which allows the actual image rendering++-- | A generalisation of 'convertFormulaFilesInline' which allows the actual image rendering -- function to be customised, so that (e.g) caching can be added or other image processing.-convertFormulaFilesWith+convertFormulaFilesInlineWith :: (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+convertFormulaFilesInlineWith f ns bn o (Math t s) = f (formulaOptions o (Just 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;\"/>"+ Right svg -> makeSvgFile ns bn (Just t) svg -convertFormulaFilesWith _ _ _ _ x = return x+convertFormulaFilesInlineWith _ _ _ _ x = return x +makeSvgFile :: NameSupply -> FilePath -> Maybe MathType -> SVG -> IO Inline+makeSvgFile ns bn t svg = do+ let baseline = getBaseline svg+ fn <- readIORef ns+ modifyIORef ns (+1)+ let uri = bn </> show fn <.> "svg"+ writeFile uri svg+ let classArg = case t of+ Nothing -> ""+ Just InlineMath -> " class='inline-math'"+ Just DisplayMath -> " class='display-math'"+ return $ RawInline (Format "html") $ fromString $+ "<img src=\"" ++ uri ++ "\"" ++ classArg +++ " style=\"margin:0; vertical-align:-" ++ showFFloat (Just 6) baseline "" ++ "pt;\"/>"++-- | A generalisation of 'convertFormulaFilesBlock' which allows the actual image rendering+-- function to be customised, so that (e.g) caching can be added or other image processing.+convertFormulaFilesBlockWith+ :: (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+ -> Block -> IO Block+convertFormulaFilesBlockWith f ns bn o (RawBlock format s)+ | format == Format "tex"+ = do+ res <- f (formulaOptions o Nothing) (toString s)+ case res of+ Left e -> return $ Para $ singleton $ errorDisplay o e+ Right svg -> fmap (Para . singleton) $ makeSvgFile ns bn Nothing svg+convertFormulaFilesBlockWith f ns bn o b = walkM (convertFormulaFilesInlineWith f ns bn o) b+ -- | Convert a formula in a pandoc document to an image, storing the images in a separate directory.-convertFormulaFiles+convertFormulaFilesInline :: 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+convertFormulaFilesInline = convertFormulaFilesInlineWith . imageForFormula+--+-- | Convert every formula in a pandoc block to an image, storing the images in a separate directory.+convertFormulaFilesBlock+ :: 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+ -> Block -> IO Block+convertFormulaFilesBlock = convertFormulaFilesBlockWith . imageForFormula -- | Convert every formula in a pandoc document to an image, storing the images in a separate directory.-convertAllFormulaeFiles+convertFormulaFilesPandoc :: 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+convertFormulaFilesPandoc eo ns fp = walkM . convertFormulaFilesInline eo ns fp --- | A generalisation of 'convertAllFormulaeFiles' which allows the actual image rendering+-- | A generalisation of 'convertFormulaFilesPandoc' which allows the actual image rendering -- function to be customised, so that (e.g) caching can be added or other image processing.-convertAllFormulaeFilesWith+convertFormulaFilesPandocWith :: (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+convertFormulaFilesPandocWith x y a = walkM . convertFormulaFilesInlineWith x y a -- | Render all errors simply as "Error" hideError :: RenderError -> Inline@@ -190,3 +253,6 @@ instance ToString T.Text where toString = T.unpack++singleton :: a -> [a]+singleton = return