-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ansible-doc text output handling. * Add updated test vectors from Python project. * Add changelog. * Fix formatting.
- Loading branch information
1 parent
c86b8ed
commit 64cae07
Showing
7 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- "Add support for ansible-doc like text output (https://github.com/ansible-community/antsibull-docs-ts/pull/36)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) | ||
SPDX-FileCopyrightText: Ansible Project | ||
SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
import { toAnsibleDocText } from './ansible-doc-text'; | ||
import { PartType } from './dom'; | ||
|
||
describe('toRST tests', (): void => { | ||
it('no paragraphs', (): void => { | ||
expect(toAnsibleDocText([])).toBe(''); | ||
}); | ||
it('single paragraph with simple text', (): void => { | ||
expect(toAnsibleDocText([[{ type: PartType.TEXT, text: 'test' }]])).toBe('test'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) | ||
SPDX-FileCopyrightText: Ansible Project | ||
SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
import { AnsibleDocTextOptions, AllFormatOptions, mergeOpts, LinkProviders } from './opts'; | ||
import { OptionNamePart, Paragraph, ReturnValuePart } from './dom'; | ||
import { addToDestination } from './format'; | ||
|
||
function formatOptionLike(part: OptionNamePart | ReturnValuePart): string { | ||
let text = part.value === undefined ? `\`${part.name}'` : `\`${part.name}=${part.value}'`; | ||
if (part.plugin) { | ||
const plugin_suffix = ['role', 'module', 'playbook'].includes(part.plugin.type) ? '' : ' plugin'; | ||
let plugin = `${part.plugin.type}${plugin_suffix} ${part.plugin.fqcn}`; | ||
if (part.plugin.type === 'role' && part.entrypoint !== undefined) { | ||
plugin = `${plugin}, ${part.entrypoint} entrypoint`; | ||
} | ||
text = `${text} (of ${plugin})`; | ||
} | ||
return text; | ||
} | ||
|
||
const DEFAULT_FORMATTER: AllFormatOptions = { | ||
formatError: (part) => `[[ERROR while parsing: ${part.message}]]`, | ||
formatBold: (part) => `*${part.text}*`, | ||
formatCode: (part) => `\`${part.text}'`, | ||
formatHorizontalLine: () => '\n-------------\n', | ||
formatItalic: (part) => `\`${part.text}'`, | ||
formatLink: (part) => `${part.text} <${part.url}>`, | ||
formatModule: (part) => `[${part.fqcn}]`, | ||
formatRSTRef: (part) => part.text, | ||
formatURL: (part) => part.url, | ||
formatText: (part) => part.text, | ||
formatEnvVariable: (part) => `\`${part.name}'`, | ||
formatOptionName: (part) => formatOptionLike(part), | ||
formatOptionValue: (part) => `\`${part.value}'`, | ||
formatPlugin: (part) => `[${part.plugin.fqcn}]`, | ||
formatReturnValue: (part) => formatOptionLike(part), | ||
}; | ||
|
||
export function toAnsibleDocText(paragraphs: Paragraph[], opts?: AnsibleDocTextOptions): string { | ||
const mergedOpts = mergeOpts(Object.assign({} as LinkProviders, opts), DEFAULT_FORMATTER); | ||
const result: string[] = []; | ||
for (const paragraph of paragraphs) { | ||
const line: string[] = []; | ||
addToDestination(line, paragraph, mergedOpts); | ||
result.push(line.join('')); | ||
} | ||
return result.join('\n\n'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.