diff --git a/pdf/README.md b/pdf/README.md index 71ab9a6..dd43774 100644 --- a/pdf/README.md +++ b/pdf/README.md @@ -25,7 +25,7 @@ Printing to PDF * Open `index.html` in text editor and first remove element `


` before the `

Libraries

`. * Then replace the index and footer with contents of `pdf/index_for_pdf_print.html` file. * Disable internet connection and open the file in Chrome with 'Cache killer' extension enabled. -* Change all links in text to normal text and optionally add a page number in brackets like that: '(p. )'. Links can be found with this regex: `.*a href.*`. +* Change all links in text to normal text and add a page number in brackets like that: '(p. )' by running 'pdf/remove_links.py' (Links can be found with this regex: `.*a href.*`). * Save and open `index.html` in Chrome. * Change brightness of elements by right clicking on them and selecting inspect. Then click on the rectangle that represents color and toggle the color space to HSLA by clicking on the button with two vertical arrows. * Change lightness (L) percentage to: diff --git a/pdf/remove_links.py b/pdf/remove_links.py new file mode 100755 index 0000000..74d0a22 --- /dev/null +++ b/pdf/remove_links.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +# +# Usage: ./remove_links.py +# Removes links from index.html and adds page numbers in brackets instead (p. XX). + +from pathlib import Path + + +MATCHES = { + 'Module operator provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above.': 'Module operator (p. 31) provides functions itemgetter() and mul() that offer the same functionality as lambda expressions above.', + '\'!r\' calls object\'s repr() method, instead of str(), to get a string.': '\'!r\' calls object\'s repr() method, instead of str(), to get a string (p. 14).', + 'Default_factory can be any callable.': 'Default_factory can be any callable (p. 17).', + 'Iterators returned by the iter() function, such as list_iterator and set_iterator.': 'Iterators returned by the iter() function, such as list_iterator and set_iterator (p. 3).', + 'Objects returned by the itertools module, such as count, repeat and cycle.': 'Objects returned by the itertools module, such as count, repeat and cycle (p. 3).', + 'Generators returned by the generator functions and generator expressions.': 'Generators returned by the generator functions (p. 4) and generator expressions (p. 11).', + 'File objects returned by the open() function, etc.': 'File objects returned by the open() function (p. 22), etc.', + 'Another solution in this particular case is to use built-in functions and_() and or_() from the module operator.': 'Another solution in this particular case is to use built-in functions and_() and or_() from the module operator (p. 31).', + 'Functions report OS related errors by raising either OSError or one of its subclasses.': 'Functions report OS related errors by raising OSError or one of its subclasses (p. 23).', + 'Bools will be stored and returned as ints and dates as ISO formatted strings.': 'Bools will be stored and returned as ints and dates as ISO formatted strings (p. 9).', + 'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes.': 'Asyncio module also provides its own Queue, Event, Lock and Semaphore classes (p. 30).', +} + + +def main(): + index_path = Path('..', 'index.html') + lines = read_file(index_path) + out = ''.join(lines) + for from_, to_ in MATCHES.items(): + out = out.replace(from_, to_, 1) + write_to_file(index_path, out) + + +### +## UTIL +# + +def read_file(filename): + p = Path(__file__).resolve().parent / filename + with open(p, encoding='utf-8') as file: + return file.readlines() + + +def write_to_file(filename, text): + p = Path(__file__).resolve().parent / filename + with open(p, 'w', encoding='utf-8') as file: + file.write(text) + + +if __name__ == '__main__': + main()