This commit is contained in:
Yann Esposito (Yogsototh) 2020-06-23 19:28:41 +02:00
parent 3a13435710
commit aef6f81274
Signed by untrusted user who does not match committer: yogsototh
GPG key ID: 7B19A4C650D59646
4 changed files with 137 additions and 80 deletions

View file

@ -6,33 +6,28 @@ import Protolude
import Development.Shake import Development.Shake
import Development.Shake.Command import Development.Shake.Command
import Development.Shake.FilePath import Development.Shake.FilePath
import Development.Shake.Util
import Control.Monad.Fail import Control.Monad.Fail
import Data.Aeson import Data.Aeson
import Text.Megaparsec -- import qualified Text.Megaparsec as Megaparsec
import Data.Default ( Default(def) ) import Data.Default ( Default(def) )
import qualified Data.Set as Set
import qualified Data.Text as T import qualified Data.Text as T
import Text.Mustache import Text.Mustache
import Text.Pandoc.Class ( PandocPure, PandocMonad) import Text.Pandoc.Class (PandocMonad)
import qualified Text.Pandoc.Class as Pandoc import qualified Text.Pandoc.Class as Pandoc
import Text.Pandoc.Definition ( Pandoc(..) import Text.Pandoc.Definition ( Pandoc(..)
, Block(..) , Block(..)
, Inline , Inline
, nullMeta , nullMeta
, docTitle , docTitle
, lookupMeta
, docDate , docDate
, docAuthors , docAuthors
) )
import Text.Pandoc.Extensions ( getDefaultExtensions )
import Text.Pandoc.Options ( ReaderOptions(..) import Text.Pandoc.Options ( ReaderOptions(..)
, WriterOptions(..) , WriterOptions(..)
, TrackChanges(RejectChanges) , ObfuscationMethod(..)
) )
import qualified Text.Pandoc.Readers as Readers import qualified Text.Pandoc.Readers as Readers
import qualified Text.Pandoc.Templates as PandocTemplates
import qualified Text.Pandoc.Writers as Writers import qualified Text.Pandoc.Writers as Writers
main :: IO () main :: IO ()
@ -47,6 +42,9 @@ main = shakeArgs shOpts buildRules
-- Configuration -- Configuration
-- Should probably go in a Reader Monad -- Should probably go in a Reader Monad
srcDir :: FilePath
srcDir = "src"
siteDir :: FilePath siteDir :: FilePath
siteDir = "_site" siteDir = "_site"
@ -82,57 +80,115 @@ getBlogpostFromMetas path toc pandoc@(Pandoc meta _) = do
sortByPostDate :: [BlogPost] -> [BlogPost] sortByPostDate :: [BlogPost] -> [BlogPost]
sortByPostDate = sortByPostDate =
sortBy (\b a -> compare (Down (postDate a)) (Down (postDate b))) sortBy (\a b-> compare (Down (postDate a)) (Down (postDate b)))
build :: FilePath -> FilePath build :: FilePath -> FilePath
build = (</>) siteDir build = (</>) siteDir
genAllDeps :: [FilePattern] -> Action [FilePath]
genAllDeps patterns = do
allMatchedFiles <- getDirectoryFiles srcDir patterns
allMatchedFiles &
filter ((/= "html") . takeExtension) &
filter (null . takeExtension) &
map (siteDir </>) &
return
buildRules :: Rules () buildRules :: Rules ()
buildRules = do buildRules = do
cleanRule cleanRule
-- build "//*" %> copy
allRule allRule
getPost <- mkGetPost getPost <- mkGetPost
getPosts <- mkGetPosts getPost getPosts <- mkGetPosts getPost
getTemplate <- mkGetTemplate getTemplate <- mkGetTemplate
let cssDeps = map (siteDir </>) <$> getDirectoryFiles "src" ["css/*.css"] alternatives $ do
-- templateDeps = getDirectoryFiles "templates" ["*.mustache"] -- build "articles.html" %> \out -> do
build "//*.html" %> \out -> do -- css <- genAllDeps ["//*.css"]
css <- cssDeps -- posts <- getPosts ()
-- templates <- templateDeps -- need $ css <> map postUrl (sortByPostDate posts)
template <- getTemplate ("templates" </> "main.mustache") -- let titles = toS $ T.intercalate "\n" $ map postTitle posts
let srcFile = "src" </> (dropDirectory1 (replaceExtension out "org")) -- writeFile' out titles
liftIO $ putText $ "need: " <> (toS srcFile) <> " <- " <> (toS out) build "//*.html" %> genHtmlAction getPost getTemplate
need $ css <> [srcFile] -- build "//*.org" %> copy
bp <- getPost srcFile -- build "//*.jpg" %> copy
eitherHtml <- liftIO $ Pandoc.runIO $ Writers.writeHtml5String (def { writerTableOfContents = (postToc bp) }) (postBody bp)
copy :: FilePath -> Action ()
copy out = do
let src = srcDir </> (dropDirectory1 out)
copyFileChanged src out
genHtml :: (MonadIO m, MonadFail m) => BlogPost -> m Text
genHtml bp = do
eitherHtml <- liftIO $
Pandoc.runIO $
Writers.writeHtml5String
(def { writerTableOfContents = (postToc bp)
, writerEmailObfuscation = ReferenceObfuscation
})
(postBody bp)
case eitherHtml of case eitherHtml of
Left _ -> fail "BAD" Left _ -> fail "BAD"
Right innerHtml -> Right innerHtml -> return innerHtml
let htmlContent = renderMustache template $ object [ "title" .= postTitle bp
genHtmlAction
:: (FilePath -> Action BlogPost)
-> (FilePath -> Action Template) -> [Char] -> Action ()
genHtmlAction getPost getTemplate out = do
template <- getTemplate ("templates" </> "main.mustache")
let srcFile = srcDir </> (dropDirectory1 (out -<.> "org"))
liftIO $ putText $ "need: " <> (toS srcFile) <> " -> " <> (toS out)
need [srcFile]
bp <- getPost srcFile
innerHtml <- genHtml bp
let htmlContent =
renderMustache template $ object [ "title" .= postTitle bp
, "authors" .= postAuthors bp , "authors" .= postAuthors bp
, "date" .= postDate bp , "date" .= postDate bp
, "body" .= innerHtml , "body" .= innerHtml
] ]
in writeFile' out (toS htmlContent) writeFile' out (toS htmlContent)
build "articles.html" %> \out -> do
css <- cssDeps allHtmlAction :: Action ()
posts <- getPosts () allHtmlAction = do
need $ css <> map postUrl (sortByPostDate posts) allOrgFiles <- getDirectoryFiles srcDir ["//*.org"]
let titles = toS $ T.intercalate "\n" $ map postTitle posts let allHtmlFiles = map (-<.> "html") allOrgFiles
writeFile' out titles need (map build (allHtmlFiles
build "css/*.css" %> \out -> do -- <> ["articles.html"]
let src = "src" </> (dropDirectory1 out) ))
dst = out
liftIO $ putText $ toS $ "src:" <> src <> " => dst: " <> dst compressImage :: CmdResult b => FilePath -> Action b
copyFile' src dst compressImage img = do
let src = srcDir </> img
dst = siteDir </> img
need [src]
let dir = takeDirectory dst
dirExists <- doesDirectoryExist dir
when (not dirExists) $
command [] "mkdir" ["-p", dir]
command [] "convert" [src
, "-strip"
, "-resize","320x320>"
, "-interlace","Plane"
, "-quality","85"
, "-define","filter:blur=0.75"
, "-filter","Gaussian"
, "-ordered-dither","o4x4,4"
, dst ]
allRule :: Rules () allRule :: Rules ()
allRule = allRule =
phony "all" $ do phony "all" $ do
allOrgFiles <- getDirectoryFiles "src" ["//*.org"] allAssets <- filter (/= ".DS_Store") <$> getDirectoryFiles srcDir ["//*.*"]
let allHtmlFiles = map (flip replaceExtension "html") allOrgFiles forM_ allAssets $ \asset ->
need (map build (allHtmlFiles <> ["index.html", "articles.html"])) case (takeExtension asset) of
".jpg" -> compressImage asset
".jpeg" -> compressImage asset
".gif" -> compressImage asset
".png" -> compressImage asset
_ -> copyFileChanged (srcDir </> asset) (siteDir </> asset)
allHtmlAction
cleanRule :: Rules () cleanRule :: Rules ()
cleanRule = cleanRule =
@ -161,7 +217,7 @@ mkGetPost :: Rules (FilePath -> Action BlogPost)
mkGetPost = newCache $ \path -> do mkGetPost = newCache $ \path -> do
fileContent <- readFile' path fileContent <- readFile' path
let toc = tocRequested (toS fileContent) let toc = tocRequested (toS fileContent)
eitherResult <- liftIO $ Pandoc.runIO $ Readers.readOrg def (toS fileContent) eitherResult <- liftIO $ Pandoc.runIO $ Readers.readOrg (def { readerStandalone = True }) (toS fileContent)
case eitherResult of case eitherResult of
Left _ -> fail "BAD" Left _ -> fail "BAD"
Right pandoc -> getBlogpostFromMetas path toc pandoc Right pandoc -> getBlogpostFromMetas path toc pandoc

