<s> import os <EOL> import re <EOL> from datetime import datetime <EOL> from catsup . options import g <EOL> from catsup . utils import html_to_raw_text <EOL> from . utils import Pagination <EOL> class CatsupPage ( object ) : <EOL> @ property <EOL> def class_name ( self ) : <EOL> return self . __class__ . __name__ . lower ( ) <EOL> def get_permalink_args ( self ) : <EOL> return { } <EOL> @ property <EOL> def permalink ( self ) : <EOL> kwargs = self . __dict__ . copy ( ) <EOL> kwargs . update ( self . get_permalink_args ( ) ) <EOL> return g . permalink [ self . class_name ] . format ( ** kwargs ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT:->" ) <EOL> def render ( self , renderer , ** kwargs ) : <EOL> if hasattr ( self , "<STR_LIT>" ) : <EOL> template_name = self . template_name <EOL> else : <EOL> template_name = self . class_name + "<STR_LIT>" <EOL> kwargs [ self . class_name ] = self <EOL> kwargs . update ( self . __dict__ ) <EOL> renderer . render_to ( template_name , self . permalink , ** kwargs ) <EOL> class Tag ( CatsupPage ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> self . posts = [ ] <EOL> def add_post ( self , post ) : <EOL> self . posts . append ( post ) <EOL> @ property <EOL> def count ( self ) : <EOL> return len ( self . posts ) <EOL> def __iter__ ( self ) : <EOL> for post in self . posts : <EOL> yield post <EOL> class Tags ( CatsupPage ) : <EOL> def __init__ ( self , tags = None ) : <EOL> if tags is None : <EOL> tags = { } <EOL> self . tags_dict = tags <EOL> def get ( self , name ) : <EOL> return self . tags_dict . setdefault ( <EOL> name , <EOL> Tag ( name ) <EOL> ) <EOL> def render ( self , renderer , ** kwargs ) : <EOL> for tag in self . tags : <EOL> tag . render ( renderer ) <EOL> super ( Tags , self ) . render ( renderer , ** kwargs ) <EOL> @ property <EOL> def tags ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _tags = list ( self . tags_dict . values ( ) ) <EOL> self . _tags . sort ( <EOL> key = lambda x : x . count , <EOL> reverse = True <EOL> ) <EOL> return self . _tags <EOL> def __iter__ ( self ) : <EOL> for tag in self . tags : <EOL> yield tag <EOL> class Archive ( CatsupPage ) : <EOL> def __init__ ( self , year ) : <EOL> self . year = int ( year ) <EOL> self . posts = [ ] <EOL> def add_post ( self , post ) : <EOL> self . posts . append ( post ) <EOL> @ property <EOL> def count ( self ) : <EOL> return len ( self . posts ) <EOL> def __iter__ ( self ) : <EOL> for post in self . posts : <EOL> yield post <EOL> class Archives ( CatsupPage ) : <EOL> def __init__ ( self , archives = None ) : <EOL> if archives is None : <EOL> archives = { } <EOL> self . archives_dict = archives <EOL> def get ( self , year ) : <EOL> return self . archives_dict . setdefault ( <EOL> year , <EOL> Archive ( year ) <EOL> ) <EOL> def render ( self , renderer , ** kwargs ) : <EOL> for tag in self . archives : <EOL> tag . render ( renderer ) <EOL> super ( Archives , self ) . render ( renderer , ** kwargs ) <EOL> @ property <EOL> def archives ( self ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> self . _archives = list ( self . archives_dict . values ( ) ) <EOL> self . _archives . sort ( <EOL> key = lambda x : x . year , <EOL> reverse = True <EOL> ) <EOL> return self . _archives <EOL> def __iter__ ( self ) : <EOL> for archive in self . archives : <EOL> yield archive <EOL> class Post ( CatsupPage ) : <EOL> DATE_RE = re . compile ( '<STR_LIT>' ) <EOL> def __init__ ( self , path , meta , content ) : <EOL> self . path = path <EOL> self . filename , _ = os . path . splitext ( os . path . basename ( path ) ) <EOL> self . meta = meta <EOL> self . content = content <EOL> self . tags = [ ] <EOL> self . date = self . datetime . strftime ( "<STR_LIT>" ) <EOL> filename , _ = os . path . splitext ( os . path . basename ( path ) ) <EOL> if self . DATE_RE . match ( filename [ : <NUM_LIT:10> ] ) : <EOL> self . meta . setdefault ( "<STR_LIT:date>" , filename [ : <NUM_LIT:10> ] ) <EOL> self . filename = filename [ <NUM_LIT:11> : ] <EOL> else : <EOL> self . filename = filename <EOL> if "<STR_LIT:date>" in self . meta : <EOL> self . date = self . meta . date <EOL> else : <EOL> self . date = self . datetime . strftime ( "<STR_LIT>" ) <EOL> def add_archive_and_tags ( self ) : <EOL> year = self . datetime . strftime ( "<STR_LIT>" ) <EOL> g . archives . get ( year ) . add_post ( self ) <EOL> for tag in self . meta . pop ( "<STR_LIT>" , "<STR_LIT>" ) . split ( "<STR_LIT:U+002C>" ) : <EOL> tag = tag . strip ( ) <EOL> tag = g . tags . get ( tag ) <EOL> tag . add_post ( self ) <EOL> self . tags . append ( tag ) <EOL> @ property <EOL> def permalink ( self ) : <EOL> if "<STR_LIT>" in self . meta : <EOL> return self . meta . permalink <EOL> return super ( Post , self ) . permalink <EOL> def get_permalink_args ( self ) : <EOL> args = self . meta . copy ( ) <EOL> args . update ( <EOL> title = self . title , <EOL> datetime = self . datetime <EOL> ) <EOL> return args <EOL> @ property <EOL> def datetime ( self ) : <EOL> import os <EOL> if "<STR_LIT:time>" in self . meta : <EOL> return datetime . strptime ( <EOL> self . meta . time , "<STR_LIT>" <EOL> ) <EOL> elif "<STR_LIT:date>" in self . meta : <EOL> return datetime . strptime ( <EOL> self . meta . date , "<STR_LIT>" <EOL> ) <EOL> st_ctime = os . stat ( self . path ) . st_ctime <EOL> return datetime . fromtimestamp ( st_ctime ) <EOL> @ property <EOL> def description ( self ) : <EOL> if "<STR_LIT:description>" not in self . meta : <EOL> description = self . meta . get ( <EOL> "<STR_LIT:description>" , <EOL> self . content <EOL> ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> description = html_to_raw_text ( description ) <EOL> if "<STR_LIT>" in description : <EOL> description = description . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> elif "<STR_LIT>" in description : <EOL> description = description . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> if len ( description ) > <NUM_LIT> : <EOL> description = description [ : <NUM_LIT> ] <EOL> self . meta . description = description . strip ( ) <EOL> return self . meta . description <EOL> @ property <EOL> def allow_comment ( self ) : <EOL> if self . meta . get ( "<STR_LIT>" , None ) == "<STR_LIT>" : <EOL> return False <EOL> else : <EOL> return g . config . comment . allow <EOL> @ property <EOL> def title ( self ) : <EOL> return self . meta . get ( "<STR_LIT:title>" , self . filename ) <EOL> @ property <EOL> def type ( self ) : <EOL> return self . meta . type <EOL> class Page ( CatsupPage ) : <EOL> def __init__ ( self , posts ) : <EOL> self . posts = posts <EOL> self . per_page = g . theme . post_per_page <EOL> @ staticmethod <EOL> def get_permalink ( page ) : <EOL> if page == <NUM_LIT:1> : <EOL> return "<STR_LIT:/>" <EOL> return g . permalink [ "<STR_LIT>" ] . format ( page = page ) <EOL> @ property <EOL> def permalink ( self ) : <EOL> return Page . get_permalink ( self . page ) <EOL> def render_all ( self , renderer ) : <EOL> count = int ( ( len ( self . posts ) - <NUM_LIT:1> ) / self . per_page ) + <NUM_LIT:1> <EOL> for i in range ( count ) : <EOL> page = i + <NUM_LIT:1> <EOL> if page == <NUM_LIT:1> : <EOL> self . _permalink = "<STR_LIT:/>" <EOL> self . page = page <EOL> pagination = Pagination ( <EOL> page = page , <EOL> posts = self . posts , <EOL> per_page = self . per_page , <EOL> get_permalink = self . get_permalink <EOL> ) <EOL> self . render ( renderer = renderer , pagination = pagination ) <EOL> class Feed ( CatsupPage ) : <EOL> def __init__ ( self , posts ) : <EOL> self . posts = posts <EOL> self . template_name = "<STR_LIT>" <EOL> class NotFound ( CatsupPage ) : <EOL> def __init__ ( self ) : <EOL> self . template_name = "<STR_LIT>" <EOL> @ property <EOL> def permalink ( self ) : <EOL> return "<STR_LIT>" </s>
