Open Tech Press向けの記事作成支援ツール
リビジョン | 56d914350dcfc8481148922f6c96f1de5e2db55b (tree) |
---|---|
日時 | 2013-05-13 18:52:17 |
作者 | hylom <hylom@hylo...> |
コミッター | hylom |
fix: use thumbnail when figure size is smaller than thumbnail size
@@ -19,6 +19,7 @@ usage = """usage: %s <target file> <output_file> <image_dir> [link_prefix] [imag | ||
19 | 19 | rex_imgtag = re.compile(r"""<img\s+src=["'](.*?)["'].*?>""") |
20 | 20 | rex_atag = re.compile(r"""<a\s+href=["'](.*?)["'].*?>""") |
21 | 21 | rex_alt = re.compile(r"""alt=["'](.*?)["']""") |
22 | +rex_figuretag = re.compile(r"""<figure\s+style=["'](.*?)["'].*?>""") | |
22 | 23 | |
23 | 24 | try: |
24 | 25 | in_f = codecs.open(sys.argv[1], "r", "utf_8" ) |
@@ -60,21 +61,21 @@ def replace_img_tag(line, tagstr, path): | ||
60 | 61 | return line |
61 | 62 | |
62 | 63 | attrs = htmltaglib.parse_attributes(tagstr) |
64 | + (root, ext) = os.path.splitext(os.path.basename(path)) | |
63 | 65 | |
66 | + filename = "" | |
64 | 67 | if 'width' in attrs: |
65 | 68 | (w, h) = _get_png_geom(path) |
66 | - if int(w) != image_width: | |
69 | + if int(w) > image_width: | |
67 | 70 | attrs['height'] = str(h * image_width / w) |
68 | 71 | attrs['width'] = str(image_width) |
72 | + filename = '''%s-%sx%s%s''' % (root, attrs['width'], attrs['height'], ext) | |
69 | 73 | else: |
70 | 74 | attrs['height'] = str(h) |
71 | 75 | attrs['width'] = str(w) |
76 | + filename = '''%s%s''' % (root, ext) | |
72 | 77 | |
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 | + wp_image_url = '''%s%s''' % (image_dir, filename) | |
78 | 79 | attrs['src'] = wp_image_url |
79 | 80 | # if tag has 'alt' attribute, use it |
80 | 81 | if rex_alt.search(tagstr): |
@@ -68,6 +68,7 @@ def replace_img_tag(line, tagstr, path): | ||
68 | 68 | attrs = htmltaglib.parse_attributes(tagstr) |
69 | 69 | (root, ext) = os.path.splitext(os.path.basename(path)) |
70 | 70 | |
71 | + filename = "" | |
71 | 72 | if 'width' in attrs: |
72 | 73 | (w, h) = _get_png_geom(path) |
73 | 74 | if int(w) > image_width: |
@@ -79,7 +80,6 @@ def replace_img_tag(line, tagstr, path): | ||
79 | 80 | attrs['width'] = str(w) |
80 | 81 | filename = '''%s%s''' % (root, ext) |
81 | 82 | |
82 | - | |
83 | 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 |