{-# LANGUAGE QuasiQuotes, FlexibleContexts #-}
module Preprocessing.Replacement where
import Hakyll
import Text.Regex.PCRE.Heavy (Regex, re, gsub)
import Debug.Trace
type Html = String
type LLConverter = String -> String -> Html
replaceLogoLinks :: Item String -> Compiler (Item String)
replaceLogoLinks = return . fmap replaceAllLogoLinks
fullLogoLinkRegex :: Regex
fullLogoLinkRegex = [re|\[(.*?)\|(.*?)\]\((.*?)\)|]
shortLogoLinkRegex :: Regex
shortLogoLinkRegex = [re|\[(.*?)\|(.*?)\](?!\()|]
replaceAllLogoLinks :: Html -> Html
replaceAllLogoLinks = gsub fullLogoLinkRegex f . gsub shortLogoLinkRegex g
where
g (key:s1:_)
| key == "gh" = github s1 ("https://github.com/" ++ s1)
| key == "gl" = gitlab s1 ("https://gitlab.com/" ++ s1)
| key == "ga" = gitea s1 ("https://gitea.com/" ++ s1)
| key == "nani" = nani s1 ("https://git.nani.wtf/" ++ s1)
| key == "pub" = pub s1 ("https://pub.dev/packages/" ++ s1)
| key == "nxp" = nixpackages s1 ("https://search.nixos.org/packages?query=" ++ s1)
| key == "nxo" = nixoptions s1 ("https://search.nixos.org/options?query=" ++ s1)
| key == "npm" = npm s1 ("https://www.npmjs.com/package/" ++ s1)
| key == "crt" = crates s1 ("https://crates.io/crates/" ++ s1)
| key == "hk" = hackage s1 ("https://hackage.haskell.org/package/" ++ s1)
| key == "hg" = hoogle s1 ("https://hoogle.haskell.org/?hoogle=" ++ s1)
| key == "yt" = youtube s1 ("https://www.youtube.com/embed/" ++ s1)
-- Reconstruct the original text
| otherwise = foldr1 (++) ["[", key, "|", s1, "]"]
f (key:s1:s2:_)
| key == "kan" = kan s1 s2
| key == "so" = stackoverflow s1 s2
| key == "rd" = reddit s1 s2
| key == "wiki" = wikipedia s1 s2
| key == "jisho" = jisho s1 s2
-- Reconstruct the original text
| otherwise = foldr1 (++) ["[", key, "|", s1, "](", s2, ")"]
{- This should be removed once all icons are added, and all functions are implemented -}
generateGenericLink :: String -> LLConverter
generateGenericLink linkTitle = f
where
f name link = foldr1 (++) ["", linkTitle, ": ", name, ""]
badgeLinkWithCustomClasses :: String -> [String] -> LLConverter
badgeLinkWithCustomClasses imageName classes = f
where
f name link = foldr1 (++) [
"",
"",
"" ++ name ++ "",
"",
""
]
badgeLink :: String -> LLConverter
badgeLink imageName = badgeLinkWithCustomClasses imageName []
kan :: LLConverter
kan kanji kana = foldr1 (++) ["