From 42a2d42eecf551dd1990c97c1ae2586cd46674be Mon Sep 17 00:00:00 2001 From: Chris Wells Date: Mon, 30 Dec 2024 21:23:52 -0600 Subject: Started writing the main utilities for my webpage --- src/Hakyll/Core/Identifier/Pattorn.hs | 16 ++++++++++++++++ src/Hakyll/TeX/BibTeX.hs | 36 +++++++++++++++++++++++++++++++++++ src/Hakyll/TeX/HaTeX.hs | 15 +++++++++++++++ src/Hakyll/TeX/LaTeX.hs | 18 ++++++++++++++++++ src/Hakyll/TeX/TikZ.hs | 29 ++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 src/Hakyll/Core/Identifier/Pattorn.hs create mode 100644 src/Hakyll/TeX/BibTeX.hs create mode 100644 src/Hakyll/TeX/HaTeX.hs create mode 100644 src/Hakyll/TeX/LaTeX.hs create mode 100644 src/Hakyll/TeX/TikZ.hs (limited to 'src/Hakyll') diff --git a/src/Hakyll/Core/Identifier/Pattorn.hs b/src/Hakyll/Core/Identifier/Pattorn.hs new file mode 100644 index 0000000..0910320 --- /dev/null +++ b/src/Hakyll/Core/Identifier/Pattorn.hs @@ -0,0 +1,16 @@ +module Hakyll.Core.Identifier.Pattorn + ( Pattorn (..) + ) where + +import Prolog +import Hakyll + +-- | Wrapper around 'Pattern' to get a semigroup with "or" as its operation +newtype Pattorn = Pattorn { getPattern :: Pattern } + deriving newtype (Binary, Show, IsString) + +instance Semigroup Pattorn where + Pattorn a <> Pattorn b = Pattorn $ a .||. b + +instance Monoid Pattorn where + mempty = Pattorn $ complement mempty diff --git a/src/Hakyll/TeX/BibTeX.hs b/src/Hakyll/TeX/BibTeX.hs new file mode 100644 index 0000000..b085dfa --- /dev/null +++ b/src/Hakyll/TeX/BibTeX.hs @@ -0,0 +1,36 @@ +{-# OPTIONS_GHC -fno-warn-orphans #-} +module Hakyll.TeX.BibTeX + ( bibtexCompiler + , compileBibtex + , BibTeX + ) where + +import Prolog + +import Data.List qualified as L +import Hakyll +import Text.BibTeX.Entry qualified as B +import Text.BibTeX.Format qualified as B +import Text.BibTeX.Parse qualified as B +import Text.Parsec qualified as P +import Text.Parsec.Error qualified as P + +type BibTeX = B.T + +deriving instance Generic B.T +instance Binary B.T + +instance Writable B.T where + write p = write p . B.entry + +instance Writable [B.T] where + write p = write p . fold . L.intersperse "\n" . fmap B.entry + + +compileBibtex :: (StringConv s String) => Item s -> Compiler (Item [B.T]) +compileBibtex (Item i b) = either (throwError . fmap P.messageString . P.errorMessages) (pure . Item i) $ P.parse p (show i) (toSL b) + where + p = B.skippingLeadingSpace $ B.file <* P.eof + +bibtexCompiler :: Compiler (Item [B.T]) +bibtexCompiler = getResourceBody >>= compileBibtex diff --git a/src/Hakyll/TeX/HaTeX.hs b/src/Hakyll/TeX/HaTeX.hs new file mode 100644 index 0000000..9b83c62 --- /dev/null +++ b/src/Hakyll/TeX/HaTeX.hs @@ -0,0 +1,15 @@ +module Hakyll.TeX.HaTeX + ( hatexCompiler + , compileHatex + ) where + +import Prolog + +import Hakyll +import Text.LaTeX.Base.Parser + +compileHatex :: (StringConv s Text, Traversable t) => t s -> Compiler (t LaTeX) +compileHatex = traverse $ either (throwError . fmap messageString . errorMessages) pure . parseLaTeX . toSL + +hatexCompiler :: Compiler (Item LaTeX) +hatexCompiler = getResourceLBS >>= compileHatex diff --git a/src/Hakyll/TeX/LaTeX.hs b/src/Hakyll/TeX/LaTeX.hs new file mode 100644 index 0000000..8f6d5cc --- /dev/null +++ b/src/Hakyll/TeX/LaTeX.hs @@ -0,0 +1,18 @@ +module Hakyll.TeX.LaTeX + ( latexPdfCompiler + , compileLatexPdf + , rubberPipe + ) where + +import Prolog + +import Hakyll + +rubberPipe :: [String] -> LazyByteString -> Compiler LazyByteString +rubberPipe = unixFilterLBS "rubber-pipe" + +compileLatexPdf :: (StringConv s LazyByteString, Traversable t) => t s -> Compiler (t LazyByteString) +compileLatexPdf = traverse $ rubberPipe [ "--pdf" ] . toSL + +latexPdfCompiler :: Compiler (Item LazyByteString) +latexPdfCompiler = getResourceLBS >>= compileLatexPdf diff --git a/src/Hakyll/TeX/TikZ.hs b/src/Hakyll/TeX/TikZ.hs new file mode 100644 index 0000000..9df98fd --- /dev/null +++ b/src/Hakyll/TeX/TikZ.hs @@ -0,0 +1,29 @@ +module Hakyll.TeX.TikZ + ( TikzTemplate + , tikzfilter + , tikzBlockFilter + ) where + +import Network.URI.Encode qualified as URI +import Text.Pandoc.Definition +import Text.Pandoc.Walk (walkM) + +import Hakyll + +import Prolog + +import Hakyll.TeX.LaTeX (rubberPipe) + +type TikzTemplate = Identifier + +tikzFilter :: TikzTemplate -> Pandoc -> Compiler Pandoc +tikzFilter = walkM tikzBlockFilter + +-- | Requires `rubber` and `poppler_utils` +tikzBlockFilter :: TikzTemplate -> Block -> Compiler Block +tikzBlockFilter tmpl (CodeBlock info@(_, "tikzpicture":_, _) contents) = do + let imageBlock fname = Para [ Image info [] (toSL fname, "") ] + makeItem (toSL contents) >>= loadAndApplyTemplate tmpl (bodyField "body") >>= + traverse (convSL $ rubberPipe [ "--pdf" ] >=> unixFilterLBS "pdftocairo" [ "-svg", "-", "-" ]) >>= + \(Item _ b) -> imageBlock $ ("data:image/svg+sml;utf8," <>) $ URI.encode $ filter (/= '\n') b +tikzBlockFilter _ x = pure x -- cgit v1.2.3