-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactored backend; bump version
- Loading branch information
Showing
14 changed files
with
223 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,3 +196,4 @@ vite.config.ts.timestamp-* | |
# project | ||
hymnal/temp/ | ||
scripts/ | ||
static/hymnal/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ services: | |
ports: | ||
- "8080:80" | ||
volumes: | ||
- ./dist:/usr/share/nginx/html:ro | ||
- ./build:/usr/share/nginx/html:ro |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Transform HTML to JSON.""" | ||
|
||
# standard | ||
from dataclasses import asdict | ||
from pathlib import Path | ||
from json import dump | ||
|
||
# external | ||
from bs4 import BeautifulSoup | ||
|
||
# local | ||
from hymnal.sketch import Hymn, Verse | ||
|
||
|
||
def _html_song_parser(hymn_file: Path): | ||
"""Parse songs from reveal.js HTML files.""" | ||
with open(file=hymn_file, mode="rt", encoding="utf-8") as html_file: | ||
html_content = html_file.read() | ||
soup = BeautifulSoup(html_content, "lxml") | ||
hymn = Hymn() | ||
if hymn_number_tag := soup.select_one("h1.hymn-no"): | ||
hymn.number = int(hymn_number_tag.text) | ||
hymn.starts = ( | ||
"refrain" | ||
if '<section id="refrain" class="start-here">' in html_content | ||
else "verse-1" | ||
) | ||
for each_verse in soup.select('section[id^="verse-"]'): | ||
verse_number = ( | ||
int(verse_number_tag.text.rstrip(".")) | ||
if (verse_number_tag := each_verse.select_one("p.verse-no")) | ||
else -1 | ||
) | ||
lines = [line_tag.text.strip() for line_tag in each_verse.select("h3")] | ||
lines.extend([line_tag.text.strip() for line_tag in each_verse.select("h4")]) | ||
hymn.verses.append(Verse(number=verse_number, lines=lines)) | ||
if (refrain_tag := soup.select_one('section[id^="refrain"]')) and ( | ||
refrain := refrain_tag.select("em") | ||
): | ||
hymn.refrain = [line_tag.text.strip() for line_tag in refrain] | ||
hymn.verses.sort(key=lambda verses: verses.number) | ||
return hymn | ||
|
||
|
||
def h2j_transform(source: Path, destination: Path): | ||
"""Convert HTML to JSON.""" | ||
for idx in range(1, 1632): | ||
hymn_path = source / f"{str(idx).zfill(4)}.html" | ||
if not hymn_path.exists() or not hymn_path.is_file(): | ||
continue | ||
with open( | ||
file=destination / f"{str(idx).zfill(4)}.json", | ||
mode="wt", | ||
encoding="utf-8", | ||
) as json_file: | ||
dump( | ||
obj=asdict(_html_song_parser(hymn_path)), | ||
fp=json_file, | ||
ensure_ascii=False, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""Transform JSON to HTML.""" | ||
|
||
# standard | ||
from logging import DEBUG, INFO, debug, getLogger | ||
from pathlib import Path | ||
from json import load | ||
|
||
# external | ||
from jinja2 import Environment, PackageLoader, select_autoescape | ||
|
||
# local | ||
from hymnal.sketch import Hymn | ||
|
||
|
||
def _json_song_parser(hymn_path: Path, hymnal_name: str): | ||
"""Parse songs from JSON files.""" | ||
with open(file=hymn_path, mode="rb") as json_file: | ||
hymn = Hymn(**load(json_file)) | ||
if hymn.number == -1: | ||
return ( | ||
"<html>" | ||
+ "<head><title>BAD FILE</title></head>" | ||
+ f"<body><p>Bad File Error: {hymn_path}</p><body>" | ||
+ "</html>" | ||
) | ||
cathedral = Environment( | ||
loader=PackageLoader("main"), | ||
autoescape=select_autoescape(), | ||
trim_blocks=True, | ||
lstrip_blocks=True, | ||
) | ||
song_template = ( | ||
( | ||
"start-at-refrain-without-verses.html.j2" | ||
if not hymn.verses | ||
else "start-at-refrain.html.j2" | ||
) | ||
if hymn.starts == "refrain" | ||
else ( | ||
"start-at-verse-without-refrain.html.j2" | ||
if not hymn.refrain | ||
else "start-at-verse-with-refrain.html.j2" | ||
) | ||
) | ||
# .render(**kwargs) ignores keys not found in template | ||
return cathedral.get_template(song_template).render( | ||
hymn_number=hymn.number, | ||
hymnal_name=hymnal_name, | ||
relative_path="..", | ||
start_tag="<h3>", | ||
first_verse=hymn.verses[0] if hymn.verses else None, | ||
verses=hymn.verses if hymn.starts == "refrain" else hymn.verses[1:], | ||
end_tag="</h3>", | ||
start_ref_tag="<h3><em>", | ||
refrain=hymn.refrain, | ||
end_ref_tag="</em></h3>", | ||
) | ||
|
||
|
||
def j2h_transform(source: Path, destination: Path, hymnal_name: str): | ||
"""Convert JSON to HTML.""" | ||
getLogger().setLevel(DEBUG) | ||
for idx in range(1, 1632): # NOTE: this is not cool! | ||
file_name = str(idx).zfill(4) | ||
hymn_path = source / f"{file_name}.json" | ||
if not hymn_path.exists() or not hymn_path.is_file(): | ||
continue | ||
with open( | ||
file=destination / f"{file_name}.html", | ||
mode="wt", | ||
encoding="utf-8", | ||
) as html_file: | ||
html_file.write(_json_song_parser(hymn_path, hymnal_name)) | ||
debug(f" created file {file_name}.html") | ||
getLogger().setLevel(INFO) |
Oops, something went wrong.