From b8f14d50f58dfca9f3aed331bf30a473d29e36b2 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Fri, 25 Dec 2020 17:44:51 +0000 Subject: [PATCH] Desktop, Cli: Add table captions when importing ENEX files --- .../tests/enex_to_md/tableWithCaption.html | 11 ++++ .../tests/enex_to_md/tableWithCaption.md | 5 ++ packages/lib/import-enex-md-gen.js | 55 ++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 packages/app-cli/tests/enex_to_md/tableWithCaption.html create mode 100644 packages/app-cli/tests/enex_to_md/tableWithCaption.md diff --git a/packages/app-cli/tests/enex_to_md/tableWithCaption.html b/packages/app-cli/tests/enex_to_md/tableWithCaption.html new file mode 100644 index 000000000..c07f023c6 --- /dev/null +++ b/packages/app-cli/tests/enex_to_md/tableWithCaption.html @@ -0,0 +1,11 @@ + + + + + + + + + + +
MP4
Video SegmentsDownload
onetwo
\ No newline at end of file diff --git a/packages/app-cli/tests/enex_to_md/tableWithCaption.md b/packages/app-cli/tests/enex_to_md/tableWithCaption.md new file mode 100644 index 000000000..ef5318b3f --- /dev/null +++ b/packages/app-cli/tests/enex_to_md/tableWithCaption.md @@ -0,0 +1,5 @@ +| Video Segments | Download | +| --- | --- | +| one | two | + +MP4 \ No newline at end of file diff --git a/packages/lib/import-enex-md-gen.js b/packages/lib/import-enex-md-gen.js index 57d1de62a..b7346fb4a 100644 --- a/packages/lib/import-enex-md-gen.js +++ b/packages/lib/import-enex-md-gen.js @@ -422,6 +422,27 @@ function displaySaxWarning(context, message) { console.warn(line.join(': ')); } +// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars +function removeSectionParent(section) { + if (typeof section === 'string') return section; + + section = { ...section }; + delete section.parent; + + section.lines = section.lines.slice(); + + for (let i = 0; i < section.lines.length; i++) { + section.lines[i] = removeSectionParent(section.lines[i]); + } + + return section; +} + +// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars +function printSection(section) { + console.info(JSON.stringify(removeSectionParent(section), null, 4)); +} + function enexXmlToMdArray(stream, resources) { const remainingResources = resources.slice(); @@ -570,6 +591,20 @@ function enexXmlToMdArray(stream, resources) { parent: section, }; + section.lines.push(newSection); + section = newSection; + } else if (n == 'caption') { + if (section.type != 'table') { + displaySaxWarning(this, 'Found a tag outside of a '); + // return; + } + + const newSection = { + type: 'caption', + lines: [], + parent: section, + }; + section.lines.push(newSection); section = newSection; } else if (isInvisibleBlock(nodeAttributes)) { @@ -780,7 +815,7 @@ function enexXmlToMdArray(stream, resources) { section.lines.push(BLOCK_CLOSE); } else if (n == 'td' || n == 'th') { if (section && section.parent) section = section.parent; - } else if (n == 'tr') { + } else if (n == 'tr' || n == 'caption') { if (section && section.parent) section = section.parent; } else if (n == 'table') { if (section && section.parent) section = section.parent; @@ -978,6 +1013,10 @@ function tableHasSubTables(table) { for (let tdIndex = 0; tdIndex < tr.lines.length; tdIndex++) { const td = tr.lines[tdIndex]; + + // We are inside a CAPTION, not a TD + if (typeof td === 'string') continue; + for (let i = 0; i < td.lines.length; i++) { if (typeof td.lines[i] === 'object') return true; } @@ -1003,8 +1042,15 @@ function drawTable(table) { let lines = []; lines.push(BLOCK_OPEN); let headerDone = false; + let caption = null; for (let trIndex = 0; trIndex < table.lines.length; trIndex++) { const tr = table.lines[trIndex]; + + if (tr.type === 'caption') { + caption = tr; + continue; + } + const isHeader = tr.isHeader; const line = []; const headerLine = []; @@ -1027,7 +1073,7 @@ function drawTable(table) { // In here, recursively render the tables for (let i = 0; i < td.lines.length; i++) { const c = td.lines[i]; - if (typeof c === 'object' && ['table', 'td', 'tr', 'th'].indexOf(c.type) >= 0) { + if (typeof c === 'object' && ['table', 'td', 'tr', 'th', 'caption'].indexOf(c.type) >= 0) { // This is a table renderCurrentCells(); currentCells = currentCells.concat(drawTable(c)); @@ -1095,6 +1141,11 @@ function drawTable(table) { lines.push(BLOCK_CLOSE); + if (caption) { + const captionLines = renderLines(caption.lines); + lines = lines.concat(captionLines); + } + return flatRender ? lines : lines.join(`<<<<:D>>>>${NEWLINE}<<<<:D>>>>`).split('<<<<:D>>>>'); }