Discussion:
[hakyll] Multiple version of the same file given it's metadata.
p***@gmail.com
2018-03-04 13:48:05 UTC
Permalink
Hi!

First of all, as many, I'm neither a Haskell guru nor a Hakyll one so the
question might sound silly.

I'd like to generate multiple versions of the same file (say, markdown
file) depending on a metadata.
The goal here is to be able to write some articles (but no all) in multiple
languages (French and English).

Given it's input file:

---
title: example article
language: Fr, En
---

Fr: Je suis une ligne en français.
En: Im written in English.

Je suis toujours là.
I'm always here.

I'd like look at the language metadata and output:

---
title: example article
language: Fr, En
---

Fr: Je suis une ligne en français.

Je suis toujours là.
I'm always here.

And :

---
title: example article
language: Fr, En
---

Fr: Je suis une ligne en français.

Je suis toujours là.
I'm always here.
---


My quick-fix is as follow:

type Language = String

filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]

languageRoute :: Language -> Routes
languageRoute language =
customRoute createLanguageRoute `composeRoutes` setExtension "html"
where createLanguageRoute identifier =
map toLower language </> toFilePath identifier

articles :: Rules ()
articles =
forM_ ["Fr", "En"] $ \language -> do
match "articles/*" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
It allows me to have N versions of the same file but this N does not depend on the file itself.

I saw multiple pages related to my issue but none of them led me to a solution:


- https://jaspervdj.be/hakyll/tutorials/06-versions.html
- https://github.com/jaspervdj/hakyll/issues/263
- https://groups.google.com/forum/#!topic/hakyll/fpmTT8ub-nA
- https://github.com/jaspervdj/hakyll/pull/300

Many thanks in advance!
Paco V.
--
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.
p***@gmail.com
2018-03-04 18:15:16 UTC
Permalink
Hi again!

In the mean time I discovered https://github.com/jaspervdj/hakyll-bibtex
which could be the solution I need.

A two step approach:
The first will preprocess my files and outputs their languages.
The second is in my previous mail but will take its input from the first
step instead of being hard coded.

Tell me if I'm wrong.
Post by p***@gmail.com
Hi!
First of all, as many, I'm neither a Haskell guru nor a Hakyll one so the
question might sound silly.
I'd like to generate multiple versions of the same file (say, markdown
file) depending on a metadata.
The goal here is to be able to write some articles (but no all) in
multiple languages (French and English).
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
En: Im written in English.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
type Language = String
filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]
languageRoute :: Language -> Routes
languageRoute language =
customRoute createLanguageRoute `composeRoutes` setExtension "html"
where createLanguageRoute identifier =
map toLower language </> toFilePath identifier
articles :: Rules ()
articles =
forM_ ["Fr", "En"] $ \language -> do
match "articles/*" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
It allows me to have N versions of the same file but this N does not depend on the file itself.
- https://jaspervdj.be/hakyll/tutorials/06-versions.html
- https://github.com/jaspervdj/hakyll/issues/263
- https://groups.google.com/forum/#!topic/hakyll/fpmTT8ub-nA
- https://github.com/jaspervdj/hakyll/pull/300
Many thanks in advance!
Paco V.
--
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.
p***@gmail.com
2018-03-04 22:01:54 UTC
Permalink
Annnd, it's me replying to myself again.

readLanguages :: FilePath -> IO [Language]
readLanguages filePath = do
ls <- lines <$> readFile filePath
return $ map (filter isAlpha) $ tail $ words $ head $ filter (isPrefixOf "languages:") ls



languages <- preprocess $ readLanguages "articles/2015-08-23-example.markdown"
forM_ languages $ \language -> do
match "articles/2015-08-23-example.markdown" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
Post by p***@gmail.com
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
This, works well for a single, hard-coded markdown file.
Still looking for a way to use a pattern to create some sort of map from my items to their languages.

