-- trying to merge this helper with the generic one results in cyclic imports

{-# OPTIONS -fno-monomorphism-restriction #-}

module Panda.Helper.StateHelper where

import Panda.Helper.Env
import qualified Panda.Config.Global as G
import Prelude hiding ((.), (/), (^), id)


-- G.root = /blog
-- raw_uri = blog/x
-- full_uri = /blog/x
-- uri = full_uri - (/blog/)
remove_root s
  | G.root.is "/" = s
  | otherwise     = s.slice (G.root.length) (s.length)

uri = raw_uri ^ remove_root
-- uri = raw_uri


-- global
parse_date format s = case maybe_d of
  Nothing -> Nothing
  Just d -> Just $ if d.ctYear < 1910 then d {ctYear = d.ctYear + 100} else d 
  where
    maybe_d = parseCalendarTime defaultTimeLocale format s
  
format_date          = formatCalendarTime defaultTimeLocale
default_date         = parse_date "%Y-%m-%d %T" "2000-1-1 00:00:00".fromJust
parse_post_date      = parse_date G.post_date_format
default_parse_date s = s.parse_post_date .fromMaybe default_date

has_extension x = G.extensions.has x
only_for ext x = if has_extension ext then x else toHtml ""

-- controller
paginate = length >>> full_paginate (G.per_page)

cut       = G.cut
cut_re    = "^\\s*" ++ cut
match_cut = to_utf8 >>> match cut_re
is_cut    = match_cut >>> isJust
split_cut = to_utf8 >>> split cut_re