Skip to content

Commit

Permalink
feat: Improve generated doc formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kpears201 committed Nov 15, 2023
1 parent db5dcc9 commit 769818f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 34 deletions.
6 changes: 2 additions & 4 deletions languages/javascript/templates/examples/default.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ${module} } from '${package.name}'

${module}.${method.name}(${example.params})
.then(${method.result.name} => {
console.log(${method.result.name})
})
let ${method.result.name} = await ${module}.${method.name}(${example.params})
console.log(${method.result.name})
Original file line number Diff line number Diff line change
@@ -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)
})
return ${originator.params[1].example.value}
})
console.log(success)
6 changes: 2 additions & 4 deletions languages/javascript/templates/examples/polymorphic-pull.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ${module} } from '${package.name}'

${module}.${method.name}(${method.params[1].example.value})
.then(success => {
console.log(success)
})
let success = await ${module}.${method.name}(${method.params[1].example.value})
console.log(success)
6 changes: 2 additions & 4 deletions languages/javascript/templates/examples/setter.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ${module} } from '${package.name}'

${module}.${method.setter.for}(${example.params})
.then(${method.result.name} => {
console.log(${method.result.name})
})
let ${method.result.name} = await ${module}.${method.setter.for}(${example.params})
console.log(${method.result.name})
7 changes: 3 additions & 4 deletions languages/javascript/templates/examples/subscriber.md
Original file line number Diff line number Diff line change
@@ -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)
})
})
console.log(listenerId)
2 changes: 1 addition & 1 deletion languages/markdown/templates/codeblocks/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
37 changes: 25 additions & 12 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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')
}
Expand All @@ -1318,20 +1323,24 @@ 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)

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)
})


Expand All @@ -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)
})
}

Expand Down

0 comments on commit 769818f

Please sign in to comment.