Added lyrics to scores

This commit is contained in:
marc
2025-04-05 15:10:56 +02:00
parent d5eb40e300
commit 211a1fbb05
7 changed files with 89 additions and 9 deletions

View File

@@ -1,12 +1,14 @@
import asyncio
import dataclasses
import itertools
import os
import re
from pathlib import Path
from typing import Literal
import aiofiles
from folkugat_web.model.lilypond import RenderError
from folkugat_web.model.lilypond import (LyricsColumn, LyricsParagraph,
LyricsText, RenderError)
from folkugat_web.model.temes import Tema
from folkugat_web.services import files as files_service
from folkugat_web.templates import templates
@@ -17,10 +19,15 @@ RenderFormat = Literal["png"] | Literal["pdf"]
def source_to_single_score(source: str, tema: Tema) -> str:
if tema.lyrics:
lyrics_text = build_lyrics(tema.lyrics[0].content)
else:
lyrics_text = None
return templates.get_template("lilypond/single_score.ly").render(
score_beginning=SCORE_BEGINNING,
score_source=source,
tema=tema,
lyrics_text=lyrics_text,
)
@@ -68,3 +75,15 @@ async def render(source: str, fmt: RenderFormat, output_filename: Path | None =
output_filename = output_filename.with_suffix(".pdf")
return output_filename, []
def build_lyrics(text: str, max_cols: int = 2, min_pars: int = 2) -> LyricsText:
paragraphs = [LyricsParagraph(lines=par_str.splitlines()) for par_str in text.split("\n\n")]
n_cols = next(filter(lambda nc: len(paragraphs) // nc >= min_pars, range(max_cols, 0, -1)), 1)
n_long_cols = len(paragraphs) % n_cols
pars_per_col = [len(paragraphs) // n_cols + 1] * n_long_cols + [len(paragraphs) // n_cols] * (n_cols - n_long_cols)
acc_pars_per_col = [0] + list(itertools.accumulate(pars_per_col))
return LyricsText(columns=[
LyricsColumn(paragraphs=paragraphs[acc_pars_per_col[i]:acc_pars_per_col[i+1]])
for i in range(n_cols)
])

View File

@@ -1,7 +1,26 @@
import dataclasses
import re
from collections.abc import Iterable, Iterator
from typing import Callable
from folkugat_web.dal.sql.temes import lyrics as lyrics_dal
from folkugat_web.model import temes as model
from folkugat_web.utils import FnChain
def _sub(pattern: str, sub: str) -> Callable[[str], str]:
def _inner_sub(text: str) -> str:
return re.sub(pattern, sub, text)
return _inner_sub
def _clean_string(lyrics_str: str) -> str:
return (
FnChain.transform(lyrics_str) |
_sub(r" *", " ") |
_sub(r"\s*\n\s*", "\n") |
_sub(r"\n*", "\n")
).result()
def add_lyrics_to_tema(tema: model.Tema) -> model.Tema:
@@ -19,6 +38,7 @@ def get_lyric_by_id(lyric_id: int, tema_id: int | None = None) -> model.Lyrics |
def update_lyric(lyric: model.Lyrics):
lyric = dataclasses.replace(lyric, content=_clean_string(lyric.content))
lyrics_dal.update_lyric(lyric=lyric)

View File

@@ -4,6 +4,7 @@ from fastapi import HTTPException
from folkugat_web.dal.sql.temes import scores as scores_dal
from folkugat_web.model import temes as model
from folkugat_web.services import lilypond
from folkugat_web.services.temes import lyrics as lyrics_service
from folkugat_web.services.temes import properties as properties_service
from folkugat_web.services.temes import query as temes_q
from folkugat_web.utils import FnChain
@@ -51,6 +52,7 @@ def build_single_tune_full_source(tema_id: int, source: str) -> str:
raise HTTPException(status_code=404, detail="Could not find tema!")
tema = (
FnChain.transform(tema) |
properties_service.add_properties_to_tema
properties_service.add_properties_to_tema |
lyrics_service.add_lyrics_to_tema
).result()
return lilypond.source_to_single_score(source=source, tema=tema)