2 # -*- encoding: utf-8 -*-
4 # wp_imgswap.py : WordPress image tag swapper
18 usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [max_image_width] [max_image_height]""" % (os.path.basename(sys.argv[0]),)
20 rex_imgtag = re.compile(r"""<img\s+src=["'](.*?)["'].*?>""")
21 rex_atag = re.compile(r"""<a\s+href=["'](.*?)["'].*?>""")
22 rex_alt = re.compile(r"""alt=["'](.*?)["']""")
23 rex_figuretag = re.compile(r"""<figure\s+style=["'](.*?)["'].*?>""")
26 in_f = codecs.open(sys.argv[1], "r", "utf_8" )
27 out_f = codecs.open(sys.argv[2], "w", "utf_8" )
28 image_dir = sys.argv[3]
33 link_prefix = sys.argv[4]
37 def _get_png_geom(filepath):
38 s = filepath.split('.')
40 if (ext == 'JPG') or (ext == 'jpg'):
41 (w, h) = getjpggeom.get_jpeg_geometory(filepath)
44 desc = deterfile.file(filepath)
47 m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
50 raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
54 return (int(w), int(h))
58 def replace_img_tag(line, tagstr, path):
59 if not os.path.isfile(path):
62 attrs = htmltaglib.parse_attributes(tagstr)
63 (root, ext) = os.path.splitext(os.path.basename(path))
64 (w, h) = _get_png_geom(path)
67 attrs['height'] = str(h)
68 attrs['width'] = str(w)
70 wp_image_url = '''%s/%s''' % (image_dir, filename)
71 attrs['src'] = wp_image_url
72 # if tag has 'alt' attribute, use it
73 if rex_alt.search(tagstr):
74 alt_text = rex_alt.search(tagstr).group(1)
75 attrs['alt'] = alt_text
77 new_tag_str = htmltaglib.build_tag('img', attrs)
78 return line.replace(tagstr, new_tag_str)
80 def replace_a_tag(line, tagstr, path):
81 if not os.path.isfile(path):
84 attrs = htmltaglib.parse_attributes(tagstr)
85 # wp_image_url = image_dir + os.path.basename(path)
86 (root, ext) = os.path.splitext(os.path.basename(path))
87 wp_image_url = link_prefix + "/" + root + ext
88 attrs['href'] = wp_image_url
89 new_tag_str = htmltaglib.build_tag('a', attrs)
91 return line.replace(tagstr, new_tag_str)
93 def replace_figure_tag(line, tagstr, path):
94 attrs = htmltaglib.parse_attributes(tagstr)
95 #width = max_image_width
97 # m = re.search(ur'width:\s*([0-9]+)px', attrs['style'])#
103 #attrs['style'] = "width:" + str(width) + "px;"
105 new_tag_str = htmltaglib.build_tag('figure', attrs)
107 return line.replace(tagstr, new_tag_str)
111 match = rex_imgtag.search(line)
113 tagstr = match.group(0)
114 path = match.group(1)
115 line = replace_img_tag(line, tagstr, path)
118 match = rex_atag.search(line)
120 tagstr = match.group(0)
121 path = match.group(1)
122 line = replace_a_tag(line, tagstr, path)
125 match = rex_figuretag.search(line)
127 tagstr = match.group(0)
128 style= match.group(1)
129 line = replace_figure_tag(line, tagstr, style)
131 print >> out_f, line,