Convert Python templates to django template engine

This commit is contained in:
Carlos Galindo 2020-12-24 16:17:13 +01:00
parent 7f52ff6b6c
commit fc90400f8f
18 changed files with 225 additions and 196 deletions

View file

@ -1,14 +1,15 @@
from song_types import Chord, Line, Song, Verse
from song_types import readfile, join_list
from audio_scanner import find_audios
from os.path import join
from pathlib import Path
import shutil
import argparse
import urllib.parse
import os
import re
import shutil
from django.conf import settings
from django.template import Engine, Context
from os.path import join
from pathlib import Path
from audio_scanner import find_audios
from model import Chord, Line, Song, Verse
def mkdir(path):
if not os.path.exists(path):
@ -31,14 +32,6 @@ def extra_put(extra, index, the_type, data=None):
extra[index].append(payload)
page_template = readfile("res/page.html")
index_template = readfile("res/index.html")
index_per_song_template = readfile("res/song_li.html")
song_redir_template = readfile("res/song_redir.html")
index_css = '<link rel="stylesheet" href="main.css">\n\t<link rel="stylesheet" href="index.css">'
song_css = '<link rel="stylesheet" href="../song.css">\n\t<link rel="stylesheet" href="../main.css">'
class SongLoader:
def __init__(self, latex_file, audio_dir):
self.index = 1
@ -235,35 +228,28 @@ class SongLoader:
continue
current_verse.add_line(Line(text, extras))
def print_index(self, index_file="index.html"):
self.songs = sorted(self.songs, key=lambda s: s.number)
song_list = join_list([index_per_song_template.format(
url=s.get_url(),
li_class=' class="hasChords"' if not s.chorded() else '',
number=s.number,
name=s.name,
author=" por %s " % s.author if s.author else "",
origin=" basado en %s " % s.origin if s.origin else "")
for s in self.songs])
body = index_template.format(list_content=song_list)
def print_index(self, index_file, dj_engine):
songs = sorted(self.songs, key=lambda s: s.number)
html = dj_engine.get_template("index.html").render(Context({'songs': songs}))
with open(index_file, 'w') as f:
f.write(page_template.format(css=index_css, main=body))
f.write(html)
def print_songs(self, directory="."):
for song in self.songs:
num_dir = join(directory, "%03d" % (song.number))
mkdir(num_dir)
with open(join(num_dir, "index.html"), 'w') as f:
f.write(song_redir_template.format(url=urllib.parse.quote("../" + song.get_url())))
song_dir = join(directory, song.get_url())
mkdir(song_dir)
with open(join(song_dir, "index.html"), 'w') as f:
f.write(page_template.format(css=song_css, main=str(song)))
def print_song(self, song, directory, dj_engine):
context = Context({'song': song})
num_dir = join(directory, "%03d" % (song.number))
mkdir(num_dir)
with open(join(num_dir, "index.html"), 'w') as f:
f.write(dj_engine.get_template("song_redir.html").render(context))
song_dir = join(directory, song.url())
mkdir(song_dir)
with open(join(song_dir, "index.html"), 'w') as f:
f.write(dj_engine.get_template("song.html").render(context))
def generate_html(self, output_dir):
def generate_html(self, output_dir, dj_engine):
mkdir(output_dir)
self.print_songs(output_dir)
self.print_index(join(output_dir, "index.html"))
for song in self.songs:
self.print_song(song, output_dir, dj_engine)
self.print_index(join(output_dir, "index.html"), dj_engine)
def parse_args():
@ -277,4 +263,6 @@ def parse_args():
if __name__ == '__main__':
args = parse_args()
loader = SongLoader(args.latex[0], args.audios[0])
loader.generate_html(args.output_dir[0])
settings.configure(USE_TZ=False, USE_I18N=False)
e = Engine(dirs=["res/html/"])
loader.generate_html(args.output_dir[0], e)