Any clue?
Thanks in advance and sorry for the noise.
Post by p***@gmail.com
Hi again!
In the mean time I discovered https://github.com/jaspervdj/hakyll-bibtex
which could be the solution I need.
The first will preprocess my files and outputs their languages.
The second is in my previous mail but will take its input from the first
step instead of being hard coded.
Tell me if I'm wrong.
Hi!
First of all, as many, I'm neither a Haskell guru nor a Hakyll one so the
question might sound silly.
I'd like to generate multiple versions of the same file (say, markdown
file) depending on a metadata.
The goal here is to be able to write some articles (but no all) in
multiple languages (French and English).
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
En: Im written in English.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
type Language = String
filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]
languageRoute :: Language -> Routes
languageRoute language =
customRoute createLanguageRoute `composeRoutes` setExtension "html"
where createLanguageRoute identifier =
map toLower language </> toFilePath identifier
articles :: Rules ()
articles =
forM_ ["Fr", "En"] $ \language -> do
match "articles/*" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
It allows me to have N versions of the same file but this N does not depend on the file itself.
- https://jaspervdj.be/hakyll/tutorials/06-versions.html
- https://github.com/jaspervdj/hakyll/issues/263
- https://groups.google.com/forum/#!topic/hakyll/fpmTT8ub-nA
- https://github.com/jaspervdj/hakyll/pull/300
Many thanks in advance!
Paco V.
--
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.
Beerend Lauwers
2018-03-05 10:21:47 UTC
Permalink
Hi Paco,

I approached this in another way: I have one file per language. Here's the
code I used for a few multilingual
projects: https://github.com/beerendlauwers/hakyll-extra#hakylltranslation

There are a few examples available as well. I don't know if I added the
ability to have content in one language, but not the other, though.

Kind Regards,

Beerend Lauwers
Post by p***@gmail.com
Annnd, it's me replying to myself again.
readLanguages :: FilePath -> IO [Language]
readLanguages filePath = do
ls <- lines <$> readFile filePath
return $ map (filter isAlpha) $ tail $ words $ head $ filter (isPrefixOf "languages:") ls
languages <- preprocess $ readLanguages "articles/2015-08-23-example.markdown"
forM_ languages $ \language -> do
match "articles/2015-08-23-example.markdown" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
Post by p***@gmail.com
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
This, works well for a single, hard-coded markdown file.
Still looking for a way to use a pattern to create some sort of map from my items to their languages.
Any clue?
Thanks in advance and sorry for the noise.
Post by p***@gmail.com
Hi again!
In the mean time I discovered https://github.com/jaspervdj/hakyll-bibtex
which could be the solution I need.
The first will preprocess my files and outputs their languages.
The second is in my previous mail but will take its input from the first
step instead of being hard coded.
Tell me if I'm wrong.
Hi!
First of all, as many, I'm neither a Haskell guru nor a Hakyll one so
the question might sound silly.
I'd like to generate multiple versions of the same file (say, markdown
file) depending on a metadata.
The goal here is to be able to write some articles (but no all) in
multiple languages (French and English).
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
En: Im written in English.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
type Language = String
filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]
languageRoute :: Language -> Routes
languageRoute language =
customRoute createLanguageRoute `composeRoutes` setExtension "html"
where createLanguageRoute identifier =
map toLower language </> toFilePath identifier
articles :: Rules ()
articles =
forM_ ["Fr", "En"] $ \language -> do
match "articles/*" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
It allows me to have N versions of the same file but this N does not depend on the file itself.
- https://jaspervdj.be/hakyll/tutorials/06-versions.html
- https://github.com/jaspervdj/hakyll/issues/263
- https://groups.google.com/forum/#!topic/hakyll/fpmTT8ub-nA
- https://github.com/jaspervdj/hakyll/pull/300
Many thanks in advance!
Paco V.
--
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.
p***@gmail.com
2018-03-05 13:18:42 UTC
Permalink
Hi! And thanks for your reply!

I really wanted to have every languages in a single file for
maintainability purpose.

I finally managed to do it as
follow:


type Language = String

filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]

languagesFromLine :: String -> [Language]
languagesFromLine = languagesFromLine' . tail . dropWhile (/= ':')
where languagesFromLine' = map trim . splitAll ","

