You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
3.9 KiB

  1. #!/usr/bin/env python3
  2. #
  3. # Usage: ./remove_links.py
  4. # Removes links from index.html and adds page numbers in brackets instead (p. XX).
  5. from pathlib import Path
  6. MATCHES = {
  7. '<strong>Module <a href="#operator">operator</a> provides functions itemgetter() and mul() that offer the same functionality as <a href="#lambda">lambda</a> expressions above.</strong>': '<strong>Module \'operator\' (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions (p. 11) above.</strong>',
  8. '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its <a href="#class">repr()</a> method.</strong>': '<strong>Adding <code class="python hljs"><span class="hljs-string">\'!r\'</span></code> before the colon converts object to string by calling its repr() method (p. 14).</strong>',
  9. '<strong>It can be any <a href="#callable">callable</a>, but is usually implemented as a function that returns a <a href="#closure">closure</a>.</strong>': '<strong>It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).</strong>',
  10. '<strong>Default_factory can be any <a href="#callable">callable</a>.</strong>': '<strong>Default_factory can be any callable (p. 17).</strong>',
  11. '<strong>Sequence iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p.&nbsp;3).</strong>',
  12. '<strong>Objects returned by the <a href="#itertools">itertools</a> module, such as count, repeat and cycle.</strong>': '<strong>Objects returned by the itertools module, such as count, repeat and cycle (p. 3).</strong>',
  13. '<strong>Generators returned by the <a href="#generator">generator functions</a> and <a href="#comprehensions">generator expressions</a>.</strong>': '<strong>Generators returned by the generator functions (p. 4) and generator expressions (p. 11).</strong>',
  14. '<strong>File objects returned by the <a href="#open">open()</a> function, etc.</strong>': '<strong>File objects returned by the open() function (p. 22), etc.</strong>',
  15. '<strong>Another solution in this particular case is to use functions and_() and or_() from the module <a href="#operator">operator</a>.</strong>': '<strong>Another solution in this particular case is to use functions and_() and or_() from the module \'operator\' (p. 31).</strong>',
  16. '<strong>Functions report OS related errors by raising either OSError or one of its <a href="#exceptions-1">subclasses</a>.</strong>': '<strong>Functions report OS related errors by raising OSError or one of its subclasses (p. 23).</strong>',
  17. '<strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong>': '<strong>Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).</strong>',
  18. '<strong>Asyncio module also provides its own <a href="#queue">Queue</a>, <a href="#semaphoreeventbarrier">Event</a>, <a href="#lock">Lock</a> and <a href="#semaphore-event-barrier">Semaphore</a> classes.</strong>': '<strong>Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).</strong>',
  19. }
  20. def main():
  21. index_path = Path('..', 'index.html')
  22. lines = read_file(index_path)
  23. out = ''.join(lines)
  24. for from_, to_ in MATCHES.items():
  25. out = out.replace(from_, to_, 1)
  26. write_to_file(index_path, out)
  27. ###
  28. ## UTIL
  29. #
  30. def read_file(filename):
  31. p = Path(__file__).resolve().parent / filename
  32. with open(p, encoding='utf-8') as file:
  33. return file.readlines()
  34. def write_to_file(filename, text):
  35. p = Path(__file__).resolve().parent / filename
  36. with open(p, 'w', encoding='utf-8') as file:
  37. file.write(text)
  38. if __name__ == '__main__':
  39. main()