Open Tech Press向けの記事作成支援ツール
Revision | 3cec808899621e1ef46c0a7cd53c041b272a8fc2 (tree) |
---|---|
Time | 2013-04-24 19:30:02 |
Author | hylom <hylom@hylo...> |
Commiter | hylom |
fix: change filename and figure size when figure size is large
@@ -66,21 +66,21 @@ def replace_img_tag(line, tagstr, path): | ||
66 | 66 | return line |
67 | 67 | |
68 | 68 | attrs = htmltaglib.parse_attributes(tagstr) |
69 | + (root, ext) = os.path.splitext(os.path.basename(path)) | |
69 | 70 | |
70 | 71 | if 'width' in attrs: |
71 | 72 | (w, h) = _get_png_geom(path) |
72 | - if int(w) != image_width: | |
73 | + if int(w) > image_width: | |
73 | 74 | attrs['height'] = str(h * image_width / w) |
74 | 75 | attrs['width'] = str(image_width) |
76 | + filename = '''%s-%sx%s%s''' % (root, attrs['width'], attrs['height'], ext) | |
75 | 77 | else: |
76 | 78 | attrs['height'] = str(h) |
77 | 79 | attrs['width'] = str(w) |
80 | + filename = '''%s%s''' % (root, ext) | |
78 | 81 | |
79 | - (root, ext) = os.path.splitext(os.path.basename(path)) | |
80 | - wp_image_url = '''%s%s-%sx%s%s''' % (image_dir, root, attrs['width'], attrs['height'], ext) | |
81 | - else: | |
82 | - wp_image_url = image_dir + os.path.basename(path) | |
83 | 82 | |
83 | + wp_image_url = '''%s%s''' % (image_dir, filename) | |
84 | 84 | attrs['src'] = wp_image_url |
85 | 85 | # if tag has 'alt' attribute, use it |
86 | 86 | if rex_alt.search(tagstr): |
@@ -105,7 +105,15 @@ def replace_a_tag(line, tagstr, path): | ||
105 | 105 | |
106 | 106 | def replace_figure_tag(line, tagstr, path): |
107 | 107 | attrs = htmltaglib.parse_attributes(tagstr) |
108 | - attrs['style'] = "width:480px;" | |
108 | + width = image_width | |
109 | + if 'style' in attrs: | |
110 | + m = re.search(ur'width:\s*([0-9]+)px', attrs['style']) | |
111 | + if m: | |
112 | + w = int(m.group(1)) | |
113 | + if w <= 480: | |
114 | + width = w | |
115 | + | |
116 | + attrs['style'] = "width:" + str(width) + "px;" | |
109 | 117 | new_tag_str = htmltaglib.build_tag('figure', attrs) |
110 | 118 | |
111 | 119 | return line.replace(tagstr, new_tag_str) |