Skip to content

Commit 9110a76

Browse files
committed
Began exploratory work on storybooks
Started taking code from PR sillsdev#645. Discovered that storybook files seem to be modified before the code in convertBooks runs.
1 parent 8002e5f commit 9110a76

2 files changed

Lines changed: 79 additions & 8 deletions

File tree

convert/convertBooks.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { convertMarkdownsToMilestones } from './convertMarkdown';
1111
import { hasAudioExtension, hasImageExtension } from './stringUtils';
1212
import { Promisable, Task, TaskOutput } from './Task';
1313
import { verifyGlossaryEntries } from './verifyGlossaryEntries';
14+
import { convertStorybookElements } from './storybook';
1415

1516
const base = process.env.BUILD_BASE_PATH || '';
1617

@@ -291,12 +292,19 @@ function applyFilters(
291292
text: string,
292293
filterFunctions: FilterFunction[],
293294
bcId: string,
294-
bookId: string
295+
bookId: string, bookType?: string
295296
): string {
296297
let filteredText = text;
298+
if (bookType === 'story') {
299+
console.log("Before filters:");
300+
console.log(text);
301+
}
297302
for (const filterFn of filterFunctions) {
298303
filteredText = filterFn(filteredText, bcId, bookId);
299304
}
305+
if (bookType === 'story') {
306+
filteredText = convertStorybookElements(filteredText);
307+
}
300308
return filteredText;
301309
}
302310

@@ -394,7 +402,6 @@ export async function convertBooks(
394402
for (const book of collection.books) {
395403
let bookConverted = false;
396404
switch (book.type) {
397-
case 'story':
398405
case 'songs':
399406
case 'audio-only':
400407
case 'bloom-player':
@@ -702,7 +709,7 @@ function convertScriptureBook(
702709
function processBookContent(resolve: () => void, err: any, content: string) {
703710
//process.stdout.write(`processBookContent: bookId:${book.id}, error:${err}\n`);
704711
if (err) throw err;
705-
content = applyFilters(content, usfmFilterFunctions, context.bcId, book.id);
712+
content = applyFilters(content, usfmFilterFunctions, context.bcId, book.id, book.type);
706713
if (context.scriptureConfig.traits['has-glossary']) {
707714
content = verifyGlossaryEntries(content, bcGlossary);
708715
}
@@ -727,10 +734,10 @@ function convertScriptureBook(
727734
if (context.verbose) {
728735
console.log(
729736
context.docSet +
730-
' <- ' +
731-
book.name +
732-
': ' +
733-
path.join(context.dataDir, 'books', context.bcId, book.file)
737+
' <- ' +
738+
book.name +
739+
': ' +
740+
path.join(context.dataDir, 'books', context.bcId, book.file)
734741
);
735742
}
736743
displayBookId(context.bcId, book.id);
@@ -780,8 +787,22 @@ function convertScriptureBook(
780787
const filePath = path.join(context.dataDir, 'books', context.bcId, file);
781788
fileContents.push(fs.readFileSync(filePath, 'utf-8'));
782789
});
790+
// Collect the file contents into a single document
791+
let usfm: string;
792+
793+
if (book.type == 'story') {
794+
// The first file contains meta-content (id, title, etc)
795+
usfm = fileContents[0];
783796

784-
processBookContent(resolve, null, fileContents.join(''));
797+
// Subsequent files represent storybook pages.
798+
// SAB deletes the \page tags. Replace them with chapter tags.
799+
for (let i = 1; i < fileContents.length; i++) {
800+
usfm += `\\c ${i} ${fileContents[i]}`;
801+
}
802+
} else {
803+
usfm = fileContents.join('');
804+
}
805+
processBookContent(resolve, null, usfm);
785806
}
786807
})
787808
);

convert/storybook.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//It seems that even before the code in convertBooks, the storybook already has had its img tags removed. I need to figure out how to prevent this from happening.
2+
/*By the time that applyFilters (In convertBooks) is called, which is at the beginning of convertScriptureBook and thus the beginning of where I would expect
3+
anything to alter the file, the storybook has already had some changes made to it compared to what is displayed in the Scripture App Builder in the "Book File"
4+
tab of the Unmerciful Servant book. These changes include the removal of img tags. The Unmerciful Servant book has its source in a .docx file, so I am guessing
5+
that the changes are made in the conversion of the .docx to the .sfm files.
6+
*/
7+
8+
/**
9+
* Replace page tags with chapters
10+
*/
11+
export function replacePageTags(usfm: string): string {
12+
return usfm.replace(/\\page\s+(\d+)/g, '\\c $1');
13+
}
14+
15+
/**
16+
* Replace img tags with figs
17+
*/
18+
export function replaceImageTags(usfm: string): string {
19+
return usfm.replace(/\\img\s+(\S+)/g, '\\fig caption |src="$1"\\fig*');
20+
}//"caption" is just a placeholder for now.
21+
//\fig Jesus Birth|src="JesusBirth.jpg" size="span"\fig*
22+
//\img image1.jpeg
23+
24+
25+
26+
/**
27+
* Convert list tags to milestones (I need to test this to see if this is needed or if the list tags can be parsed as they currently are)
28+
*/
29+
export function transformLists(usfm: string): string {
30+
return usfm
31+
.replace(/\\zuli1\s+([^\\]*)/g, '\\zuli1-s\\* $1 \\zuli1-e\\* ')
32+
.replace(/\\zon1 (\d+)(([^\\]|\\zoli)*)/g, '\\zon1-s |start="$1"\\* $2 \\zon1-e\\* ')
33+
.replace(/\\zoli1\s+([^\\]*)/g, '\\zoli1-s\\* $1 \\zoli1-e\\* ');
34+
}//Maybe keep, maybe not
35+
36+
export function transformHeadings(usfm: string): string {
37+
return usfm.replace(/\\(m?s\d?)([^\\]*)/g, '\\m \\zusfm-s |class="$1"\\* $2 \\zusfm-e ');
38+
}//Also maybe keep, maybe not
39+
40+
export function convertStorybookElements(usfm: string) {
41+
console.log("Storybook start:");
42+
console.log(usfm);
43+
usfm = replacePageTags(usfm);
44+
usfm = replaceImageTags(usfm);
45+
//usfm = transformLists(usfm);
46+
//usfm = transformHeadings(usfm);
47+
console.log("Storybook end:");
48+
console.log(usfm);
49+
return usfm;
50+
}

0 commit comments

Comments
 (0)