packages feed

latex-svg-hakyll (empty) → 0.1

raw patch · 4 files changed

+185/−0 lines, 4 filesdep +basedep +hakylldep +latex-svg-imagesetup-changed

Dependencies added: base, hakyll, latex-svg-image, latex-svg-pandoc, lrucache, pandoc-types

Files

+ 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
+ latex-svg-hakyll.cabal view
@@ -0,0 +1,37 @@+cabal-version: 2.2+name:          latex-svg-hakyll+version:       0.1+synopsis:      Use actual LaTeX to render formulae inside Hakyll pages+description:+  This library provides functions to render all math formulae inside+  Pandoc-processed Hakyll pages using real LaTeX.+  .+  https://github.com/liamoc/latex-formulae++homepage:      https://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-hakyll++library+  default-language: Haskell2010+  hs-source-dirs:   src+  ghc-options:      -Wall+  exposed-modules:  Hakyll.Contrib.LaTeX+  build-depends:+    , base              >=4.10      && <4.14+    , hakyll            ^>=4.12.5.2 || ^>=4.13.0.0+    , latex-svg-image   ^>=0.1+    , latex-svg-pandoc  ^>=0.1+    , lrucache          ^>=1.2+    , pandoc-types      ^>=1.17.6.1 || ^>=1.20
+ src/Hakyll/Contrib/LaTeX.hs view
@@ -0,0 +1,116 @@+-- | This module provides a basic framework to render LaTeX formulae inside Pandoc documents+--   for Hakyll pages.+--+--   See the latex-svg page on GitHub for more information.+--+--      https://github.com/phadej/latex-svg#readme+--+module Hakyll.Contrib.LaTeX (+    initFormulaCompilerSVG,+    initFormulaCompilerSVGPure,+    CacheSize,+    compileFormulaeSVG,+    ) where++import Data.Char              (isSpace)+import Hakyll.Core.Compiler   (Compiler, unsafeCompiler)+import Text.Pandoc.Definition (Pandoc)++import Image.LaTeX.Render+import Image.LaTeX.Render.Pandoc++import qualified Data.Cache.LRU.IO as LRU++-- | Number of formula images to keep in memory during a @watch@ session.+type CacheSize = Integer++-- | Creates a formula compiler with caching. Can be used as in the following minimal example:+--+-- @+-- main = do+--     renderFormulae <- 'initFormulaCompilerSVG' 1000 'defaultEnv"+--     hakyll $+--         match "posts/*.markdown" $ do+--             route $ setExtension "html"+--             compile $ pandocCompilerWithTransformM+--                  defaultHakyllReaderOptions+--                  defaultHakyllWriterOptions+--                  (renderFormulae 'defaultPandocFormulaOptions')+-- @+--+initFormulaCompilerSVG+    :: CacheSize+    -> EnvironmentOptions+    -> IO (PandocFormulaOptions -> Pandoc -> Compiler Pandoc)+initFormulaCompilerSVG cs eo = do+    mImageForFormula <- curry <$> memoizeLru (Just cs) (uncurry drawFormula)+    let eachFormula x y = do+          putStrLn $ "    formula (" ++ environment x ++ ") \"" ++ equationPreview y ++ "\""+          mImageForFormula x y+    return $ \fo -> unsafeCompiler . convertAllFormulaeSVGWith eachFormula fo+  where+    drawFormula x y = do+        putStrLn "      drawing..."+        imageForFormula eo x y+--+-- | Creates a formula compiler. Can be used as in the following minimal example:+--+-- @+-- main = hakyll $ do+--     let renderFormulae = 'initFormulaCompilerSVGPure' 'defaultEnv"+--     match "posts/*.markdown" $ do+--         route $ setExtension "html"+--         compile $ pandocCompilerWithTransformM+--              defaultHakyllReaderOptions+--              defaultHakyllWriterOptions+--              (renderFormulae 'defaultPandocFormulaOptions')+-- @+--+initFormulaCompilerSVGPure+    :: EnvironmentOptions+    -> PandocFormulaOptions -> Pandoc -> Compiler Pandoc+initFormulaCompilerSVGPure eo fo pandoc = do+    let mImageForFormula = drawFormula+    let eachFormula x y = do+          putStrLn $ "    formula (" ++ environment x ++ ") \"" ++ equationPreview y ++ "\""+          mImageForFormula x y++    unsafeCompiler (convertAllFormulaeSVGWith eachFormula fo pandoc)+  where+    drawFormula x y = do+        putStrLn "      drawing..."+        imageForFormula eo x y++-- | A formula compiler that does not use caching, which works in a more drop-in fashion, as in:+--+-- > compile $ pandocCompilerWithTransformM (compileFormulaeSVG defaultEnv defaultPandocFormulaOptions)+--+compileFormulaeSVG+    :: EnvironmentOptions+    -> PandocFormulaOptions+    -> Pandoc -> Compiler Pandoc+compileFormulaeSVG eo po =+    let eachFormula x y = do+          putStrLn $ "    formula (" ++ environment x ++ ") \"" ++ equationPreview y ++ "\""+          putStrLn   "      drawing..."+          imageForFormula eo x y+    in unsafeCompiler . convertAllFormulaeSVGWith eachFormula po++equationPreview :: String -> String+equationPreview x'+    | length x <= 16 = x+    | otherwise      = take 16 $ filter (/= '\n') x ++ "..."+  where+    x = dropWhile isSpace x'++memoizeLru :: Ord a => Maybe Integer -> (a -> IO b) -> IO (a -> IO b)+memoizeLru msize action = do+    lru <- LRU.newAtomicLRU msize+    return $ \arg -> do+        mret <- LRU.lookup arg lru+        case mret of+            Just ret -> return ret+            Nothing -> do+                ret <- action arg+                LRU.insert arg ret lru+                return ret