packagemanual.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. remove_images = []
  26. for p in (source / 'static/images').glob('**/*'):
  27. if p.is_file():
  28. rel = p.relative_to(source)
  29. remove_images.append(rel)
  30. htmls = source.rglob(f'{PAGE_NAME}/index.html')
  31. for page in htmls:
  32. with page.open(encoding='utf8') as f:
  33. contents = f.read()
  34. remove_images = list(filter(
  35. lambda im: contents.find(str(im)) == -1, remove_images))
  36. contents = static_re.sub(f'"{STATIC_PATH}/\\1"', contents)
  37. contents = link_re.sub(clean_urls, contents)
  38. rel = page.relative_to(source)
  39. dest_name = str(list(rel.parents)[-2].name)
  40. if dest_name == PAGE_NAME:
  41. dest_name = 'en'
  42. dest_name += '.html'
  43. with (target / dest_name).open('w', encoding='utf8') as f:
  44. f.write(contents)
  45. def ignore_images(path, names):
  46. return [
  47. n
  48. for n in names
  49. if Path(path, n).relative_to(source) in remove_images
  50. ]
  51. shutil.rmtree(target / 'static', ignore_errors=True)
  52. shutil.copytree(source / 'static', target / 'static', ignore=ignore_images)