Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(doc-util): escape dotted keys #34

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions doc-util/render.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
%s
|||,

sectionTitle: '%(abbr)s %(prefix)s%(name)s',
sectionTitle: '%(abbr)s %(prefixAndName)s',

sectionLink: '* [`%(abbr)s %(linkName)s`](#%(link)s)',

value: '* `%(prefix)s%(name)s` (`%(type)s`): `"%(value)s"` - %(help)s',
value: '* `%(type)s` `%(prefixAndName)s`: `"%(value)s"` - %(help)s',

section: |||
%(headerDepth)s %(title)s
Expand All @@ -33,17 +33,34 @@
|||,
},

joinPrefixes(prefixes, sep='.')::
joinPathPrefixes(prefixes, sep='/')::
std.join(sep, prefixes)
+ (if std.length(prefixes) > 0
then sep
else ''),

joinPrefixes(prefixes, sep='.')::
std.join('', [
local key = root.escapeDottedKey(prefixes[i]);
if i == 0
then key
else
if key != prefixes[i]
then key
else sep + key
for i in std.range(0, std.length(prefixes) - 1)
]),

escapeDottedKey(key)::
if std.member(key, '.')
then '["%s"]' % key
else key,

renderSectionTitle(section, prefixes)::
root.templates.sectionTitle % {
name: section.name,
abbr: section.type.abbr,
prefix: root.joinPrefixes(prefixes),
prefixAndName: root.joinPrefixes(prefixes + [section.name]),
},

renderValues(values, prefixes=[])::
Expand All @@ -52,7 +69,7 @@
std.join('\n', [
root.templates.value
% value {
prefix: root.joinPrefixes(prefixes),
prefixAndName: root.joinPrefixes(prefixes + [value.name]),
}
for value in values
]) + '\n'
Expand Down Expand Up @@ -119,7 +136,10 @@
link:
std.asciiLower(
std.strReplace(
std.strReplace(root.renderSectionTitle(section, prefixes), '.', '')
std.strReplace(
root.renderSectionTitle(section, prefixes)
, '.', ''
)
, ' ', '-'
)
),
Expand Down Expand Up @@ -161,7 +181,7 @@

help: self.doc.help,

linkName: self.name,
linkName: root.escapeDottedKey(self.name),

content:
if self.help != ''
Expand Down Expand Up @@ -244,11 +264,20 @@
(depth == 0)
)


// Field definition
else if std.startsWith(key, '#')
then (
local realKey = key[1:];
if 'value' in obj[key]

if !std.isObject(obj[key])
then
std.trace(
'INFO: docstring "%s" cannot be parsed, ignored while rendering.' % key,
{}
)

else if 'value' in obj[key]
then {
values+: [root.sections.value(
key,
Expand All @@ -272,7 +301,11 @@
depth
)],
}
else {}
else
std.trace(
'INFO: docstring "%s" cannot be parsed, ignored while rendering.' % key,
{}
)
)

// subPackage definition
Expand Down Expand Up @@ -314,7 +347,7 @@
renderIndexPage(package, prefixes)::
root.templates.indexPage % {
name: package.name,
prefix: root.joinPrefixes(prefixes),
prefix: root.joinPrefixes(prefixes + [package.name]),
index: std.join('\n', [
'* [%(name)s](%(name)s.md)' % sub
for sub in package.subPackages
Expand All @@ -326,7 +359,7 @@
if std.length(prefixes) > 0
then package.name + '.md'
else 'README.md';
local path = root.joinPrefixes(prefixes, '/');
local path = root.joinPathPrefixes(prefixes);
{
[path + key]: root.renderPackage(package),
}
Expand Down