Skip to content

Commit

Permalink
Convert special storybook tags
Browse files Browse the repository at this point in the history
  • Loading branch information
gmjgeek committed Jul 29, 2024
1 parent 95b4b64 commit 59776a4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 8 deletions.
17 changes: 9 additions & 8 deletions convert/convertBooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { queries, postQueries, freeze } from '../sab-proskomma-tools';
import { convertMarkdownsToMilestones } from './convertMarkdown';
import { verifyGlossaryEntries } from './verifyGlossaryEntries';
import { hasAudioExtension, hasImageExtension } from './stringUtils';
import { convertStorybookElements } from './storybook';

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

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

function applyFilters(text: string, bcId: string, bookId: string): string {
function applyFilters(text: string, bcId: string, bookId: string, bookType?: string): string {
let filteredText = text;
for (const filterFn of filterFunctions) {
filteredText = filterFn(filteredText, bcId, bookId);
}
if (bookType === 'story') {
filteredText = convertStorybookElements(filteredText);
}
if (bcId == 'C01' && bookId == '1') {
console.log(filteredText.slice(0, 1000));
}
return filteredText;
}

Expand Down Expand Up @@ -204,7 +206,6 @@ export async function convertBooks(
for (const book of collection.books) {
let bookConverted = false;
switch (book.type) {
case 'story':
case 'songs':
case 'audio-only':
case 'bloom-player':
Expand Down Expand Up @@ -490,7 +491,7 @@ function convertScriptureBook(
function processBookContent(resolve: () => void, err: any, content: string) {
//process.stdout.write(`processBookContent: bookId:${book.id}, error:${err}\n`);
if (err) throw err;
content = applyFilters(content, context.bcId, book.id);
content = applyFilters(content, context.bcId, book.id, book.type);
if (context.configData.traits['has-glossary']) {
content = verifyGlossaryEntries(content, bcGlossary);
}
Expand Down
60 changes: 60 additions & 0 deletions convert/storybook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, test } from 'vitest';
import { transformLists, replacePageTags as transformPageTags, removeImageTags } from './storybook';

function tokensOf(str: string) {
return str.split(/\s+/).filter((token) => token.length);
}

test('replace page tags', () => {
const input = 'abc \\page 41 xyz \\page 45 122';
const expected = 'abc \\c 41 xyz \\c 45 122';
const result = transformPageTags(input);
expect(tokensOf(result)).toEqual(tokensOf(expected));
});

// For now, get image data from config.js (may change this in the future)
test('remove img tags', () => {
const input = 'abc \\img img1.jpeg efg \\img image-2.jpeg \\img image_with_underscores.jpeg';
const expected = 'abc efg';
const result = removeImageTags(input);
expect(tokensOf(result)).toEqual(tokensOf(expected));
});

test('convert unordered list to milestones', () => {
const input = `
\\m Some content
\\zuli1 One
\\zuli1 Two \\zuli1 Three in a row!
\\b
`;
const expected = `
\\m Some content
\\zuli1-s\\* One \\zuli1-e\\*
\\zuli1-s\\* Two \\zuli1-e\\*
\\zuli1-s\\* Three in a row! \\zuli1-e\\*
\\b
`;
const result = transformLists(input);
expect(tokensOf(result)).toEqual(tokensOf(expected));
});

test('convert ordered list to milestones', () => {
const input = `
\\m Some content
\\zon1 10
\\zoli1 One
\\zoli1 Two \\zoli1 Three in a row!
\\b
`;
const expected = `
\\m Some content
\\zon1-s |start="10"\\*
\\zoli1-s\\* One \\zoli1-e\\*
\\zoli1-s\\* Two \\zoli1-e\\*
\\zoli1-s\\* Three in a row! \\zoli1-e\\*
\\zon1-e\\*
\\b
`;
const result = transformLists(input);
expect(tokensOf(result)).toEqual(tokensOf(expected));
});
32 changes: 32 additions & 0 deletions convert/storybook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Replace page tags with chapters
*/
export function replacePageTags(usfm: string): string {
return usfm.replace(/\\page\s+(\d+)/g, '\\c $1');
}

/**
* Remove img tags
*
* For now, get images from config (may change this later)
*/
export function removeImageTags(usfm: string): string {
return usfm.replace(/\\img\s+\S+/g, '');
}

/**
* Convert list tags to milestones
*/
export function transformLists(usfm: string): string {
return usfm
.replace(/\\zuli1\s+([^\\]*)/g, '\\zuli1-s\\* $1 \\zuli1-e\\* ')
.replace(/\\zon1 (\d+)(([^\\]|\\zoli)*)/g, '\\zon1-s |start="$1"\\* $2 \\zon1-e\\* ')
.replace(/\\zoli1\s+([^\\]*)/g, '\\zoli1-s\\* $1 \\zoli1-e\\* ');
}

export function convertStorybookElements(usfm: string) {
usfm = replacePageTags(usfm);
usfm = removeImageTags(usfm);
usfm = transformLists(usfm);
return usfm;
}

0 comments on commit 59776a4

Please sign in to comment.