e824c086a484037713453babdd8fca1548859204
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 article pages
52 def generate_article_pages (dbname
, conf
, templates
, category_str
, bestrated_str
):
54 tpl_main
= string
.Template (templates
[0][1])
56 tpl_articlebit
= string
.Template (templates
[1][1])
58 # get all articles from the database
59 articles
= biaweb_db
.site_articles (dbname
)
63 # walk through each article and generate the file in the appropriate category
66 art_cdate
= time
.ctime (art
[5])
67 art_mdate
= time
.ctime (art
[6])
68 rating_str
= html_format_rating (art
[9])
69 # now build the article from the article bit template
70 article_str
= tpl_articlebit
.safe_substitute (article_title
= art
[1],
71 article_cdate
= art_cdate
,
72 article_mdate
= art_mdate
,
74 article_contents
= art
[4])
76 # now build the article page
77 articlepage_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
78 site_url
= "http://" + conf
[0],
79 meta_keywords
= art
[3],
80 meta_description
= art
[2],
83 contents_bit
= article_str
,
84 list_of_categories
= category_str
,
85 list_best_rated
= bestrated_str
,
87 # write to the article file
89 farticle
= open (os
.path
.join (conf
[5], art
[13], art
[8] + ".html"), "w+")
90 farticle
.write (articlepage_str
)
91 except OSError, IOError:
97 # function to generate category directories and indices
98 def generate_category_indices (dbname
, conf
, templates
, category_str
, bestrated_str
, category_list
):
100 tpl_main
= string
.Template (templates
[0][1])
102 tpl_tablebit
= string
.Template (templates
[3][1])
104 tpl_trowbit
= string
.Template (templates
[4][1])
106 # run through each category and generate category index page
107 for cat
in category_list
:
109 # create the category directory
110 os
.mkdir (os
.path
.join (conf
[5], cat
[3]))
111 except IOError, OSError:
114 # now get the list of articles for the specified category
115 articles_list
= biaweb_db
.site_articles (dbname
, cat
[0])
116 if articles_list
== False:
120 # run through the list of articles in category
121 for art
in articles_list
:
122 url
= art
[13] + "/" + art
[8] + ".html"
123 creattime
= time
.ctime (art
[5])
124 rating_str
= html_format_rating (art
[9])
125 # now build the table rows for each article
126 tableitem_str
= tpl_trowbit
.safe_substitute (article_url
= url
,
130 tableitems
.append (tableitem_str
)
131 # generate the rows as a string
132 tablerows_str
= "".join (tableitems
)
134 # now create the page template
135 table_str
= tpl_tablebit
.safe_substitute (category_title
= cat
[1],
136 category_desc
= cat
[2],
137 table_rows
= tablerows_str
)
139 # now create the index page
140 categoryindex_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
141 site_url
= "http://" + conf
[0],
142 meta_keywords
= conf
[2],
143 meta_description
= cat
[2],
144 page_title
= conf
[1],
146 contents_bit
= table_str
,
147 list_of_categories
= category_str
,
148 list_best_rated
= bestrated_str
,
151 # now write to Category/index.html
153 fcatindex
= open (os
.path
.join (conf
[5], cat
[3], "index.html"), "w+")
154 fcatindex
.write (categoryindex_str
)
156 except OSError, IOError:
159 # finally return true
162 # function to generate main index file and stylesheet
163 def generate_home_page (dbname
, conf
, templates
, category_str
, bestrated_str
):
165 tpl_main
= string
.Template (templates
[0][1])
167 tpl_indexbit
= string
.Template (templates
[6][1])
169 tpl_newsbit
= string
.Template (templates
[2][1])
171 # get the latest articles - conf[4] is num of rss entries to be used also
172 latest_arts
= biaweb_db
.site_latest_articles (dbname
, conf
[4])
173 if latest_arts
== False:
178 # Run through the latest articles
179 # for the num of latest news items on index
180 for art
in latest_arts
:
181 # url is Category/Article.html
182 url
= art
[13] + "/" + art
[8] + ".html"
183 # art[5] is creation time
184 strdate
= time
.ctime (art
[5])
185 # now populate the template variables. art[1] is title, art[2] is summary
186 strnews
= tpl_newsbit
.safe_substitute (news_title
= art
[1],
188 news_datetime
= strdate
,
189 news_description
= art
[2]
191 news_items
.append (strnews
)
192 # now convert it into a string
193 newsbit_str
= "".join (news_items
)
195 # now populate the index template
196 indexbit_str
= tpl_indexbit
.safe_substitute (site_name
= conf
[1],
197 news_updates
= newsbit_str
199 # now populate the main page template
200 main_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
201 site_url
= "http://" + conf
[0],
202 meta_keywords
= conf
[2],
203 meta_description
= conf
[3],
204 page_title
= conf
[1],
206 contents_bit
= indexbit_str
,
207 list_of_categories
= category_str
,
208 list_best_rated
= bestrated_str
,
211 # write the index.html file in the destination directory
213 findex
= open (os
.path
.join (conf
[5], "index.html"), "w+")
214 findex
.write (main_str
)
216 except IOError, OSError:
219 # write the style.css file in the destination directory
221 fstyle
= open (os
.path
.join (conf
[5], "style.css"), "w+")
222 fstyle
.write (templates
[5][1])
224 except IOError, OSError:
229 # superfunction to generate the site
230 def generate_site (dbname
, files_to_copy
, folders_to_copy
, search_type_full
=True):
231 # get the configuration
232 conf
= biaweb_db
.get_configuration (dbname
)
234 tpls
= biaweb_db
.get_templates (dbname
)
236 # get the list of categories
237 cats
= biaweb_db
.get_categories (dbname
)
238 # cannot get categories return false
242 # format the categories as a html bulleted list
243 cats_str
= html_format_categories (cats
)
245 # get the best rated articles
246 best_rated
= biaweb_db
.site_get_bestrated (dbname
)
248 if best_rated
== False:
250 # format the best rated articles as a html bulleted list
251 best_rated_str
= html_format_best_rated (best_rated
)
253 # remove the destination tree and recreate it
255 if os
.path
.exists (conf
[5]):
256 shutil
.rmtree (conf
[5])
261 # generate the index page including style sheet
262 ret
= generate_home_page (dbname
, conf
, tpls
, cats_str
, best_rated_str
)
266 # generate the category directories and indices
267 ret
= generate_category_indices (dbname
, conf
, tpls
, cats_str
, best_rated_str
, cats
)
271 # generate the article pages
272 ret
= generate_article_pages (dbname
, conf
, tpls
, cats_str
, best_rated_str
)
276 # finally when all is successfully done return true