Skip to content

Instantly share code, notes, and snippets.

@c-bata
Created August 2, 2014 09:28
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 c-bata/60d58b8c7fdcd5b306ac to your computer and use it in GitHub Desktop.
Save c-bata/60d58b8c7fdcd5b306ac to your computer and use it in GitHub Desktop.
markdownの更新を検知したら、TeXに変換してPDF作成
#!/usr/local/python
# coding: utf-8
import os
import sys
import commands
from time import sleep, strftime, localtime
def get_time_stamp(filename):
return os.stat(filename).st_mtime
def output_pdf(filename, root_file):
"""Build pdf from latex file.
"""
if root_file is None:
root_file = filename
root, ext = os.path.splitext(root_file)
if not os.path.isfile(root+'.tex'):
print "Error! - %s.texがありません" % root
return
# ここでPDF出力して,プレビューで開く.
print commands.getoutput("platex -kanji=utf8 -interaction=nonstopmode -file-line-error %s.tex" % root )
# ------ platex options ------
# -shell-escape : エラーなく実行できたら、自動でdvipdfmxを実行
# -interaction=nonstopmode : エラーで止まらない.
# -file-line-error : ???
# -synctex=1 : zlib で圧縮した SyncTeX 用データを出力 -> 意味がわからない.
print commands.getoutput("dvipdfmx %s" % root )
commands.getoutput("open %s.pdf" % root )
print "コンパイルしました."
def md2latex(folderpath, root_file):
"""Convert markdown to latex.
"""
if not os.path.isdir(folderpath):
print "No such directory."
sys.exit()
ft = {}
while 1:
sleep(1)
filelist = os.listdir(folderpath)
for f in filelist:
root, ext = os.path.splitext(f)
if ext == '.md':
time_stamp = get_time_stamp(f)
else:
continue
if not ft.has_key(root):
print root + ".texを作成しました."
ft[root] = time_stamp
print commands.getoutput("pandoc %s.md -o %s.tex" % (root,root))
continue
if ft[root] != time_stamp:
print f + "が更新されました."
ft[root] = time_stamp
commands.getoutput("pandoc %s.md -o %s.tex" % (root,root))
output_pdf(f, root_file)
if __name__ == '__main__':
root_file = None # 更新されたらこの変数に格納されたtexファイルのPDFを出力
if len(sys.argv) == 1 or len(sys.argv) > 3:
print "以下のように引数を指定して下さい"
print " 第一引数 : 監視するフォルダ"
print " 第ニ引数 : PDF出力するtex(or md)ファイル(省略可)"
print " ※第二引数を省略した場合、更新されたファイルをPDF出力します"
sys.exit()
elif len(sys.argv) == 3:
root_file = sys.argv[2]
md2latex(sys.argv[1], root_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment