Skip to content

Commit 59776a4

Browse files
committed
Convert special storybook tags
1 parent 95b4b64 commit 59776a4

File tree

3 files changed

+101
-8
lines changed

3 files changed

+101
-8
lines changed

convert/convertBooks.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { queries, postQueries, freeze } from '../sab-proskomma-tools';
1010
import { convertMarkdownsToMilestones } from './convertMarkdown';
1111
import { verifyGlossaryEntries } from './verifyGlossaryEntries';
1212
import { hasAudioExtension, hasImageExtension } from './stringUtils';
13+
import { convertStorybookElements } from './storybook';
1314

1415
/**
1516
* Loops through bookCollections property of configData.
@@ -22,10 +23,6 @@ function replaceVideoTags(text: string, _bcId: string, _bookId: string): string
2223
return text.replace(/\\video (.*)/g, '\\zvideo-s |id="$1"\\*\\zvideo-e\\*');
2324
}
2425

25-
// This is the start of supporting story books, but it still fails if there is no chapter.
26-
function replacePageTags(text: string, _bcId: string, _bookId: string): string {
27-
return text.replace(/\\page (.*)/g, '\\zpage-s |id="$1"\\*\\zpage-e\\*');
28-
}
2926
function loadGlossary(collection: any, dataDir: string): string[] {
3027
const glossary: string[] = [];
3128
for (const book of collection.books) {
@@ -101,16 +98,21 @@ function isImageMissing(imageSource: string): boolean {
10198
const filterFunctions: ((text: string, bcId: string, bookId: string) => string)[] = [
10299
removeStrongNumberReferences,
103100
replaceVideoTags,
104-
replacePageTags,
105101
convertMarkdownsToMilestones,
106102
removeMissingFigures
107103
];
108104

109-
function applyFilters(text: string, bcId: string, bookId: string): string {
105+
function applyFilters(text: string, bcId: string, bookId: string, bookType?: string): string {
110106
let filteredText = text;
111107
for (const filterFn of filterFunctions) {
112108
filteredText = filterFn(filteredText, bcId, bookId);
113109
}
110+
if (bookType === 'story') {
111+
filteredText = convertStorybookElements(filteredText);
112+
}
113+
if (bcId == 'C01' && bookId == '1') {
114+
console.log(filteredText.slice(0, 1000));
115+
}
114116
return filteredText;
115117
}
116118

@@ -204,7 +206,6 @@ export async function convertBooks(
204206
for (const book of collection.books) {
205207
let bookConverted = false;
206208
switch (book.type) {
207-
case 'story':
208209
case 'songs':
209210
case 'audio-only':
210211
case 'bloom-player':
@@ -490,7 +491,7 @@ function convertScriptureBook(
490491
function processBookContent(resolve: () => void, err: any, content: string) {
491492
//process.stdout.write(`processBookContent: bookId:${book.id}, error:${err}\n`);
492493
if (err) throw err;
493-
content = applyFilters(content, context.bcId, book.id);
494+
content = applyFilters(content, context.bcId, book.id, book.type);
494495
if (context.configData.traits['has-glossary']) {
495496
content = verifyGlossaryEntries(content, bcGlossary);
496497
}

convert/storybook.test.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect, test } from 'vitest';
2+
import { transformLists, replacePageTags as transformPageTags, removeImageTags } from './storybook';
3+
4+
function tokensOf(str: string) {
5+
return str.split(/\s+/).filter((token) => token.length);
6+
}
7+
8+
test('replace page tags', () => {
9+
const input = 'abc \\page 41 xyz \\page 45 122';
10+
const expected = 'abc \\c 41 xyz \\c 45 122';
11+
const result = transformPageTags(input);
12+
expect(tokensOf(result)).toEqual(tokensOf(expected));
13+
});
14+
15+
// For now, get image data from config.js (may change this in the future)
16+
test('remove img tags', () => {
17+
const input = 'abc \\img img1.jpeg efg \\img image-2.jpeg \\img image_with_underscores.jpeg';
18+
const expected = 'abc efg';
19+
const result = removeImageTags(input);
20+
expect(tokensOf(result)).toEqual(tokensOf(expected));
21+
});
22+
23+
test('convert unordered list to milestones', () => {
24+
const input = `
25+
\\m Some content
26+
\\zuli1 One
27+
\\zuli1 Two \\zuli1 Three in a row!
28+
\\b
29+
`;
30+
const expected = `
31+
\\m Some content
32+
\\zuli1-s\\* One \\zuli1-e\\*
33+
\\zuli1-s\\* Two \\zuli1-e\\*
34+
\\zuli1-s\\* Three in a row! \\zuli1-e\\*
35+
\\b
36+
`;
37+
const result = transformLists(input);
38+
expect(tokensOf(result)).toEqual(tokensOf(expected));
39+
});
40+
41+
test('convert ordered list to milestones', () => {
42+
const input = `
43+
\\m Some content
44+
\\zon1 10
45+
\\zoli1 One
46+
\\zoli1 Two \\zoli1 Three in a row!
47+
\\b
48+
`;
49+
const expected = `
50+
\\m Some content
51+
\\zon1-s |start="10"\\*
52+
\\zoli1-s\\* One \\zoli1-e\\*
53+
\\zoli1-s\\* Two \\zoli1-e\\*
54+
\\zoli1-s\\* Three in a row! \\zoli1-e\\*
55+
\\zon1-e\\*
56+
\\b
57+
`;
58+
const result = transformLists(input);
59+
expect(tokensOf(result)).toEqual(tokensOf(expected));
60+
});

convert/storybook.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Replace page tags with chapters
3+
*/
4+
export function replacePageTags(usfm: string): string {
5+
return usfm.replace(/\\page\s+(\d+)/g, '\\c $1');
6+
}
7+
8+
/**
9+
* Remove img tags
10+
*
11+
* For now, get images from config (may change this later)
12+
*/
13+
export function removeImageTags(usfm: string): string {
14+
return usfm.replace(/\\img\s+\S+/g, '');
15+
}
16+
17+
/**
18+
* Convert list tags to milestones
19+
*/
20+
export function transformLists(usfm: string): string {
21+
return usfm
22+
.replace(/\\zuli1\s+([^\\]*)/g, '\\zuli1-s\\* $1 \\zuli1-e\\* ')
23+
.replace(/\\zon1 (\d+)(([^\\]|\\zoli)*)/g, '\\zon1-s |start="$1"\\* $2 \\zon1-e\\* ')
24+
.replace(/\\zoli1\s+([^\\]*)/g, '\\zoli1-s\\* $1 \\zoli1-e\\* ');
25+
}
26+
27+
export function convertStorybookElements(usfm: string) {
28+
usfm = replacePageTags(usfm);
29+
usfm = removeImageTags(usfm);
30+
usfm = transformLists(usfm);
31+
return usfm;
32+
}

0 commit comments

Comments
 (0)