5bbe32b906b484f746f0eca72f072686a8f2a516
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 # function to generate main index file and stylesheet
36 def generate_home_page (dbname
, conf
, templates
, category_str
, bestrated_str
):
38 tpl_main
= string
.Template (templates
[0][1])
40 tpl_indexbit
= string
.Template (templates
[6][1])
42 tpl_newsbit
= string
.Template (templates
[2][1])
44 # get the latest articles - conf[4] is num of rss entries to be used also
45 latest_arts
= biaweb_db
.site_latest_articles (dbname
, conf
[4])
46 if latest_arts
== False:
51 # Run through the latest articles
52 # for the num of latest news items on index
53 for art
in latest_arts
:
54 # url is Category/Article.html
55 url
= art
[13] + "/" + art
[8] + ".html"
56 # art[5] is creation time
57 strdate
= time
.ctime (art
[5])
58 # now populate the template variables. art[1] is title, art[2] is summary
59 strnews
= tpl_newsbit
.safe_substitute (news_title
= art
[1],
61 news_datetime
= strdate
,
62 news_description
= art
[2]
64 news_items
.append (strnews
)
65 # now convert it into a string
66 newsbit_str
= "".join (news_items
)
68 # now populate the index template
69 indexbit_str
= tpl_indexbit
.safe_substitute (site_name
= conf
[1],
70 news_updates
= newsbit_str
72 # now populate the main page template
73 main_str
= tpl_main
.safe_substitute (site_title
= conf
[1],
74 site_url
= "http://" + conf
[0],
75 meta_keywords
= conf
[2],
76 meta_description
= conf
[3],
79 contents_bit
= indexbit_str
,
80 list_of_categories
= category_str
,
81 list_best_rated
= bestrated_str
,
84 # write the index.html file in the destination directory
86 findex
= open (os
.path
.join (conf
[5], "index.html"), "w+")
87 findex
.write (main_str
)
89 except IOError, OSError:
92 # write the style.css file in the destination directory
94 fstyle
= open (os
.path
.join (conf
[5], "style.css"), "w+")
95 fstyle
.write (templates
[5][1])
98 except IOError, OSError:
103 # superfunction to generate the site
104 def generate_site (dbname
, files_to_copy
, folders_to_copy
, search_type_full
=True):
105 # get the configuration
106 conf
= biaweb_db
.get_configuration (dbname
)
108 tpls
= biaweb_db
.get_templates (dbname
)
110 # get the list of categories
111 cats
= biaweb_db
.get_categories (dbname
)
112 # cannot get categories return false
116 # format the categories as a html bulleted list
117 cats_str
= html_format_categories (cats
)
119 # get the best rated articles
120 best_rated
= biaweb_db
.site_get_bestrated (dbname
)
122 if best_rated
== False:
124 # format the best rated articles as a html bulleted list
125 best_rated_str
= html_format_best_rated (best_rated
)
127 # remove the destination tree and recreate it
129 if os
.path
.exists (conf
[5]):
130 shutil
.rmtree (conf
[5])
135 # generate the index page including style sheet
136 ret
= generate_home_page (dbname
, conf
, tpls
, cats_str
, best_rated_str
)
140 # finally when all is successfully done return true