~yoctocell/yoctocell.xyz

0ee07b94a76a0151cc4e3ba34a7424c8435bb7a2 — Xinglu Chen 1 year, 9 months ago c2b7009
Main: Add context for source code url

Generate a url pointing to the source code of the current page.

* app/Main.hs (baseUrl):
(feedConfiguration):
(main):
(sourceContext):
1 files changed, 30 insertions(+), 14 deletions(-)

M app/Main.hs
M app/Main.hs => app/Main.hs +30 -14
@@ 1,28 1,31 @@
--------------------------------------------------------------------------------
{-# LANGUAGE OverloadedStrings #-}
import           Data.Char                      ( toLower
                                                , toUpper
                                                )
import           Data.Semigroup                 ( (<>) )
import           Data.Maybe
import           Data.List                      ( isPrefixOf )
import           Data.List.Split                ( splitOn )
import           System.FilePath.Posix          ( joinPath
import           Data.Semigroup                 ( (<>) )
import           System.FilePath.Posix          ( (</>)
                                                , joinPath
                                                , replaceExtension
                                                , splitDirectories
                                                , takeDirectory
                                                , splitPath
                                                , takeDirectory
                                                , takeFileName
                                                , (</>)
                                                )

import           Hakyll
import           Hakyll.Web.Template.Context
import           Hakyll.Core.Identifier         ( toFilePath )
import           Hakyll.Core.Item               ( Item(..) )



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

baseUrl :: String
baseUrl = "https://git.sr.ht/~yoctocell/yoctocell.xyz/tree/master/item/src"

feedConfiguration :: FeedConfiguration
feedConfiguration = FeedConfiguration
  { feedTitle       = "yoctocell's blog"


@@ 32,6 35,9 @@ feedConfiguration = FeedConfiguration
  , feedRoot        = "https://yoctocell.xyz"
  }

context :: Context String
context = defaultContext `mappend` sourceContext


main :: IO ()
main = hakyllWith config $ do


@@ 47,7 53,7 @@ main = hakyllWith config $ do
    route $ setExtension' "html"
    compile
      $   pandocCompiler
      >>= loadAndApplyTemplate "src/templates/default.html" defaultContext
      >>= loadAndApplyTemplate "src/templates/default.html" context
      >>= relativizeUrls

  match "src/posts/*.org" $ do


@@ 61,7 67,7 @@ main = hakyllWith config $ do
      let archiveCtx =
            listField "posts" postCtx (return posts)
              `mappend` constField "title" "Archives"
              `mappend` defaultContext
              `mappend` context

      makeItem ""
        >>= loadAndApplyTemplate "src/templates/archive.html" archiveCtx


@@ 76,8 82,7 @@ main = hakyllWith config $ do
    route $ cleanRouteFromTemp
    compile $ do
      posts <- recentFirst =<< loadAll "_temp/src/posts/*"
      let indexCtx =
            listField "posts" postCtx (return posts) `mappend` defaultContext
      let indexCtx = listField "posts" postCtx (return posts) `mappend` context

      pandocCompiler
        >>= applyAsTemplate indexCtx


@@ 91,7 96,7 @@ main = hakyllWith config $ do
            pandocCompiler
      >>= saveSnapshot "content"
      >>= loadAndApplyTemplate "src/templates/post.html"    postCtx
      >>= loadAndApplyTemplate "src/templates/default.html" postCtx
      >>= loadAndApplyTemplate "src/templates/default.html" context
      >>= relativizeUrls

  create ["atom.xml"] $ do


@@ 105,8 110,8 @@ main = hakyllWith config $ do
  match "src/templates/*" $ compile templateBodyCompiler


--------------------------------------------------------------------------------
-- | Contexts
-- Add @date@ and @modified@ fields.
postCtx :: Context String
postCtx =
  dateField "date" "%Y-%m-%d"


@@ 116,6 121,17 @@ postCtx =
feedCtx :: Context String
feedCtx = postCtx <> bodyField "description"

-- | Return the url for the source code, this reads the @baseUrl@
-- variable.  Based on @urlField@.
sourceContext :: Context a
sourceContext = field "source" $ \i -> do
  let id     = itemIdentifier i
      empty' = fail $ "No route url found for item " ++ show id
  flip replaceExtension ".org"
    <$> mappend baseUrl
    <$> maybe empty' toUrl
    <$> getRoute id

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