Skip to content

Commit

Permalink
Merge pull request #757 from friendsofagape/ft/tsvObsNotes_support
Browse files Browse the repository at this point in the history
Ft/tsv obs notes support
  • Loading branch information
vipinpaul authored Jan 19, 2023
2 parents 5d76bb9 + 36de8f3 commit 97492c4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
36 changes: 25 additions & 11 deletions renderer/src/components/EditorPage/Reference/OBS/ObsTn.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import localForage from 'localforage';
import { getObsTn } from './getObsTn';
import ObsResourceCard from './ObsResourceCard';
import * as logger from '../../../../logger';
import tsvJSON from './TsvToJson';
import ObsTsvToChapterLevelMd from './ObsTsvToChapterLevel';

function ObsTnCard({
resource,
Expand Down Expand Up @@ -50,26 +52,38 @@ function ObsTnCard({
const currentUser = user?.username;
const folder = path.join(newpath, 'autographa', 'users', `${currentUser}`, 'resources');
const projectName = `${offlineResource.data?.value?.meta?.name}_${offlineResource.data?.value?.meta?.owner}_${offlineResource.data?.value?.meta?.release?.tag_name}`;
if (fs.existsSync(path.join(folder, projectName))) {
const contentDir = offlineResource.data?.value?.projects[0]?.path;
const notesDir = path.join(folder, projectName, contentDir, chapter.toString().padStart(2, 0));
const items = [];
fs.readdir(notesDir, async (err, files) => {
if (fs.existsSync(path.join(folder, projectName))) {
if (offlineResource.data?.value?.dublin_core?.format?.toLowerCase() === 'text/tsv') {
logger.debug('inside OBS TN offline TSV resource');
const tsvFileName = offlineResource.data?.value?.projects[0]?.path;
const obsTsvData = await fs.readFileSync(path.join(folder, projectName, tsvFileName), 'utf8');
const obsTsvJson = obsTsvData && await tsvJSON(obsTsvData);
logger.debug('inside OBS TN offline TSV resource : created TSV JSON');
await ObsTsvToChapterLevelMd(obsTsvJson, chapter).then((chapterTsvData) => {
logger.debug('inside OBS TN offline TSV resource : generated chapter Md level occurencenot Array');
setItems(chapterTsvData);
});
} else {
const contentDir = offlineResource.data?.value?.projects[0]?.path;
const notesDir = path.join(folder, projectName, contentDir, chapter.toString().padStart(2, 0));
const items = [];
fs.readdir(notesDir, async (err, files) => {
if (err) {
// console.log(`Unable to scan directory: ${ err}`);
logger.debug('OfflineResourceFetch.js', 'reading offline dir not found err : ', err);
throw err;
// console.log(`Unable to scan directory: ${ err}`);
logger.debug('OfflineResourceFetch.js', 'reading offline dir not found err : ', err);
throw err;
}
// listing all files using forEach
await files.forEach(async (file) => {
const filecontent = await fs.readFileSync(path.join(notesDir, file), 'utf8');
items.push({ OccurrenceNote: filecontent });
const filecontent = await fs.readFileSync(path.join(notesDir, file), 'utf8');
items.push({ OccurrenceNote: filecontent });
});
if (items.length === files.length) {
logger.debug('OfflineResourceFetch.js', 'reading offline obs-tn finished ');
setItems(items);
}
});
});
}
}
});
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// filter current chapter data from whole TSV JSON and generate array of notes of md content
// based on reference of tsv
import * as logger from '../../../../logger';

export default async function ObsTsvToChapterLevelMd(tsvJSON, chapter) {
return new Promise((resolve) => {
logger.debug('in ObsTsvToChapterLevel.js : in promise');
const filteredData = tsvJSON.filter((data) => data.Reference.split(':')[0].toString() === chapter.toString());
const chapterTsvData = [];
const notesObj = {};
filteredData.forEach((tsvObj) => {
let mdstring = '';
mdstring += `# ${ tsvObj.Quote }\n\n${ tsvObj.Note }\n`;
if (tsvObj.Reference.split(':')[1] in notesObj) {
notesObj[tsvObj.Reference.split(':')[1]].OccurrenceNote += mdstring;
} else {
notesObj[tsvObj.Reference.split(':')[1]] = { OccurrenceNote: mdstring };
}
});
Object.values(notesObj).forEach((value) => {
chapterTsvData.push(value);
});
if (chapterTsvData?.length > 0) {
logger.debug('in ObsTsvToChapterLevel.js : finished tsv md array resolved');
resolve(chapterTsvData);
}
});
}
23 changes: 23 additions & 0 deletions renderer/src/components/EditorPage/Reference/OBS/TsvToJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// convert TSV to line by line json
import * as logger from '../../../../logger';

export default async function tsvJSON(tsv) {
const lines = tsv.split('\n');
logger.debug('in TsvToJson.js : in convert function');
const result = [];

const headers = lines[0].split('\t');

for (let i = 1; i < lines.length; i++) {
const obj = {};
const currentline = lines[i].split('\t');

for (let j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}

result.push(obj);
}
logger.debug('in TsvToJson.js : in convert function , Finished');
return result;
}
2 changes: 1 addition & 1 deletion renderer/src/components/Resources/ListResources.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const ListResources = ({
await fetchTranslationResource('Translation Questions', setTranslationQuestion, selectResource, selectedPreProd);
break;
case 'obs-tn':
await fetchTranslationResource('OBS Translation Notes', setObsTranslationNote, selectResource, selectedPreProd);
await fetchTranslationResource('OBS Translation Notes&subject=tsv obs Translation notes', setObsTranslationNote, selectResource, selectedPreProd);
break;
case 'obs-tq':
await fetchTranslationResource('OBS Translation Questions', setObsTranslationQuestion, selectResource, selectedPreProd);
Expand Down

0 comments on commit 97492c4

Please sign in to comment.