~yoctocell/yoctocell.xyz

446563565e59aad1b3578d8bbe4d4641626e10da — yoctocell 1 year, 4 days ago 878d171
Use org-mode metadata instead of yaml

Based on this repo: https://github.com/nasyxx/NasyMoe//blob/a0de680bacad1396a165fc35336387d8d28a6f35/src/Site.hs#L333.
4 files changed, 63 insertions(+), 16 deletions(-)

D posts/2020-11-27-hello-world.org
A posts/hello-world.org
M site.hs
M yoctocell-xyz.cabal
D posts/2020-11-27-hello-world.org => posts/2020-11-27-hello-world.org +0 -7
@@ 1,7 0,0 @@
---
title: Hello World
author: yoctocell
---

* Hello World!
This is my first blog post!

A posts/hello-world.org => posts/hello-world.org +5 -0
@@ 0,0 1,5 @@
#+TITLE: Hello World
#+DATE: 2020-11-27
#+AUTHOR: yoctocell

This is my first blog post!

M site.hs => site.hs +56 -9
@@ 1,9 1,16 @@
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import           Data.Monoid (mappend)
import           Data.Char             (toLower)
import           Data.List             (isPrefixOf)
import           Data.List.Split       (splitOn)
import           System.FilePath.Posix (joinPath, replaceExtension,
                                        splitDirectories, takeDirectory,
                                        takeFileName, (</>))

import           Hakyll



--------------------------------------------------------------------------------
config :: Configuration
config = defaultConfiguration { destinationDirectory = "_dist" }


@@ 24,17 31,14 @@ main = hakyllWith config $ do
            >>= loadAndApplyTemplate "templates/default.html" defaultContext
            >>= relativizeUrls

    match "posts/*" $ do
        route $ setExtension "html"
        compile $ pandocCompiler
            >>= loadAndApplyTemplate "templates/post.html"    postCtx
            >>= loadAndApplyTemplate "templates/default.html" postCtx
            >>= relativizeUrls
    match "posts/*.org" $ do
      route tempRoute
      compile $ getResourceString >>= orgCompiler

    create ["archive.html"] $ do
        route idRoute
        compile $ do
            posts <- recentFirst =<< loadAll "posts/*"
            posts <- recentFirst =<< loadAll "_temp/posts/*"
            let archiveCtx =
                    listField "posts" postCtx (return posts) `mappend`
                    constField "title" "Archives"            `mappend`


@@ 49,7 53,7 @@ main = hakyllWith config $ do
    match "index.html" $ do
        route idRoute
        compile $ do
            posts <- recentFirst =<< loadAll "posts/*"
            posts <- recentFirst =<< loadAll "_temp/posts/*"
            let indexCtx =
                    listField "posts" postCtx (return posts) `mappend`
                    defaultContext


@@ 59,6 63,14 @@ main = hakyllWith config $ do
                >>= loadAndApplyTemplate "templates/default.html" indexCtx
                >>= relativizeUrls

    match blogPattern $ do
        route cleanRouteFromTemp
        compile $ do
            pandocCompiler
            >>= loadAndApplyTemplate "templates/post.html"    postCtx
            >>= loadAndApplyTemplate "templates/default.html" postCtx
            >>= relativizeUrls

    match "templates/*" $ compile templateBodyCompiler




@@ 68,3 80,38 @@ postCtx =
    dateField "date" "%B %e, %Y" `mappend`
    defaultContext

-- | From org get metadatas.
orgCompiler :: Item String -> Compiler (Item String)
orgCompiler = pure . fmap (\s -> (metadatasToStr . orgMetadatas) s ++ s)

orgMetadatas :: String -> [String]
orgMetadatas = map (format . lower . clean) . takeWhile (/= "") . lines
  where
    clean = concat . splitOn "#+"
    lower s = (map toLower . takeWhile (/= ':')) s ++ dropWhile (/= ':') s
    format xs
        | -- drop weekday str. 2018-05-03 Thu -> 2018-05-03
          "date" `isPrefixOf` xs
        = take 16 . concat . splitOn ">" . concat . splitOn "<" $ xs
        | otherwise
        = xs

metadatasToStr :: [String] -> String
metadatasToStr = ("----------\n" ++) . (++ "----------\n") . unlines

-- | Temp Route
tempRoute :: Routes
tempRoute = customRoute tempRoute'
  where
    tempRoute' i = ".." </> "_temp" </> takeDirectory p </> takeFileName p
        where p = toFilePath i

cleanRouteFromTemp :: Routes
cleanRouteFromTemp = customRoute createIndexRoute
  where
    createIndexRoute ident =
      joinPath . tail . splitDirectories . (flip replaceExtension "html") $ toFilePath ident

blogPattern :: Pattern
blogPattern =
    "_temp/posts/*.org"

M yoctocell-xyz.cabal => yoctocell-xyz.cabal +2 -0
@@ 7,5 7,7 @@ executable site
  main-is:          site.hs
  build-depends:    base == 4.*
                  , hakyll == 4.13.*
                  , split >= 0.2.3.4
                  , filepath >= 1.4.2.1
  ghc-options:      -threaded
  default-language: Haskell2010