packages feed

latex-formulae-pandoc 0.1.1.0 → 0.2.0.0

raw patch · 3 files changed

+81/−47 lines, 3 files

Files

filter/Main.hs view
@@ -3,7 +3,5 @@ import Text.Pandoc.JSON  main :: IO ()-main = toJSONFilter $ convertFormulaDataURI 2 defaultEnv fopts- where fopts InlineMath  = math-       fopts DisplayMath = displaymath+main = toJSONFilter $ convertFormulaDataURI defaultEnv defaultPandocFormulaOptions 
latex-formulae-pandoc.cabal view
@@ -1,5 +1,5 @@ name:                latex-formulae-pandoc-version:             0.1.1.0+version:             0.2.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)
src/Image/LaTeX/Render/Pandoc.hs view
@@ -9,8 +9,13 @@          -- ** Name Supplies        , NameSupply        , newNameSupply-         -- * Scaling+         -- * Options+       , PandocFormulaOptions(..)        , ShrinkSize+       , defaultPandocFormulaOptions+         -- ** Error display functions+       , hideError+       , displayError          -- * Generalised versions          -- ** Data URIs        , convertFormulaDataURIWith@@ -45,6 +50,26 @@ dimensions (ImageYA16 i)   = dims i dimensions _               = error "Unsupported image format somehow!" ++-- | All options pertaining to the actual display of formulae. +data PandocFormulaOptions = PandocFormulaOptions+        { shrinkBy       :: ShrinkSize+          -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.+        , 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+   { shrinkBy = 2+   , errorDisplay = displayError+   , formulaOptions = \case DisplayMath -> displaymath; _ -> 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.@@ -52,52 +77,48 @@  -- | 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)+  :: EnvironmentOptions           -- ^ System environment settings+  -> PandocFormulaOptions         -- ^ Formula display settings   -> Inline -> IO Inline-convertFormulaDataURI sh o1 = convertFormulaDataURIWith (imageForFormula o1) sh+convertFormulaDataURI = convertFormulaDataURIWith . imageForFormula  -- | 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)+  :: EnvironmentOptions           -- ^ System environment settings+  -> PandocFormulaOptions         -- ^ Formula display settings   -> Pandoc -> IO Pandoc-convertAllFormulaeDataURI a b c = walkM $ convertFormulaDataURI a b c+convertAllFormulaeDataURI e = walkM . convertFormulaDataURI e  -- | A generalisation of 'convertFormulaDataURI' which allows the actual image rendering --   function to be customised, so that (e.g) caching can be added or other image processing. convertFormulaDataURIWith   :: (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))      -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@-  -> ShrinkSize                   -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.-  -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+  -> PandocFormulaOptions -- ^ Formula display settings   -> Inline -> IO Inline-convertFormulaDataURIWith f sh o (Math t s) = f (o t) s >>= \case+convertFormulaDataURIWith f o (Math t s) = f (formulaOptions o 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)+       (w,h) = (ow `div` shrinkBy o, oh `div` shrinkBy o)      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;\"/>"-convertFormulaDataURIWith _ _ _ x = return x+            " style=\"margin:0; vertical-align:-" ++ show (b `div` shrinkBy o) ++ "px;\"/>"+convertFormulaDataURIWith _ _ x = return x  -- | A generalisation of 'convertAllFormulaeDataURI' which allows the actual image rendering --   function to be customised, so that (e.g) caching can be added or other image processing. convertAllFormulaeDataURIWith   :: (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))      -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@-  -> ShrinkSize                   -- ^ Denominator for all dimensions. Useful for displaying high DPI images in small sizes, for retina displays. Otherwise set to 1.-  -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+  -> PandocFormulaOptions -- ^ Formula display settings   -> Pandoc -> IO Pandoc-convertAllFormulaeDataURIWith a b c = walkM $ convertFormulaDataURIWith a b c+convertAllFormulaeDataURIWith f = walkM . convertFormulaDataURIWith 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.@@ -113,58 +134,73 @@ -- | 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-  :: 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.-  -> (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))+  :: (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))      -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@-  -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+  -> 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 ns bn sh f o (Math t s) = f (o t) s >>= \case+convertFormulaFilesWith f ns bn o (Math t s) = f (formulaOptions o 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)+         (w,h) = (ow `div` shrinkBy o, oh `div` shrinkBy o)      _ <- 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;\"/>"-convertFormulaFilesWith _ _ _ _ _ x = return x+            " style=\"margin:0; vertical-align:-" ++ show (b `div` shrinkBy o) ++ "px;\"/>"+convertFormulaFilesWith _ _ _ _ x = return x  -- | Convert a formula in a pandoc document to an image, storing the images in a separate directory. convertFormulaFiles-  :: NameSupply                   -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+  :: 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-  -> 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)+  -> PandocFormulaOptions         -- ^ Formula display settings   -> Inline -> IO Inline-convertFormulaFiles ns fp ss eo = convertFormulaFilesWith ns fp ss (imageForFormula eo)+convertFormulaFiles = convertFormulaFilesWith . imageForFormula  -- | Convert every formula in a pandoc document to an image, storing the images in a separate directory. convertAllFormulaeFiles-  :: NameSupply                   -- ^ Unique file name supply. Reuse this for every invocation that shares the same image directory.+  :: 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-  -> 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)+  -> PandocFormulaOptions         -- ^ Formula display settings   -> Pandoc -> IO Pandoc-convertAllFormulaeFiles x y a b c = walkM $ convertFormulaFiles x y a b c+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-  :: 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.-  -> (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))+  :: (FormulaOptions -> Formula -> IO (Either RenderError (Baseline, DynamicImage)))      -- ^ Function that renders a formula, such as @imageForFormula defaultEnv@-  -> (MathType -> FormulaOptions) -- ^ LaTeX environment settings for each equation type (display and inline)+  -> 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 b c = walkM $ convertFormulaFilesWith x y a b c+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 ImageIsEmpty           = pandocError  [Str "The rendered image was empty"]+displayError CannotDetectBaseline   = pandocError [Str "Cannot detect baseline in rendered image"]+displayError (LaTeXFailure str)     = pandocError [Str "LaTeX failed:", LineBreak, Code nullAttr str]+displayError (DVIPSFailure str)     = pandocError [Str "DVIPS failed:", LineBreak, Code nullAttr str]+displayError (IMConvertFailure str) = pandocError [Str "convert failed:", LineBreak, Code nullAttr str]+displayError (ImageReadError str)   = pandocError [Str "Error reading image:", LineBreak, Code nullAttr str]+displayError (IOException e)        = pandocError [Str "IO Exception:", LineBreak, Code nullAttr $ show e]++pandocError :: [Inline] -> Inline+pandocError = Strong . (Emph [Str "Error:"] :)