-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync_readme_example.py
More file actions
68 lines (55 loc) · 2.16 KB
/
sync_readme_example.py
File metadata and controls
68 lines (55 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import re
# Read the content of counter.rs
with open('examples/counter.rs', 'r') as file:
lines = file.readlines()
def position(iterable, predicate):
for i, value in enumerate(iterable):
if predicate(value):
return i
return None
# Remove comments but keep the first line of the top module comment
first_module_comment_line = None
in_module_comment = False
filtered_lines = []
for i, line in enumerate(lines):
# Check if it's a module comment line
if line.strip().startswith('//!'):
if first_module_comment_line is None:
# This is the first line of the module comment, keep it
first_module_comment_line = line
filtered_lines.append(line)
in_module_comment = True
# Skip all other module comment lines
continue
# Check if it's a regular comment line
elif line.strip().startswith('//'):
# Skip regular comment lines
continue
else:
# Not a comment line, keep it
filtered_lines.append(line)
lines = filtered_lines
mod_utils_line = position(lines, lambda line: line.startswith('mod utils'))
lines = lines[:mod_utils_line] + lines[mod_utils_line + 3:]
# Join the lines into a single string
content = ''.join(lines)
# Replace `example_plugin` with `((DefaultPlugins, JonmoPlugin))`
content = re.sub(r'examples_plugin', '(DefaultPlugins, JonmoPlugin)', content)
# Read the content of README.md
with open('README.md', 'r') as file:
readme_content = file.read()
# Insert the content after the marker
# Define the start and end markers for the Rust code block
start_marker = '```rust,ignore'
end_marker = '```'
# Find the start and end positions of the Rust code block
start_pos = readme_content.find(start_marker)
end_pos = readme_content.find(end_marker, start_pos + len(start_marker))
# Replace the content between the markers
if start_pos != -1 and end_pos != -1:
new_readme_content = (readme_content[:start_pos + len(start_marker)] + '\n' + content + readme_content[end_pos:])
else:
new_readme_content = readme_content
# Write the updated content back to README.md
with open('README.md', 'w') as file:
file.write(new_readme_content)