|
1 | | -import sys |
2 | | -import os |
3 | | - |
4 | | -def process_file(input_file, output_file, extra_file): |
5 | | - # 确保 extra.txt 文件存在 |
6 | | - if not os.path.exists(extra_file): |
7 | | - print(f"Error: The file '{extra_file}' does not exist.") |
8 | | - return |
9 | | - |
10 | | - # 获取 extra.txt 文件内容 |
11 | | - with open(extra_file, 'r', encoding='utf-8') as e: |
12 | | - extra = e.read().replace("{{PTH}}", input_file[1:]) # 用路径替换 {{PTH}} |
13 | | - |
14 | | - # 逐行读取输入文件并处理内容 |
15 | | - with open(input_file, 'r', encoding='utf-8') as f, open(output_file, 'w', encoding='utf-8') as g: |
16 | | - for line in f: |
17 | | - line = line.strip() # 去除行首和行尾空白字符 |
18 | | - if line: # 忽略空行 |
19 | | - # 如果是特定标签,添加 extra 和 <hr/> 内容 |
20 | | - if line == '<div role="contentinfo">': |
21 | | - g.write(extra + "<hr/>\n") |
22 | | - else: |
23 | | - g.write(line + '\n') # 写入处理后的行 |
24 | | - |
25 | | - # 替换原始文件 |
26 | | - os.rename(output_file, input_file) |
27 | | - |
28 | | -def main(): |
29 | | - if len(sys.argv) < 2: |
30 | | - print("Usage: python script.py <input_file>") |
31 | | - return |
32 | | - |
33 | | - input_file = sys.argv[1] |
34 | | - output_file = './qpydoc.tmp' |
35 | | - extra_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'extra.txt') |
36 | | - |
37 | | - # 处理文件 |
38 | | - process_file(input_file, output_file, extra_file) |
39 | | - |
40 | | -if __name__ == '__main__': |
41 | | - main() |
| 1 | +import sys,os |
| 2 | +with open(sys.argv[1], 'r') as f, open('./qpydoc.tmp', 'wb') as g, open(os.path.dirname(os.path.abspath(__file__))+'/extra.txt','r') as e: |
| 3 | + pth = sys.argv[1][1:] |
| 4 | + extra = "".join(e.readlines()).replace("{{PTH}}",pth) |
| 5 | + g.write('\n'.join( |
| 6 | + filter(lambda s: len(s), |
| 7 | + map(lambda s: |
| 8 | + ('',extra+"<hr/>")[s=='<div role="contentinfo">']+s, |
| 9 | + map(str.strip, f.readlines()))))) |
| 10 | +os.rename('./qpydoc.tmp', sys.argv[1]) |
0 commit comments