nani.wtf/static-site-generator/Formats/Gogen.hs

71 lines
2.1 KiB
Haskell
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{-# 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