Skip to content

Commit

Permalink
PAUSED: Use git resume to continue working. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
himdel committed May 29, 2024
1 parent 848f913 commit 2004db8
Showing 1 changed file with 117 additions and 146 deletions.
263 changes: 117 additions & 146 deletions src/components/render-plugin-doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,47 +53,37 @@ const Legend = ({ children }: { children: ReactNode }) => (
</div>
);

const Nesting = ({
children,
level = 0,
}: {
children: ReactNode;
level: number;
}) => {
if (level < 1) {
return children;
}

return (
<Nesting level={level - 1}>
<div
style={{
borderLeft: '4px solid var(--pf-v5-global--info-color--100)',
paddingLeft: '20px',
}}
>
{children}
</div>
</Nesting>
);
};
const Nesting = ({ children }: { children: ReactNode }) => (
<div
style={{
borderLeft: '4px solid var(--pf-v5-global--info-color--100)',
paddingLeft: '20px',
}}
>
{children}
</div>
);

const DescriptionListHorizontal = ({
items,
}: {
items: [string, ReactNode][];
items: [ReactNode, ReactNode][];
}) => (
<DescriptionList
isCompact
isHorizontal
style={{ gridTemplateColumns: 'none' }}
>
{items.map(([k, v]) =>
{items.map(([k, v], i) =>
v ? (
<DescriptionListGroup key={k}>
<DescriptionListTerm>{k}</DescriptionListTerm>
<DescriptionListDescription>{v}</DescriptionListDescription>
</DescriptionListGroup>
k ? (
<DescriptionListGroup key={i}>
<DescriptionListTerm>{k}</DescriptionListTerm>
<DescriptionListDescription>{v}</DescriptionListDescription>
</DescriptionListGroup>
) : (
<DescriptionListGroup key={i}>{v}</DescriptionListGroup>
)
) : null,
)}
</DescriptionList>
Expand Down Expand Up @@ -215,7 +205,7 @@ export class RenderPluginDoc extends Component<IProps, IState> {

const doc: PluginDoc = { ...plugin.doc_strings.doc };

const parseOptions = (options: PluginOption[], depth) => {
const parseOptions = (options: PluginOption[]) => {
for (const op of options) {
// Description is expected to be an array of strings. If its not,
// do what we can to make it one
Expand All @@ -227,7 +217,7 @@ export class RenderPluginDoc extends Component<IProps, IState> {

// recursively parse sub options
if (op.suboptions) {
parseOptions(op.suboptions, depth + 1);
parseOptions(op.suboptions);
}
}
};
Expand Down Expand Up @@ -266,14 +256,14 @@ export class RenderPluginDoc extends Component<IProps, IState> {
return null;
}

const parseReturnRecursive = (returnV: ReturnedValue[], depth) => {
const parseReturnRecursive = (returnV: ReturnedValue[]) => {
for (const ret of returnV) {
// Description is expected to be an array of strings. If its not, do what we can to make it one
ret.description = this.ensureListofStrings(ret.description);

// recursively parse sub options
if (ret.contains) {
parseReturnRecursive(ret.contains, depth + 1);
parseReturnRecursive(ret.contains);
}
}
};
Expand Down Expand Up @@ -534,7 +524,6 @@ export class RenderPluginDoc extends Component<IProps, IState> {
const paramEntries = this.renderParameterEntries(
parameters,
content_type,
1,
'params',
);

Expand All @@ -549,7 +538,6 @@ export class RenderPluginDoc extends Component<IProps, IState> {
private renderParameterEntries(
parameters: PluginOption[],
content_type: string,
depth: number,
parent: string,
) {
let output = [];
Expand All @@ -560,74 +548,66 @@ export class RenderPluginDoc extends Component<IProps, IState> {
// TODO: add support for sub options. Example:
// https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/network/fortios/fortios_dlp_fp_doc_source.py#L93
output.push(
<Nesting level={depth} key={key}>
<DescriptionListHorizontal
items={[
[
depth > 1 ? t`Key` : t`Parameter`,
<>
<span className='hub-doc-option-name'>{option.name}</span>
<small>
{this.documentedType(option['type'])}
{option['elements'] ? (
<span>
{' '}
/ {t`elements`}=
{this.documentedType(option['elements'])}
</span>
) : null}
{option['required'] ? (
<span>
{' '}
/ <span className='hub-doc-red'>{t`required`}</span>
</span>
) : null}
</small>
</>,
],
[t`Choices`, this.renderChoices(option)],
[t`Default`, this.renderDefault(option)],
[
t`Configuration`,
content_type !== 'module'
? this.renderPluginConfiguration(option)
: null,
],
[
t`Comments`,
option.description.map((d, i) => (
<p key={i}>{this.applyDocFormatters(d)}</p>
)),
],
[
t`Aliases`,
option['aliases'] ? (
<small>
<span className='hub-doc-green'>
{option['aliases'].join(', ')}
</span>
</small>
) : null,
],
]}
/>
</Nesting>,
<DescriptionListHorizontal
items={[
[
<tt className='hub-doc-option-name'>{option.name}</tt>,
<small>
{this.documentedType(option['type'])}
{option['elements'] ? (
<span>
{' '}
/ {t`elements`}={this.documentedType(option['elements'])}
</span>
) : null}
{option['required'] ? (
<span>
{' '}
/ <span className='hub-doc-red'>{t`required`}</span>
</span>
) : null}
</small>,
],
[t`Choices`, this.renderChoices(option)],
[t`Default`, this.renderDefault(option)],
[
t`Configuration`,
content_type !== 'module'
? this.renderPluginConfiguration(option)
: null,
],
[
t`Comments`,
option.description.map((d, i) => (
<p key={i}>{this.applyDocFormatters(d)}</p>
)),
],
[
t`Aliases`,
option['aliases'] ? (
<small>
<span className='hub-doc-green'>
{option['aliases'].join(', ')}
</span>
</small>
) : null,
],
[t`Fields`, option.suboptions?.length ? <br /> : null],
[
false, // grid-column-start: 1
option.suboptions?.length ? (
<Nesting key={key}>
{this.renderParameterEntries(
option.suboptions,
content_type,
key,
)}
</Nesting>
) : null,
],
]}
/>,
);

// recursively render sub options
if (option.suboptions) {
output.push(<br />);
output = output.concat(
this.renderParameterEntries(
option.suboptions,
content_type,
depth + 1,
key,
),
);
}

output.push(<br />);
});

return output;
Expand Down Expand Up @@ -816,14 +796,13 @@ export class RenderPluginDoc extends Component<IProps, IState> {
return (
<>
<h2 id='return-values'>{t`Return Values`}</h2>
{this.renderReturnValueEntries(returnV, 1, 'return')}
{this.renderReturnValueEntries(returnV, 'return')}
</>
);
}

private renderReturnValueEntries(
returnValues: ReturnedValue[],
depth: number,
parent: string,
) {
let entries = [];
Expand All @@ -832,48 +811,40 @@ export class RenderPluginDoc extends Component<IProps, IState> {
const key = `${parent}-${option.name}`;

entries.push(
<Nesting level={depth} key={key}>
<DescriptionListHorizontal
items={[
[
t`Key`,
<>
<span className='hub-doc-option-name'>{option.name}</span>
<small>{option.type}</small>
</>,
],
[t`Returned`, option.returned],
[
t`Description`,
option.description.map((d, i) => (
<p key={i}>{this.applyDocFormatters(d)}</p>
)),
],
[
t`Sample`,
option.sample ? (
typeof option.sample === 'string' ? (
option.sample
) : (
<pre>{JSON.stringify(option.sample, null, 2)}</pre>
)
) : null,
],
]}
/>
</Nesting>,
<DescriptionListHorizontal
items={[
[
<tt className='hub-doc-option-name'>{option.name}</tt>,
<small>{option.type}</small>,
],
[t`Returned`, option.returned],
[
t`Description`,
option.description.map((d, i) => (
<p key={i}>{this.applyDocFormatters(d)}</p>
)),
],
[
t`Sample`,
option.sample ? (
typeof option.sample === 'string' ? (
option.sample
) : (
<pre>{JSON.stringify(option.sample, null, 2)}</pre>
)
) : null,
],
[
t`Fields`,
option.contains?.length ? (
<Nesting key={key}>
{this.renderReturnValueEntries(option.contains, key)}
</Nesting>
) : null,
],
]}
/>,
);

if (option.contains) {
entries.push(<br />);

// recursively render values
entries = entries.concat(
this.renderReturnValueEntries(option.contains, depth + 1, key),
);
}

entries.push(<br />);
});

return entries;
Expand Down

0 comments on commit 2004db8

Please sign in to comment.