1
0

build_references.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import re
  2. filepath = 'include/pocketpy/vm.h'
  3. with open(filepath, 'r', encoding='utf-8') as f:
  4. lines = f.readlines()
  5. REGION_PATTERN = re.compile(r'#if PK_REGION\("(.+)"\)')
  6. current_region = None
  7. output = []
  8. def parse_line(line: str):
  9. output.append(line)
  10. for line in lines:
  11. if current_region:
  12. if line.startswith('#endif'):
  13. current_region = None
  14. output.append('```\n\n')
  15. else:
  16. parse_line(line.strip(' '))
  17. else:
  18. m = REGION_PATTERN.match(line)
  19. if m:
  20. current_region = m.group(1)
  21. output.append(f'### {current_region}\n')
  22. output.append('```cpp\n')
  23. with open('docs/references.md', 'w', encoding='utf-8') as f:
  24. f.write('''---
  25. label: References
  26. icon: code
  27. order: 2
  28. ---
  29. This page contains all useful methods of `VM` class.
  30. ''')
  31. content = ''.join(output)
  32. # replace {...} to ; (multi-line match)
  33. content = re.sub(r'\{[^}]+?\}', r';', content, flags=re.DOTALL)
  34. f.write(content)