packages feed

panda-2008.10.13: Panda/Config/Global.hs

module Panda.Config.Global where

import Panda.Helper.Helper
import MPS
import Prelude hiding ((.), (/), (^), id, readFile)

import System.FilePath hiding ((<.>))
import System.IO.UTF8 (readFile, writeFile)
import Control.Arrow ((>>>), (&&&), (***))
import Data.Maybe
import System.Directory

import Panda.Type.Reader
import Panda.Type.Sidebar
import Panda.Type.Theme

db_id       = "db"
flat_id     = "."
blog_id     = "blog"
config_id   = "config"
tag_id      = "tag"
comment_id  = "comment"
sidebar_id  = "sidebar"
theme_id    = "theme"

db_uri      = db_id
flat_uri    = db_uri / flat_id

config_uri  = flat_uri / config_id / "site.txt"
sidebar_uri = flat_uri / config_id / sidebar_id
blog_uri    = flat_uri / blog_id
tag_uri     = flat_uri / tag_id
comment_uri = flat_uri / comment_id
theme_uri   = flat_uri / config_id / theme_id


-- unsafe, must restart server after changing config file, sorry about that ...
-- but these configs are read only, and keep the view pure, so no monad headaches involved.

user_config'  s   = readFile s .purify .filter_comment .lines .map strip .map (split "\\s*=\\s*") .map tuple2
user_config = user_config' config_uri

config_for' s d = user_config.lookup s .fromMaybe d
config_for s    = config_for' s s

blog_title      = config_for "blog_title"
blog_subtitle   = config_for "blog_subtitle"
host_name       = config_for "host_name"
author_email    = config_for "author_email" 

per_page        = config_for' "per_page" "7" .read :: Int
navigation      = config_for' "navigation" "About" .parse_config.("Home" :)

panda_url       = "http://github.com/nfjinjing/panda"
root            
  | user_root.belongs_to ["/", ""]  = "/"
  | otherwise                       =  user_root.("/" /).remove_trailing_slash
  where user_root = config_for' "root" "/"

default_reader  = Markdown

sidebar = config_for "sidebar" .parse_config .map (sidebar_uri /).select (to_utf8 >>> doesFileExist >>> purify )
  .map (read_sidebar_item default_reader >>> purify)
  
favicon = config_for' "favicon" "/favicon.ico"


-- extensions
analytics_account_id = config_for "analytics_account_id"

-- theme
default_theme = blank_theme
user_theme_name = config_for "theme"
user_theme_uri = (theme_uri / user_theme_name) ++ ".txt"

theme = if user_theme_uri.doesFileExist.purify
  then user_config' user_theme_uri .(("name", user_theme_name) : ) .to_theme
  else default_theme