languagesFromFile :: FilePath -> IO [Language]
languagesFromFile filePath =
readFile filePath
Post by Beerend Lauwers
= (return . lines)
= (return . filter (isPrefixOf "languages"))
= (return . head)
= (return . languagesFromLine )
languagesFromFilePair :: FilePath -> IO (FilePath, [Language])
languagesFromFilePair filePath =
languagesFromFile filePath
Post by Beerend Lauwers
= return . (,) filePath
languagesFromRegex :: String -> IO [(FilePath, [Language])]
languagesFromRegex regex = namesMatching regex >>= mapM
languagesFromFilePair

And in the main:

foo <- preprocess $ languagesFromRegex "articles/*"
forM_ foo $ \(filePath, languages) -> do
forM_ languages $ \language -> do
match (fromGlob filePath) $ version language $ do
route $ languageRoute language
compile $ getResourceBody
Post by Beerend Lauwers
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html"
articleContext
Post by Beerend Lauwers
= loadAndApplyTemplate "templates/default.html"
articleContext
Post by Beerend Lauwers
= relativizeUrls
It requires a pre-process step which allows me to have a fine-grained
control over each article languages.
Post by Beerend Lauwers
Hi Paco,
I approached this in another way: I have one file per language. Here's the
https://github.com/beerendlauwers/hakyll-extra#hakylltranslation
There are a few examples available as well. I don't know if I added the
ability to have content in one language, but not the other, though.
Kind Regards,
Beerend Lauwers
Annnd, it's me replying to myself again.
readLanguages :: FilePath -> IO [Language]
readLanguages filePath = do
ls <- lines <$> readFile filePath
return $ map (filter isAlpha) $ tail $ words $ head $ filter (isPrefixOf "languages:") ls
languages <- preprocess $ readLanguages "articles/2015-08-23-example.markdown"
forM_ languages $ \language -> do
match "articles/2015-08-23-example.markdown" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
Post by p***@gmail.com
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
This, works well for a single, hard-coded markdown file.
Still looking for a way to use a pattern to create some sort of map from my items to their languages.
Any clue?
Thanks in advance and sorry for the noise.
Post by p***@gmail.com
Hi again!
In the mean time I discovered https://github.com/jaspervdj/hakyll-bibtex
which could be the solution I need.
The first will preprocess my files and outputs their languages.
The second is in my previous mail but will take its input from the first
step instead of being hard coded.
Tell me if I'm wrong.
Hi!
First of all, as many, I'm neither a Haskell guru nor a Hakyll one so
the question might sound silly.
I'd like to generate multiple versions of the same file (say, markdown
file) depending on a metadata.
The goal here is to be able to write some articles (but no all) in
multiple languages (French and English).
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
En: Im written in English.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
title: example article
language: Fr, En
---
Fr: Je suis une ligne en français.
Je suis toujours là.
I'm always here.
---
type Language = String
filterLanguage :: Language -> String -> Compiler String
filterLanguage language =
unixFilter "sed"
[ "-e", "s/^" ++ language ++ ": //"
, "-e", "/^..: /d"
]
languageRoute :: Language -> Routes
languageRoute language =
customRoute createLanguageRoute `composeRoutes` setExtension "html"
where createLanguageRoute identifier =
map toLower language </> toFilePath identifier
articles :: Rules ()
articles =
forM_ ["Fr", "En"] $ \language -> do
match "articles/*" $ version language $ do
route $ languageRoute language
compile $ getResourceBody
= withItemBody (filterLanguage language)
= renderPandoc
= loadAndApplyTemplate "templates/article.html" articleContext
= loadAndApplyTemplate "templates/default.html" articleContext
= relativizeUrls
It allows me to have N versions of the same file but this N does not depend on the file itself.
- https://jaspervdj.be/hakyll/tutorials/06-versions.html
- https://github.com/jaspervdj/hakyll/issues/263
- https://groups.google.com/forum/#!topic/hakyll/fpmTT8ub-nA
- https://github.com/jaspervdj/hakyll/pull/300
Many thanks in advance!
Paco V.
--
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...