[4775] | 1 | import os |
---|
| 2 | |
---|
| 3 | #### FOR HTML OUTPUTS ##### |
---|
| 4 | |
---|
| 5 | class HtmlWriters(object): |
---|
| 6 | def __init__(self): |
---|
| 7 | pass |
---|
| 8 | |
---|
| 9 | |
---|
| 10 | def html_header(self,title): |
---|
| 11 | """ create the header for c3s HTML docs """ |
---|
| 12 | header = """ |
---|
| 13 | <html> |
---|
| 14 | <body> |
---|
| 15 | <head> |
---|
| 16 | <title> """+ title + """ |
---|
| 17 | </title> |
---|
| 18 | <style> |
---|
| 19 | html { min-height:100%; } /* to get the gradient to stetch to the bottom of the view port */ |
---|
| 20 | body { |
---|
| 21 | background: #c1c1c1; |
---|
| 22 | background: -moz-linear-gradient(left, #ffffff 0%, #c1c1c1 100%), url(img.png) no-repeat 50% 300px; |
---|
| 23 | background: -webkit-gradient(linear, 100% 0%,0% 0%, from(#ffffff), to(#c1c1c1)), url(img.png) no-repeat 50% 300px; |
---|
| 24 | background: -webkit-linear-gradient(left, #ffffff 0%,#c1c1c1 100%), url(img.png) no-repeat 50% 300px; |
---|
| 25 | background: -o-linear-gradient(left, #ffffff 0%,#c1c1c1 100%), url(img.png) no-repeat 50% 300px; |
---|
| 26 | background: -ms-linear-gradient(left, #ffffff 0%,#c1c1c1 100%), url(img.png) no-repeat 50% 300px; |
---|
| 27 | background: linear-gradient(left, #ffffff 0%,#c1c1c1 100%), url(img.png) no-repeat 50% 300px; |
---|
| 28 | margin-top:100px; |
---|
| 29 | margin-bottom:100px; |
---|
| 30 | margin-right:50px; |
---|
| 31 | margin-left:50px; |
---|
| 32 | } |
---|
| 33 | h1 { |
---|
| 34 | |
---|
| 35 | } |
---|
| 36 | h2 { |
---|
| 37 | padding-left:10px; |
---|
| 38 | } |
---|
| 39 | h3 { |
---|
| 40 | padding-left:20px; |
---|
| 41 | } |
---|
| 42 | h4 { |
---|
| 43 | padding-left:40px; |
---|
| 44 | } |
---|
| 45 | h5 { |
---|
| 46 | padding-left:50px; |
---|
| 47 | } |
---|
| 48 | p { |
---|
| 49 | padding-left:50px; |
---|
| 50 | } |
---|
| 51 | </style> |
---|
| 52 | </head> |
---|
| 53 | """ |
---|
| 54 | return header |
---|
| 55 | |
---|
| 56 | def html_closing(self): |
---|
| 57 | """ append to and html file before closure""" |
---|
| 58 | closing = "</html></body>" |
---|
| 59 | |
---|
| 60 | return closing |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | def html_create_image(self,filename): |
---|
| 64 | img_declare = [] |
---|
| 65 | img_declare.append('<div id="wrapper" style="width:100%; text-align:center">\n') |
---|
| 66 | img_declare.append('<img src="'+filename+'"; style="margin:auto;">\n') |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | captionfile = filename.replace(".gif",".txt") |
---|
| 70 | if os.path.exists(captionfile): |
---|
| 71 | img_declare.append("<br>") |
---|
| 72 | print "Debug : a txt exists" |
---|
| 73 | tmpfile= open(captionfile, 'r') |
---|
| 74 | caption_text= tmpfile.readlines() |
---|
| 75 | tmpfile.close() |
---|
| 76 | for line in caption_text : |
---|
| 77 | img_declare.append(line.replace("\n","<br>")) |
---|
| 78 | |
---|
| 79 | img_declare.append("</div>\n") |
---|
| 80 | print img_declare |
---|
| 81 | |
---|
| 82 | return img_declare |
---|
| 83 | |
---|
| 84 | def html_create_table(self,caption,list_header,content): |
---|
| 85 | """ create an html table, output is a list to write in a file""" |
---|
| 86 | |
---|
| 87 | linelist = [] |
---|
| 88 | linelist.append("<table border='1'>\n") |
---|
| 89 | linelist.append("<caption>"+caption+"</caption>\n") |
---|
| 90 | |
---|
| 91 | linelist.append(" <tr>\n") |
---|
| 92 | for header in list_header: |
---|
| 93 | linelist.append(" <th>"+header+"</th>\n") |
---|
| 94 | linelist.append(" </tr>\n") |
---|
| 95 | |
---|
| 96 | for lines in content : |
---|
| 97 | |
---|
| 98 | linelist.append(" <tr>\n") |
---|
| 99 | for cell in lines : |
---|
| 100 | # intelligent line wrapper, for pathes and spaces. |
---|
| 101 | final_cell= "" |
---|
| 102 | maxlenght=35 |
---|
| 103 | |
---|
| 104 | # the '#' symbol is used in comments to skip a line |
---|
| 105 | for sentence in cell.split("#"): |
---|
| 106 | if len(sentence) < maxlenght : |
---|
| 107 | final_cell+=sentence+"<br>" |
---|
| 108 | else : |
---|
| 109 | # cut sentences btw words |
---|
| 110 | bit = "" |
---|
| 111 | stage1 = "" |
---|
| 112 | for word in sentence.split(" "): |
---|
| 113 | bit+=" "+word |
---|
| 114 | if len(bit) > maxlenght: |
---|
| 115 | stage1+=bit+"<br> " |
---|
| 116 | bit="" |
---|
| 117 | |
---|
| 118 | stage1+=bit+"<br> " |
---|
| 119 | |
---|
| 120 | final_cell = stage1 |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | if len(final_cell.split("/")) > 1: |
---|
| 124 | bit = "" |
---|
| 125 | stage2 = "" |
---|
| 126 | for word in final_cell.split("/"): |
---|
| 127 | bit+="/"+word |
---|
| 128 | if len(bit) > maxlenght: |
---|
| 129 | stage2+=bit+"<br> " |
---|
| 130 | bit="" |
---|
| 131 | |
---|
| 132 | stage2+=bit+"<br> " |
---|
| 133 | final_cell=stage2 |
---|
| 134 | |
---|
| 135 | linelist.append(" <td>"+final_cell+"</td>\n") |
---|
| 136 | linelist.append(" </tr>\n") |
---|
| 137 | |
---|
| 138 | linelist.append("</table>\n") |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | return linelist |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | def html_add_toc (self,filename): |
---|
| 145 | """ add a table of content based on headers to an html file""" |
---|
| 146 | initfile = open(filename,"r") |
---|
| 147 | lines = initfile.readlines() |
---|
| 148 | initfile.close() |
---|
| 149 | |
---|
| 150 | lines_changed=[] |
---|
| 151 | menu=[] |
---|
| 152 | |
---|
| 153 | ref_h1 = 0 |
---|
| 154 | ref_h2 = 0 |
---|
| 155 | ref_h3 = 0 |
---|
| 156 | ref_h4 = 0 |
---|
| 157 | |
---|
| 158 | h1tags=['','A.','B.','C.','D.','E.','F.','G.','H.','I.','J.'] |
---|
| 159 | h2tags=['','I.','II.','III.','IV.','V.','VI.','VII.','VIII.','IX.','X.','XI.','XII.','XIII.','XIV.','XV.','XVI.','XVII.','XVIII.','XIX.','XX.' ] |
---|
| 160 | h3tags=['','1.','2.','3.','4.','5.','6.','7.','8.','9.','10.','11.','12.','13.','14.','15.','16.','17.','18.','19.','20.' ] |
---|
| 161 | h4tags=['','1.','2.','3.','4.','5.','6.','7.','8.','9.','10.','11.','12.','13.','14.','15.','16.','17.','18.','19.','20.' ] |
---|
| 162 | |
---|
| 163 | # parsing document |
---|
| 164 | for line in lines : |
---|
| 165 | newline=line |
---|
| 166 | |
---|
| 167 | for balise in ['h1', 'h2', 'h3', 'h4'] : |
---|
| 168 | if line.startswith('<'+balise+'>'): |
---|
| 169 | title=line.split('</')[0] |
---|
| 170 | title=title.split('>')[1] |
---|
| 171 | |
---|
| 172 | if balise == 'h1' : |
---|
| 173 | ref_h1 +=1 |
---|
| 174 | ref_h2 = 0 |
---|
| 175 | ref_h3 = 0 |
---|
| 176 | ref_h4 = 0 |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | if balise == 'h2' : |
---|
| 181 | ref_h2 +=1 |
---|
| 182 | ref_h3 = 0 |
---|
| 183 | ref_h4 = 0 |
---|
| 184 | |
---|
| 185 | if balise == 'h3' : |
---|
| 186 | ref_h3 +=1 |
---|
| 187 | ref_h4 = 0 |
---|
| 188 | |
---|
| 189 | |
---|
| 190 | if balise == 'h4' : |
---|
| 191 | ref_h4 +=1 |
---|
| 192 | |
---|
| 193 | h1tag=h1tags[ref_h1] |
---|
| 194 | h2tag=h2tags[ref_h2] |
---|
| 195 | h3tag=h3tags[ref_h3] |
---|
| 196 | h4tag=h4tags[ref_h4] |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | tag = h1tag+h2tag+h3tag+h4tag |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | newline = '<'+balise+'>'+ tag + ' ' +title +' <a name=title_'+tag+'></a> </'+ balise +'>' |
---|
| 203 | newmenu = '<'+balise+'> <a href="#title_'+tag+'">'+ tag +' '+title +'</a></'+ balise +'>' |
---|
| 204 | menu.append(newmenu) |
---|
| 205 | |
---|
| 206 | lines_changed.append(newline) |
---|
| 207 | |
---|
| 208 | # creating the final document by adding menu |
---|
| 209 | final_lines = [] |
---|
| 210 | for line in lines_changed : |
---|
| 211 | final_lines.append(line) |
---|
| 212 | if line.strip()=="</head>" : |
---|
| 213 | final_lines.extend(menu) |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | newhtml = open(filename, 'w') |
---|
| 217 | newhtml.writelines(final_lines) |
---|
| 218 | newhtml.close() |
---|
| 219 | |
---|
| 220 | return |
---|
| 221 | |
---|
| 222 | def get_html_content(self,filename): |
---|
| 223 | html_list=[] |
---|
| 224 | |
---|
| 225 | if os.path.exists(filename): |
---|
| 226 | tmpfile= open(filename, 'r') |
---|
| 227 | html_list= tmpfile.readlines() |
---|
| 228 | tmpfile.close() |
---|
| 229 | else : |
---|
| 230 | html_list.append("File "+filename+" is not created yet...\n" ) |
---|
| 231 | |
---|
| 232 | return html_list |
---|
| 233 | |
---|
| 234 | if __name__ == '__main__': |
---|
| 235 | pass # Do tests |
---|