Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5772548

Browse files
committed
Added remove_links.py script
1 parent f89456b commit 5772548

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

pdf/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Printing to PDF
2525
* Open `index.html` in text editor and first remove element `<p><br></p>` before the `<h1>Libraries</h1>`.
2626
* Then replace the index and footer with contents of `pdf/index_for_pdf_print.html` file.
2727
* Disable internet connection and open the file in Chrome with 'Cache killer' extension enabled.
28-
* Change all links in text to normal text and optionally add a page number in brackets like that: '(p. <num>)'. Links can be found with this regex: `<strong>.*a href.*</strong>`.
28+
* Change all links in text to normal text and add a page number in brackets like that: '(p. <num>)' by running 'pdf/remove_links.py' (Links can be found with this regex: `<strong>.*a href.*</strong>`).
2929
* Save and open `index.html` in Chrome.
3030
* 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.
3131
* Change lightness (L) percentage to:

pdf/remove_links.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
6+
from pathlib import Path
7+
8+
9+
MATCHES = {
10+
'<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 above.</strong>',
11+
'<strong><code class="python hljs"><span class="hljs-string">\'!r\'</span></code> calls object\'s <a href="#class">repr()</a> method, instead of <a href="#class">str()</a>, to get a string.</strong>': '<strong><code class="python hljs"><span class="hljs-string">\'!r\'</span></code> calls object\'s repr() method, instead of str(), to get a string (p. 14).</strong>',
12+
'<strong>Default_factory can be any <a href="#callable">callable</a>.</strong>': '<strong>Default_factory can be any callable (p. 17).</strong>',
13+
'<strong>Iterators returned by the <a href="#iterator">iter()</a> function, such as list_iterator and set_iterator.</strong>': '<strong>Iterators returned by the iter() function, such as list_iterator and set_iterator (p. 3).</strong>',
14+
'<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>',
15+
'<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>',
16+
'<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>',
17+
'<strong>Another solution in this particular case is to use built-in functions and_() and or_() from the module <a href="#operator">operator</a>.</strong>': '<strong>Another solution in this particular case is to use built-in functions and_() and or_() from the module operator (p. 31).</strong>',
18+
'<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>',
19+
'<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>',
20+
'<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>',
21+
}
22+
23+
24+
def main():
25+
index_path = Path('..', 'index.html')
26+
lines = read_file(index_path)
27+
out = ''.join(lines)
28+
for from_, to_ in MATCHES.items():
29+
out = out.replace(from_, to_, 1)
30+
write_to_file(index_path, out)
31+
32+
33+
###
34+
## UTIL
35+
#
36+
37+
def read_file(filename):
38+
p = Path(__file__).resolve().parent / filename
39+
with open(p, encoding='utf-8') as file:
40+
return file.readlines()
41+
42+
43+
def write_to_file(filename, text):
44+
p = Path(__file__).resolve().parent / filename
45+
with open(p, 'w', encoding='utf-8') as file:
46+
file.write(text)
47+
48+
49+
if __name__ == '__main__':
50+
main()

0 commit comments

Comments
 (0)