View file

@ -556,9 +556,10 @@ a,a:visited { color: var(--hl); }
/* ---- SYNTAX HIGHLIGHTING ---- */ /* ---- SYNTAX HIGHLIGHTING ---- */
#table-of-contents { text-align: left; } #table-of-contents { text-align: left; }
.org-rainbow-delimiters-depth-1, .org-rainbow-delimiters-depth-9, .org-rainbow-delimiters-depth-1, .org-rainbow-delimiters-depth-9,
.org-css-selector, .org-builtin, .org-css-selector, .org-builtin,
.IN_REVIEW { .IN_REVIEW, .ex {
color:var(--c); color:var(--c);
} }
@ -573,7 +574,7 @@ a,a:visited { color: var(--hl); }
} }
.org-rainbow-delimiters-depth-4, .org-diff-hunk-header, .org-sh-quoted-exec, .org-rainbow-delimiters-depth-4, .org-diff-hunk-header, .org-sh-quoted-exec,
.CANCELED { .CANCELED, .bu {
color:var(--m); color:var(--m);
} }
.org-rainbow-delimiters-depth-5, .org-diff-removed, .TODO { .org-rainbow-delimiters-depth-5, .org-diff-removed, .TODO {

View file

@ -44,9 +44,9 @@ goblins.
Those costume looks very bad and cheap. Those costume looks very bad and cheap.
So much you can only find them not terrorizing but funny and ridiculous. So much you can only find them not terrorizing but funny and ridiculous.
#+ATTR_HTML: A goblin
#+CAPTION: One goblin during the introduction scene of Troll 2 #+CAPTION: One goblin during the introduction scene of Troll 2
#+NAME: fig:troll-2-intro #+NAME: fig:troll-2-intro
#+ATTR_HTML: A goblin
[[./Troll-2-intro.jpg]] [[./Troll-2-intro.jpg]]
Soon after that, you realize the acting of all actors is extremely bad. Soon after that, you realize the acting of all actors is extremely bad.
@ -55,9 +55,9 @@ To give you an idea, the only equal bad acting I ever witnessed was while
looking at amateurs first Youtube movies trying to follow a scenario. looking at amateurs first Youtube movies trying to follow a scenario.
Apparently most actors were amateurs, it was their first and last movie. Apparently most actors were amateurs, it was their first and last movie.
#+ATTR_HTML: A bad acting demonstration
#+CAPTION: One particularly terrible acting scene #+CAPTION: One particularly terrible acting scene
#+NAME: fig:bad-acting #+NAME: fig:bad-acting
#+ATTR_HTML: A bad acting demonstration
[[file:bad-acting.png]] [[file:bad-acting.png]]
The dialog are, really something... The dialog are, really something...
@ -83,9 +83,9 @@ They win against the monsters with, what I believe was a failed attempt at
humor. humor.
It misses the point so bad, that the irony still make it funny. It misses the point so bad, that the irony still make it funny.
#+ATTR_HTML: Eliott prevents his family to eat the food by urinating on the table
#+CAPTION: Our hero save the day by urinating on the table. His family is frozen for 30s said grandpa, they were for 70s. #+CAPTION: Our hero save the day by urinating on the table. His family is frozen for 30s said grandpa, they were for 70s.
#+NAME: fig:prevent-eating #+NAME: fig:prevent-eating
#+ATTR_HTML: Eliott prevents his family to eat the food by urinating on the table
[[./prevent-eating-scene.jpg]] [[./prevent-eating-scene.jpg]]
Of course, the very last scene is a classical so terrible cliché. Of course, the very last scene is a classical so terrible cliché.

View file

@ -1492,7 +1492,7 @@ The only way to work around this problem is to use some meta-programming
trick, for example using the pre-processor. trick, for example using the pre-processor.
In C++ there is a better way, C++ templates: In C++ there is a better way, C++ templates:
#+BEGIN_SRC c++ #+BEGIN_SRC cpp
#include <iostream> #include <iostream>
#include <complex> #include <complex>
using namespace std; using namespace std;
@ -3880,9 +3880,7 @@ I will not argue much, but mainly, semantic versionning and Haskell
versionning are just a "right to break things to your users". versionning are just a "right to break things to your users".
I don't want to talk a lot more about this, but, it would be nice if more I don't want to talk a lot more about this, but, it would be nice if more
people would watch this talk[fn:9] related to versionning. people would watch this talk[fn:8] related to versionning.
[fn:9]: [[https://www.youtube.com/watch?v=oyLBGkS5ICk][Spec-ulation Keynote - Rich Hickey]]
If you want to know more about Haskell versionning convention: If you want to know more about Haskell versionning convention:
https://pvp.haskell.org https://pvp.haskell.org
@ -4109,3 +4107,5 @@ Libraries - parts of hslibs/lang.
[fn:7] Well, you'll certainly need to practice a bit to get used to them [fn:7] Well, you'll certainly need to practice a bit to get used to them
and to understand when you can use them and create your own. But and to understand when you can use them and create your own. But
you already made a big step in this direction. you already made a big step in this direction.
[fn:8] [[https://www.youtube.com/watch?v=oyLBGkS5ICk][Spec-ulation Keynote - Rich Hickey]]