-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_pages.py
More file actions
41 lines (36 loc) · 1.18 KB
/
html_pages.py
File metadata and controls
41 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import html as html_converter
class FileAccessWrapper:
def __init__(self, filename):
self.filename = filename
def open(self):
return open(self.filename, "r", encoding="UTF08")
class HtmlPagesConverter:
def __init__(self, file_access):
"""Read the file and note the positions of the page breaks so we can access them"""
self.file_access = file_access
self.breaks = [0]
with self.file_access.open() as f:
while True:
line = f.readline()
if not line:
break
line = line.rstrip()
if "PAGE_BREAK" in line:
page_break_position = f.tell()
self.breaks.append(f.tell())
self.breaks.append(f,.tell())
def get_html_page(self, page):
"""Return Html page with the g iven number (zero indexed) """
page_start = self.breaks[page]
page_end = self.breaks[page+1]
html = ""
with self.file_access.open() as f:
f.seek(page_start)
while f.tell() != page_end:
line = f.readline()
line = line.rstrip()
if "PAGE_BREAK" in line:
continue
html += html_converter.escape(line, quote=True)
html += "<br/>"
return html