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 \
res/index.html res/page.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
rm -f public/audios

View file

@ -5,18 +5,17 @@ import re
from datetime import datetime
AUDIO_DIR = "audios/"
def find_audios(index):
def find_audios(index, audio_dir):
"""
Finds all audios in a folder that match the given index.
Audios must be in the format [index]_[YYYY]-[MM]-[DD].mp3
: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
"""
res = []
for f in listdir(AUDIO_DIR):
full_file = join(AUDIO_DIR, f)
for f in listdir(audio_dir):
full_file = join(audio_dir, 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:
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 os.path import join
from pathlib import Path
import shutil
import argparse
import urllib.parse
import os
import re
@ -38,13 +40,14 @@ song_css = '<link rel="stylesheet" href="../song.css">\n\t<link rel="stylesheet"
class SongLoader:
def __init__(self, latex_file):
def __init__(self, latex_file, audio_dir):
self.index = 1
self.category = None
self.categories = []
self.songs = []
self.scan(latex_file)
self.memory = {}
self.audio_dir = audio_dir
def scan(self, latex_file):
main_file = open(latex_file, 'r')
@ -124,16 +127,17 @@ class SongLoader:
memory = None
memorizing = False
replay_index = 0
for a in find_audios(self.index):
a_split = a.audio_file.split("/")
renamed_dir = join(join(".", "audios"), "Canciones")
new_name = "%03d %s - %s - %s.mp3" % (
current_song.number,
current_song.name,
current_song.author if current_song.author else "-",
a.date.strftime("%Y-%m-%d"))
shutil.copy2("/".join(a_split[1:]), join(renamed_dir, new_name))
current_song.add_audio(a)
if hasattr(self, "audio_dir"):
for a in find_audios(self.index, self.audio_dir):
a_split = a.audio_file.split("/")
renamed_dir = join(join(".", "audios"), "Canciones")
new_name = "%03d %s - %s - %s.mp3" % (
current_song.number,
current_song.name,
current_song.author if current_song.author else "-",
a.date.strftime("%Y-%m-%d"))
shutil.copy2("/".join(a_split[1:]), join(renamed_dir, new_name))
current_song.add_audio(a)
continue
if re.match(r"\\endsong", remain):
text = beginning + text[i + len("\\endsong"):]
@ -256,17 +260,21 @@ class SongLoader:
with open(join(song_dir, "index.html"), 'w') as f:
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):
if re.search(r"\.(css|js|svg)$", f):
shutil.copy2(join(source_dir, f), target_dir)
def parse_args():
parser = argparse.ArgumentParser()
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__':
loader = SongLoader("latex/cancionero.tex")
target_dir = "public/"
mkdir(target_dir)
loader.print_songs(target_dir)
loader.print_index(target_dir + "index.html")
copy_static("res", target_dir)
args = parse_args()
loader = SongLoader(args.latex[0], args.audios[0])
loader.generate_html(args.output_dir[0])