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 #!/usr/bin/env python3
import sys
import time
import shutil import shutil
from pathlib import Path from pathlib import Path
from jinja2 import Template from jinja2 import Template
from markdown import markdown from markdown import markdown
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
src_dir = Path('src') src_dir = Path('src')
out_dir = Path('www') out_dir = Path('www')
@@ -19,7 +24,6 @@ def create_dir_tree(path):
(out_dir / child.relative_to(src_dir)).mkdir(exist_ok=True) (out_dir / child.relative_to(src_dir)).mkdir(exist_ok=True)
create_dir_tree(child) create_dir_tree(child)
# generates html from a md file and a jinja template # generates html from a md file and a jinja template
def generate_html(md): def generate_html(md):
return template.render(md=markdown(text=md, extensions=['codehilite'])); 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: with open(str(out_dir/'assets/style.css'), 'ab') as dst:
shutil.copyfileobj(src, dst) shutil.copyfileobj(src, dst)
# Every js file is just copied over to its destination # Every js file is just copied over to its destination
def process_js(path): def process_js(path):
new_path = (out_dir / path.relative_to(src_dir)) 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)) new_path = (out_dir / path.relative_to(src_dir))
shutil.copyfile(str(path), str(new_path)) shutil.copyfile(str(path), str(new_path))
def execute():
if __name__ == '__main__':
shutil.rmtree(out_dir) shutil.rmtree(out_dir)
create_dir_tree(src_dir) create_dir_tree(src_dir)
@@ -72,3 +74,36 @@ if __name__ == '__main__':
elif not path.is_dir(): elif not path.is_dir():
print('parsing {} as any'.format(path)) print('parsing {} as any'.format(path))
process_any(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 jinja2
markdown markdown
pygments 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, 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. 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) ##[Random](/random.html)
Links a cosas que me parecen interesantes o utiles. Links a cosas que me parecen interesantes o utiles.