Skip to content

Commit

Permalink
fix: Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
jlacivita committed May 28, 2024
1 parent 3cde328 commit ed4258c
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 23 deletions.
3 changes: 3 additions & 0 deletions languages/markdown/templates/codeblocks/interface.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface ${name} {
${methods}
}
1 change: 1 addition & 0 deletions languages/markdown/templates/declarations/default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function ${method.name}(${method.params}): Promise<${method.result.type}>
1 change: 1 addition & 0 deletions languages/markdown/templates/interfaces/default.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${method.name}(${method.signature.params}, session: ProviderSession): Promise<${method.result.type}>
1 change: 1 addition & 0 deletions languages/markdown/templates/interfaces/focusable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${method.name}(${method.signature.params}, session: FocusableProviderSession): Promise<${method.result.type}>
3 changes: 0 additions & 3 deletions languages/markdown/templates/types/default.md

This file was deleted.

5 changes: 0 additions & 5 deletions languages/markdown/templates/types/enum.md

This file was deleted.

3 changes: 0 additions & 3 deletions languages/markdown/templates/types/object.md

This file was deleted.

1 change: 1 addition & 0 deletions src/docs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const run = async ({
examples: examples,
templatesPerModule: config.templatesPerModule,
templatesPerSchema: config.templatesPerSchema,
operators: config.operators,
libraryName: libraryName,
hidePrivate: false
})
Expand Down
33 changes: 27 additions & 6 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ const generateMacros = (obj, templates, languages, options = {}) => {
const eventsEnum = generateEvents(obj, templates)

const examples = generateExamples(obj, templates, languages)
const allMethodsArray = generateMethods(obj, examples, templates, options.type)
const allMethodsArray = generateMethods(obj, examples, templates, languages, options.type)

Array.from(new Set(['methods'].concat(config.additionalMethodTemplates))).filter(dir => dir).forEach(dir => {

Expand Down Expand Up @@ -1186,7 +1186,7 @@ function generateMethodResult(type, templates) {
return result
}

function generateMethods(json = {}, examples = {}, templates = {}, type = '') {
function generateMethods(json = {}, examples = {}, templates = {}, languages = [], type = '') {
const methods = compose(
option([]),
getMethods
Expand Down Expand Up @@ -1215,7 +1215,7 @@ function generateMethods(json = {}, examples = {}, templates = {}, type = '') {
else if (dir.includes('methods') && (suffix && config.templateExtensionMap[dir] ? config.templateExtensionMap[dir].includes(suffix) : true)) {
const template = getTemplateForMethod(methodObj, templates, dir)
if (template && template.length) {
result.body[dir] = insertMethodMacros(template, methodObj, json, templates, type, examples)
result.body[dir] = insertMethodMacros(template, methodObj, json, templates, type, examples, languages)
}
}
})
Expand All @@ -1242,7 +1242,7 @@ function generateMethods(json = {}, examples = {}, templates = {}, type = '') {
}

// TODO: this is called too many places... let's reduce that to just generateMethods
function insertMethodMacros(template, methodObj, json, templates, type = '', examples = {}) {
function insertMethodMacros(template, methodObj, json, templates, type = '', examples = {}, languages = {}) {
const moduleName = getModuleName(json)

const info = {
Expand Down Expand Up @@ -1378,6 +1378,23 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
itemType = types.getSchemaType(result.schema.items, json, { destination: state.destination, templateDir: state.typeTemplateDir, section: state.section })
}

let signature

if (Object.keys(languages).length && template.indexOf('${method.signature}') >= 0) {
const lang = languages[Object.keys(languages)[0]]
signature = getTemplateForDeclaration(methodObj, templates, 'declarations')
types.setTemplates(lang)
const currentConfig = JSON.parse(JSON.stringify(config))
config.operators = config.operators || {}
config.operators.paramDelimiter = ', '
signature = insertMethodMacros(signature, methodObj, json, lang, type)
config = currentConfig
types.setTemplates(templates)
}
else {
signature = ''
}

template = insertExampleMacros(template, examples[methodObj.name] || [], methodObj, json, templates)
template = template.replace(/\$\{method\.name\}/g, method.name)
.replace(/\$\{method\.rpc\.name\}/g, methodObj.rpc_name || methodObj.name)
Expand All @@ -1403,6 +1420,7 @@ function insertMethodMacros(template, methodObj, json, templates, type = '', exa
.replace(/\$\{method\.params\.serialization\}/g, serializedParams)
.replace(/\$\{method\.params\.serialization\.with\.indent\}/g, indent(serializedParams, ' '))
// Typed signature stuff
.replace(/\$\{method\.signature\}/g, signature)
.replace(/\$\{method\.signature\.params\}/g, types.getMethodSignatureParams(methodObj, json, { destination: state.destination, section: state.section }))
.replace(/\$\{method\.signature\.result\}/g, types.getMethodSignatureResult(methodObj, json, { destination: state.destination, section: state.section }))
.replace(/\$\{method\.context\}/g, method.context.join(', '))
Expand Down Expand Up @@ -1691,7 +1709,7 @@ function insertParameterMacros(template, param, method, module) {
constraints = '<br/>' + constraints
}

return template
template = template
.replace(/\$\{method.param.name\}/g, param.name)
.replace(/\$\{method.param.Name\}/g, param.name[0].toUpperCase() + param.name.substring(1))
.replace(/\$\{method.param.summary\}/g, param.summary || '')
Expand All @@ -1700,7 +1718,10 @@ function insertParameterMacros(template, param, method, module) {
.replace(/\$\{json.param.type\}/g, jsonType)
.replace(/\$\{method.param.link\}/g, getLinkForSchema(param.schema, module)) //getType(param))
.replace(/\$\{method.param.constraints\}/g, constraints) //getType(param))
}

return template

}

function insertCapabilityMacros(template, capabilities, method, module) {
const content = []
Expand Down
21 changes: 16 additions & 5 deletions src/macrofier/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ const macrofy = async (
const templates = Object.assign(await readFiles(sharedTemplateList, sharedTemplates),
await readFiles(sdkTemplateList, template)) // sdkTemplates are second so they win ties

typer.setTemplates && typer.setTemplates(templates)
typer.setPrimitives(primitives)
typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies)
typer.setConvertTuples(convertTuplesToArraysOrObjects)

let templatesPermission = {}
if (persistPermission) {
templatesPermission = Object.assign(await readFilesPermissions(sharedTemplateList, sharedTemplates),
Expand All @@ -133,6 +128,22 @@ const macrofy = async (
exampleTemplates[config.name]['__config'] = config
}

// check if this is a "real" language or just documentation broiler-plate, e.g. markdown
if (Object.keys(templates).find(key => key.startsWith('/types/primitive'))) {
typer.setTemplates && typer.setTemplates(templates)
typer.setPrimitives(primitives)
}
else {
const lang = Object.entries(exampleTemplates)[0][1]
const prims = Object.entries(exampleTemplates)[0][1]['__config'].primitives
// add the templates from the first example language and the wrapper langauage
typer.setTemplates && typer.setTemplates(lang)
typer.setTemplates && typer.setTemplates(templates)
typer.setPrimitives(prims)
}
typer.setAllocatedPrimitiveProxies(allocatedPrimitiveProxies)
typer.setConvertTuples(convertTuplesToArraysOrObjects)

const staticCodeList = staticContent ? await readDir(staticContent, { recursive: true }) : []
const staticModules = staticModuleNames.map(name => ( { info: { title: name } } ))

Expand Down
2 changes: 1 addition & 1 deletion src/macrofier/types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ function getSchemaType(schema, module, { destination, templateDir = 'types', lin
const theTitle = insertSchemaMacros(namespaceStr + getTemplate(path.join(templateDir, 'title' + suffix)), schema, module, { name: schema.title, parent: getXSchemaGroup(schema, module), recursive: false })
const allocatedProxy = event || result

const title = schema.type === "object" || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false
const title = schema.type === "object" || schema.anyOf || schema.oneOf || Array.isArray(schema.type) && schema.type.includes("object") || schema.enum ? true : false

if (schema['$ref']) {
if (schema['$ref'][0] === '#') {
Expand Down

0 comments on commit ed4258c

Please sign in to comment.