Added lyrics to scores
This commit is contained in:
@@ -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)
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user