-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_html.py
113 lines (86 loc) · 2.84 KB
/
make_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import sys
from Maker.BaseTags import *
from Maker.VoidTags import *
from Maker.HTMLGenerator import HTMLGenerator
# Create an index.html file for a web-directory with .png files
STYLE_STR = """
div.image {
float: left;
margin: 5px;
text-align: center;
font-size: 10pt;
font-family: Verdana, Arial, sans-serif;
background-color: white;
border: 1px solid #ccc;
/* padding: 2px; */
/* margin: 2px 10px 10px 2px; */
-moz-box-shadow: 7px 5px 5px rgb(80,80,80); /* Firefox 3.5 */
-webkit-box-shadow: 7px 5px 5px rgb(80,80,80); /* Chrome, Safari */
box-shadow: 7px 5px 5px rgb(80,80,80); /* New browsers */
}
div.image img {
width: 355px;
height: 229px;
}
div.image div {width: 355px;}
"""
# Return list of files with a specified file_type in a directory
def getImages(tar_dir,file_type='png'):
fnames = []
for out in sorted(os.listdir(tar_dir)):
fpath = os.path.join(tar_dir,out)
if (os.path.isdir(fpath)):
continue
f,ftype = out.rsplit(".",1)
if ftype != file_type:
continue
fnames.append(out)
return fnames
# Creates an index.html file at the specified location for displaying .png files in a web-browser
def make_html(tar_dir):
home_dir = os.getcwd()
if not os.path.exists(tar_dir):
print "Target directory does not exists: {}".format(tar_dir)
return
os.chdir(tar_dir)
my_html = HTMLGenerator()
meta_tag = MetaTag()
style_tag = StyleTag()
my_html.addHeadTag(meta_tag)
my_html.addHeadTag(style_tag)
meta_tag.addAttributes(charset='UTF-8')
style_tag.setContent(STYLE_STR)
style_tag.addAttributes(type='text/css')
image_files = getImages(tar_dir)
for fname in image_files:
image_name,ftype = fname.rsplit(".",1)
div_tag = DivisionTag()
my_html.addBodyTag(div_tag)
image_tag = ImgTag()
text_div = DivisionTag()
link_tag = HyperLinkTag(link_location="./{}".format(fname),link_name='')
# This ensures the pretty_print setting gets inherited properly
div_tag.addTag(link_tag)
div_tag.addTag(text_div)
link_tag.addTag(image_tag)
image_tag.addAttributes(src="./{}".format(fname))
link_tag.addAttributes(target='_blank')
text_div.addAttributes(id='imgName')
text_div.setContent(image_name)
div_tag.addAttributes(cls='image')
print my_html.dumpHTML()
# my_html.saveHTML(f_name='index.html',f_dir=tar_dir)
os.chdir(home_dir)
def main():
if len(sys.argv) == 2:
fpath = sys.argv[1]
else:
print "ERROR: Must specify path"
return
if not os.path.exists(fpath):
print "ERROR: Unknown path {}".format(fpath)
return
make_html(fpath)
if __name__ == "__main__":
main()