nani.wtf/static-site-generator/Formats/Gogen.hs
h7x4 dcdf36f4dd
Revamp:
- Switch from `cabal2nix` and haskell overlay to `developPackage`
- Restructure directories to have more descriptive names
- Fix `nix run`
2023-03-11 16:56:32 +01:00

71 lines
2.1 KiB
Haskell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# LANGUAGE QuasiQuotes, FlexibleContexts #-}
module Formats.Gogen (gogenCtx) where
import Hakyll
import Debug.Trace (traceId)
import Data.Maybe (fromMaybe)
import Control.Applicative (empty)
import Text.Regex.PCRE.Heavy (Regex, gsub, re)
import Util.Hakyll.Context
--------------------------------------------------------------------------------
-- FURIGANA CONVERSION
type FuriganaTemplate = String
convertTitle :: (String -> String) -> Item a -> Compiler String
convertTitle = updateFieldWith "title" "???"
furiganaRegex :: Regex
furiganaRegex = [re|\[(.*?)\]\((.*?)\)|]
replaceFuriganaWithKanji :: FuriganaTemplate -> String
replaceFuriganaWithKanji = gsub furiganaRegex (\(kanji:_) -> kanji :: String)
replaceFuriganaWithHtml :: FuriganaTemplate -> String
replaceFuriganaWithHtml = between "<ruby>" "</ruby>" . gsub furiganaRegex matchToHtml
where
between :: String -> String -> String -> String
between x y s = x ++ s ++ y
matchToHtml :: [String] -> String
matchToHtml (kanji:kana:_) =
let defaultKanji = if kanji == "" then "" else kanji
in foldr1 (++) ["<rb>", defaultKanji, "</rb> <rp>(</rp><rt>", kana, "</rt><rp>)</rp>"]
convertFuriganaTitle :: Item a -> Compiler String
convertFuriganaTitle = convertTitle replaceFuriganaWithKanji
convertFuriganaTitleHtml :: Item a -> Compiler String
convertFuriganaTitleHtml = convertTitle replaceFuriganaWithHtml
convertFinishedValue :: Item a -> Compiler ContextField
convertFinishedValue i = do
s <- getMetadataField (itemIdentifier i) "finished"
case s of
Just "true" -> empty
_ -> return (StringField "true")
{- |
title: String
titleHtml: String
finished: Boolean
updated: String
lang: String
antonyms: Maybe [String]
synonyms: Maybe [String]
leads_here: Maybe [String]
alternatives: Maybe [String]
see_also: Maybe [String]
sources: Maybe [String]
-}
gogenCtx :: Context String
gogenCtx =
dateField "date" "%Y-%m-%d"
<> ifField "notFinishedYet" convertFinishedValue
<> field "title" convertFuriganaTitle
<> field "titleHtml" convertFuriganaTitleHtml
<> constField "lang" "en"
<> defaultContext