mirror of
https://gitlab.com/parroquia-san-leandro/cancionero-web.git
synced 2025-04-29 12:56:07 +02:00
Convert Python templates to django template engine
This commit is contained in:
parent
7f52ff6b6c
commit
fc90400f8f
18 changed files with 225 additions and 196 deletions
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
|
@ -1,6 +1,6 @@
|
|||
from os import listdir
|
||||
from os.path import isfile, join
|
||||
from song_types import Audio
|
||||
from model import Audio
|
||||
import re
|
||||
from datetime import datetime
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -8,13 +8,7 @@ def join_list(the_list, separator="\n"):
|
|||
return ft.reduce(lambda x, y: x + (separator if x else "") + str(y), the_list, "")
|
||||
|
||||
|
||||
def readfile(file):
|
||||
with open(file, 'r') as f:
|
||||
return join_list(f.readlines(), '')
|
||||
|
||||
|
||||
locale.setlocale(locale.LC_ALL, "es_ES.UTF-8")
|
||||
song_template = readfile("res/song.html")
|
||||
|
||||
|
||||
class Song:
|
||||
|
@ -32,17 +26,7 @@ class Song:
|
|||
self.category = category
|
||||
|
||||
def __str__(self):
|
||||
return song_template.format(
|
||||
name=self.name,
|
||||
author="<div>Autor: %s</div>" % self.author if self.author else "",
|
||||
origin="<div>Basada en: %s</div>" % self.origin if self.origin else "",
|
||||
capo_settings="""<div><span class="capo">Tono original: Cejilla {s.capo}</span>
|
||||
<button style="margin-left: 0.5em;" onclick="transpose({s.capo})">Transponer para quitarla</button></div>"""
|
||||
.format(s=self) if self.capo != 0 else "",
|
||||
song_html=join_list(self.verses),
|
||||
audios_header="<h3>Audios</h3>" if len(self.audios) > 0 else "",
|
||||
audios_html=join_list(self.audios),
|
||||
latex_file=self.latex_file)
|
||||
return self.name
|
||||
|
||||
def set_capo(self, capo):
|
||||
self.capo = capo
|
||||
|
@ -55,7 +39,7 @@ class Song:
|
|||
assert isinstance(verse, Verse)
|
||||
self.verses.append(verse)
|
||||
|
||||
def get_url(self):
|
||||
def url(self):
|
||||
return "%03d %s" % (self.number, self.name.replace("¿", "").replace("?", ""))
|
||||
|
||||
def chorded(self):
|
||||
|
@ -69,13 +53,10 @@ class Verse:
|
|||
def __init__(self, is_chorus=False):
|
||||
self.is_chorus = is_chorus
|
||||
self.lines = []
|
||||
self.kind = "chorus" if is_chorus else "verse"
|
||||
|
||||
def __str__(self):
|
||||
return """
|
||||
<div class="%s">
|
||||
%s
|
||||
</div>
|
||||
""" % ("chorus" if self.is_chorus else "verse", join_list([str(l) for l in self.lines], "\n<br>\n"))
|
||||
return join_list(self.lines, " ")
|
||||
|
||||
def add_line(self, line):
|
||||
assert isinstance(line, Line)
|
||||
|
@ -99,15 +80,11 @@ class Line:
|
|||
self.lyric_arr = []
|
||||
self.build()
|
||||
self.remove_brackets()
|
||||
assert len(self.chord_arr) == len(self.lyric_arr)
|
||||
self.zipped_arr = zip(self.chord_arr, self.lyric_arr)
|
||||
|
||||
def __str__(self):
|
||||
assert len(self.chord_arr) == len(self.lyric_arr)
|
||||
return join_list(["""<table class="chordedline"><tr class="chord"><td rowspan="%s">%s%s</td></tr>%s</table>"""
|
||||
% (self.chord_arr[i]["rowspan"] if "rowspan" in self.chord_arr[i] else 1,
|
||||
'<span class="%s"></span>' % self.chord_arr[i]["class"] if "class" in self.chord_arr[i] else "",
|
||||
self.chord_arr[i]["chord"] if "chord" in self.chord_arr[i] else "",
|
||||
'<tr class="lyric"><td>%s</td></tr>' % self.lyric_arr[i] if "rowspan" not in self.chord_arr[i] else ''
|
||||
) for i in range(len(self.chord_arr))], separator='')
|
||||
return self.text
|
||||
|
||||
def add_chord(self, index, chord):
|
||||
self.add_item(index, "chord", chord)
|
||||
|
@ -172,8 +149,7 @@ class Line:
|
|||
self.lyric_arr.append(Line.ECHO_BEGIN if inside_echo else '')
|
||||
mid = True
|
||||
self.lyric_arr[-1] += self.text[i]
|
||||
for i in range(len(self.lyric_arr)):
|
||||
self.lyric_arr[i] = re.sub(r"(^ | $)", " ", self.lyric_arr[i])
|
||||
self.lyric_arr = [re.sub(r"(^ | $)", " ", l) for l in self.lyric_arr]
|
||||
|
||||
def remove_brackets(self):
|
||||
self.lyric_arr = [l.replace('}', '').replace('{', '') for l in self.lyric_arr]
|
||||
|
@ -186,6 +162,10 @@ class Line:
|
|||
return False
|
||||
|
||||
|
||||
def chord_eng2lat(text):
|
||||
return Chord.CHORDS_LAT[Chord.ENG_INDEX[text]]
|
||||
|
||||
|
||||
class Chord:
|
||||
N_CHORDS = 12
|
||||
CHORDS_LAT = ['Do', 'Do#', 'Re', 'Re#', 'Mi', 'Fa', 'Fa#', 'Sol', 'Sol#', 'La', 'Sib', 'Si']
|
||||
|
@ -194,7 +174,7 @@ class Chord:
|
|||
|
||||
def __init__(self, text, base_transpose=0):
|
||||
self.text = text
|
||||
self.chords = []
|
||||
self.items = []
|
||||
self.base_transpose = base_transpose
|
||||
ignore = False
|
||||
for i, char in enumerate(text):
|
||||
|
@ -203,21 +183,15 @@ class Chord:
|
|||
continue
|
||||
if "A" <= char <= "G":
|
||||
if len(text) > i + 1 and (text[i + 1] == "#" or text[i + 1] == "&"):
|
||||
self.chords.append({'text': char + text[i + 1], 'chord': True})
|
||||
self.items.append({'text': chord_eng2lat(char + text[i + 1]), 'chord': True})
|
||||
ignore = True
|
||||
else:
|
||||
self.chords.append({'text': char, 'chord': True})
|
||||
self.items.append({'text': chord_eng2lat(char), 'chord': True})
|
||||
else:
|
||||
self.chords.append({'text': char, 'chord': False})
|
||||
self.items.append({'text': char, 'chord': False})
|
||||
|
||||
def __str__(self):
|
||||
res = ""
|
||||
for c in self.chords:
|
||||
if c['chord']:
|
||||
res += "<span class='c'>%s</span>" % Chord.CHORDS_LAT[Chord.ENG_INDEX[c['text']]]
|
||||
else:
|
||||
res += c['text']
|
||||
return res
|
||||
return self.text
|
||||
|
||||
|
||||
class Audio:
|
||||
|
@ -228,11 +202,4 @@ class Audio:
|
|||
self.audio_file = audio_file
|
||||
|
||||
def __str__(self):
|
||||
return """
|
||||
<div>
|
||||
Audio del %s <a href="%s"><span>Descargar</span></a>
|
||||
<audio controls style='width: 100%%;'>
|
||||
<source src='%s' type='audio/mpeg'/>
|
||||
</audio>
|
||||
</div>
|
||||
""" % (self.date_text, self.audio_file, self.audio_file)
|
||||
return self.audio_file
|
Loading…
Add table
Add a link
Reference in a new issue