Watcher para ejecutar el generador cuando se cambian los archivos

This commit is contained in:
2020-11-23 01:31:43 -03:00
parent 0d88381104
commit b8ef0dec1b
3 changed files with 42 additions and 5 deletions

View File

@@ -1,9 +1,14 @@
#!/usr/bin/env python3
import sys
import time
import shutil
from pathlib import Path
from jinja2 import Template
from markdown import markdown
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
src_dir = Path('src')
out_dir = Path('www')
@@ -19,7 +24,6 @@ def create_dir_tree(path):
(out_dir / child.relative_to(src_dir)).mkdir(exist_ok=True)
create_dir_tree(child)
# generates html from a md file and a jinja template
def generate_html(md):
return template.render(md=markdown(text=md, extensions=['codehilite']));
@@ -41,7 +45,6 @@ def process_css(path):
with open(str(out_dir/'assets/style.css'), 'ab') as dst:
shutil.copyfileobj(src, dst)
# Every js file is just copied over to its destination
def process_js(path):
new_path = (out_dir / path.relative_to(src_dir))
@@ -51,8 +54,7 @@ def process_any(path):
new_path = (out_dir / path.relative_to(src_dir))
shutil.copyfile(str(path), str(new_path))
if __name__ == '__main__':
def execute():
shutil.rmtree(out_dir)
create_dir_tree(src_dir)
@@ -72,3 +74,36 @@ if __name__ == '__main__':
elif not path.is_dir():
print('parsing {} as any'.format(path))
process_any(path)
class WatchHandler(PatternMatchingEventHandler):
patterns = ["*.py", "*.md", "*.css", "*.js"]
def process(self, event):
execute()
def on_modified(self, event):
self.process(event)
def on_created(self, event):
self.process(event)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--watch':
observer = Observer()
observer.schedule(WatchHandler(), str(src_dir))
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
else:
execute()

View File

@@ -1,3 +1,4 @@
jinja2
markdown
pygments
watchdog

View File

@@ -1,4 +1,4 @@
#Daniel Cortes
#Daniel Cortés
Hola!, soy un estudiante de ingeniería en informática al que le gusta programar,
jugar videojuegos cuando se puede y escuchar música, mucha, mucha música.
@@ -40,3 +40,4 @@ por lo que no creo que lo mantenga tan actualizado, pero aqui estara :3.
##[Random](/random.html)
Links a cosas que me parecen interesantes o utiles.