Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@
"react-router": "^5.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "5.0.1",
"sj-usfm-grammar": "3.0.8",
"styled-components": "^5.3.6",
"tc-ui-toolkit": "5.3.3",
"terser-webpack-plugin": "^5.3.10",
Expand All @@ -232,6 +231,7 @@
"use-deep-compare": "^1.1.0",
"usfm-editor": "0.8.7",
"usfm-grammar": "^2.3.1",
"usfm-grammar-web": "^3.0.0",
"uuid": "^9.0.1",
"wavesurfer.js": "^6.6.4",
"webpack-node-externals": "^3.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const ReferenceAudio = ({
const {
actions: {
setReferenceLoading,
setCounter,
// setCounter,
},
} = useContext(ReferenceContext);
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const useReadReferenceUsfmFile = ({
const {
actions: {
setReferenceLoading,
setCounter,
// setCounter,
},
} = useContext(ReferenceContext);

Expand Down Expand Up @@ -78,7 +78,7 @@ export const useReadReferenceUsfmFile = ({
status: true,
text: t('dynamic-msg-load-ref-bible-success'),
});
setCounter(4);
// setCounter(4);
} else if (chosenResource.type === 'common') {
const commonResourcePath = path.join(newpath, packageInfo.name, 'common', 'resources', refName, key);
const commonResourceIngredients = await readIngredients({ filePath: commonResourcePath });
Expand All @@ -98,7 +98,7 @@ export const useReadReferenceUsfmFile = ({
status: true,
text: t('dynamic-msg-load-ref-bible-success'),
});
setCounter(4);
// setCounter(4);
} else {
setUsfmData([]);
setBookAvailable(false);
Expand Down
71 changes: 49 additions & 22 deletions renderer/src/components/EditorPage/TextEditor/conversionUtils.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,66 @@
import USFMParser from 'sj-usfm-grammar';
import { USFMParser, Validator } from 'usfm-grammar-web';

let usfmParserInstance;
let usfmParserInitialized;
let validatorInstance;
let validatorInitialized;

export async function initializeParser() {
if (!usfmParserInstance) {
if (!usfmParserInitialized) {
usfmParserInitialized = await USFMParser.init();
}
await usfmParserInitialized;
usfmParserInstance = new USFMParser();
export async function initializeParser(usfm = null, usj = null) {
if (!usfmParserInitialized) {
usfmParserInitialized = await USFMParser.init(
'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0/tree-sitter-usfm.wasm',
'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0/tree-sitter.wasm',
);
}
await usfmParserInitialized;
if (usfm) {
usfmParserInstance = new USFMParser(usfm);
} else if (usj) {
usfmParserInstance = new USFMParser(null, usj);
}
return usfmParserInstance;
}

export async function convertUsfmToUsj(usfm) {
if (!usfmParserInstance) {
usfmParserInstance = await initializeParser();
export async function initializeValidator() {
if (!validatorInitialized) {
validatorInitialized = await Validator.init(
'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter-usfm.wasm',
'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter.wasm',
);
}
await validatorInitialized;
validatorInstance = new Validator();
return validatorInstance;
}
export async function convertUsfmToUsj(usfm) {
usfmParserInstance = await initializeParser(usfm, null);
try {
const usj = usfmParserInstance.usfmToUsj(usfm);
const usj = usfmParserInstance.toUSJ();
console.log('converting', { usj });

Check warning on line 39 in renderer/src/components/EditorPage/TextEditor/conversionUtils.js

View workflow job for this annotation

GitHub Actions / Lint Run

Unexpected console statement

Check warning on line 39 in renderer/src/components/EditorPage/TextEditor/conversionUtils.js

View workflow job for this annotation

GitHub Actions / Lint Run

Unexpected console statement
return { usj };
} catch (e) {
return { usj: { content: [] }, error: e };
}
}

export async function convertUsjToUsfm(usj) {
if (!usfmParserInstance) {
usfmParserInstance = await initializeParser();
}
const usfm = usfmParserInstance.usjToUsfm(usj);
usfmParserInstance = await initializeParser(null, usj);
// }
const usfm = usfmParserInstance.usfm;
return usfm;
}

export async function validateUsfm(usfm) {
if (!usfmParserInstance) {
usfmParserInstance = await initializeParser();
}
validatorInstance = await initializeValidator();

try {
const { isValid, validUSFM, bookCode } = usfmParserInstance.validate(usfm);
// console.log('USFM validation, isValid:', isValid, 'validUSFM:', validUSFM, bookCode);
return { isValid, validUSFM, bookCode };
let isValid = validatorInstance.isValidUSFM(usfm);
if (isValid) {
return { isValid, validUSFM: usfm };
}
const validUSFM = validatorInstance.autoFixUSFM(usfm);
isValid = validatorInstance.isValidUSFM(validUSFM);
return { isValid, validUSFM };
} catch (e) {
return { isValid: false, error: e };
}
Expand All @@ -56,3 +75,11 @@
// eslint-disable-next-line no-console
console.error('Error initializing USFM Parser:', err);
});

initializeValidator().then(() => {
// eslint-disable-next-line no-console
console.log('USFM Validator initialized successfully');
}).catch((err) => {
// eslint-disable-next-line no-console
console.error('Error initializing USFM Validator:', err);
});
14 changes: 10 additions & 4 deletions renderer/src/components/EditorPage/TextEditor/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ const defaultScrRef = {
};

export default function TextEditor() {
const [chapterNumber, setChapterNumber] = useState(1);
const [verseNumber, setVerseNumber] = useState(1);

const [usjInput, setUsjInput] = useState();
const [scrRef, setScrRef] = useState(defaultScrRef);
const [navRef, setNavRef] = useState();
const [parseError, setParseError] = useState(false);
const {
state: {
bookId: defaultBookId,
chapter,
verse,
selectedFont,
editorFontSize,
projectScriptureDir,
Expand All @@ -41,6 +40,9 @@ export default function TextEditor() {
handleEditorFontSize,
},
} = useContext(ReferenceContext);
// console.log({ chapter, verse });
const [chapterNumber, setChapterNumber] = useState(chapter);
const [verseNumber, setVerseNumber] = useState(verse);
const { showSnackbar } = useAutoSnackbar();
const { t } = useTranslation();
const [book, setBook] = useState(defaultBookId);
Expand Down Expand Up @@ -83,6 +85,11 @@ export default function TextEditor() {
);
}, [cachedData, loading]);

// useEffect(() => {
// setChapterNumber(chapter);
// setVerseNumber(verse);
// }, [chapter, verse]);

useEffect(() => {
setScrRef({
bookCode: book,
Expand All @@ -105,7 +112,6 @@ export default function TextEditor() {
const handleUsjChange = useMemo(
() => debounce(async (updatedUsj) => {
updateCacheNSaveFile(updatedUsj, book);
// console.log('usj updated', updatedUsj);
}, 1000),
[book],
);
Expand Down
30 changes: 14 additions & 16 deletions renderer/src/components/Projects/ImportPopUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import styles from './ImportPopUp.module.css';
import * as logger from '../../logger';
import CloseIcon from '@/illustrations/close-button-black.svg';
import { updateJsonJuxta } from './utils/updateJsonJuxta';
import { extractBookCode } from './utils/extractBookCode';

const grammar = require('usfm-grammar');
const advanceSettings = require('../../lib/AdvanceSettings.json');

export default function ImportPopUp(props) {
Expand Down Expand Up @@ -119,7 +119,8 @@ export default function ImportPopUp(props) {
switch (projectType) {
case 'Translation': {
const usfm = fs.readFileSync(filePath, 'utf8');
const { isValid, validUSFM, bookCode } = await validateUsfm(usfm);
const { isValid, validUSFM } = await validateUsfm(usfm);
const bookCode = extractBookCode(validUSFM);
if (isValid) {
// If importing a USFM file then ask user for replace of USFM with the new content or not
replaceConformation(true);
Expand All @@ -137,15 +138,14 @@ export default function ImportPopUp(props) {

case 'Audio': {
const usfm = fs.readFileSync(filePath, 'utf8');
const myUsfmParser = new grammar.USFMParser(usfm, grammar.LEVEL.RELAXED);
const isJsonValid = myUsfmParser.validate();
if (isJsonValid) {
const { isValid, validUSFM } = await validateUsfm(usfm);
const bookCode = extractBookCode(validUSFM);
if (isValid) {
// If importing a USFM file then ask user for replace of USFM with the new content or not
replaceConformation(true);
logger.debug('ImportPopUp.js', 'Valid USFM file.');
const jsonOutput = myUsfmParser.toJSON();
files.push({ id: jsonOutput.book.bookCode, content: usfm });
bookCodeList.push(jsonOutput.book.bookCode);
files.push({ id: bookCode, content: validUSFM });
bookCodeList.push(bookCode);
} else {
logger.warn('ImportPopUp.js', 'Invalid USFM file.');
setNotify('failure');
Expand Down Expand Up @@ -186,17 +186,15 @@ export default function ImportPopUp(props) {
const fileExt = filename.split('.').pop()?.toLowerCase();
if (fileExt === 'txt' || fileExt === 'usfm' || fileExt === 'text' || fileExt === 'sfm'
|| fileExt === undefined) {
const myUsfmParser = new grammar.USFMParser(file, grammar.LEVEL.RELAXED);
const isJsonValid = myUsfmParser.validate();
const { isValid, validUSFM } = await validateUsfm(file);
const bookCode = extractBookCode(validUSFM);
// if the USFM is valid
if (isJsonValid) {
if (isValid) {
replaceConformation(true);
logger.debug('ImportPopUp.js', 'Valid USFM file.');
// then we get the book code and we transform our data to our Juxta json file
const jsonOutput = myUsfmParser.toJSON();
const juxtaJson = JSON.stringify(readUsfm(file, jsonOutput.book.bookCode));
files.push({ id: jsonOutput.book.bookCode, content: juxtaJson });
bookCodeList.push(jsonOutput.book.bookCode);
const juxtaJson = JSON.stringify(readUsfm(file, bookCode));
files.push({ id: bookCode, content: juxtaJson });
bookCodeList.push(bookCode);
} else {
logger.warn('ImportPopUp.js', 'Invalid USFM file.');
setNotify('failure');
Expand Down
4 changes: 4 additions & 0 deletions renderer/src/components/Projects/utils/extractBookCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const extractBookCode = (text) => {
const idMatch = text.match(/\\id\s+([A-Za-z0-9]{3})/);
return idMatch ? idMatch[1].toUpperCase() : null;
};
6 changes: 3 additions & 3 deletions renderer/src/components/context/ReferenceContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function ReferenceContextProvider({ children }) {
status: false,
text: '',
});
const [counter, setCounter] = useState(7);
// const [counter, setCounter] = useState(7);
const [bookmarksVerses, setBookmarksVerses] = useState([]);
const myEditorRef = useRef();
const [closeNavigation, setCloseNavigation] = useState(false);
Expand Down Expand Up @@ -227,7 +227,7 @@ export default function ReferenceContextProvider({ children }) {
layout,
row,
referenceLoading,
counter,
// counter,
bookmarksVerses,
myEditorRef,
closeNavigation,
Expand Down Expand Up @@ -280,7 +280,7 @@ export default function ReferenceContextProvider({ children }) {
setLayout,
setRow,
setReferenceLoading,
setCounter,
// setCounter,
setBookmarksVerses,
setCloseNavigation,
setProjectScriptureDir,
Expand Down
2 changes: 1 addition & 1 deletion renderer/src/translations/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export const En = {
'dynamic-msg-resource-added': 'resource added successfully',
'dynamic-msg-resource-unable-fetch-url': 'unable to fetch selected resource from the given url',
'dynamic-msg-load-ref-bible-snack': 'successfully loaded {{refName}} files',
'dynamic-msg-load-ref-bible-snack-fail': 'failed to loaded {{refName}} files',
'dynamic-msg-load-ref-bible-snack-fail': 'failed to load {{refName}} files',
'dynamic-msg-load-ref-bible-success': 'Reference-burrito loaded succesfully',
'dynamic-msg-validate-hook-project-name': 'The input has to be between {{minLen}} and {{maxLen}} characters long',
'dynamic-msg-update-burrito-version': 'Update the the burrito from {{version1}} to {{version2}}',
Expand Down
Loading
Loading