Open Tech Press向けの記事作成支援ツール
修订版 | 09f5903aad761f4889bd643f1a12de3d1f71a01d (tree) |
---|---|
时间 | 2017-03-27 20:19:00 |
作者 | hylom <hylom@hylo...> |
Commiter | hylom |
add wp_imgswap2.py for new OSDN Magazine
@@ -0,0 +1,131 @@ | ||
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 | +import math | |
13 | + | |
14 | +import htmltaglib | |
15 | +import deterfile | |
16 | +import getjpggeom | |
17 | + | |
18 | +usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [max_image_width] [max_image_height]""" % (os.path.basename(sys.argv[0]),) | |
19 | + | |
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=["'](.*?)["'].*?>""") | |
24 | + | |
25 | +try: | |
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] | |
29 | +except IndexError: | |
30 | + sys.exit(usage) | |
31 | + | |
32 | +try: | |
33 | + link_prefix = sys.argv[4] | |
34 | +except IndexError: | |
35 | + link_prefix = "" | |
36 | + | |
37 | +def _get_png_geom(filepath): | |
38 | + s = filepath.split('.') | |
39 | + ext = s[-1] | |
40 | + if (ext == 'JPG') or (ext == 'jpg'): | |
41 | + (w, h) = getjpggeom.get_jpeg_geometory(filepath) | |
42 | + return (w, h) | |
43 | + else: | |
44 | + desc = deterfile.file(filepath) | |
45 | + | |
46 | + try: | |
47 | + m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1]) | |
48 | + except IndexError: | |
49 | + err = ", ".join(desc) | |
50 | + raise Exception("deterfile error: %s, file: %s . " % (err,filepath)) | |
51 | + if m: | |
52 | + w = m.group(1) | |
53 | + h = m.group(2) | |
54 | + return (int(w), int(h)) | |
55 | + else: | |
56 | + return None | |
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 | + (root, ext) = os.path.splitext(os.path.basename(path)) | |
64 | + (w, h) = _get_png_geom(path) | |
65 | + | |
66 | + filename = root + ext | |
67 | + attrs['height'] = str(h) | |
68 | + attrs['width'] = str(w) | |
69 | + | |
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 | |
76 | + | |
77 | + new_tag_str = htmltaglib.build_tag('img', attrs) | |
78 | + return line.replace(tagstr, new_tag_str) | |
79 | + | |
80 | +def replace_a_tag(line, tagstr, path): | |
81 | + if not os.path.isfile(path): | |
82 | + return line | |
83 | + | |
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) | |
90 | + | |
91 | + return line.replace(tagstr, new_tag_str) | |
92 | + | |
93 | +def replace_figure_tag(line, tagstr, path): | |
94 | + attrs = htmltaglib.parse_attributes(tagstr) | |
95 | + #width = max_image_width | |
96 | + #if 'style' in attrs: | |
97 | + # m = re.search(ur'width:\s*([0-9]+)px', attrs['style'])# | |
98 | + # if m: | |
99 | + # w = int(m.group(1)) | |
100 | + # if w <= 480: | |
101 | + # width = w | |
102 | + | |
103 | + #attrs['style'] = "width:" + str(width) + "px;" | |
104 | + del attrs['style'] | |
105 | + new_tag_str = htmltaglib.build_tag('figure', attrs) | |
106 | + | |
107 | + return line.replace(tagstr, new_tag_str) | |
108 | + | |
109 | +for line in in_f: | |
110 | + # proc for IMG tag | |
111 | + match = rex_imgtag.search(line) | |
112 | + if match: | |
113 | + tagstr = match.group(0) | |
114 | + path = match.group(1) | |
115 | + line = replace_img_tag(line, tagstr, path) | |
116 | + | |
117 | + #proc for A tag | |
118 | + match = rex_atag.search(line) | |
119 | + if match: | |
120 | + tagstr = match.group(0) | |
121 | + path = match.group(1) | |
122 | + line = replace_a_tag(line, tagstr, path) | |
123 | + | |
124 | + #proc for FIGURE tag | |
125 | + match = rex_figuretag.search(line) | |
126 | + if match: | |
127 | + tagstr = match.group(0) | |
128 | + style= match.group(1) | |
129 | + line = replace_figure_tag(line, tagstr, style) | |
130 | + | |
131 | + print >> out_f, line, |