Open Tech Press向けの記事作成支援ツール
修订版 | df322ef3f0b99f289c308fe5f2cfaa4597225eeb (tree) |
---|---|
时间 | 2013-04-18 19:35:55 |
作者 | hylom <hylom@hylo...> |
Commiter | hylom |
add sakura_imgswap.py
@@ -0,0 +1,115 @@ | ||
1 | +#!/usr/bin/env python | |
2 | +# -*- encoding: utf-8 -*- | |
3 | +# | |
4 | +# wp_imgswap.py : WordPress image tag swapper | |
5 | +# | |
6 | + | |
7 | +import sys | |
8 | +import codecs | |
9 | +import re | |
10 | +import os.path | |
11 | +import hashlib | |
12 | + | |
13 | +import htmltaglib | |
14 | +import deterfile | |
15 | +import getjpggeom | |
16 | + | |
17 | +usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [image_width]""" % (os.path.basename(sys.argv[0]),) | |
18 | + | |
19 | +rex_imgtag = re.compile(r"""<img\s+src=["'](.*?)["'].*?>""") | |
20 | +rex_atag = re.compile(r"""<a\s+href=["'](.*?)["'].*?>""") | |
21 | +rex_alt = re.compile(r"""alt=["'](.*?)["']""") | |
22 | + | |
23 | +try: | |
24 | + in_f = codecs.open(sys.argv[1], "r", "utf_8" ) | |
25 | + out_f = codecs.open(sys.argv[2], "w", "utf_8" ) | |
26 | + image_dir = sys.argv[3] | |
27 | +except IndexError: | |
28 | + sys.exit(usage) | |
29 | + | |
30 | +try: | |
31 | + image_width = int(sys.argv[4]) | |
32 | +except IndexError: | |
33 | + image_width = 440 | |
34 | + | |
35 | + | |
36 | +def _get_png_geom(filepath): | |
37 | + s = filepath.split('.') | |
38 | + ext = s[-1] | |
39 | + if (ext == 'JPG') or (ext == 'jpg'): | |
40 | + (w, h) = getjpggeom.get_jpeg_geometory(filepath) | |
41 | + return (w, h) | |
42 | + else: | |
43 | + desc = deterfile.file(filepath) | |
44 | + | |
45 | + try: | |
46 | + m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1]) | |
47 | + except IndexError: | |
48 | + err = ", ".join(desc) | |
49 | + raise Exception("deterfile error: %s, file: %s . " % (err,filepath)) | |
50 | + if m: | |
51 | + w = m.group(1) | |
52 | + h = m.group(2) | |
53 | + return (int(w), int(h)) | |
54 | + else: | |
55 | + return None | |
56 | + | |
57 | + | |
58 | +def replace_img_tag(line, tagstr, path): | |
59 | + if not os.path.isfile(path): | |
60 | + return line | |
61 | + | |
62 | + attrs = htmltaglib.parse_attributes(tagstr) | |
63 | + | |
64 | + if 'width' in attrs: | |
65 | + (w, h) = _get_png_geom(path) | |
66 | + if int(w) != image_width: | |
67 | + attrs['height'] = str(h * image_width / w) | |
68 | + attrs['width'] = str(image_width) | |
69 | + else: | |
70 | + attrs['height'] = str(h) | |
71 | + attrs['width'] = str(w) | |
72 | + | |
73 | + (root, ext) = os.path.splitext(os.path.basename(path)) | |
74 | + wp_image_url = '''%s%s-%sx%s%s''' % (image_dir, root, attrs['width'], attrs['height'], ext) | |
75 | + else: | |
76 | + wp_image_url = image_dir + os.path.basename(path) | |
77 | + | |
78 | + attrs['src'] = wp_image_url | |
79 | + # if tag has 'alt' attribute, use it | |
80 | + if rex_alt.search(tagstr): | |
81 | + alt_text = rex_alt.search(tagstr).group(1) | |
82 | + attrs['alt'] = alt_text | |
83 | + | |
84 | + new_tag_str = htmltaglib.build_tag('img', attrs) | |
85 | + return line.replace(tagstr, new_tag_str) | |
86 | + | |
87 | +def replace_a_tag(line, tagstr, path): | |
88 | + if not os.path.isfile(path): | |
89 | + return line | |
90 | + | |
91 | + attrs = htmltaglib.parse_attributes(tagstr) | |
92 | + wp_image_url = image_dir + os.path.basename(path) | |
93 | +# (root, ext) = os.path.splitext(os.path.basename(path)) | |
94 | +# wp_image_url = link_prefix + "/" + root | |
95 | + attrs['href'] = wp_image_url | |
96 | + new_tag_str = htmltaglib.build_tag('a', attrs) | |
97 | + | |
98 | + return line.replace(tagstr, new_tag_str) | |
99 | + | |
100 | +for line in in_f: | |
101 | + # proc for IMG tag | |
102 | + match = rex_imgtag.search(line) | |
103 | + if match: | |
104 | + tagstr = match.group(0) | |
105 | + path = match.group(1) | |
106 | + line = replace_img_tag(line, tagstr, path) | |
107 | + | |
108 | + #proc for A tag | |
109 | + match = rex_atag.search(line) | |
110 | + if match: | |
111 | + tagstr = match.group(0) | |
112 | + path = match.group(1) | |
113 | + line = replace_a_tag(line, tagstr, path) | |
114 | + | |
115 | + print >> out_f, line, |