Discussion:
[hakyll] I'm hitting a wall trying to scrape context fields from a file's body
k***@gmail.com
2017-04-02 22:49:28 UTC
Permalink
I've got some org-mode files that I'd like to include in my site. They're
used outside of my hakyll project, (indeed, they're a sub module in my
site's repo) so I'd like to avoid adding the yaml metadata header. Instead
I whipped up function to scrape the #+TITLE: and #+AUTHOR lines off the
file, but I'm not sure how to actually utilize that.

Here's what I've got so far:

match "dotfiles/*.org" $ do
route $ setExtension "html"

compile $ do
body <- getResourceBody
metadata <- return $ (fmap metadataFrom body)
-- metadataFrom :: String -> [(String, String)]
-- metadataFrom scrapes #+TITLE:, #+AUTHOR: keywords from org
files
-- [(String, String)] is a list of keys/values
-- that should end up as fields in the context.
--
-- But how? I don't understand monads well enough yet.
renderPandoc body
= loadAndApplyTemplate "templates/post.html" postCtx
= loadAndApplyTemplate "templates/default.html" postCtx
= relativizeUrls
Does it look like I'm on the right track so far? When I set out to do
this, I didn't realize I was going to be jumping ahead of where I was
learning Haskell. I've inched that forward a bit this weekend trying to
solve this, but would really appreciate some help. Be it of the "here's
some code that does something similar" variety, or "maybe you should read
$learning_resource$". Sorry if is this a redundant question. I did some
searching but didn't see anything that seemed particularly related.

Thanks!
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Gnoutcheff
2017-04-03 21:42:46 UTC
Permalink
Post by k***@gmail.com
I've got some org-mode files that I'd like to include in my site. They're
used outside of my hakyll project, (indeed, they're a sub module in my
site's repo) so I'd like to avoid adding the yaml metadata header. Instead
I whipped up function to scrape the #+TITLE: and #+AUTHOR lines off the
file, but I'm not sure how to actually utilize that.
So normally, we'd use metadataField [1] (which is part of the
defaultContext [2]) to convert Hakyll metadata fields into template
variables. What I'd do here is create the template variables myself
instead.

Something like this, perhaps:

match "dotfiles/*.org" $ do
route $ setExtension "html"

compile $ do
body <- getResourceBody
let metadata = metadataFrom (itemBody body)
let metadataCtxs =
map (\(key, value) -> constField key value) metadata
let metadataCtx = mconcat metadataCtxs
let postCtx' = metadataCtx `mappend` postCtx
renderPandoc body
Post by k***@gmail.com
= loadAndApplyTemplate "templates/post.html" postCtx'
= loadAndApplyTemplate "templates/default.html" postCtx'
= relativizeUrls
You may need to add a "import Data.Monoid" near the top of the file to
bring 'mconcat' and 'mappend' into scope.

HTH! I'm relatively new to Haskell myself, so please don't take this as
an authoritative example of good Haskell style.

Later,
Daniel

[1]
https://www.stackage.org/haddock/lts-8.8/hakyll-4.9.5.1/Hakyll-Web-Template-Context.html#v:metadataField
[2]
https://www.stackage.org/haddock/lts-8.8/hakyll-4.9.5.1/Hakyll-Web-Template-Context.html#v:defaultContext
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Keifer Miller
2017-04-05 02:08:00 UTC
Permalink
Post by Daniel Gnoutcheff
So normally, we'd use metadataField [1] (which is part of the
defaultContext [2]) to convert Hakyll metadata fields into template
variables. What I'd do here is create the template variables myself
instead.
Thanks for the response! I managed to get it working after setting it
aside and pondering for a while, but your
solution is cleaner than mine:

match "dotfiles/*.org" $ do
route $ setExtension "html"

compile $ do
body <- getResourceBody
orgMetadata <- return $ (fmap metadataFrom body)
let orgCtx = (mconcat $ fmap (\ (x,y)-> constField x y) $
itemBody orgMetadata) `mappend` defaultContext
renderPandoc body
Post by Daniel Gnoutcheff
= loadAndApplyTemplate "templates/post.html" orgCtx
= loadAndApplyTemplate "templates/default.html" orgCtx
= relativizeUrls
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
v***@gmail.com
2017-04-11 02:51:13 UTC
Permalink
пПМеЎельМОк, 3 апреля 2017 г., 1:51:34 UTC+3 пПльзПватель Keifer Miller
Post by k***@gmail.com
I've got some org-mode files that I'd like to include in my site.
They're used outside of my hakyll project, (indeed, they're a sub module in
my site's repo) so I'd like to avoid adding the yaml metadata header.
Instead I whipped up function to scrape the #+TITLE: and #+AUTHOR lines off
the file, but I'm not sure how to actually utilize that.
match "dotfiles/*.org" $ do
route $ setExtension "html"
compile $ do
body <- getResourceBody
metadata <- return $ (fmap metadataFrom body)
-- metadataFrom :: String -> [(String, String)]
-- metadataFrom scrapes #+TITLE:, #+AUTHOR: keywords from org
files
-- [(String, String)] is a list of keys/values
-- that should end up as fields in the context.
--
-- But how? I don't understand monads well enough yet.
renderPandoc body
= loadAndApplyTemplate "templates/post.html" postCtx
= loadAndApplyTemplate "templates/default.html" postCtx
= relativizeUrls
Does it look like I'm on the right track so far? When I set out to do
this, I didn't realize I was going to be jumping ahead of where I was
learning Haskell. I've inched that forward a bit this weekend trying to
solve this, but would really appreciate some help. Be it of the "here's
some code that does something similar" variety, or "maybe you should read
$learning_resource$". Sorry if is this a redundant question. I did some
searching but didn't see anything that seemed particularly related.
Thanks!
--
You received this message because you are subscribed to the Google Groups "hakyll" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...