Skip to content

Commit

Permalink
Display chapter labels when included as \cp in books (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvire authored Dec 2, 2024
1 parent fa30de5 commit 1a8ae14
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type BookConfig = {
portions?: string;
chapters?: number;
chaptersN?: string; // 1-34
chaptersLabels?: { [key: string]: string };
fonts: string[];
file: string;
audio: BookCollectionAudioConfig[];
Expand Down
11 changes: 11 additions & 0 deletions convert/convertConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,18 @@ function parseBookCollections(document: Document, verbose: number) {
for (const book of bookTags) {
if (verbose >= 2) console.log(`. book: ${book.id}`);
const audio: BookCollectionAudioConfig[] = [];
let chaptersLabels: { [key: string]: string } | undefined;
for (const page of book.getElementsByTagName('page')) {
if (verbose >= 2) console.log(`.. page: ${page.attributes[0].value}`);
const char = page.attributes.getNamedItem('char')?.value;
if (char) {
if (!chaptersLabels) {
// Initialize for the first one
chaptersLabels = {};
}
const chapterNum = page.attributes.getNamedItem('num')!.value;
chaptersLabels[chapterNum] = char;
}
const audioTag = page.getElementsByTagName('audio')[0];
if (!audioTag) continue;
const fTag = audioTag.getElementsByTagName('f')[0];
Expand Down Expand Up @@ -584,6 +594,7 @@ function parseBookCollections(document: Document, verbose: number) {
?.value,
chapters,
chaptersN,
chaptersLabels,
fonts,
id: book.attributes.getNamedItem('id')!.value,
type: book.attributes.getNamedItem('type')?.value,
Expand Down
18 changes: 17 additions & 1 deletion src/lib/components/BookSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ The navbar component.
$: chapter = $nextRef.chapter === '' ? $refs.chapter : $nextRef.chapter;
$: verseCount = getVerseCount(chapter, chapters);
$: numeralSystem = numerals.systemForBook(config, $refs.collection, book);
$: chaptersLabels =
config.bookCollections
.find((x) => $refs.collection === x.id)
.books.find((x) => book === x.id).chaptersLabels ?? {};
const showChapterSelector = config.mainFeatures['show-chapter-selector-after-book'];
$: listView = $userSettingsOrDefault['book-selection'] === 'list';
Expand Down Expand Up @@ -144,6 +148,18 @@ The navbar component.
return url;
}
function getChapterLabel(chapter) {
if (chapter === 'i') {
return $t['Chapter_Introduction_Symbol'];
}
if (chaptersLabels[Number(chapter)] !== undefined) {
return chaptersLabels[Number(chapter)];
}
return numerals.formatNumber(numeralSystem, chapter);
}
let bookGridGroup = ({ colId, bookLabel = 'abbreviation' }) => {
let groups = [];
var lastGroup = null;
Expand Down Expand Up @@ -196,7 +212,7 @@ The navbar component.
? [{ label: $t['Chapter_Introduction_Title'], id: 'i' }]
: null,
cells: Object.keys(chapters).map((x) => ({
label: numerals.formatNumber(numeralSystem, x),
label: getChapterLabel(x),
id: x
}))
}
Expand Down
31 changes: 28 additions & 3 deletions src/lib/components/ChapterSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ The navbar component.
$: showVerseSelector = $userSettingsOrDefault['verse-selection'];
$: verseCount = getVerseCount(book, chapter);
$: numeralSystem = numerals.systemForBook(config, $refs.collection, book);
$: chaptersLabels =
config.bookCollections
.find((x) => $refs.collection === x.id)
.books.find((x) => book === x.id).chaptersLabels ?? {};
$: c = $t.Selector_Chapter;
$: v = $t.Selector_Verse;
Expand Down Expand Up @@ -83,6 +87,18 @@ The navbar component.
return 0;
}
function getChapterLabel(chapter) {
if (chapter === 'i') {
return $t['Chapter_Introduction_Symbol'];
}
if (chaptersLabels[Number(chapter)] !== undefined) {
return chaptersLabels[Number(chapter)];
}
return numerals.formatNumber(numeralSystem, chapter);
}
function getVerseCount(book, chapter) {
if (!chapter || chapter === 'i') {
return 0;
Expand All @@ -96,15 +112,24 @@ The navbar component.
return count;
}
let chapterIndicator = (chapter) => {
// Needs to be reactive when the chapter changes if there is a nextRef.book
let chapterIndicator = (book, chapter) => {
chaptersLabels =
config.bookCollections
.find((x) => $refs.collection === x.id)
.books.find((x) => book === x.id)?.chaptersLabels ?? {};
let value = '';
if (chapter === 'i') {
value = $t['Chapter_Introduction_Symbol'];
} else if (chaptersLabels[chapter] !== undefined) {
value = chaptersLabels[chapter];
} else {
value = numerals.formatNumber(numeralSystem, chapter);
}
return value;
};
let verseGridGroup = (chapter) => {
let value = [];
let selectedChapter = chapters[chapter];
Expand Down Expand Up @@ -147,7 +172,7 @@ The navbar component.
<Dropdown bind:this={dropdown} on:nav-end={resetNavigation} cols="5">
<svelte:fragment slot="label">
<div class="normal-case" style={convertStyle($s['ui.selector.chapter'])}>
{chapterIndicator(chapter)}
{chapterIndicator(book, chapter)}
</div>
{#if canSelect}
<DropdownIcon color="white" />
Expand Down Expand Up @@ -175,7 +200,7 @@ The navbar component.
]
: null,
cells: Object.keys(chapters).map((x) => ({
label: numerals.formatNumber(numeralSystem, x),
label: getChapterLabel(x),
id: x
}))
}
Expand Down

0 comments on commit 1a8ae14

Please sign in to comment.