Skip to content

Commit b4a8948

Browse files
committed
[Docs] Dynamic example generation
1 parent b092184 commit b4a8948

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+19785
-16703
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ __pycache__
2121
.python-version
2222
.venv/
2323
_version.py
24+
docs/examples/*

docs/.vuepress/config.js

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const dirTree = require('directory-tree');
2+
const path = require('path');
3+
4+
const examples = dirTree(path.join(__dirname, '../examples'), {extensions:/\.md/});
5+
16
module.exports = {
27
title: 'pywebview',
38
description: 'Build GUI for your Python program with JavaScript, HTML, and CSS',
@@ -10,6 +15,7 @@ module.exports = {
1015
}
1116
}
1217
},
18+
port: 8082,
1319
themeConfig: {
1420
repo: 'r0x0r/pywebview',
1521
docsDir: 'docs',
@@ -51,46 +57,9 @@ module.exports = {
5157
]
5258
}
5359
],
54-
'/examples/': [
55-
'cef',
56-
'change_url',
57-
'close_confirm',
58-
'confirmation_dialog',
59-
'cookies',
60-
'css_load',
61-
'close_confirm',
62-
'debug',
63-
'destroy_window',
64-
'events',
65-
'expose',
66-
'focus',
67-
'frameless',
68-
'fullscreen',
69-
'get_elements',
70-
'get_current_url',
71-
'hide_window',
72-
'html_load',
73-
'js_evaluate',
74-
'js_evaluate_async',
75-
'js_api',
76-
'loading_animation',
77-
'links',
78-
'localization',
79-
'menu',
80-
'min_size',
81-
'minimize_window',
82-
'move_window',
83-
'multiple_windows',
84-
'open_file_dialog',
85-
'open_url',
86-
'resize_window',
87-
'save_file_dialog',
88-
'screens',
89-
'toggle_fullscreen',
90-
'user_agent',
91-
'vibrancy',
92-
'window_title_change'
93-
],
60+
'/examples/': examples.children
61+
.filter(file => path.parse(file.name).name !== 'README')
62+
.map(file => path.parse(file.name).name),
9463

9564
'/contributing/': [
9665
'development',

docs/generate-examples.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const sourceDirectory = '../examples';
5+
const outputDirectory = './examples';
6+
7+
if (!fs.existsSync(outputDirectory)) {
8+
fs.mkdirSync(outputDirectory);
9+
}
10+
11+
function extractDescriptionFromPythonCode(code) {
12+
const match = code.match(/(['"]{3}[\s\S]*?['"]{3})/);
13+
14+
if (match) {
15+
return match[0].replace(/['"]{3}/g, '').trim();
16+
} else {
17+
return '';
18+
}
19+
}
20+
21+
function removeExtraLineBreaksFromCodeBlock(code) {
22+
return code.replace(/(\r?\n){3,}/g, '\n\n').trim();
23+
}
24+
25+
26+
function removeCommentsFromPythonCode(code) {
27+
return code.replace(/(^['"]{3}[\s\S]*?['"]{3})/gm, '').trim();
28+
}
29+
30+
31+
function convertToMarkdown(filePath) {
32+
try {
33+
const pythonCode = fs.readFileSync(filePath, 'utf8');
34+
const description = extractDescriptionFromPythonCode(pythonCode);
35+
const codeWithoutComments = removeCommentsFromPythonCode(pythonCode);
36+
const codeWithoutExtraLineBreaks = removeExtraLineBreaksFromCodeBlock(codeWithoutComments);
37+
const fileName = path.basename(filePath, '.py');
38+
const markdownFileName = fileName + '.md';
39+
const markdownFilePath = path.join(outputDirectory, markdownFileName);
40+
const title = fileName.replace(/_/g, ' ').replace(/\b\w/g, (char) => char.toUpperCase());
41+
const markdownContent = `# ${title}\n\n${description}\n\n\`\`\`python\n${codeWithoutExtraLineBreaks}\n\`\`\``;
42+
fs.writeFileSync(markdownFilePath, markdownContent);
43+
console.log(`Converted ${filePath} to ${markdownFilePath}`);
44+
} catch (error) {
45+
console.error(`Error converting ${filePath}: ${error.message}`);
46+
}
47+
}
48+
49+
function scanDirectory(directoryPath) {
50+
fs.readdir(directoryPath, (err, files) => {
51+
if (err) {
52+
console.error(`Error reading directory: ${directoryPath}`);
53+
return;
54+
}
55+
56+
files.forEach((file) => {
57+
const filePath = path.join(directoryPath, file);
58+
59+
if (file.endsWith('.py')) {
60+
convertToMarkdown(filePath);
61+
}
62+
});
63+
});
64+
}
65+
66+
scanDirectory(sourceDirectory);

0 commit comments

Comments
 (0)