Skip to content

Commit

Permalink
feat: Doc formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kpears201 committed Nov 27, 2023
1 parent 769818f commit ea68aff
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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')
}
Expand All @@ -1323,24 +1318,20 @@ 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)

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


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

Expand Down

0 comments on commit ea68aff

Please sign in to comment.