chore(letter): applying metadata to template working now as intended
This commit is contained in:
parent
104794a210
commit
21c0015ba0
@ -45,7 +45,7 @@ instance Default MetaPinRenewal where
|
|||||||
, mppURL = Nothing
|
, mppURL = Nothing
|
||||||
, mppDate = fromGregorian 2022 07 27
|
, mppDate = fromGregorian 2022 07 27
|
||||||
, mppLang = "de-de"
|
, mppLang = "de-de"
|
||||||
, mppOpening = Just "Lieber Papa Schlumpfi,"
|
, mppOpening = Just "Lieber $recipient$ Schlumpfi,"
|
||||||
, mppClosing = Nothing
|
, mppClosing = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -101,24 +101,27 @@ postPrintCenterR = do
|
|||||||
$(widgetFile "print-center")
|
$(widgetFile "print-center")
|
||||||
|
|
||||||
|
|
||||||
getPrintSendR, postPrintSendR:: Handler Html
|
getPrintSendR, postPrintSendR:: Handler TypedContent
|
||||||
getPrintSendR = postPrintSendR
|
getPrintSendR = postPrintSendR
|
||||||
postPrintSendR = do
|
postPrintSendR = do
|
||||||
((sendResult, sendWidget), sendEnctype) <- runFormPost $ makeRenewalForm Nothing
|
((sendResult, sendWidget), sendEnctype) <- runFormPost $ makeRenewalForm Nothing
|
||||||
let procFormSend mpr@MetaPinRenewal{..} = do
|
let procFormSend mpr@MetaPinRenewal{..} = do
|
||||||
addMessage Info . toHtml $ "Brief wird gesendet an " <> mppRecipient
|
addMessage Info . toHtml $ "Brief wird gesendet an " <> mppRecipient
|
||||||
e_pdf <- pdfRenewal $ mprToMeta mpr
|
e_pdf <- pdfRenewal $ mprToMeta mpr
|
||||||
-- now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
case e_pdf of
|
_ <- case e_pdf of
|
||||||
Right bs -> do
|
Right bs -> do
|
||||||
liftIO $ LBS.writeFile "/tmp/generated.pdf" bs
|
liftIO $ LBS.writeFile "/tmp/generated.pdf" bs
|
||||||
addMessage Warning "PDF momentan nur gespeicher unter /tmp/generated.pdf"
|
sendByteStringAsFile "demoPDF.pdf" (LBS.toStrict bs) now
|
||||||
Left err -> addMessage Error . toHtml $ P.renderError err
|
-- addMessage Warning "PDF momentan nur gespeicher unter /tmp/generated.pdf"
|
||||||
|
Left err ->
|
||||||
|
-- addMessage Error . toHtml $ P.renderError err
|
||||||
|
sendResponseStatus internalServerError500 $ toTypedContent $ P.renderError err
|
||||||
-- TODO: continue here with acutal letter sending!
|
-- TODO: continue here with acutal letter sending!
|
||||||
return $ Just ()
|
return $ Just ()
|
||||||
mbPdfLink <- formResultMaybe sendResult procFormSend
|
mbPdfLink <- formResultMaybe sendResult procFormSend
|
||||||
-- actionUrl <- fromMaybe AdminAvsR <$> getCurrentRoute
|
-- actionUrl <- fromMaybe AdminAvsR <$> getCurrentRoute
|
||||||
siteLayoutMsg MsgMenuPrintSend $ do
|
answer <- siteLayoutMsg MsgMenuPrintSend $ do
|
||||||
setTitleI MsgMenuPrintSend
|
setTitleI MsgMenuPrintSend
|
||||||
let sendForm = wrapForm sendWidget def
|
let sendForm = wrapForm sendWidget def
|
||||||
{ formEncoding = sendEnctype
|
{ formEncoding = sendEnctype
|
||||||
@ -126,3 +129,4 @@ postPrintSendR = do
|
|||||||
}
|
}
|
||||||
-- TODO: use i18nWidgetFile instead if this is to become permanent
|
-- TODO: use i18nWidgetFile instead if this is to become permanent
|
||||||
$(widgetFile "print-send")
|
$(widgetFile "print-send")
|
||||||
|
sendResponse $ toTypedContent answer
|
||||||
|
|||||||
@ -64,7 +64,7 @@ appMeta f (P.Pandoc m bs) = P.Pandoc (f m) bs
|
|||||||
|
|
||||||
-- | Add meta to pandoc. Existing variables will be overwritten.
|
-- | Add meta to pandoc. Existing variables will be overwritten.
|
||||||
addMeta :: P.Meta -> P.Pandoc -> P.Pandoc
|
addMeta :: P.Meta -> P.Pandoc -> P.Pandoc
|
||||||
addMeta m = appMeta (m <>)
|
addMeta m = appMeta (<> m)
|
||||||
--addMeta m p = meta <> p
|
--addMeta m p = meta <> p
|
||||||
-- where meta = P.Pandoc m mempty
|
-- where meta = P.Pandoc m mempty
|
||||||
|
|
||||||
@ -79,6 +79,11 @@ setIsDeFromLang m
|
|||||||
where
|
where
|
||||||
isde = "is-de"
|
isde = "is-de"
|
||||||
|
|
||||||
|
defReaderOpts :: P.ReaderOptions
|
||||||
|
defReaderOpts = def { P.readerExtensions = P.pandocExtensions, P.readerStripComments = True }
|
||||||
|
|
||||||
|
defWriterOpts :: P.Template Text -> P.WriterOptions
|
||||||
|
defWriterOpts t = def { P.writerExtensions = P.pandocExtensions, P.writerTemplate = Just t }
|
||||||
|
|
||||||
|
|
||||||
-------------------------
|
-------------------------
|
||||||
@ -92,12 +97,10 @@ setIsDeFromLang m
|
|||||||
reTemplateLetter :: P.PandocMonad m => P.Meta -> StoredMarkup -> m Text
|
reTemplateLetter :: P.PandocMonad m => P.Meta -> StoredMarkup -> m Text
|
||||||
reTemplateLetter meta StoredMarkup{..} = do
|
reTemplateLetter meta StoredMarkup{..} = do
|
||||||
tmpl <- compileTemplate strictMarkupInput
|
tmpl <- compileTemplate strictMarkupInput
|
||||||
-- TODO: write cacheHere Version using DB Key of StoredMarkup with Unique DB Argument instead of StoredMarkup
|
|
||||||
doc <- areader readerOpts strictMarkupInput
|
doc <- areader readerOpts strictMarkupInput
|
||||||
let writerOpts = def { P.writerExtensions = P.pandocExtensions
|
let writerOpts = def { P.writerExtensions = P.pandocExtensions
|
||||||
, P.writerTemplate = Just tmpl }
|
, P.writerTemplate = Just tmpl }
|
||||||
P.writeMarkdown writerOpts
|
P.writeMarkdown writerOpts
|
||||||
$ P.setMeta ("foooooo"::Text) ("baaaaaaar"::Text) -- TODO: just for debugging
|
|
||||||
$ appMeta setIsDeFromLang
|
$ appMeta setIsDeFromLang
|
||||||
$ addMeta meta doc
|
$ addMeta meta doc
|
||||||
where
|
where
|
||||||
@ -111,26 +114,45 @@ reTemplateLetter meta StoredMarkup{..} = do
|
|||||||
MarkupMarkdown -> P.readMarkdown
|
MarkupMarkdown -> P.readMarkdown
|
||||||
MarkupPlaintext -> P.readMarkdown
|
MarkupPlaintext -> P.readMarkdown
|
||||||
|
|
||||||
|
reTemplateLetter' :: P.PandocMonad m => P.Meta -> Text -> m Text
|
||||||
|
reTemplateLetter' meta md = do
|
||||||
|
tmpl <- compileTemplate md
|
||||||
|
doc <- P.readMarkdown readerOpts md
|
||||||
|
let writerOpts = def { P.writerExtensions = P.pandocExtensions
|
||||||
|
, P.writerTemplate = Just tmpl }
|
||||||
|
P.writeMarkdown writerOpts
|
||||||
|
$ appMeta setIsDeFromLang
|
||||||
|
$ addMeta meta doc
|
||||||
|
where
|
||||||
|
readerOpts = def { P.readerExtensions = P.pandocExtensions
|
||||||
|
, P.readerStripComments = True
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
--pdfDIN5008 :: P.PandocMonad m => Text -> m L.ByteString -- for pandoc > 2.18
|
--pdfDIN5008 :: P.PandocMonad m => Text -> m L.ByteString -- for pandoc > 2.18
|
||||||
pdfDIN5008' :: Text -> P.PandocIO L.ByteString
|
pdfDIN5008' :: P.Meta -> Text -> P.PandocIO L.ByteString
|
||||||
pdfDIN5008' md = do
|
pdfDIN5008' meta md = do
|
||||||
tmpl <- compileTemplate templateDIN5008
|
tmpl <- compileTemplate templateDIN5008
|
||||||
let readerOpts = def { P.readerExtensions = P.pandocExtensions }
|
let readerOpts = def { P.readerExtensions = P.pandocExtensions }
|
||||||
writerOpts = def { P.writerExtensions = P.pandocExtensions
|
writerOpts = def { P.writerExtensions = P.pandocExtensions
|
||||||
, P.writerTemplate = Just tmpl }
|
, P.writerTemplate = Just tmpl }
|
||||||
doc <- P.readMarkdown readerOpts md
|
doc <- P.readMarkdown readerOpts md
|
||||||
makePDF writerOpts doc
|
makePDF writerOpts
|
||||||
|
$ appMeta setIsDeFromLang
|
||||||
|
$ addMeta meta doc
|
||||||
|
|
||||||
-- | creates a PDF using the din5008 template
|
-- | creates a PDF using the din5008 template
|
||||||
pdfDIN5008 :: Text -> HandlerFor UniWorX (Either P.PandocError L.ByteString)
|
pdfDIN5008 :: P.Meta -> Text -> HandlerFor UniWorX (Either P.PandocError L.ByteString)
|
||||||
pdfDIN5008 md = do
|
pdfDIN5008 meta md = do
|
||||||
e_tmpl <- $cachedHereBinary ("din5008"::Text) (liftIO . P.runIO $ compileTemplate templateDIN5008)
|
e_tmpl <- $cachedHereBinary ("din5008"::Text) (liftIO . P.runIO $ compileTemplate templateDIN5008)
|
||||||
actRight e_tmpl $ \tmpl -> liftIO . P.runIO $ do
|
actRight e_tmpl $ \tmpl -> liftIO . P.runIO $ do
|
||||||
let readerOpts = def { P.readerExtensions = P.pandocExtensions }
|
let readerOpts = def { P.readerExtensions = P.pandocExtensions }
|
||||||
writerOpts = def { P.writerExtensions = P.pandocExtensions
|
writerOpts = def { P.writerExtensions = P.pandocExtensions
|
||||||
, P.writerTemplate = Just tmpl }
|
, P.writerTemplate = Just tmpl }
|
||||||
doc <- P.readMarkdown readerOpts md
|
doc <- P.readMarkdown readerOpts md
|
||||||
makePDF writerOpts doc
|
makePDF writerOpts
|
||||||
|
$ appMeta setIsDeFromLang
|
||||||
|
$ addMeta meta doc
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -176,4 +198,10 @@ pdfRenewal :: P.Meta -> HandlerFor UniWorX (Either P.PandocError L.ByteString)
|
|||||||
pdfRenewal meta = do
|
pdfRenewal meta = do
|
||||||
e_txt <- mdRenewal' meta
|
e_txt <- mdRenewal' meta
|
||||||
--actRight e_txt (pdfDIN5008 . appMeta setIsDeFromLang . addMeta meta) -- try this
|
--actRight e_txt (pdfDIN5008 . appMeta setIsDeFromLang . addMeta meta) -- try this
|
||||||
actRight e_txt pdfDIN5008
|
actRight e_txt $ pdfDIN5008 meta
|
||||||
|
|
||||||
|
-- | like pdfRenewal but without caching
|
||||||
|
pdfRenewal' :: P.Meta -> P.PandocIO L.ByteString
|
||||||
|
pdfRenewal' meta = do
|
||||||
|
doc <- reTemplateLetter' meta templateRenewal
|
||||||
|
pdfDIN5008' meta doc
|
||||||
|
|||||||
@ -34,7 +34,21 @@ address:
|
|||||||
- Musterstraße 11
|
- Musterstraße 11
|
||||||
- 12345 Musterstadt
|
- 12345 Musterstadt
|
||||||
...
|
...
|
||||||
|
$if(titleblock)$
|
||||||
|
$titleblock$
|
||||||
|
|
||||||
|
$endif$
|
||||||
|
$for(header-includes)$
|
||||||
|
$header-includes$
|
||||||
|
|
||||||
|
$endfor$
|
||||||
|
$for(include-before)$
|
||||||
|
$include-before$
|
||||||
|
|
||||||
|
$endfor$
|
||||||
|
|
||||||
$if(is-de)$
|
$if(is-de)$
|
||||||
|
|
||||||
<!-- deutsche Version des Briefes -->
|
<!-- deutsche Version des Briefes -->
|
||||||
die Gültigkeit Ihres Vorfeldführerscheines läuft demnächst ab.
|
die Gültigkeit Ihres Vorfeldführerscheines läuft demnächst ab.
|
||||||
Durch die erfolgreiche Teilnahme an einem E-Lernen können Sie
|
Durch die erfolgreiche Teilnahme an einem E-Lernen können Sie
|
||||||
@ -57,6 +71,7 @@ erneut der Grundkurs bei der Fahrerausbildung absolviert werden.
|
|||||||
Bei Fragen können Sie sich gerne an das Team der Fahrerausbildung wenden.
|
Bei Fragen können Sie sich gerne an das Team der Fahrerausbildung wenden.
|
||||||
|
|
||||||
$else$
|
$else$
|
||||||
|
|
||||||
<!-- englische Version des Briefes -->
|
<!-- englische Version des Briefes -->
|
||||||
|
|
||||||
your apron diving licence is about to expire soon.
|
your apron diving licence is about to expire soon.
|
||||||
|
|||||||
35
testdata/test_letters.hs
vendored
35
testdata/test_letters.hs
vendored
@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
-- usage:
|
-- usage:
|
||||||
-- > npm run build
|
-- > npm run build
|
||||||
-- > stack ghci -- testdata/test_letters.hs
|
-- > stack ghci -- testdata/test_letters.hs
|
||||||
@ -17,6 +18,10 @@ import Utils.Print
|
|||||||
import Handler.PrintCenter
|
import Handler.PrintCenter
|
||||||
|
|
||||||
|
|
||||||
|
mdTmpl :: Text
|
||||||
|
mdTmpl = "---\nfoo: fooOrg\nbar: barOrg\n---\nHere is some text\n - foo: $foo$\n - bar: $bar$\nbody\n$body$\nend\n"
|
||||||
|
|
||||||
|
|
||||||
test :: IO T.Text
|
test :: IO T.Text
|
||||||
test = do
|
test = do
|
||||||
res <- P.runIO $ reTemplateLetter (Handler.PrintCenter.mprToMeta def) (markdownToStoredMarkup templateRenewal)
|
res <- P.runIO $ reTemplateLetter (Handler.PrintCenter.mprToMeta def) (markdownToStoredMarkup templateRenewal)
|
||||||
@ -38,3 +43,33 @@ test3 = do
|
|||||||
let doc2 = P.setMeta (T.pack "foooooo") (T.pack "baaaaaaar") $ appMeta setIsDeFromLang $ addMeta (mprToMeta def) doc1
|
let doc2 = P.setMeta (T.pack "foooooo") (T.pack "baaaaaaar") $ appMeta setIsDeFromLang $ addMeta (mprToMeta def) doc1
|
||||||
writerOpts = def { P.writerExtensions = P.enableExtension P.Ext_yaml_metadata_block P.pandocExtensions}
|
writerOpts = def { P.writerExtensions = P.enableExtension P.Ext_yaml_metadata_block P.pandocExtensions}
|
||||||
P.runIOorExplode $ P.writeMarkdown writerOpts doc2
|
P.runIOorExplode $ P.writeMarkdown writerOpts doc2
|
||||||
|
|
||||||
|
test4 = do
|
||||||
|
pdf <- P.runIOorExplode $ pdfRenewal' $ mprToMeta def
|
||||||
|
LBS.writeFile "./testgen.pdf" pdf
|
||||||
|
|
||||||
|
|
||||||
|
t5meta :: P.Meta
|
||||||
|
t5meta = P.setMeta "lang" ("de-de"::Text) $ P.setMeta "foo" ("HERE"::Text) mempty
|
||||||
|
|
||||||
|
t5meta2 :: P.Meta
|
||||||
|
t5meta2 = P.setMeta "lang" ("en-gb"::Text) $ P.setMeta "bar" ("XYZ"::Text) mempty
|
||||||
|
|
||||||
|
|
||||||
|
t5redoc :: IO Text
|
||||||
|
t5redoc = P.runIOorExplode $ reTemplateLetter' t5meta mdTmpl
|
||||||
|
|
||||||
|
t5tmpl :: IO (P.Template Text)
|
||||||
|
t5tmpl = P.runIOorExplode $ compileTemplate mdTmpl
|
||||||
|
|
||||||
|
t5doc :: IO P.Pandoc
|
||||||
|
t5doc = P.runIOorExplode $ P.readMarkdown defReaderOpts mdTmpl
|
||||||
|
|
||||||
|
t5reDoc2 :: IO Text
|
||||||
|
t5reDoc2 = do
|
||||||
|
t <- t5tmpl
|
||||||
|
d <- t5doc
|
||||||
|
let P.Pandoc _ di = d
|
||||||
|
-- d2 = P.Pandoc t5meta di -- this works
|
||||||
|
d2 = addMeta t5meta d
|
||||||
|
P.runIOorExplode $ P.writeMarkdown (defWriterOpts t) d2
|
||||||
Reference in New Issue
Block a user