Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedded pdf #341

Merged
merged 11 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions default/templates/filters/embed-pdf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<object class="mb-3 embedded-pdf" type="application/pdf" data="${ema:url}">
<a href="${ema:url}">Open pdf</a>
</object>
5 changes: 5 additions & 0 deletions default/templates/hooks/more-head.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@
.header-font {
font-family: 'MavenPro', sans-serif;
}

.embedded-pdf {
width: 100%;
height: 800px;
}
mnaoumov marked this conversation as resolved.
Show resolved Hide resolved
</style>
7 changes: 7 additions & 0 deletions docs/demo/embed.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ It is also posible to add images inline (example, here's the site favicon: [![[f
The following is the result of using `![[death-note.mp4]]`.

![[death-note.mp4]]


### PDFs

An embedded version of [GitHub's Git Cheat Sheet](https://education.github.com/git-cheat-sheet-education.pdf) is shown below:

![[git-cheat-sheet-education.pdf]]
Binary file added docs/demo/git-cheat-sheet-education.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Emanote/Pandoc/Renderer/Embed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ embedStaticFileRoute model wl staticFile = do
| any (`T.isSuffixOf` toText fp) videoExts -> do
pure . runEmbedTemplate "video" $ do
"ema:url" ## HI.textSplice url
| any (`T.isSuffixOf` toText fp) ["pdf"] -> do
mnaoumov marked this conversation as resolved.
Show resolved Hide resolved
pure . runEmbedTemplate "pdf" $ do
"ema:url" ## HI.textSplice url
| otherwise -> Nothing

imageExts :: [Text]
Expand Down
18 changes: 12 additions & 6 deletions src/Heist/Extra/Splices/Pandoc/Render.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Data.Map.Syntax ((##))
import Data.Text qualified as T
import Emanote.Pandoc.Markdown.Syntax.WikiLink qualified as WL
import Heist qualified as H
import Heist.Extra (runCustomNode)
import Heist.Extra qualified as HE
import Heist.Extra.Splices.Pandoc.Attr (concatAttr, rpAttr)
import Heist.Extra.Splices.Pandoc.Ctx
( RenderCtx (..),
Expand All @@ -40,7 +40,7 @@ withTplTag :: RenderCtx -> Text -> H.Splices (HI.Splice Identity) -> HI.Splice I
withTplTag RenderCtx {..} name splices default_ =
case X.childElementTag name =<< rootNode of
Nothing -> default_
Just node -> runCustomNode node splices
Just node -> HE.runCustomNode node splices

rpBlock' :: RenderCtx -> B.Block -> HI.Splice Identity
rpBlock' ctx@RenderCtx {..} b = case b of
Expand Down Expand Up @@ -224,10 +224,16 @@ rpInline' ctx@RenderCtx {..} i = case i of
<> rpAttr (concatAttr attr $ iAttr i)
one . X.Element "a" attrs <$> foldMapM (rpInline ctx) is
B.Image attr is (url, tit) -> do
let attrs =
catMaybes [pure ("src", url), guard (not $ T.null tit) >> pure ("title", tit), pure ("alt", WL.plainify is)]
<> rpAttr (rewriteClass ctx attr)
pure $ one . X.Element "img" attrs $ mempty
let lowerUrl = T.toLower url
if T.isInfixOf ".pdf?t=" lowerUrl || T.isSuffixOf ".pdf" lowerUrl
then do
tpl <- HE.lookupHtmlTemplateMust "/templates/filters/embed-pdf"
HE.runCustomTemplate tpl ("ema:url" ## HI.textSplice url)
else do
let attrs =
catMaybes [pure ("src", url), guard (not $ T.null tit) >> pure ("title", tit), pure ("alt", WL.plainify is)]
<> rpAttr (rewriteClass ctx attr)
pure $ one . X.Element "img" attrs $ mempty
srid marked this conversation as resolved.
Show resolved Hide resolved
B.Note _bs -> do
-- Footnotes are to be handled separately; see Footenotes.hs
pure $ one $ X.Element "sup" mempty $ one $ X.TextNode "*"
Expand Down