@apify/docusaurus-plugin-typedoc-api 4.2.10 → 4.2.11-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/lib/components/ApiItem.js +97 -2
  2. package/lib/components/ApiItem.js.map +1 -1
  3. package/lib/components/ApiPage.js +0 -3
  4. package/lib/components/ApiPage.js.map +1 -1
  5. package/lib/components/Markdown.js +0 -2
  6. package/lib/components/Markdown.js.map +1 -1
  7. package/lib/components/MemberGetterSetter.js +0 -1
  8. package/lib/components/MemberGetterSetter.js.map +1 -1
  9. package/lib/components/MemberSignatureBody.js +2 -7
  10. package/lib/components/MemberSignatureBody.js.map +1 -1
  11. package/lib/components/Reflection.js +0 -1
  12. package/lib/components/Reflection.js.map +1 -1
  13. package/lib/components/SourceLink.js +5 -1
  14. package/lib/components/SourceLink.js.map +1 -1
  15. package/lib/components/Type.js +2 -3
  16. package/lib/components/Type.js.map +1 -1
  17. package/lib/index.js +23 -10
  18. package/lib/index.js.map +1 -1
  19. package/lib/plugin/data.js +1 -9
  20. package/lib/plugin/data.js.map +1 -1
  21. package/lib/plugin/python/consts.js +47 -0
  22. package/lib/plugin/python/consts.js.map +1 -0
  23. package/lib/plugin/python/index.js +36 -0
  24. package/lib/plugin/python/index.js.map +1 -0
  25. package/lib/plugin/python/inheritance.js +71 -0
  26. package/lib/plugin/python/inheritance.js.map +1 -0
  27. package/lib/plugin/python/packageVersions.js +46 -0
  28. package/lib/plugin/python/packageVersions.js.map +1 -0
  29. package/lib/plugin/python/transformation.js +359 -0
  30. package/lib/plugin/python/transformation.js.map +1 -0
  31. package/lib/plugin/python/type-parsing/index.js +79 -0
  32. package/lib/plugin/python/type-parsing/index.js.map +1 -0
  33. package/lib/plugin/python/types.js +2 -0
  34. package/lib/plugin/python/types.js.map +1 -0
  35. package/lib/plugin/python/utils.js +106 -0
  36. package/lib/plugin/python/utils.js.map +1 -0
  37. package/lib/plugin/structure/0.23.js +0 -2
  38. package/lib/plugin/structure/0.23.js.map +1 -1
  39. package/lib/utils/icons.js +1 -2
  40. package/lib/utils/icons.js.map +1 -1
  41. package/lib/utils/reexports.js +96 -0
  42. package/lib/utils/reexports.js.map +1 -0
  43. package/package.json +5 -3
  44. package/src/components/ApiItem.tsx +103 -9
  45. package/src/components/ApiItemLayout.tsx +4 -2
  46. package/src/components/ApiOptionsLayout.tsx +18 -16
  47. package/src/components/ApiPage.tsx +0 -2
  48. package/src/components/DefaultValue.tsx +0 -1
  49. package/src/components/Flags.tsx +1 -1
  50. package/src/components/Markdown.tsx +0 -1
  51. package/src/components/Member.tsx +19 -17
  52. package/src/components/MemberGetterSetter.tsx +0 -1
  53. package/src/components/MemberSignatureBody.tsx +42 -38
  54. package/src/components/MemberSignatureTitle.tsx +18 -15
  55. package/src/components/Reflection.tsx +1 -1
  56. package/src/components/SourceLink.tsx +6 -8
  57. package/src/components/Type.tsx +6 -14
  58. package/src/components/VersionBanner.tsx +5 -1
  59. package/src/index.ts +39 -9
  60. package/src/plugin/data.ts +6 -12
  61. package/src/plugin/python/consts.ts +50 -0
  62. package/src/plugin/python/docspec-gen/__init__.py +0 -0
  63. package/src/plugin/python/docspec-gen/generate_ast.py +73 -0
  64. package/src/plugin/python/docspec-gen/google_docstring_processor.py +185 -0
  65. package/src/plugin/python/index.ts +47 -0
  66. package/src/plugin/python/inheritance.ts +80 -0
  67. package/src/plugin/python/packageVersions.ts +43 -0
  68. package/src/plugin/python/transformation.ts +444 -0
  69. package/src/plugin/python/type-parsing/index.ts +88 -0
  70. package/src/plugin/python/type-parsing/parse_types.py +82 -0
  71. package/src/plugin/python/types.ts +83 -0
  72. package/src/plugin/python/utils.ts +123 -0
  73. package/src/plugin/structure/0.23.ts +0 -2
  74. package/src/plugin/version.ts +2 -2
  75. package/src/types.ts +9 -0
  76. package/src/utils/icons.ts +4 -3
  77. package/src/utils/reexports.ts +105 -0
