packagemanual.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. from pathlib import Path
  3. import re
  4. import sys
  5. import shutil
  6. PAGE_NAME = 'offline-docs'
  7. STATIC_PATH = 'chrome://browser/content/manual/static'
  8. if len(sys.argv) < 3:
  9. print(f'Usage: {sys.argv[0]} lektor-out-directory target-directory')
  10. sys.exit(1)
  11. source = Path(sys.argv[1])
  12. target = Path(sys.argv[2])
  13. if not target.exists():
  14. target.mkdir(exist_ok=True)
  15. static_re = re.compile('"(?:../)*static/([^"]+)"')
  16. link_re = re.compile('href="../([^"]+)"')
  17. def clean_urls(match):
  18. m = re.match(r'(?:../)?([^/#]+)[/]?[#]?(.*)', match.group(1))
  19. slug = m.group(1)
  20. if m.group(2):
  21. anchor = '_' + m.group(2)
  22. else:
  23. anchor = ''
  24. return f'href="#{slug}{anchor}"'
  25. htmls = source.rglob(f'{PAGE_NAME}/index.html')
  26. for page in htmls:
  27. with page.open(encoding='utf8') as f:
  28. contents = f.read()
  29. contents = static_re.sub(f'"{STATIC_PATH}/\\1"', contents)
  30. contents = link_re.sub(clean_urls, contents)
  31. rel = page.relative_to(source)
  32. dest_name = str(list(rel.parents)[-2].name)
  33. if dest_name == PAGE_NAME:
  34. dest_name = 'en'
  35. dest_name += '.html'
  36. with (target / dest_name).open('w', encoding='utf8') as f:
  37. f.write(contents)
  38. shutil.rmtree(target / 'static', ignore_errors=True)
  39. shutil.copytree(source / 'static', target / 'static')