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