{-# LANGUAGE OverloadedStrings #-}
module Config.Blogs.Utils (
    entryPattern
  , entryFilesPattern
  , contentSnapshot
  , tagPagesPath
  , buildTags
  , yearlyPagePath
  , buildYearlyArchives
  , monthlyPagePath
  , buildMonthlyArchives
) where

import qualified Hakyll            as H
import           System.FilePath   (joinPath)

import qualified Archives          as A
import           Config.Program    (contentsRoot)
import           Config.RegexUtils (dd, mm, yyyy)
import           Config.Site       (defaultTimeLocale', timeZoneJST)
import           Utils             (sanitizeTagName)

{-# INLINE postRoot #-}
postRoot :: String -> FilePath
postRoot :: String -> String
postRoot String
blogName = [String] -> String
joinPath [String
contentsRoot, String
blogName]

-- contents/roki.log/year/month/day/title/index.md
entryPattern :: String -> H.Pattern
entryPattern :: String -> Pattern
entryPattern String
blogName = String -> Pattern
H.fromRegex (String -> Pattern) -> String -> Pattern
forall a b. (a -> b) -> a -> b
$
    String
"(^"
    String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [String] -> String
joinPath [String -> String
postRoot String
blogName, String
yyyy, String
mm, String
dd, String
".+", String
"index\\.md"]
    String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"$)"

entryFilesPattern :: String -> H.Pattern
entryFilesPattern :: String -> Pattern
entryFilesPattern String
blogName = String -> Pattern
H.fromRegex (String -> Pattern) -> String -> Pattern
forall a b. (a -> b) -> a -> b
$
    String
"(^"
    String -> String -> String
forall a. Semigroup a => a -> a -> a
<> [String] -> String
joinPath [String -> String
postRoot String
blogName, String
yyyy, String
mm, String
dd, String
".+", String
".+"]
    String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"$)"

{-# INLINE contentSnapshot #-}
contentSnapshot :: String -> H.Snapshot
contentSnapshot :: String -> String
contentSnapshot = (String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
".content")

{-# INLINE tagPagesPath #-}
tagPagesPath :: String -> FilePath -> FilePath
tagPagesPath :: String -> String -> String
tagPagesPath String
blogName String
tag = [String] -> String
joinPath [
    String
blogName
  , String
"tags"
  , String -> String
sanitizeTagName String
tag
  , String
"index.html"
  ]

buildTags :: H.MonadMetadata m => String -> m H.Tags
buildTags :: forall (m :: * -> *). MonadMetadata m => String -> m Tags
buildTags String
blogName = Pattern -> (String -> Identifier) -> m Tags
forall (m :: * -> *).
MonadMetadata m =>
Pattern -> (String -> Identifier) -> m Tags
H.buildTags (String -> Pattern
entryPattern String
blogName) ((String -> Identifier) -> m Tags)
-> (String -> Identifier) -> m Tags
forall a b. (a -> b) -> a -> b
$ String -> Identifier
H.fromFilePath (String -> Identifier)
-> (String -> String) -> String -> Identifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
tagPagesPath String
blogName

{-# INLINE yearlyPagePath #-}
yearlyPagePath :: String -> FilePath -> FilePath
yearlyPagePath :: String -> String -> String
yearlyPagePath String
blogName String
year = [String] -> String
joinPath [
    String
blogName
  , String
year
  , String
"index.html"
  ]

buildYearlyArchives :: (H.MonadMetadata m, MonadFail m) => String -> m A.YearlyArchives
buildYearlyArchives :: forall (m :: * -> *).
(MonadMetadata m, MonadFail m) =>
String -> m YearlyArchives
buildYearlyArchives String
blogName = TimeLocale
-> TimeZone
-> Pattern
-> (String -> Identifier)
-> m YearlyArchives
forall (m :: * -> *).
(MonadMetadata m, MonadFail m) =>
TimeLocale
-> TimeZone
-> Pattern
-> (String -> Identifier)
-> m YearlyArchives
A.buildYearlyArchives TimeLocale
defaultTimeLocale' TimeZone
timeZoneJST (String -> Pattern
entryPattern String
blogName) ((String -> Identifier) -> m YearlyArchives)
-> (String -> Identifier) -> m YearlyArchives
forall a b. (a -> b) -> a -> b
$
    String -> Identifier
H.fromFilePath (String -> Identifier)
-> (String -> String) -> String -> Identifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> String
yearlyPagePath String
blogName

{-# INLINE monthlyPagePath #-}
monthlyPagePath :: String -> (FilePath, FilePath) -> FilePath
monthlyPagePath :: String -> (String, String) -> String
monthlyPagePath String
blogName (String
year, String
month) = [String] -> String
joinPath [
    String
blogName
  , String
year
  , String
month
  , String
"index.html"
  ]

buildMonthlyArchives :: (H.MonadMetadata m, MonadFail m) => String -> m A.MonthlyArchives
buildMonthlyArchives :: forall (m :: * -> *).
(MonadMetadata m, MonadFail m) =>
String -> m MonthlyArchives
buildMonthlyArchives String
blogName = TimeLocale
-> TimeZone
-> Pattern
-> ((String, String) -> Identifier)
-> m MonthlyArchives
forall (m :: * -> *).
(MonadMetadata m, MonadFail m) =>
TimeLocale
-> TimeZone
-> Pattern
-> ((String, String) -> Identifier)
-> m MonthlyArchives
A.buildMonthlyArchives TimeLocale
defaultTimeLocale' TimeZone
timeZoneJST (String -> Pattern
entryPattern String
blogName) (((String, String) -> Identifier) -> m MonthlyArchives)
-> ((String, String) -> Identifier) -> m MonthlyArchives
forall a b. (a -> b) -> a -> b
$
    String -> Identifier
H.fromFilePath (String -> Identifier)
-> ((String, String) -> String) -> (String, String) -> Identifier
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> (String, String) -> String
monthlyPagePath String
blogName