From 0eb165da9bfcd9db541eee96ece71a4a447570de Mon Sep 17 00:00:00 2001 From: Steffen Jost Date: Wed, 6 Jul 2022 08:56:15 +0200 Subject: [PATCH] refactor(pandoc): avoid unnecessary explicit dependency for doctemplates, which is now a hidden dependency of pandoc again --- package.yaml | 3 +-- src/Handler/Admin/Apc.hs | 19 +++++++++++++++++++ src/Utils.hs | 8 ++++++++ src/Utils/Print.hs | 12 +++++++----- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/package.yaml b/package.yaml index 8ee999e89..9391c68ad 100644 --- a/package.yaml +++ b/package.yaml @@ -146,8 +146,7 @@ dependencies: - wai-middleware-prometheus - extended-reals - rfc5051 - - unidecode - - doctemplates + - unidecode - pandoc - pandoc-types - insert-ordered-containers diff --git a/src/Handler/Admin/Apc.hs b/src/Handler/Admin/Apc.hs index 3f7d9d5d3..691ee2977 100644 --- a/src/Handler/Admin/Apc.hs +++ b/src/Handler/Admin/Apc.hs @@ -46,6 +46,25 @@ formToMetaValues MetaPinRenewal{..} = P.Meta . mconcat $ catMaybes where toMeta k = singletonMap k . P.toMetaValue + +formToMetaValues2 :: MetaPinRenewal -> P.Meta +formToMetaValues2 MetaPinRenewal{..} = P.Meta $ mconcat + [ mbMeta "opening" mppOpening + , mbMeta "closing" mppClosing + , mbMeta "date" mppDate + , mbMeta "url" mppURL + , toMeta "login" mppLogin + , toMeta "pin" mppPin + , toMeta "recipient" mppRecipient + , toMeta "address" mppAddress + , toMeta "lang" mppLang + , toMeta "is-de" mppIsDe + ] + where + mbMeta _ Nothing = mempty + mbMeta k (Just x) = toMeta k x + toMeta k = singletonMap k . P.toMetaValue + {- makePrintForm :: Maybe MetaPinRenewal -> Form MetaPinRenewal makePrintForm tmpl = identifyForm FIDLmsLetter . validateForm validateMetaPinRenewal $ \html -> diff --git a/src/Utils.hs b/src/Utils.hs index fbd57f6d2..951be7675 100644 --- a/src/Utils.hs +++ b/src/Utils.hs @@ -663,6 +663,14 @@ partMap = Map.fromListWith mappend invertMap :: (Ord k, Ord v) => Map k v -> Map v (Set k) invertMap = groupMap . map swap . Map.toList +maybeMap :: IsMap p => ContainerKey p -> Maybe (MapValue p) -> p +maybeMap _ Nothing = mempty +maybeMap k (Just v) = singletonMap k v + +maybeMapWith :: IsMap p => (t -> MapValue p) -> ContainerKey p -> Maybe t -> p +maybeMapWith _ _ Nothing = mempty +maybeMapWith f k (Just v) = singletonMap k $ f v + -- | Counts how often a value appears in a map (not derived from invertMap for efficiency reasons) countMapElems :: (Ord v) => Map k v -> Map v Int countMapElems = Map.fromListWith (+) . map (\(_k,v)->(v,1)) . Map.toList diff --git a/src/Utils/Print.hs b/src/Utils/Print.hs index b31b963ca..d2901e8de 100644 --- a/src/Utils/Print.hs +++ b/src/Utils/Print.hs @@ -8,7 +8,6 @@ import Control.Monad.Except import Import hiding (embedFile) import Data.FileEmbed (embedFile) -import qualified Text.DocTemplates as P import qualified Text.Pandoc as P -- import qualified Text.Pandoc.PDF as P import qualified Text.Pandoc.Builder as P @@ -34,12 +33,15 @@ addMeta :: P.Meta -> P.Pandoc -> P.Pandoc addMeta m p = meta <> p where meta = P.Pandoc m mempty - --- reTemplateLetter :: (P.PandocMonad m, P.TemplateMonad m, Foldable t, P.ToMetaValue a) => t (Text, a) -> StoredMarkup -> m Text -reTemplateLetter :: (P.TemplateMonad m, P.PandocMonad m) => P.Meta -> StoredMarkup -> m Text +-- | Apply StoredMarkup as a template to itself and return the resulting Markup +-- This is a hack to allow variable interpolation within a document. +-- Pandoc currently only allows interpolation within templates. +-- An alternative Route would be to use Builders, but this prevents User-edited Markup Templates +reTemplateLetter :: P.PandocMonad m => P.Meta -> StoredMarkup -> m Text reTemplateLetter meta StoredMarkup{..} = do let strictMarkupInput = toStrict markupInput - mdTemplate <- P.compileTemplate "" strictMarkupInput + partialPath = "" -- no partials used, see Text.DocTemplates + mdTemplate <- P.runWithDefaultPartials $ P.compileTemplate partialPath strictMarkupInput case mdTemplate of (Left err) -> throwError . P.PandocTemplateError $ pack err (Right templ) -> do