packages feed

panda-0.0.0.3: Panda/Model/Tag.hs

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

module Panda.Model.Tag where

import qualified Panda.Model.Post as Post

-- env
import Panda.Helper.Env
import Prelude hiding ((.), (/), id)
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.List as List
import Panda.Config.Global as Config


data Tag = Tag 
  { uid :: String     -- tag/name
  , resources :: Set.Set String
  }
  deriving (Show, Eq)

name x       = x.uid.id_to_name
id_to_name x = x.split "/" .tail.join'
sorted xs    = xs.List.sortBy(\a b -> (b.resources.Set.size) `compare` (a.resources.Set.size))
name_to_id x = tag_id / x

list = ls Config.tag_uri <.> map (Config.tag_id /) >>= mapM get

get id = get_resources id <.> Tag id
get_resources id = (Config.flat_uri / id) .readFile 
  <.> filter_comment <.> lines <.> map (blog_id / ) <.> to_set


to_tuple x        = (x.name, x.resources)
tag_map' xs       = xs . map (to_tuple) . to_h
tag_map           = list <.> tag_map'

for_resource xs x = xs.select (resources >>> Set.member x) .map name
fill_tag xs x     = x { Post.tags = for_resource xs (x.Post.uid) }