forked from milesj/docusaurus-plugin-typedoc-api
-
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.
feat: automatically generate docspec for Python projects (#10)
- Loading branch information
Showing
9 changed files
with
567 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { parseWithPydocMarkdown } from "./pydoc-markdown"; | ||
import { pydocToTypedoc } from "./transform-docs"; | ||
|
||
export async function generateJsonFromPythonProject({ | ||
outFile, | ||
projectRoot, | ||
} : { outFile: string, projectRoot: string }): Promise<void> { | ||
const pydocJson = await parseWithPydocMarkdown({ projectRoot }); | ||
|
||
await pydocToTypedoc({ | ||
moduleName: 'python', // TODO: get from project config files or passed options | ||
outFile, | ||
pydocJson, | ||
}); | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/plugin/src/plugin/python-generator/pydoc-markdown.ts
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,65 @@ | ||
import { rmSync, writeFileSync } from 'fs'; | ||
import path from 'path'; | ||
import { $ } from 'zx'; | ||
|
||
/** | ||
* Generates the pydoc-markdown configuration file | ||
* @returns The pydoc-markdown configuration file as a string | ||
*/ | ||
function getConfigYml({ | ||
projectRoot | ||
}: { projectRoot: string }): string { | ||
return ` | ||
loaders: | ||
- type: python | ||
search_path: ["${projectRoot}"] | ||
processors: | ||
- type: filter | ||
skip_empty_modules: true | ||
- type: crossref | ||
renderer: | ||
type: docusaurus | ||
docs_base_path: docs | ||
relative_output_path: reference | ||
relative_sidebar_path: sidebar.json | ||
sidebar_top_level_label: null | ||
` | ||
} | ||
|
||
export async function parseWithPydocMarkdown({ | ||
projectRoot, | ||
}: { | ||
projectRoot: string, | ||
} | ||
): Promise<string> { | ||
// Check whether the user has Python and pydoc-markdown installed | ||
for (const cmd of ['python', 'pydoc-markdown']) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
await $`${cmd} --version`; | ||
} catch { | ||
throw new Error(`Please install ${cmd} to use this plugin with Python projects.`); | ||
} | ||
}; | ||
|
||
// Generate the JSON file | ||
try { | ||
const configYml = getConfigYml({ projectRoot }); | ||
const configPath = path.join(__dirname, 'pydoc-markdown.temp.yml'); | ||
writeFileSync(configPath, configYml); | ||
|
||
const pydoc = await $`pydoc-markdown --quiet --dump ${configPath}`; | ||
|
||
rmSync(configPath); | ||
|
||
let json = await pydoc.text(); | ||
|
||
json = json.replaceAll(path.resolve(projectRoot), 'REPO_ROOT_PLACEHOLDER'); | ||
|
||
return json; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (error: any) { | ||
// eslint-disable-next-line | ||
throw new Error(`Failed to generate JSON file from Python project:\n\t${error.stderr.split('\n').slice(-2).join('\n')}`); | ||
} | ||
} |
Oops, something went wrong.