From 1030977dfd0c861e186b3b142cbb161feaa0c32e Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sat, 19 Mar 2022 00:59:36 +0100 Subject: [PATCH] Add support for rendering furigana --- flake.nix | 2 +- src/index.html | 37 +++++++++++++++++--------- src/naritachi/suru.md | 9 +++++++ src/templates/default.html | 50 +++++++++++++++++++++-------------- src/templates/naritachi.html | 20 ++++++++++++++ src/templates/post.html | 10 +++---- ssg/src/Formats/Naritachi.hs | 51 ++++++++++++++++++++++++++++++++++++ ssg/src/Formats/Posts.hs | 1 + ssg/src/Main.hs | 44 +++++++++++++++++-------------- ssg/src/Util/Routes.hs | 23 ++++++++++++++++ ssg/ssg.cabal | 1 + 11 files changed, 189 insertions(+), 59 deletions(-) create mode 100644 src/naritachi/suru.md create mode 100644 src/templates/naritachi.html create mode 100644 ssg/src/Formats/Naritachi.hs create mode 100644 ssg/src/Formats/Posts.hs create mode 100644 ssg/src/Util/Routes.hs diff --git a/flake.nix b/flake.nix index 124233e..b2cb153 100644 --- a/flake.nix +++ b/flake.nix @@ -36,7 +36,7 @@ packages = p: [ p.ssg ]; buildInputs = with pkgs.myHaskellPackages; [ - ssg + # ssg # Helpful tools for `nix develop` shells ghcid # https://github.com/ndmitchell/ghcid diff --git a/src/index.html b/src/index.html index 9cedece..6fe23cf 100644 --- a/src/index.html +++ b/src/index.html @@ -1,13 +1,13 @@ --- -desc: "This is me saying hello to the world" +desc: "Nani.wtf homepage" image: "./images/robert-pearce-UwHN0jU_YqQ-unsplash-800w.jpg" lang: "en" stylesheet: "default" -title: "Hello, world!" +title: "Nani" ---
-

Hello, world!

+

Welcome to nani.wtf

A woman sitting on a bench amongst trees at the end of a boardwalk leading to a pond with mountains in the background
-

Blog Posts

-
    - $for(posts)$ -
  • - - $date$ -
  • - $endfor$ -
+
+
+

Posts

+
    + $for(posts)$ +
  • + + $date$ +
  • + $endfor$ +
+
+
+

成り立ち

+
    + $for(naritachi)$ +
  • + +
  • + $endfor$ +
+
diff --git a/src/naritachi/suru.md b/src/naritachi/suru.md new file mode 100644 index 0000000..72b2eb8 --- /dev/null +++ b/src/naritachi/suru.md @@ -0,0 +1,9 @@ +--- +desc: "I announce myself to the world" +keywords: "japanese, naritachi, grammar" +lang: "en" +updated: "2020-09-22T12:00:00Z" +title: "[為](す)る" +--- + +suru diff --git a/src/templates/default.html b/src/templates/default.html index 0de6347..9eaef4c 100644 --- a/src/templates/default.html +++ b/src/templates/default.html @@ -6,42 +6,48 @@ + $if(author)$ - + $endif$ + $if(keywords)$ - + $endif$ + $if(image)$ - + $endif$ + $if(type)$ - + $else$ - + $endif$ + $if(image)$ - - $endif$ - $if(authorTwitter)$ - + $endif$ - + + + + + - - - + + + @@ -56,14 +62,16 @@ $siteName$ @@ -81,7 +89,9 @@ - $body$ + +$body$ + diff --git a/src/templates/naritachi.html b/src/templates/naritachi.html new file mode 100644 index 0000000..d32b4dd --- /dev/null +++ b/src/templates/naritachi.html @@ -0,0 +1,20 @@ +
+
+
+

$titleHtml$

+
+ $if(updated)$ + (updated: $updated$) + $endif$ +
+ $if(alternative_writings)$ +
+ $alternative_writings$ +
+ $endif$ +
+
+ $body$ +
+
+
diff --git a/src/templates/post.html b/src/templates/post.html index 7023a6c..9b24320 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1,17 +1,15 @@
-
-

- $title$ -

+
+

$title$

$date$ $if(updated)$ - (updated: $updated$) + (updated: $updated$) $endif$
-
+
$body$
diff --git a/ssg/src/Formats/Naritachi.hs b/ssg/src/Formats/Naritachi.hs new file mode 100644 index 0000000..0d420e3 --- /dev/null +++ b/ssg/src/Formats/Naritachi.hs @@ -0,0 +1,51 @@ +{-# LANGUAGE QuasiQuotes, FlexibleContexts #-} + +module Formats.Naritachi ( + convertFuriganaTitle, + convertFuriganaTitleHtml +) where + +import Hakyll (Item, Metadata, Compiler, itemIdentifier, getMetadata, lookupString) +import Debug.Trace (traceId) +import Data.Maybe (fromMaybe) +import Text.Regex.PCRE.Heavy + +-------------------------------------------------------------------------------- +-- FURIGANA CONVERSION + +type FuriganaTemplate = String + +convertTitle = updateFieldWith "title" "???" + +convertFuriganaTitle :: Item a -> Compiler String +convertFuriganaTitle = convertTitle replaceFuriganaWithKanji + +convertFuriganaTitleHtml :: Item a -> Compiler String +convertFuriganaTitleHtml = convertTitle replaceFuriganaWithHtml + +updateFieldWith :: String -> String -> (String -> String) -> Item a -> Compiler String +updateFieldWith field defaultPreviousValue f = + fmap updateField . getMetadata . itemIdentifier + where + updateField :: Metadata -> String + updateField = traceId . f . fromMaybe defaultPreviousValue . lookupString field + +replaceFuriganaWithKanji :: FuriganaTemplate -> String +replaceFuriganaWithKanji = gsub [re|\[(.*?)\]\((.*?)\)|] (\(kanji:kana:_) -> kanji :: String) + +replaceFuriganaWithHtml :: FuriganaTemplate -> String +replaceFuriganaWithHtml = between "" "" . gsub [re|\[(.*?)\]\((.*?)\)|] (\(kanji:kana:_) -> "" ++ kanji ++" ( " ++ kana ++ ")" :: String) + where + between x y s = x ++ s ++ y +-- toHtml :: FuriganaTemplate -> String +-- toHtml input = +-- "" +-- <> subRegex (mkRegex "[(.*?)]\\((.*?)\\)") input "\1 (\2)" +-- <> "" + +-- +-- +-- +-- +-- + diff --git a/ssg/src/Formats/Posts.hs b/ssg/src/Formats/Posts.hs new file mode 100644 index 0000000..e5dd913 --- /dev/null +++ b/ssg/src/Formats/Posts.hs @@ -0,0 +1 @@ +module Formats.Posts where diff --git a/ssg/src/Main.hs b/ssg/src/Main.hs index c0d00b2..29fde9d 100644 --- a/ssg/src/Main.hs +++ b/ssg/src/Main.hs @@ -3,7 +3,6 @@ import Control.Monad (forM_) import Data.Maybe (fromMaybe) import Hakyll -import Debug.Trace (trace) import qualified Data.Text as T import qualified Data.Text.Slugger as Slugger import Text.Pandoc @@ -18,6 +17,11 @@ import Text.Pandoc ) import Text.Pandoc.Highlighting (Style, breezeDark, styleToCss) +-- --------- + +import Formats.Naritachi +import Util.Routes + -------------------------------------------------------------------------------- -- CONFIG @@ -76,13 +80,31 @@ main = hakyllWith config $ do >>= saveSnapshot "content" >>= loadAndApplyTemplate "templates/default.html" ctx + match "naritachi/*" $ do + let ctx = + constField "type" "article" + <> field "title" convertFuriganaTitle + <> field "titleHtml" convertFuriganaTitleHtml + <> postCtx + + route $ setExtension ".html" + compile $ do + + pandocCompilerCustom + >>= loadAndApplyTemplate "templates/naritachi.html" ctx + >>= saveSnapshot "content" + >>= loadAndApplyTemplate "templates/default.html" ctx + match "index.html" $ do route idRoute compile $ do + -- posts :: Compiler posts <- recentFirst =<< loadAll "posts/*" + naritachi <- loadAll "naritachi/*" let indexCtx = - listField "posts" postCtx (return posts) + listField "posts" postCtx (return posts) + <> listField "naritachi" postCtx (return naritachi) <> constField "root" root <> constField "siteName" siteName <> defaultContext @@ -225,21 +247,3 @@ feedConfiguration = , feedAuthorEmail = "h7x4@protonmail.com" , feedRoot = root } - --------------------------------------------------------------------------------- --- CUSTOM ROUTE - -getTitleFromMeta :: Metadata -> String -getTitleFromMeta = - fromMaybe "no title" . lookupString "title" - -fileNameFromTitle :: Metadata -> FilePath -fileNameFromTitle = - T.unpack . (`T.append` ".html") . Slugger.toSlug . T.pack . getTitleFromMeta - -titleRoute :: Metadata -> Routes -titleRoute = - constRoute . fileNameFromTitle - -prefixRoute :: FilePath -> Routes -prefixRoute p = customRoute $ \id -> p ++ (toFilePath id) diff --git a/ssg/src/Util/Routes.hs b/ssg/src/Util/Routes.hs new file mode 100644 index 0000000..e6ddd07 --- /dev/null +++ b/ssg/src/Util/Routes.hs @@ -0,0 +1,23 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Util.Routes where + +import Hakyll +import Data.Maybe (fromMaybe) +import qualified Data.Text as T +import qualified Data.Text.Slugger as Slugger + +prefixRoute :: FilePath -> Routes +prefixRoute p = customRoute $ \id' -> p ++ (toFilePath id') + + +titleRoute :: Metadata -> Routes +titleRoute = constRoute . fileNameFromTitle + where + getTitleFromMeta :: Metadata -> String + getTitleFromMeta = + fromMaybe "no title" . lookupString "title" + + fileNameFromTitle :: Metadata -> FilePath + fileNameFromTitle = + T.unpack . (`T.append` ".html") . Slugger.toSlug . T.pack . getTitleFromMeta diff --git a/ssg/ssg.cabal b/ssg/ssg.cabal index ff92dbc..d526f57 100644 --- a/ssg/ssg.cabal +++ b/ssg/ssg.cabal @@ -14,5 +14,6 @@ executable hakyll-site , pandoc == 2.14.* , slugger >= 0.1.0.1 , text >= 1.2 + , pcre-heavy >= 1.0.0.2 ghc-options: -Wall -threaded default-language: Haskell2010