diff --git a/scripts/odf/templating/fillOdtElementTemplate.js b/scripts/odf/templating/fillOdtElementTemplate.js index f4f48be..fd6995f 100644 --- a/scripts/odf/templating/fillOdtElementTemplate.js +++ b/scripts/odf/templating/fillOdtElementTemplate.js @@ -1,5 +1,5 @@ import {traverse, Node} from '../../DOMUtils.js' -import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex} from './markers.js' +import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js' /** * @typedef TextPlaceToFill @@ -14,7 +14,8 @@ import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, if * @returns {TextPlaceToFill | undefined} */ function findPlacesToFillInString(str, compartment) { - const matches = str.matchAll(/\{([^{#\/]+?)\}/g) + const varRexExp = new RegExp(variableRegex.source, 'g'); + const matches = str.matchAll(varRexExp) /** @type {TextPlaceToFill['expressions']} */ const expressions = [] diff --git a/scripts/odf/templating/markers.js b/scripts/odf/templating/markers.js index da1928a..1a09f1e 100644 --- a/scripts/odf/templating/markers.js +++ b/scripts/odf/templating/markers.js @@ -1,4 +1,6 @@ // the regexps below are shared, so they shoudn't have state (no 'g' flag) +export const variableRegex = /\{([^{#\/]+?)\}/ + export const ifStartMarkerRegex = /{#if\s+([^}]+?)\s*}/; export const elseMarker = '{:else}' export const closingIfMarker = '{/if}' diff --git a/scripts/odf/templating/prepareTemplateDOMTree.js b/scripts/odf/templating/prepareTemplateDOMTree.js index 50fae71..da3af78 100644 --- a/scripts/odf/templating/prepareTemplateDOMTree.js +++ b/scripts/odf/templating/prepareTemplateDOMTree.js @@ -1,5 +1,5 @@ import {traverse, Node} from "../../DOMUtils.js"; -import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex} from './markers.js' +import {closingIfMarker, eachClosingMarker, eachStartMarkerRegex, elseMarker, ifStartMarkerRegex, variableRegex} from './markers.js' /** * @@ -183,8 +183,6 @@ function consolidateMarkers(document){ containerTextNodesInTreeOrder.push(/** @type {Text} */(node)) } }) - - console.log('containerTextNodesInTreeOrder', containerTextNodesInTreeOrder.map(n => n.textContent)) } refreshContainerTextNodes() @@ -200,18 +198,19 @@ function consolidateMarkers(document){ ...findAllMatches(fullText, elseMarker), ...findAllMatches(fullText, closingIfMarker), ...findAllMatches(fullText, eachStartMarkerRegex), - ...findAllMatches(fullText, eachClosingMarker) + ...findAllMatches(fullText, eachClosingMarker), + ...findAllMatches(fullText, variableRegex) ]; - if(positionedMarkers.length >= 1) - console.log('positionedMarkers', positionedMarkers) + /*if(positionedMarkers.length >= 1) + console.log('positionedMarkers', positionedMarkers)*/ while(consolidatedMarkers.length < positionedMarkers.length) { refreshContainerTextNodes() // For each marker, check if it's contained within a single text node for(const positionedMarker of positionedMarkers.slice(consolidatedMarkers.length)) { - console.log('positionedMarker', positionedMarker) + //console.log('positionedMarker', positionedMarker) let currentPos = 0; let startNode; @@ -222,8 +221,6 @@ function consolidateMarkers(document){ const nodeStart = currentPos; const nodeEnd = nodeStart + textNode.textContent.length; - console.log('nodeStart, nodeEnd', nodeStart, nodeEnd) - // If start of marker is in this node if(!startNode && positionedMarker.index >= nodeStart && positionedMarker.index < nodeEnd) { startNode = textNode; @@ -239,8 +236,6 @@ function consolidateMarkers(document){ currentPos = nodeEnd; } - console.log('startNode, endNode', startNode?.textContent, endNode?.textContent) - if(!startNode){ throw new Error(`Could not find startNode for marker '${positionedMarker.marker}'`) } @@ -251,8 +246,6 @@ function consolidateMarkers(document){ // Check if marker spans multiple nodes if(startNode !== endNode) { - const commonAncestor = findCommonAncestor(startNode, endNode); - // Calculate relative positions within the nodes let startNodeTextContent = startNode.textContent || ''; let endNodeTextContent = endNode.textContent || ''; diff --git a/tests/fill-odt-template/formatting.js b/tests/fill-odt-template/formatting.js index cf05508..fda17fc 100644 --- a/tests/fill-odt-template/formatting.js +++ b/tests/fill-odt-template/formatting.js @@ -86,7 +86,7 @@ Les nombres : 3 5 8 13  !! }); -// + test('template filling - {/each} and text after partially formatted', async t => { const templatePath = join(import.meta.dirname, '../fixtures/formatting-liste-nombres-each-end-and-after-formatted.odt') const templateContent = `Liste de nombres @@ -112,3 +112,30 @@ Les nombres : 5 8 13 21  !! `) }); + + + + +test('template filling - partially formatted variable', async t => { + const templatePath = join(import.meta.dirname, '../fixtures/partially-formatted-variable.odt') + const templateContent = `Nombre + +Voici le nombre : {nombre} !!! +` + + const data = {nombre : 37} + + const odtTemplate = await getOdtTemplate(templatePath) + + const templateTextContent = await getOdtTextContent(odtTemplate) + t.deepEqual(templateTextContent, templateContent, 'reconnaissance du template') + + const odtResult = await fillOdtTemplate(odtTemplate, data) + + const odtResultTextContent = await getOdtTextContent(odtResult) + t.deepEqual(odtResultTextContent, `Nombre + +Voici le nombre : 37 !!! +`) + +}); \ No newline at end of file diff --git a/tests/fixtures/partially-formatted-variable.odt b/tests/fixtures/partially-formatted-variable.odt new file mode 100644 index 0000000..cedf874 Binary files /dev/null and b/tests/fixtures/partially-formatted-variable.odt differ