Last active
August 29, 2015 14:17
-
-
Save hrsano645/12eaa6c5d915668d15f1 to your computer and use it in GitHub Desktop.
remove_wp_plugin_tag.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from __future__ import division, print_function, absolute_import, unicode_literals | |
__author__ = 'hiroshi sano<hrs.sano645@gmail.com>' | |
# Wordpressのプラグインで使っていたタグを、mdに適した形に置き換える | |
import glob | |
import re | |
import os | |
MD_FILEPATHS = "./output/*.md" | |
REPLACE_MAP = [ | |
# WordPress › Picasa and Google Plus Express « WordPress Plugins - https://wordpress.org/plugins/picasa-express-x2/ | |
(r"\[pe2-image\nsrc=\"(?P<src>.+)\"\n.+\n.+alt=\"(?P<alt>.+)\" \]", | |
lambda x:u"[![{}]({})]({})".format(x.groupdict()["alt"], x.groupdict()["src"].replace("s144-o", "w400-o"), x.groupdict()["src"].replace("s144-o", "s0"))), | |
# AmaQuick http://creazy.net/amazon_quick_affiliate/ | |
(r"<td colspan=\"2\">\s*\[(?P<title>.+\n?.+)\]\((?P<url>.+)\)\s*</td>", | |
lambda x: "<td colspan=\"2\"><a href=\"{}\">{}</a></td>".format(x.groupdict()["url"], x.groupdict()["title"].replace("\n", ""))), | |
(r"<td>\s+\[!\[(?P<asin>.+)\]\((?P<img>.+)\)\]\((?P<url>.+)\)\s+</td>", | |
lambda x: "<td><a href=\"{}\"><img src=\"{}\" alt=\"{}\"></a></td>".format(x.groupdict()["url"], x.groupdict()["img"], x.groupdict()["asin"])), | |
(r"<td style=\"vertical-align: top;\">\s*(?P<asin_prefix>.+)\s*\[Amazonで詳しく見る\]\((?P<url>.+)\)\s*Powered by \[Amazon Quick Affiliate\s*\(JP\)\]\(http://creazy.net/amazon_quick_affiliate/\)", | |
lambda x: "<td style=\"vertical-align: top;\"><p>{}</p><a href=\"{}\">Amazonで詳しく見る</a><br>Powered by <a href=\"http://creazy.net/amazon_quick_affiliate/\">Amazon Quick Affiliate(JP)</a>".format(x.groupdict()["asin_prefix"], x.groupdict()["url"])) | |
] | |
OUTPUT_DIR_NAME = "output-remove-wp-plugin-tag" | |
def main(): | |
target_filenames = glob.glob(MD_FILEPATHS) | |
for md_filename in target_filenames: | |
# ファイルを開いて readlines を取得 | |
with open(md_filename, encoding="utf-8") as md_file: | |
md_file_rawtext = md_file.read() | |
# 正規表現のパターンを使って置き換える | |
for pattern, replace_func in REPLACE_MAP: | |
md_file_rawtext = re.sub(pattern, replace_func, md_file_rawtext) | |
# ファイルを保存するディレクトリを作る | |
if not os.path.exists(OUTPUT_DIR_NAME): | |
os.mkdir(OUTPUT_DIR_NAME) | |
output_dir_path = os.path.abspath(OUTPUT_DIR_NAME) | |
print("output dir path : {}".format(output_dir_path)) | |
# save files | |
export_file_path = os.path.join(output_dir_path, os.path.basename(md_filename)) | |
with open(export_file_path, mode="w", encoding="utf-8") as export_file: | |
export_file.write(md_file_rawtext) | |
print("export file {}\n".format(export_file_path)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment