forked from oscc-web/ysyx-docs-content
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtool.py
70 lines (60 loc) · 3.28 KB
/
tool.py
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
69
70
import re
import argparse
def transform_markdown(markdown: str) -> str:
# Define a mapping from original keywords to new formats
mapping = {
'hint' : 'tip',
'flag': 'tip',
'comment': 'info',
'question': 'info',
'option': 'info',
'todo': 'warning',
'danger': 'danger',
'caution': 'danger'
}
# Define the regex pattern for matching the entire block
# This pattern captures three groups:
# 1. (\w+): Matches any word (keyword) following '####' and captures it as the block type.
# 2. (.*?): Matches the title after the keyword, using non-greedy matching to stop at the first newline.
# 3. ((?:>.*?\n|>\s*\n)+): Matches the content block, which consists of lines starting with '> '.
# - (?:>.*?\n): Matches lines starting with '>', followed by any characters, ending in newline.
# - (?:>\s*\n): Matches lines that contain only '>' followed by optional whitespace and a newline.
# - +: Ensures that one or more such lines are matched.
pattern = r'> #### (\w+)::(.*?)\n((?:>.*?\n|>\s*\n)+)'
# Function to replace matched patterns with the desired format
def replace_block(match):
block_type = match.group(1) # Extract block type (any keyword)
title = match.group(2).strip() # Extract the title
new_format = mapping.get(block_type, 'info') # Look up the new format based on the block type using the mapping
content = match.group(3).replace('>\n', '\n').replace('> ', '').strip() # Clean content lines
return f':::{new_format}[{title}]\n{content}\n:::' # Format block
transformed_markdown = re.sub(pattern, replace_block, markdown, flags=re.DOTALL)
return transformed_markdown
def main(input_file, output_file=None, inplace=False):
# Read the content of the input markdown file
with open(input_file, 'r', encoding='utf-8') as infile:
markdown_content = infile.read()
# Transform the content using the transform_markdown function
transformed_content = transform_markdown(markdown_content)
# Check if inplace editing is enabled
if inplace:
# Write the transformed content back to the input file
with open(input_file, 'w', encoding='utf-8') as outfile:
outfile.write(transformed_content)
elif output_file:
# Write the transformed content to the specified output file
with open(output_file, 'w', encoding='utf-8') as outfile:
outfile.write(transformed_content)
else:
# Print the transformed content to stdout
print(transformed_content) # Print to stdout if no output file is specified
if __name__ == '__main__':
# Set up command line argument parsing
parser = argparse.ArgumentParser(description='Transform markdown sections into a new format.')
parser.add_argument('input_file', type=str, help='Path to the input markdown file')
parser.add_argument('-o', '--output_file', type=str, help='Path to the output markdown file')
parser.add_argument('-i', '--inplace', action='store_true', help='Edit the input file in place') # Short flag for in-place editing
# Parse the command line arguments
args = parser.parse_args()
# Call the main function with parsed arguments
main(args.input_file, args.output_file, args.inplace)