Skip to content

Instantly share code, notes, and snippets.

@tkf
Created September 23, 2009 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkf/191796 to your computer and use it in GitHub Desktop.
Save tkf/191796 to your computer and use it in GitHub Desktop.
howm を rst2html で変換する
#!/usr/bin/env python
import os
import sys
HOWM_PATH = os.path.join(os.getenv('HOME'), "howm")
HOWM_PUB = os.path.join(os.getenv('HOME'), "misc/pub_memo/howm")
RST2HTML_PATH = '/usr/bin/rst2html.py'
def get_dir_and_file_list(path):
dlst = []
flst = []
cwd = os.getcwd()
os.chdir(path)
for (dirpath, dirnames, filenames) in os.walk('.'):
dlst.append(dirpath)
flst += [os.path.join(dirpath, f) for f in filenames]
os.chdir(cwd)
return (dlst, flst)
def make_dirs_if_not_exist(dlst):
dlst_copy = dlst[:]
dlst_copy.sort()
for d in dlst_copy:
if not os.path.exists(d):
print "mkdir:", d
os.mkdir(d)
def cnv_rst2html(source, target, options = [], script_path = RST2HTML_PATH):
copy_argv = sys.argv
sys.argv = [script_path,] + options + [source, target]
#print sys.argv
execfile(script_path)
sys.argv = copy_argv
def convert_files(flst, ext2cnv, howm_path, force=False):
for f in flst:
ext = f.split('.')[-1]
ext_len = len(ext)
if ext in ext2cnv.keys():
source = os.path.join(howm_path, f)
target = f[:-ext_len] + 'html'
if ( force or not os.path.exists(target) or
os.path.getmtime(source) > os.path.getmtime(target) ):
(func, args) = ext2cnv[ext]
print "make %s from %s" % (target, source)
func(source, target, *args)
def main(howm_path = HOWM_PATH, howm_pub = HOWM_PUB):
ext2cnv = dict(
rest = (cnv_rst2html, []),
rst = (cnv_rst2html, []),
)
cwd = os.getcwd()
os.chdir(howm_pub)
(dlst, flst) = get_dir_and_file_list(howm_path)
make_dirs_if_not_exist(dlst)
convert_files(flst, ext2cnv, howm_path)
os.chdir(cwd)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment