Add arguments to latex_scanner and update Makefile with some defaults.

This commit is contained in:
Carlos Galindo 2020-12-12 17:21:04 +01:00
parent c7bca76c8c
commit 0c5f6a0da1
3 changed files with 35 additions and 28 deletions

View file

@ -20,7 +20,7 @@ public:
public/index.html: static public public/audios \ public/index.html: static public public/audios \
res/index.html res/page.html \ res/index.html res/page.html \
res/song.html res/song_li.html res/song_redir.html res/song.html res/song_li.html res/song_redir.html
python3 src/latex_scanner.py python3 src/latex_scanner.py --latex latex/cancionero.tex --audios audios
public/audios: audios public public/audios: audios public
rm -f public/audios rm -f public/audios

View file

@ -5,18 +5,17 @@ import re
from datetime import datetime from datetime import datetime
AUDIO_DIR = "audios/" def find_audios(index, audio_dir):
def find_audios(index):
""" """
Finds all audios in a folder that match the given index. Finds all audios in a folder that match the given index.
Audios must be in the format [index]_[YYYY]-[MM]-[DD].mp3 Audios must be in the format [index]_[YYYY]-[MM]-[DD].mp3
:param index: An integer denoting the song's index :param index: An integer denoting the song's index
:param audio_dir: The directory where the songs can be found
:return: A list of matching Audio objects :return: A list of matching Audio objects
""" """
res = [] res = []
for f in listdir(AUDIO_DIR): for f in listdir(audio_dir):
full_file = join(AUDIO_DIR, f) full_file = join(audio_dir, f)
re_date_match = re.match(r"^%03d[_ ](\d{4}-[01]\d-[0-3]\d).mp3$" % index, f) re_date_match = re.match(r"^%03d[_ ](\d{4}-[01]\d-[0-3]\d).mp3$" % index, f)
if not isfile(full_file) or not re_date_match: if not isfile(full_file) or not re_date_match:
continue continue

View file

@ -1,8 +1,10 @@
from song_types import * from song_types import Chord, Line, Song, Verse
from song_types import readfile, join_list
from audio_scanner import find_audios from audio_scanner import find_audios
from os.path import join from os.path import join
from pathlib import Path from pathlib import Path
import shutil import shutil
import argparse
import urllib.parse import urllib.parse
import os import os
import re import re
@ -38,13 +40,14 @@ song_css = '<link rel="stylesheet" href="../song.css">\n\t<link rel="stylesheet"
class SongLoader: class SongLoader:
def __init__(self, latex_file): def __init__(self, latex_file, audio_dir):
self.index = 1 self.index = 1
self.category = None self.category = None
self.categories = [] self.categories = []
self.songs = [] self.songs = []
self.scan(latex_file) self.scan(latex_file)
self.memory = {} self.memory = {}
self.audio_dir = audio_dir
def scan(self, latex_file): def scan(self, latex_file):
main_file = open(latex_file, 'r') main_file = open(latex_file, 'r')
@ -124,16 +127,17 @@ class SongLoader:
memory = None memory = None
memorizing = False memorizing = False
replay_index = 0 replay_index = 0
for a in find_audios(self.index): if hasattr(self, "audio_dir"):
a_split = a.audio_file.split("/") for a in find_audios(self.index, self.audio_dir):
renamed_dir = join(join(".", "audios"), "Canciones") a_split = a.audio_file.split("/")
new_name = "%03d %s - %s - %s.mp3" % ( renamed_dir = join(join(".", "audios"), "Canciones")
current_song.number, new_name = "%03d %s - %s - %s.mp3" % (
current_song.name, current_song.number,
current_song.author if current_song.author else "-", current_song.name,
a.date.strftime("%Y-%m-%d")) current_song.author if current_song.author else "-",
shutil.copy2("/".join(a_split[1:]), join(renamed_dir, new_name)) a.date.strftime("%Y-%m-%d"))
current_song.add_audio(a) shutil.copy2("/".join(a_split[1:]), join(renamed_dir, new_name))
current_song.add_audio(a)
continue continue
if re.match(r"\\endsong", remain): if re.match(r"\\endsong", remain):
text = beginning + text[i + len("\\endsong"):] text = beginning + text[i + len("\\endsong"):]
@ -256,17 +260,21 @@ class SongLoader:
with open(join(song_dir, "index.html"), 'w') as f: with open(join(song_dir, "index.html"), 'w') as f:
f.write(page_template.format(css=song_css, main=str(song))) f.write(page_template.format(css=song_css, main=str(song)))
def generate_html(self, output_dir):
mkdir(output_dir)
self.print_songs(output_dir)
self.print_index(join(output_dir, "index.html"))
def copy_static(source_dir, target_dir):
for f in os.listdir(source_dir): def parse_args():
if re.search(r"\.(css|js|svg)$", f): parser = argparse.ArgumentParser()
shutil.copy2(join(source_dir, f), target_dir) parser.add_argument("--latex", required=True, nargs=1, help="The main LaTeX file. It may include other documents")
parser.add_argument("--audios", required=False, nargs=1, default=[None], help="The folder containing the audio files.")
parser.add_argument("--output-dir", required=False, nargs=1, default=["public"])
return parser.parse_args()
if __name__ == '__main__': if __name__ == '__main__':
loader = SongLoader("latex/cancionero.tex") args = parse_args()
target_dir = "public/" loader = SongLoader(args.latex[0], args.audios[0])
mkdir(target_dir) loader.generate_html(args.output_dir[0])
loader.print_songs(target_dir)
loader.print_index(target_dir + "index.html")
copy_static("res", target_dir)