Skip to content

Instantly share code, notes, and snippets.

@dixonsiu
Last active April 27, 2021 20:22
Show Gist options
  • Save dixonsiu/2b98aed52f2f4106f74af8978fc8245b to your computer and use it in GitHub Desktop.
Save dixonsiu/2b98aed52f2f4106f74af8978fc8245b to your computer and use it in GitHub Desktop.
Python scripts for Pandoc
import logging, logging.handlers
import argparse, datetime, re, os, sys
import fnmatch
import pypandoc
# define paths used by this command
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
LOG_PATH = os.path.join(ROOT_PATH, 'log')
CMD_NAME = os.path.basename(__file__)
LOG_FILENAME = 'markdown2html.log'
SUPPORTED_LOCALES = ['en', 'ja']
LINK_FILTER = os.path.join(ROOT_PATH, 'convert_link.py')
CURRENT_PATH = os.getcwd()
# Prepare logging mechanism
if not os.path.exists(LOG_PATH):
os.makedirs(LOG_PATH)
logger = logging.getLogger(__name__)
log_file = os.path.join(LOG_PATH, LOG_FILENAME)
handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=1024*1024*10, backupCount=5)
log_fmt = '%(asctime)s %(levelname)s: %(message)s'
log_formatter = logging.Formatter(log_fmt)
handler.setFormatter(log_formatter)
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def log_n_notify_user(temp_str):
print('### %s ###' % temp_str)
logger.info(temp_str)
def log_failure(temp_str):
print('ERROR: %s' % temp_str)
logger.error(temp_str)
def parse_command():
temp_msg = 'Convert markdown file(s) to HTML file(s) recursively in a directory where this command is executed.'
parser = argparse.ArgumentParser(description=temp_msg)
temp_msg = 'Specify the locale of your github.css.'
parser.add_argument('--locale', help=temp_msg, choices=SUPPORTED_LOCALES, default="en")
parser.add_argument('--source_dir', default=CURRENT_PATH, help=argparse.SUPPRESS)
parser.set_defaults(func=set_parameters)
args = parser.parse_args()
args.func(args)
def set_parameters(args):
source_dir = args.source_dir
css_path = '/%s/github.css' % args.locale
css_path_option = '-c%s' % css_path
convert_files(source_dir, css_path_option)
def convert_files(source_dir, css_path_option):
log_n_notify_user("Converting markdown to HTML ...")
for directory, subdirectories, files in os.walk(source_dir):
for file in fnmatch.filter(files, "*.md"):
src_name = os.path.join(directory, file)
src_filename = file.rsplit('.', 1)[0]
if src_filename == "README":
target_name = os.path.join(directory, "index.html")
else:
target_name = os.path.join(directory, "%s.html" % src_filename)
output = pypandoc.convert_file(
source_file=src_name,
outputfile=target_name,
to='html',
format='markdown_github',
extra_args=(css_path_option, '-s'),
filters=[LINK_FILTER])
logger.info("Converted from %(src)s to %(dst)s." % { "src": src_name, "dst": target_name })
def main():
try:
parse_command()
log_n_notify_user("Successfully converted all the files.")
except Exception as exc:
log_failure('Failed to execute %s, reason=%s' % (CMD_NAME, str(exc)))
exit(1)
if __name__ == '__main__':
main()
@dixonsiu
Copy link
Author

python markdown_to_html.py --locale en for "/en/github.css"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment