-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: add man-page
generator
#125
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -26,43 +26,56 @@ export default { | |
dependsOn: 'ast', | ||
|
||
async generate(input, options) { | ||
// Filter to only 'cli'. | ||
const components = input.filter(({ api }) => api === 'cli'); | ||
if (!components.length) { | ||
throw new Error('CLI.md not found'); | ||
} | ||
|
||
// Find the appropriate headers | ||
const optionsStart = input.findIndex(({ slug }) => slug === 'options'); | ||
const environmentStart = input.findIndex( | ||
const optionsStart = components.findIndex(({ slug }) => slug === 'options'); | ||
const environmentStart = components.findIndex( | ||
({ slug }) => slug === 'environment-variables-1' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be a constant (including the If that file ever gets updated with this content removed, we're screwed. |
||
); | ||
// The first header that is <3 in depth after environmentStart | ||
const environmentEnd = components.findIndex( | ||
({ heading }, index) => heading.depth < 3 && index > environmentStart | ||
); | ||
|
||
if (optionsStart + environmentStart <= 0) { | ||
throw new Error('Could not find headers'); | ||
} | ||
|
||
// Generate the option mandoc | ||
let optionsOutput = ''; | ||
for (let i = optionsStart + 1; i < environmentStart; i++) { | ||
const el = input[i]; | ||
if (el.heading.depth === 3) { | ||
optionsOutput += convertOptionToMandoc(el); | ||
} | ||
} | ||
|
||
// Generate the environment mandoc | ||
let envOutput = ''; | ||
for (let i = environmentStart + 1; i < input.length; i++) { | ||
const el = input[i]; | ||
if (el.heading.depth === 3) { | ||
envOutput += convertEnvVarToMandoc(el); | ||
} | ||
if (el.heading.depth < 3) break; | ||
} | ||
const output = { | ||
// Extract the CLI options. | ||
options: extractMandoc( | ||
components, | ||
optionsStart + 1, | ||
environmentStart, | ||
convertOptionToMandoc | ||
), | ||
// Extract the environment variables. | ||
env: extractMandoc( | ||
components, | ||
environmentStart + 1, | ||
environmentEnd, | ||
convertEnvVarToMandoc | ||
), | ||
}; | ||
|
||
const apiTemplate = await readFile( | ||
const template = await readFile( | ||
join(import.meta.dirname, 'template.1'), | ||
'utf-8' | ||
); | ||
const template = apiTemplate | ||
.replace('__OPTIONS__', optionsOutput) | ||
.replace('__ENVIRONMENT__', envOutput); | ||
|
||
await writeFile(options.output, template); | ||
const filledTemplate = template | ||
.replace('__OPTIONS__', output.options) | ||
.replace('__ENVIRONMENT__', output.env); | ||
|
||
await writeFile(options.output, filledTemplate); | ||
}, | ||
}; | ||
|
||
function extractMandoc(components, start, end, convert) { | ||
return components | ||
.slice(start, end) | ||
.filter(({ heading }) => heading.depth === 3) | ||
.map(convert) | ||
.join(''); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could have been a more laid-out error message