From 769818f35e599cfb7d18f6a362be1777a6603b51 Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Wed, 15 Nov 2023 12:06:25 -0800 Subject: [PATCH 1/2] feat: Improve generated doc formatting --- .../javascript/templates/examples/default.md | 6 +-- .../examples/polymorphic-pull-event.md | 9 ++--- .../templates/examples/polymorphic-pull.md | 6 +-- .../javascript/templates/examples/setter.md | 6 +-- .../templates/examples/subscriber.md | 7 ++-- .../markdown/templates/codeblocks/provider.md | 2 +- src/macrofier/engine.mjs | 37 +++++++++++++------ 7 files changed, 39 insertions(+), 34 deletions(-) diff --git a/languages/javascript/templates/examples/default.md b/languages/javascript/templates/examples/default.md index ecc979ba..8a407397 100644 --- a/languages/javascript/templates/examples/default.md +++ b/languages/javascript/templates/examples/default.md @@ -1,6 +1,4 @@ import { ${module} } from '${package.name}' -${module}.${method.name}(${example.params}) - .then(${method.result.name} => { - console.log(${method.result.name}) - }) \ No newline at end of file +let ${method.result.name} = await ${module}.${method.name}(${example.params}) +console.log(${method.result.name}) \ No newline at end of file diff --git a/languages/javascript/templates/examples/polymorphic-pull-event.md b/languages/javascript/templates/examples/polymorphic-pull-event.md index 7bc2baa7..5881ac08 100644 --- a/languages/javascript/templates/examples/polymorphic-pull-event.md +++ b/languages/javascript/templates/examples/polymorphic-pull-event.md @@ -1,9 +1,8 @@ import { ${module} } from '${package.name}' -${module}.${method.pulls.for}(function(parameters) { +let success = await ${module}.${method.pulls.for}(async parameters => { console.log(parameters.entityId) console.log(parameters.assetId) - return Promise.resolve(${originator.params[1].example.value}) -}).then(success => { - console.log(success) -}) \ No newline at end of file + return ${originator.params[1].example.value} +}) +console.log(success) \ No newline at end of file diff --git a/languages/javascript/templates/examples/polymorphic-pull.md b/languages/javascript/templates/examples/polymorphic-pull.md index 7c53080c..d754217d 100644 --- a/languages/javascript/templates/examples/polymorphic-pull.md +++ b/languages/javascript/templates/examples/polymorphic-pull.md @@ -1,6 +1,4 @@ import { ${module} } from '${package.name}' -${module}.${method.name}(${method.params[1].example.value}) - .then(success => { - console.log(success) - }) \ No newline at end of file +let success = await ${module}.${method.name}(${method.params[1].example.value}) +console.log(success) \ No newline at end of file diff --git a/languages/javascript/templates/examples/setter.md b/languages/javascript/templates/examples/setter.md index c258314a..85c2bcff 100644 --- a/languages/javascript/templates/examples/setter.md +++ b/languages/javascript/templates/examples/setter.md @@ -1,6 +1,4 @@ import { ${module} } from '${package.name}' -${module}.${method.setter.for}(${example.params}) - .then(${method.result.name} => { - console.log(${method.result.name}) - }) \ No newline at end of file +let ${method.result.name} = await ${module}.${method.setter.for}(${example.params}) +console.log(${method.result.name}) \ No newline at end of file diff --git a/languages/javascript/templates/examples/subscriber.md b/languages/javascript/templates/examples/subscriber.md index 99c0516f..0dd6a7a3 100644 --- a/languages/javascript/templates/examples/subscriber.md +++ b/languages/javascript/templates/examples/subscriber.md @@ -1,7 +1,6 @@ import { ${module} } from '${package.name}' -${method.alternative}(value => { +let listenerId = await ${method.alternative}(value => { console.log(value) -}).then(listenerId => { - console.log(listenerId) -}) \ No newline at end of file +}) +console.log(listenerId) \ No newline at end of file diff --git a/languages/markdown/templates/codeblocks/provider.md b/languages/markdown/templates/codeblocks/provider.md index 32823cb0..e30d3a19 100644 --- a/languages/markdown/templates/codeblocks/provider.md +++ b/languages/markdown/templates/codeblocks/provider.md @@ -23,7 +23,7 @@ import { ${info.title} } from '${package.name}' class My${provider} { ${provider.interface.start} async ${provider.interface.name}(parameters, session) { - return await Promise.resolve(${provider.interface.example.result}) + return ${provider.interface.example.result} } ${provider.interface.end} } diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 543e9a57..1a950378 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1271,6 +1271,11 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) return template } +function jsonToJsObject(json) { + json.replace(/\\"/g,"\uFFFF") + return json.replace(/"([^"]+)":/g, '$1:').replace(/\uFFFF/g, '\\\"') +} + function insertExampleMacros(template, examples, method, json, templates) { let content = '' @@ -1299,13 +1304,13 @@ function insertExampleMacros(template, examples, method, json, templates) { first = false const formatParams = (params, delimit, pretty = false) => params.map(p => JSON.stringify((example.json.params.find(x => x.name === p.name) || { value: null }).value, null, pretty ? ' ' : null)).join(delimit) - let indent = ' '.repeat(json.info.title.length + method.name.length + 2) + let hasObjectParam = example.json.params.find(p => typeof p.value === 'object') != null let params = formatParams(method.params, ', ') - if (params.length + indent > 80) { + if ((params.length > 80) || hasObjectParam) { params = formatParams(method.params, ',\n', true) params = params.split('\n') let first = params.shift() - params = params.map(p => indent + p) + params = params.map(p => ' ' + p) params.unshift(first) params = params.join('\n') } @@ -1318,7 +1323,7 @@ function insertExampleMacros(template, examples, method, json, templates) { .replace(/\$\{method\.result\.name\}/g, method.result.name) .replace(/\$\{method\.name\}/g, method.name) - .replace(/\$\{example\.params\}/g, params) + .replace(/\$\{example\.params\}/g, jsonToJsObject(params)) .replace(/\$\{example\.result\}/g, language.result) .replace(/\$\{example\.result\.item\}/g, Array.isArray(example.json.result.value) ? JSON.stringify(example.json.result.value[0], null, '\t') : '') .replace(/\$\{module\}/g, json.info.title) @@ -1326,12 +1331,16 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ + let indent = 2 + if (language.langcode === 'json') { + indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ + } } const value = JSON.stringify(method.examples[index].params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, value) + const objText = language.langcode === 'json' ? value : jsonToJsObject(value) + languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, objText) }) @@ -1340,12 +1349,16 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ + let indent = 2 + if (language.langcode === 'json') { + indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ + } } const value = JSON.stringify(originalExample.params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, value) + const objText = language.langcode === 'json' ? value : jsonToJsObject(value) + languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, objText) }) } From ea68aff7ae432562c4157862ecc373a7d2ffdb2b Mon Sep 17 00:00:00 2001 From: Kevin Pearson Date: Mon, 27 Nov 2023 09:10:12 -0800 Subject: [PATCH 2/2] feat: Doc formatting --- src/macrofier/engine.mjs | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src/macrofier/engine.mjs b/src/macrofier/engine.mjs index 1a950378..543e9a57 100644 --- a/src/macrofier/engine.mjs +++ b/src/macrofier/engine.mjs @@ -1271,11 +1271,6 @@ function insertMethodMacros(template, methodObj, json, templates, examples = {}) return template } -function jsonToJsObject(json) { - json.replace(/\\"/g,"\uFFFF") - return json.replace(/"([^"]+)":/g, '$1:').replace(/\uFFFF/g, '\\\"') -} - function insertExampleMacros(template, examples, method, json, templates) { let content = '' @@ -1304,13 +1299,13 @@ function insertExampleMacros(template, examples, method, json, templates) { first = false const formatParams = (params, delimit, pretty = false) => params.map(p => JSON.stringify((example.json.params.find(x => x.name === p.name) || { value: null }).value, null, pretty ? ' ' : null)).join(delimit) - let hasObjectParam = example.json.params.find(p => typeof p.value === 'object') != null + let indent = ' '.repeat(json.info.title.length + method.name.length + 2) let params = formatParams(method.params, ', ') - if ((params.length > 80) || hasObjectParam) { + if (params.length + indent > 80) { params = formatParams(method.params, ',\n', true) params = params.split('\n') let first = params.shift() - params = params.map(p => ' ' + p) + params = params.map(p => indent + p) params.unshift(first) params = params.join('\n') } @@ -1323,7 +1318,7 @@ function insertExampleMacros(template, examples, method, json, templates) { .replace(/\$\{method\.result\.name\}/g, method.result.name) .replace(/\$\{method\.name\}/g, method.name) - .replace(/\$\{example\.params\}/g, jsonToJsObject(params)) + .replace(/\$\{example\.params\}/g, params) .replace(/\$\{example\.result\}/g, language.result) .replace(/\$\{example\.result\.item\}/g, Array.isArray(example.json.result.value) ? JSON.stringify(example.json.result.value[0], null, '\t') : '') .replace(/\$\{module\}/g, json.info.title) @@ -1331,16 +1326,12 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 2 - if (language.langcode === 'json') { - indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ - } + let indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ } const value = JSON.stringify(method.examples[index].params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - const objText = language.langcode === 'json' ? value : jsonToJsObject(value) - languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, objText) + languageContent = languageContent.replace(/\$\{method\.params\[([0-9]+)\]\.example\.value\}/g, value) }) @@ -1349,16 +1340,12 @@ function insertExampleMacros(template, examples, method, json, templates) { const matches = [...languageContent.matchAll(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g)] matches.forEach(match => { const paramIndex = parseInt(match[1]) - let indent = 2 - if (language.langcode === 'json') { - indent = 0 - while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { - indent++ - } + let indent = 0 + while (match.index - indent >= 0 && match.input[match.index - indent] !== '\n') { + indent++ } const value = JSON.stringify(originalExample.params[paramIndex].value, null, '\t').split('\n').map((line, i) => i > 0 ? ' '.repeat(indent) + line : line).join('\n') - const objText = language.langcode === 'json' ? value : jsonToJsObject(value) - languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, objText) + languageContent = languageContent.replace(/\$\{originator\.params\[([0-9]+)\]\.example\.value\}/g, value) }) }