Last active
November 26, 2019 09:48
-
-
Save wakita/dc51736c660e531ae591f319bd105a7a to your computer and use it in GitHub Desktop.
Pandoc で Reveal JS スライドを作るときに小便利なスクリプト。Markdown が更新されたら Pandoc を起動して HTML スライドを作成し、それができたら Google Chrome をリフレッシュする。
This file contains hidden or 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
#!/usr/bin/env python | |
import os, re, sys, time | |
from watchdog import events, observers | |
DEBUG = '--debug' in set(sys.argv) | |
MD = 'talk.md' | |
DROOT = '/Users/wakita/Dropbox/projects/tdb/docs' | |
LSITE = 'http://localhost:8000/tdb-reports' | |
GSITE = 'https://smartnova.github.io/tdb-reports' | |
PAGE = 'talks/20191203-tdb' | |
HTML = f'{DROOT}/{PAGE}/index.html' | |
LURL = f'{LSITE}/{PAGE}/' | |
GURL = f'{GSITE}/{PAGE}/' | |
# Markdown を処理して HTML を生成したのちに、Google Chrome にページのリロードを促す Shell script | |
PANDOC_CMD = f''' | |
pandoc \ | |
--from markdown+smart+footnotes+fenced_code_blocks+fenced_code_attributes \ | |
--to revealjs --output={HTML} \ | |
--slide-level=2 --standalone --mathjax \ | |
{MD} | |
chrome-refresh {LURL}''' | |
def system(cmd): | |
if DEBUG: print(cmd) | |
os.system(cmd) | |
os.system(f'open -a "Google Chrome" {LURL}') | |
os.system(PANDOC_CMD) | |
imgfile_re = re.compile('images/.*') | |
class Handler(events.FileSystemEventHandler): | |
def handle(self, ev): | |
path = ev.src_path[2:] | |
if isinstance(ev, events.FileModifiedEvent): | |
# 新しい画像が作成された Docroot に追加 | |
if imgfile_re.match(path): | |
system(f'cp {path} {DROOT}/{PAGE}/{path}') | |
elif isinstance(ev, events.FileCreatedEvent): | |
if path == '.md': | |
# Markdownファイルが更新されたらPandocを起動してDocrootにHTMLを生成する | |
system(PANDOC_CMD) | |
elif imgfile_re.match(path): | |
# 画像ファイルが追加されたらDocrootに追加 | |
system(f'cp {path} {DROOT}/{PAGE}/{path}') | |
elif isinstance(ev, events.FileDeletedEvent): | |
if path[-3:] == '.md': pass | |
elif imgfile_re.match(path): # 画像ファイルが削除されたら Docroot からも削除 | |
system(f'rm -f {DROOT}/{PAGE}/{path}') | |
elif isinstance(ev, events.FileMovedEvent): | |
if imgfile_re.match(path): # 画像ファイルが移動されたら Docroot でも移動 | |
path2 = ev.dest_path[2:] | |
system(f'mv {DROOT}/{path} {DROOT}/{PAGE}/{path2}') | |
def on_any_event(self, ev): self.handle(ev) | |
observer = observers.Observer() | |
handler = Handler() | |
observer.schedule(handler, '.', recursive=True) | |
observer.start() | |
try: | |
while True: time.sleep(1) | |
except KeyboardInterrupt: observer.stop() | |
observer.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment