1 # BiaWeb Website content manager (c) 2010 V.Harishankar
2 # Site exporter/generator class
12 # to format the best rated articles in a HTML link list
13 def html_format_best_rated (best_rated
):
15 for art
in best_rated
:
16 # art[13] is category stub, art[8] is article stub
17 # thus forming the relative url as Category/Article.html
18 str_art
= '<li><a href="' + art
[13] + '/' + art
[8] + '.html">' + art
[1] + '</a></li>\n'
19 items
.append (str_art
)
20 items
.append ("</ul>\n")
21 str_items
= "".join (items
)
24 # to format categories in a HTML link list
25 def html_format_categories (cats
):
28 # cat[3] is category stub and cat[1] is category name
29 str_cat
= '<li><a href="' + cat
[3] + '/">' + cat
[1] + '</a></li>\n'
30 items
.append (str_cat
)
31 items
.append ("</ul>\n")
32 str_items
= "".join (items
)
35 # to convert a rating number into rating images out of 10 stars
36 def html_format_rating (rating
):
38 # if -1 then return unrated as the text
41 # fill up the number of stars for the rating
42 for i
in range (rating
):
43 items
.append ('<img src="star.gif" alt="*" />')
44 # fill up remaining slots (of 10) with grey stars
45 for i
in range (10 - rating
):
46 items
.append ('<img src="star-grey.gif" alt="-" />')
48 rating_str
= "".join (items
)
51 # function to generate category directories and indices
52 def generate_category_indices (dbname
, conf
, templates
, category_str
, bestrated_str
, category_list
):
54 tpl_main
= string
.Template (templates
[0][1])
56 tpl_tablebit
= string
.Template (templates
[3][1])
58 tpl_trowbit
= string
.Template (templates
[4][1])
60 # run through each category and generate category index page
61 for cat
in category_list
:
63 # create the category directory
64 os
.mkdir (os
.path
.join (conf
[5], cat
[3]))
65 except IOError, OSError:
68 # now get the list of articles for the specified category
69 articles_list
= biaweb_db
.site_articles (dbname
, cat
[0])
70 if articles_list
== False:
74 # run through the list of articles in category
75 for art
in articles_list
:
76 url
= art
[13] + "/" + art
[8] + ".html"
77 creattime
= time
.ctime (art
[5])
78 rating_str
= html_format_rating (art
[9])
79 # now build the table rows for each article
80 tableitem_str
= tpl_trowbit
.safe_substitute (article_url
= url
,
84 tableitems
.append (tableitem_str
)
85 # generate the rows as a string
86 tablerows_str
= "".join (tableitems
)
88 # now create the page template
89 table_str
= tpl_tablebit
.safe_substitute (category_title
= cat
[1],
90 category_desc
= cat
[2],
91 table_rows
= tablerows_str
)
93 # now create the index page
94 categoryindex_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
95 site_url
= "http://" + conf
[0],
96 meta_keywords
= conf
[2],
97 meta_description
= cat
[2],
100 contents_bit
= table_str
,
101 list_of_categories
= category_str
,
102 list_best_rated
= bestrated_str
,
105 # now write to Category/index.html
107 fcatindex
= open (os
.path
.join (conf
[5], cat
[3], "index.html"), "w+")
108 fcatindex
.write (categoryindex_str
)
110 except OSError, IOError:
113 # finally return true
116 # function to generate main index file and stylesheet
117 def generate_home_page (dbname
, conf
, templates
, category_str
, bestrated_str
):
119 tpl_main
= string
.Template (templates
[0][1])
121 tpl_indexbit
= string
.Template (templates
[6][1])
123 tpl_newsbit
= string
.Template (templates
[2][1])
125 # get the latest articles - conf[4] is num of rss entries to be used also
126 latest_arts
= biaweb_db
.site_latest_articles (dbname
, conf
[4])
127 if latest_arts
== False:
132 # Run through the latest articles
133 # for the num of latest news items on index
134 for art
in latest_arts
:
135 # url is Category/Article.html
136 url
= art
[13] + "/" + art
[8] + ".html"
137 # art[5] is creation time
138 strdate
= time
.ctime (art
[5])
139 # now populate the template variables. art[1] is title, art[2] is summary
140 strnews
= tpl_newsbit
.safe_substitute (news_title
= art
[1],
142 news_datetime
= strdate
,
143 news_description
= art
[2]
145 news_items
.append (strnews
)
146 # now convert it into a string
147 newsbit_str
= "".join (news_items
)
149 # now populate the index template
150 indexbit_str
= tpl_indexbit
.safe_substitute (site_name
= conf
[1],
151 news_updates
= newsbit_str
153 # now populate the main page template
154 main_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
155 site_url
= "http://" + conf
[0],
156 meta_keywords
= conf
[2],
157 meta_description
= conf
[3],
158 page_title
= conf
[1],
160 contents_bit
= indexbit_str
,
161 list_of_categories
= category_str
,
162 list_best_rated
= bestrated_str
,
165 # write the index.html file in the destination directory
167 findex
= open (os
.path
.join (conf
[5], "index.html"), "w+")
168 findex
.write (main_str
)
170 except IOError, OSError:
173 # write the style.css file in the destination directory
175 fstyle
= open (os
.path
.join (conf
[5], "style.css"), "w+")
176 fstyle
.write (templates
[5][1])
178 except IOError, OSError:
183 # superfunction to generate the site
184 def generate_site (dbname
, files_to_copy
, folders_to_copy
, search_type_full
=True):
185 # get the configuration
186 conf
= biaweb_db
.get_configuration (dbname
)
188 tpls
= biaweb_db
.get_templates (dbname
)
190 # get the list of categories
191 cats
= biaweb_db
.get_categories (dbname
)
192 # cannot get categories return false
196 # format the categories as a html bulleted list
197 cats_str
= html_format_categories (cats
)
199 # get the best rated articles
200 best_rated
= biaweb_db
.site_get_bestrated (dbname
)
202 if best_rated
== False:
204 # format the best rated articles as a html bulleted list
205 best_rated_str
= html_format_best_rated (best_rated
)
207 # remove the destination tree and recreate it
209 if os
.path
.exists (conf
[5]):
210 shutil
.rmtree (conf
[5])
215 # generate the index page including style sheet
216 ret
= generate_home_page (dbname
, conf
, tpls
, cats_str
, best_rated_str
)
220 ret
= generate_category_indices (dbname
, conf
, tpls
, cats_str
, best_rated_str
, cats
)
224 # finally when all is successfully done return true