Skip to content

Commit b8a8bcb

Browse files
fix(python): add docs + newline fix (#108)
* fix(python): add docs + newline fix * fix: allow space after : * revert: do not use single-line and multi-line * fix: catche spaces only
1 parent e860487 commit b8a8bcb

File tree

3 files changed

+99
-13
lines changed

3 files changed

+99
-13
lines changed

docs/python.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Python
22

3-
## Browser
4-
5-
Testing a function:
3+
## `getDef`
64

75
```javascript,mdbook-runnable,hidelines=#
86
# {{#rustdoc_include tools/helpers.js:1}}
@@ -11,9 +9,9 @@ a = 1
119
b = 2
1210
1311
def add(x, y):
14-
result = x + y
15-
print(f"{x} + {y} = {result}")
16-
return result
12+
result = x + y
13+
print(f"{x} + {y} = {result}")
14+
return result
1715
1816
`;
1917
@@ -28,6 +26,60 @@ def add(x, y):
2826
}
2927
```
3028

29+
## `getBlock`
30+
31+
```javascript,mdbook-runnable,hidelines=#
32+
# {{#rustdoc_include tools/helpers.js:1}}
33+
const code = `
34+
a = 1
35+
b = 2
36+
37+
def add_or_subtract(a, b, add=True):
38+
if add:
39+
return a + b
40+
else:
41+
return a - b
42+
43+
`;
44+
45+
{
46+
const equivalentPatterns = [
47+
"if add",
48+
/(if|elif) add/,
49+
];
50+
for (const pattern of equivalentPatterns) {
51+
const ifBlock = __helpers.python.getBlock(code, pattern);
52+
const { block_body, block_indentation, block_condition } = ifBlock;
53+
console.assert(block_indentation === 4);
54+
console.assert(block_condition === "if add");
55+
console.assert(block_body.match(/return a \+ b/));
56+
console.log(ifBlock);
57+
}
58+
}
59+
```
60+
61+
## `removeComments`
62+
63+
```javascript,mdbook-runnable,hidelines=#
64+
# {{#rustdoc_include tools/helpers.js:1}}
65+
// Note: Comment identifiers are escaped for docs markdown parser
66+
const code = `
67+
a = 1
68+
\# comment
69+
def b(d, e):
70+
a = 2
71+
\# comment
72+
return a #comment
73+
`;
74+
{
75+
const commentlessCode = __helpers.python.removeComments(code);
76+
console.assert(commentlessCode === `\na = 1\n\ndef b(d, e):\n a = 2\n \n return a \n`);
77+
console.log(commentlessCode);
78+
}
79+
```
80+
81+
## `__pyodide.runPython`
82+
3183
Running the code of a singluar function to get the output:
3284

3385
```javascript,mdbook-runnable,hidelines=#
@@ -69,8 +121,8 @@ assert add(a, b) == 300
69121

70122
```python
71123
def
72-
add(x, y):
73-
return x + y
124+
add(x, y):
125+
return x + y
74126
```
75127

76128
- Python **does** allow newline characters between function arguments. E.g:
@@ -80,5 +132,5 @@ def add(
80132
x,
81133
y
82134
):
83-
return x + y
135+
return x + y
84136
```

docs/tools/helpers.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const __helpers = {
22
python: {
33
getDef: (code, functionName) => {
44
const regex = new RegExp(
5-
`\\n(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
6-
"s"
5+
`^(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
6+
"ms"
77
);
88

99
const matchedCode = regex.exec(code);
@@ -25,5 +25,39 @@ const __helpers = {
2525

2626
return null;
2727
},
28+
getBlock: (code, blockPattern) => {
29+
const escapedBlockPattern =
30+
blockPattern instanceof RegExp
31+
? blockPattern.source
32+
: blockPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33+
34+
const regex = new RegExp(
35+
`^(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*:\\n(?<block_body>.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
36+
"ms"
37+
);
38+
39+
const matchedCode = regex.exec(code);
40+
if (matchedCode) {
41+
/* eslint-disable camelcase */
42+
const { block_body, block_indentation, block_condition } =
43+
matchedCode.groups;
44+
45+
const blockIndentationSansNewLine = block_indentation.replace(
46+
/\n+/g,
47+
""
48+
);
49+
return {
50+
block_body,
51+
block_condition,
52+
block_indentation: blockIndentationSansNewLine.length,
53+
};
54+
/* eslint-enable camelcase */
55+
}
56+
57+
return null;
58+
},
59+
removeComments: (code) => {
60+
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(#.*$)/gm, "");
61+
},
2862
},
2963
};

lib/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const getIsDeclaredAfter = (styleRule: CSSStyleRule) => (selector: string) => {
9999
export module python {
100100
export function getDef(code: string, functionName: string) {
101101
const regex = new RegExp(
102-
`\\n(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*:\\n(?<function_body>.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
102+
`\\n?(?<function_indentation> *?)def +${functionName} *\\((?<function_parameters>[^\\)]*)\\)\\s*: *?\\n(?<function_body> +.*?)(?=\\n\\k<function_indentation>[\\w#]|$)`,
103103
"s"
104104
);
105105

@@ -142,7 +142,7 @@ export module python {
142142
: blockPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
143143

144144
const regex = new RegExp(
145-
`\\n(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*:\\n(?<block_body>.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
145+
`\\n?(?<block_indentation> *?)(?<block_condition>${escapedBlockPattern})\\s*: *?\\n(?<block_body> +.*?)(?=\\n\\k<block_indentation>[\\w#]|$)`,
146146
"s"
147147
);
148148

0 commit comments

Comments
 (0)