@@ -1,4 +1,5 @@
1
1
  import type { JSONOutput } from 'typedoc';
2
+ import type { DocusaurusConfig } from '@docusaurus/types';
2
3
  import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
3
4
  import { useGitRefName } from '../hooks/useGitRefName';
4
5
 
@@ -11,6 +12,10 @@ export interface SourceLinkProps {
11
12
  sources?: JSONOutput.SourceReference[];
12
13
  }
13
14
 
15
+ export function resolveGithubUrl(source: JSONOutput.SourceReference, siteConfig: DocusaurusConfig, gitRefName: string): string {
16
+ return source.url || `https://${siteConfig.githubHost}${siteConfig.githubPort ? `:${siteConfig.githubPort}` : ''}/${siteConfig.organizationName}/${siteConfig.projectName}/blob/${gitRefName}/${replaceWithSrc(source.fileName)}#L${source.line}`;
17
+ }
18
+
14
19
  export function SourceLink({ sources = [] }: SourceLinkProps) {
15
20
  const { siteConfig } = useDocusaurusContext();
16
21
  const gitRefName = useGitRefName();
@@ -25,14 +30,7 @@ export function SourceLink({ sources = [] }: SourceLinkProps) {
25
30
  <a
26
31
  key={source.fileName}
27
32
  className="tsd-anchor"
28
- href={
29
- source.url ||
30
- `https://${siteConfig.githubHost}${
31
- siteConfig.githubPort ? `:${siteConfig.githubPort}` : ''
32
- }/${siteConfig.organizationName}/${
33
- siteConfig.projectName
34
- }/blob/${gitRefName}/${replaceWithSrc(source.fileName)}#L${source.line}`
35
- }
33
+ href={resolveGithubUrl(source, siteConfig, gitRefName)}
36
34
  rel="noreferrer"
37
35
  target="_blank"
38
36
  >
@@ -39,7 +39,6 @@ export interface TypeProps {
39
39
  type?: { type: string; value?: unknown };
40
40
  }
41
41
 
42
- // eslint-disable-next-line complexity
43
42
  export function Type({ needsParens = false, type: base }: TypeProps) {
44
43
  const reflections = useReflectionMap();
45
44
  const { isPython } = usePluginData('docusaurus-plugin-typedoc-api') as GlobalData;
@@ -135,7 +134,8 @@ export function Type({ needsParens = false, type: base }: TypeProps) {
135
134
  case 'literal': {
136
135
  const type = base as JSONOutput.LiteralType;
137
136
 
138
- if (isPython && type.value === null) {
137
+ if (isPython && (type.value === null
138
+ || (typeof type.value === 'object' && Object.keys(type.value).length === 0))) {
139
139
  return <span className="tsd-signature-type">None</span>;
140
140
  }
141
141
 
@@ -217,10 +217,10 @@ export function Type({ needsParens = false, type: base }: TypeProps) {
217
217
  }
218
218
 
219
219
  case 'reference': {
220
- const type = base as JSONOutput.ReferenceType;
220
+ const type = base as JSONOutput.ReferenceType & { ref?: TSDDeclarationReflection };
221
221
  // eslint-disable-next-line
222
222
  const reflectionIdentifier = type.target ?? (type as any).id;
223
- const ref = reflectionIdentifier ? reflections[Number(reflectionIdentifier)] : null;
223
+ const ref = type.ref ?? (reflectionIdentifier ? reflections[Number(reflectionIdentifier)] : null);
224
224
  const genericClass = ref?.id && !ref.sources ? 'tsd-signature-type-generic' : '';
225
225
 
226
226
  return (
@@ -238,22 +238,14 @@ export function Type({ needsParens = false, type: base }: TypeProps) {
238
238
  )}
239
239
  {type.typeArguments && type.typeArguments.length > 0 && (
240
240
  <>
241
- <span className="tsd-signature-symbol">
242
- {
243
- isPython ? '[' : '<'
244
- }
245
- </span>
241
+ <span className="tsd-signature-symbol">{isPython ? '[' : '<'}</span>
246
242
  {type.typeArguments.map((t, i) => (
247
243
  <Fragment key={t.type + i}>
248
244
  {i > 0 && <span className="tsd-signature-symbol">, </span>}
249
245
  <Type type={t} />
250
246
  </Fragment>
251
247
  ))}
252
- <span className="tsd-signature-symbol">
253
- {
254
- isPython ? ']' : '>'
255
- }
256
- </span>
248
+ <span className="tsd-signature-symbol">{isPython ? ']' : '>'}</span>
257
249
  </>
258
250
  )}
259
251
  </>
@@ -1,6 +1,10 @@
1
1
  import { useCallback } from 'react';
2
2
  import Link from '@docusaurus/Link';
3
- import { useDocsPreferredVersion, useDocsVersion, useDocVersionSuggestions } from '@docusaurus/plugin-content-docs/client';
3
+ import {
4
+ useDocsPreferredVersion,
5
+ useDocsVersion,
6
+ useDocVersionSuggestions,
7
+ } from '@docusaurus/plugin-content-docs/client';
4
8
  import { ThemeClassNames } from '@docusaurus/theme-common';
5
9
 
6
10
  export function VersionBanner(): JSX.Element | null {
package/src/index.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  generateJson,
14
14
  loadPackageJsonAndDocs,
15
15
  } from './plugin/data';
16
+ import { processPythonDocs } from './plugin/python';
16
17
  import { extractSidebar } from './plugin/sidebar';
17
18
  import { getVersionedDocsDirPath, readVersionsMetadata } from './plugin/version';
18
19
  import type {
@@ -26,6 +27,7 @@ import type {
26
27
  TSDDeclarationReflection,
27
28
  VersionMetadata,
28
29
  } from './types';
30
+ import { injectReexports } from './utils/reexports';
29
31
 
30
32
  const DEFAULT_OPTIONS: Required<DocusaurusPluginTypeDocApiOptions> = {
31
33
  banner: '',
@@ -57,6 +59,8 @@ const DEFAULT_OPTIONS: Required<DocusaurusPluginTypeDocApiOptions> = {
57
59
  rehypePlugins: [],
58
60
  versions: {},
59
61
  python: false,
62
+ pythonOptions: {},
63
+ reexports: [],
60
64
  };
61
65
 
62
66
  async function importFile<T>(file: string): Promise<T> {
@@ -167,9 +171,8 @@ export default function typedocApiPlugin(
167
171
  options.changelogName,
168
172
  );
169
173
 
170
- // eslint-disable-next-line no-param-reassign
171
174
  cfg.packageName = packageJson.name;
172
- // eslint-disable-next-line no-param-reassign
175
+
173
176
  cfg.packageVersion = packageJson.version;
174
177
  });
175
178
 
@@ -195,15 +198,33 @@ export default function typedocApiPlugin(
195
198
  if (metadata.versionName === CURRENT_VERSION_NAME) {
196
199
  const outFile = path.join(context.generatedFilesDir, `api-typedoc-${pluginId}.json`);
197
200
 
201
+ if (!fs.existsSync(context.generatedFilesDir)) {
202
+ fs.mkdirSync(context.generatedFilesDir, { recursive: true });
203
+ }
204
+
198
205
  if (options.pathToCurrentVersionTypedocJSON) {
199
- if (!fs.existsSync(context.generatedFilesDir)){
200
- fs.mkdirSync(context.generatedFilesDir, { recursive: true });
201
- }
202
206
  fs.copyFileSync(options.pathToCurrentVersionTypedocJSON, outFile);
207
+ } else if (Object.keys(options.pythonOptions).length > 0) {
208
+ if (
209
+ !options.pythonOptions.pythonModulePath ||
210
+ !options.pythonOptions.moduleShortcutsPath
211
+ ) {
212
+ throw new Error('Python options are missing required fields');
213
+ }
214
+
215
+ processPythonDocs({
216
+ pythonModulePath: options.pythonOptions.pythonModulePath,
217
+ moduleShortcutsPath: options.pythonOptions.moduleShortcutsPath,
218
+ outPath: outFile,
219
+ });
203
220
  } else {
204
221
  await generateJson(projectRoot, entryPoints, outFile, options);
205
222
  }
206
223
 
224
+ if (options.reexports && options.reexports.length > 0) {
225
+ await injectReexports(outFile, options.reexports);
226
+ }
227
+
207
228
  packages = flattenAndGroupPackages(
208
229
  packageConfigs,
209
230
  await importFile(outFile),
@@ -266,7 +287,7 @@ export default function typedocApiPlugin(
266
287
  }
267
288
 
268
289
  actions.setGlobalData({
269
- isPython: !!options.python,
290
+ isPython: !!(options.python || options.pythonOptions),
270
291
  } as GlobalData);
271
292
 
272
293
  const docs: PropVersionDocs = {};
@@ -330,7 +351,10 @@ export default function typedocApiPlugin(
330
351
  return {
331
352
  path: info.permalink,
332
353
  exact: true,
333
- component: path.join(__dirname, `./components/ApiItem.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`),
354
+ component: path.join(
355
+ __dirname,
356
+ `./components/ApiItem.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`,
357
+ ),
334
358
  modules,
335
359
  sidebar: 'api',
336
360
  // Map the ID here instead of creating a JSON data file,
@@ -399,7 +423,10 @@ export default function typedocApiPlugin(
399
423
  {
400
424
  path: indexPermalink,
401
425
  exact: false,
402
- component: path.join(__dirname, `./components/ApiPage.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`),
426
+ component: path.join(
427
+ __dirname,
428
+ `./components/ApiPage.${process.env.TYPEDOC_PLUGIN_DEV ? 'tsx' : 'js'}`,
429
+ ),
403
430
  routes,
404
431
  modules: {
405
432
  options: optionsData,
@@ -463,7 +490,10 @@ export default function typedocApiPlugin(
463
490
  remarkPlugins: options.remarkPlugins,
464
491
  rehypePlugins: options.rehypePlugins,
465
492
  siteDir: context.siteDir,
466
- staticDirs: [...context.siteConfig.staticDirectories, path.join(context.siteDir, 'static')],
493
+ staticDirs: [
494
+ ...context.siteConfig.staticDirectories,
495
+ path.join(context.siteDir, 'static'),
496
+ ],
467
497
  // Since this isn't a doc/blog page, we can get
468
498
  // away with it being a partial!
469
499
  isMDXPartial: () => true,
@@ -1,7 +1,7 @@
1
1
  import fs from 'fs';
2
2
  import path from 'path';
3
3
  import * as TypeDoc from 'typedoc';
4
- import { type InlineTagDisplayPart, type JSONOutput, ReflectionKind } from 'typedoc'
4
+ import { type InlineTagDisplayPart, type JSONOutput, ReflectionKind } from 'typedoc';
5
5
  import ts from 'typescript';
6
6
  import { normalizeUrl } from '@docusaurus/utils';
7
7
  import type {
@@ -110,7 +110,6 @@ export function createReflectionMap(
110
110
  ): TSDDeclarationReflectionMap {
111
111
  const map: TSDDeclarationReflectionMap = {};
112
112
 
113
- // eslint-disable-next-line complexity
114
113
  items.forEach((item) => {
115
114
  // Add @reference categories to reflection.
116
115
  const referenceCategories: Record<string, { title: string; children: number[] }> = {};
@@ -133,7 +132,6 @@ export function createReflectionMap(
133
132
 
134
133
  // Update categories with reference categories.
135
134
  if (!item.categories) {
136
- // eslint-disable-next-line no-param-reassign
137
135
  item.categories = [];
138
136
  }
139
137
  for (const category of Object.values(referenceCategories)) {
@@ -170,7 +168,7 @@ export function loadPackageJsonAndDocs(
170
168
  }
171
169
  }
172
170
 
173
- if(!found) {
171
+ if (!found) {
174
172
  // TODO: load the actual package information from pyproject.toml or similar
175
173
  return {
176
174
  packageJson: {
@@ -179,7 +177,7 @@ export function loadPackageJsonAndDocs(
179
177
  },
180
178
  readmePath: '',
181
179
  changelogPath: '',
182
- }
180
+ };
183
181
  }
184
182
 
185
183
  const readmePath = path.join(currentDir, readmeFileName);
@@ -203,7 +201,6 @@ export function addMetadataToReflections(
203
201
  const permalink = `/${joinUrl(urlPrefix, packageSlug)}`;
204
202
 
205
203
  if (project.children) {
206
- // eslint-disable-next-line no-param-reassign
207
204
  project.children = project.children.map((child) => {
208
205
  migrateToVersion0230(child);
209
206
 
@@ -213,7 +210,6 @@ export function addMetadataToReflections(
213
210
 
214
211
  // We need to go another level deeper and only use fragments
215
212
  if (child.kind === ReflectionKind.Namespace && child.children) {
216
- // eslint-disable-next-line no-param-reassign
217
213
  child.children = child.children.map((grandChild) => ({
218
214
  ...grandChild,
219
215
  permalink: normalizeUrl([`${childPermalink}#${grandChild.name}`]),
@@ -251,7 +247,7 @@ function mergeReflections(base: TSDDeclarationReflection, next: TSDDeclarationRe
251
247
  });
252
248
 
253
249
  // We can remove refs since were merging all reflections into one
254
- // eslint-disable-next-line no-param-reassign
250
+
255
251
  base.groups = base.groups.filter((group) => group.title !== 'References');
256
252
  }
257
253
  }
@@ -390,14 +386,13 @@ function buildSourceFileNameMap(
390
386
  const map: Record<string, boolean> = {};
391
387
  const cwd = process.cwd();
392
388
 
393
- if(project.symbolIdMap) {
389
+ if (project.symbolIdMap) {
394
390
  Object.values(project.symbolIdMap).forEach((symbol) => {
395
391
  // absolute
396
392
  map[path.normalize(path.join(cwd, symbol.sourceFileName))] = true;
397
393
  });
398
394
  }
399
395
 
400
-
401
396
  modChildren.forEach((child) => {
402
397
  child.sources?.forEach((sf) => {
403
398
  // relative
@@ -458,9 +453,8 @@ export function flattenAndGroupPackages(
458
453
  changelogPath,
459
454
  };
460
455
 
461
- // eslint-disable-next-line no-param-reassign
462
456
  cfg.packageName = packages[cfg.packagePath].packageName;
463
- // eslint-disable-next-line no-param-reassign
457
+
464
458
  cfg.packageVersion = packages[cfg.packagePath].packageVersion;
465
459
  }
466
460
 
@@ -0,0 +1,50 @@
1
+ export const REPO_ROOT_PLACEHOLDER = 'REPO_ROOT_PLACEHOLDER';
2
+
3
+ export const APIFY_CLIENT_REPO_URL = 'https://github.com/apify/apify-client-python';
4
+ export const APIFY_SDK_REPO_URL = 'https://github.com/apify/apify-sdk-python';
5
+ export const APIFY_SHARED_REPO_URL = 'https://github.com/apify/apify-shared-python';
6
+ export const CRAWLEE_PYTHON_REPO_URL = 'https://github.com/apify/crawlee-python';
7
+
8
+ export const REPO_URL_PER_PACKAGE = {
9
+ apify: APIFY_SDK_REPO_URL,
10
+ apify_client: APIFY_CLIENT_REPO_URL,
11
+ apify_shared: APIFY_SHARED_REPO_URL,
12
+ crawlee: CRAWLEE_PYTHON_REPO_URL,
13
+ };
14
+
15
+ // Taken from https://github.com/TypeStrong/typedoc/blob/v0.23.24/src/lib/models/reflections/kind.ts, modified
16
+ export const TYPEDOC_KINDS = {
17
+ class: {
18
+ kind: 128,
19
+ kindString: 'Class',
20
+ },
21
+ data: {
22
+ kind: 1024,
23
+ kindString: 'Property',
24
+ },
25
+ enum: {
26
+ kind: 8,
27
+ kindString: 'Enumeration',
28
+ },
29
+ enumValue: {
30
+ kind: 16,
31
+ kindString: 'Enumeration Member',
32
+ },
33
+ function: {
34
+ kind: 2048,
35
+ kindString: 'Method',
36
+ },
37
+ };
38
+
39
+ export const GROUP_ORDER = [
40
+ 'Classes',
41
+ 'Abstract classes',
42
+ 'Data structures',
43
+ 'Errors',
44
+ 'Functions',
45
+ 'Constructors',
46
+ 'Methods',
47
+ 'Properties',
48
+ 'Constants',
49
+ 'Enumeration Members',
50
+ ];
File without changes
@@ -0,0 +1,73 @@
1
+ """
2
+ Replaces the default pydoc-markdown shell script with a custom Python script calling the pydoc-markdown API directly.
3
+
4
+ This script generates an AST from the Python source code in the `src` directory and prints it as a JSON object.
5
+ """
6
+
7
+ from pydoc_markdown.interfaces import Context
8
+ from pydoc_markdown.contrib.loaders.python import PythonLoader
9
+ from pydoc_markdown.contrib.processors.filter import FilterProcessor
10
+ from pydoc_markdown.contrib.processors.crossref import CrossrefProcessor
11
+ from google_docstring_processor import ApifyGoogleProcessor
12
+ from docspec import dump_module
13
+
14
+ import argparse
15
+
16
+ import json
17
+ import os
18
+
19
+ def search_for_git_root(path):
20
+ if os.path.exists(os.path.join(path, '.git')):
21
+ return path
22
+ else:
23
+ parent = os.path.dirname(path)
24
+ if parent == path:
25
+ return None
26
+ return search_for_git_root(parent)
27
+
28
+ def main():
29
+ parser = argparse.ArgumentParser()
30
+ parser.add_argument("-i", "--input", help = "Path to the Python module to generate the AST from.", required=True)
31
+ parser.add_argument("output", help = "Path to store the generated AST as a JSON file in.")
32
+
33
+ args = parser.parse_args()
34
+ project_path = os.path.abspath(args.input)
35
+
36
+ repo_root_path = search_for_git_root(project_path)
37
+ if not repo_root_path:
38
+ raise Exception("Could not find git root directory. Are you sure the Python module is in a git repository?")
39
+
40
+ context = Context(directory='.')
41
+ loader = PythonLoader(search_path=[project_path])
42
+ filter = FilterProcessor(
43
+ documented_only=False,
44
+ skip_empty_modules=False,
45
+ )
46
+ crossref = CrossrefProcessor()
47
+ google = ApifyGoogleProcessor()
48
+
49
+ loader.init(context)
50
+ filter.init(context)
51
+ google.init(context)
52
+ crossref.init(context)
53
+
54
+ processors = [filter, google, crossref]
55
+
56
+ dump = []
57
+
58
+ modules = list(loader.load())
59
+
60
+ for processor in processors:
61
+ processor.process(modules, None)
62
+
63
+ for module in modules:
64
+ dump.append(dump_module(module))
65
+
66
+ with open(args.output, 'w') as f:
67
+ f.write(json.dumps(dump, indent=4).replace(
68
+ repo_root_path,
69
+ 'REPO_ROOT_PLACEHOLDER'
70
+ ))
71
+
72
+ if __name__ == "__main__":
73
+ main()
@@ -0,0 +1,185 @@
1
+ # -*- coding: utf8 -*-
2
+ # Copyright (c) 2019 Niklas Rosenstein
3
+ # !!! Modified 2024 Jindřich Bär
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to
7
+ # deal in the Software without restriction, including without limitation the
8
+ # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
+ # sell copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
+ # IN THE SOFTWARE.
22
+
23
+ import dataclasses
24
+ import re
25
+ import typing as t
26
+
27
+ import docspec
28
+
29
+ from pydoc_markdown.contrib.processors.sphinx import generate_sections_markdown
30
+ from pydoc_markdown.interfaces import Processor, Resolver
31
+
32
+ import json
33
+
34
+
35
+ @dataclasses.dataclass
36
+ class ApifyGoogleProcessor(Processor):
37
+ """
38
+ This class implements the preprocessor for Google and PEP 257 docstrings. It converts
39
+ docstrings formatted in the Google docstyle to Markdown syntax.
40
+
41
+ References:
42
+
43
+ * https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
44
+ * https://www.python.org/dev/peps/pep-0257/
45
+
46
+ Example:
47
+
48
+ ```
49
+ Attributes:
50
+ module_level_variable1 (int): Module level variables may be documented in
51
+ either the ``Attributes`` section of the module docstring, or in an
52
+ inline docstring immediately following the variable.
53
+
54
+ Either form is acceptable, but the two should not be mixed. Choose
55
+ one convention to document module level variables and be consistent
56
+ with it.
57
+
58
+ Todo:
59
+ * For module TODOs
60
+ * You have to also use ``sphinx.ext.todo`` extension
61
+ ```
62
+
63
+ Renders as:
64
+
65
+ Attributes:
66
+ module_level_variable1 (int): Module level variables may be documented in
67
+ either the ``Attributes`` section of the module docstring, or in an
68
+ inline docstring immediately following the variable.
69
+
70
+ Either form is acceptable, but the two should not be mixed. Choose
71
+ one convention to document module level variables and be consistent
72
+ with it.
73
+
74
+ Todo:
75
+ * For module TODOs
76
+ * You have to also use ``sphinx.ext.todo`` extension
77
+
78
+ @doc:fmt:google
79
+ """
80
+
81
+ _param_res = [
82
+ re.compile(r"^(?P<param>\S+):\s+(?P<desc>.+)$"),
83
+ re.compile(r"^(?P<param>\S+)\s+\((?P<type>[^)]+)\):\s+(?P<desc>.+)$"),
84
+ re.compile(r"^(?P<param>\S+)\s+--\s+(?P<desc>.+)$"),
85
+ re.compile(r"^(?P<param>\S+)\s+\{\[(?P<type>\S+)\]\}\s+--\s+(?P<desc>.+)$"),
86
+ re.compile(r"^(?P<param>\S+)\s+\{(?P<type>\S+)\}\s+--\s+(?P<desc>.+)$"),
87
+ ]
88
+
89
+ _keywords_map = {
90
+ "Args:": "Arguments",
91
+ "Arguments:": "Arguments",
92
+ "Attributes:": "Attributes",
93
+ "Example:": "Example",
94
+ "Examples:": "Examples",
95
+ "Keyword Args:": "Arguments",
96
+ "Keyword Arguments:": "Arguments",
97
+ "Methods:": "Methods",
98
+ "Note:": "Notes",
99
+ "Notes:": "Notes",
100
+ "Other Parameters:": "Arguments",
101
+ "Parameters:": "Arguments",
102
+ "Return:": "Returns",
103
+ "Returns:": "Returns",
104
+ "Raises:": "Raises",
105
+ "References:": "References",
106
+ "See Also:": "See Also",
107
+ "Todo:": "Todo",
108
+ "Warning:": "Warnings",
109
+ "Warnings:": "Warnings",
110
+ "Warns:": "Warns",
111
+ "Yield:": "Yields",
112
+ "Yields:": "Yields",
113
+ }
114
+
115
+ def check_docstring_format(self, docstring: str) -> bool:
116
+ for section_name in self._keywords_map:
117
+ if section_name in docstring:
118
+ return True
119
+ return False
120
+
121
+ def process(self, modules: t.List[docspec.Module], resolver: t.Optional[Resolver]) -> None:
122
+ docspec.visit(modules, self._process)
123
+
124
+ def _process(self, node: docspec.ApiObject):
125
+ if not node.docstring:
126
+ return
127
+
128
+ lines = []
129
+ sections = []
130
+ current_lines: t.List[str] = []
131
+ in_codeblock = False
132
+ keyword = None
133
+ multiline_argument_offset = -1
134
+
135
+ def _commit():
136
+ if keyword:
137
+ sections.append({keyword: list(current_lines)})
138
+ else:
139
+ lines.extend(current_lines)
140
+ current_lines.clear()
141
+
142
+ for line in node.docstring.content.split("\n"):
143
+ multiline_argument_offset += 1
144
+ if line.lstrip().startswith("```"):
145
+ in_codeblock = not in_codeblock
146
+ current_lines.append(line)
147
+ if not in_codeblock:
148
+ _commit()
149
+ continue
150
+
151
+ if in_codeblock:
152
+ current_lines.append(line)
153
+ continue
154
+
155
+ line = line.strip()
156
+ if line in self._keywords_map:
157
+ _commit()
158
+ keyword = self._keywords_map[line]
159
+ continue
160
+
161
+ if keyword is None:
162
+ lines.append(line)
163
+ continue
164
+
165
+ for param_re in self._param_res:
166
+ param_match = param_re.match(line)
167
+ if param_match:
168
+ current_lines.append(param_match.groupdict())
169
+ multiline_argument_offset = 0
170
+ break
171
+
172
+ if not param_match:
173
+ if multiline_argument_offset == 1:
174
+ current_lines[-1]["desc"] += "\n" + line
175
+ multiline_argument_offset = 0
176
+ else:
177
+ current_lines.append(line)
178
+
179
+ _commit()
180
+ node.docstring.content = json.dumps({
181
+ "text": "\n".join(lines),
182
+ "sections": sections,
183
+ }, indent=None)
184
+
185
+
@@ -0,0 +1,47 @@
1
+ import childProcess from 'child_process';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { DocspecTransformer } from './transformation';
5
+ import type { DocspecObject } from './types';
6
+
7
+ export { groupSort } from './utils';
8
+
9
+ /**
10
+ * Processes the Python documentation generated by `pydoc-markdown` and transforms it into a format
11
+ * accepted by the TypeDoc JSON generator.
12
+ */
13
+ export function processPythonDocs({
14
+ pythonModulePath,
15
+ moduleShortcutsPath,
16
+ outPath,
17
+ }: {
18
+ pythonModulePath: string;
19
+ moduleShortcutsPath: string;
20
+ outPath: string;
21
+ }) {
22
+ const pydocMarkdownDumpPath = path.join(__dirname, './pydoc-markdown-dump.json');
23
+
24
+ childProcess.spawnSync('python', [
25
+ path.join(__dirname, './docspec-gen/generate_ast.py'),
26
+ '-i',
27
+ pythonModulePath,
28
+ pydocMarkdownDumpPath,
29
+ ]);
30
+
31
+ const moduleShortcuts = JSON.parse(fs.readFileSync(moduleShortcutsPath, 'utf8')) as Record<
32
+ string,
33
+ string
34
+ >;
35
+
36
+ const pydocMarkdownDump = JSON.parse(
37
+ fs.readFileSync(pydocMarkdownDumpPath, 'utf8'),
38
+ ) as DocspecObject[];
39
+
40
+ const docspecTransformer = new DocspecTransformer({
41
+ moduleShortcuts,
42
+ });
43
+
44
+ const typedocApiReference = docspecTransformer.transform(pydocMarkdownDump);
45
+
46
+ fs.writeFileSync(outPath, JSON.stringify(typedocApiReference, null, 4));
47
+ }