diff options
Diffstat (limited to 'epy.py')
-rwxr-xr-x | epy.py | 67 |
1 files changed, 62 insertions, 5 deletions
@@ -14,7 +14,7 @@ Options: """ -__version__ = "2021.1.29" +__version__ = "2021.1.30" __license__ = "GPL-3.0" __author__ = "Benawi Adha" __email__ = "benawiadha@gmail.com" @@ -398,6 +398,7 @@ class HTMLtoLines(HTMLParser): pref = {"pre"} bull = {"li"} hide = {"script", "style", "head"} + ital = {"i", "em"} # hide = {"script", "style", "head", ", "sub} def __init__(self, sects={""}): @@ -415,6 +416,7 @@ class HTMLtoLines(HTMLParser): self.idpref = set() self.sects = sects self.sectsindex = {} + self.initital = [] def handle_starttag(self, tag, attrs): if re.match("h[1-6]", tag) is not None: @@ -437,6 +439,8 @@ class HTMLtoLines(HTMLParser): if i[0].endswith("href"): self.text.append("[IMG:{}]".format(len(self.imgs))) self.imgs.append(unquote(i[1])) + elif tag in self.ital: + self.initital.append([len(self.text)-1, len(self.text[-1])]) if self.sects != {""}: for i in attrs: if i[0] == "id" and i[1] in self.sects: @@ -487,6 +491,8 @@ class HTMLtoLines(HTMLParser): self.text[-1] += "}" elif tag == "image": self.text.append("") + elif tag in self.ital: + self.initital[-1] += [len(self.text)-1, len(self.text[-1])] def handle_data(self, raw): if raw and not self.ishidden: @@ -510,10 +516,26 @@ class HTMLtoLines(HTMLParser): def get_lines(self, width=0): text, sect = [], {} + formatting = { + "italic": [] + } + tmpital = [] + for i in self.initital: + if i[0] == i[2]: + tmpital.append([i[0], i[1], i[3]-i[1]]) + elif i[0] == i[2]-1: + tmpital.append([i[0], i[1], len(self.text[i[0]])-i[1]]) + tmpital.append([i[2], 0, i[3]]) + elif i[2]-i[0] > 1: + tmpital.append([i[0], i[1], len(self.text[i[0]])-i[1]]) + for j in range(i[0]+1, i[2]): + tmpital.append([j, 0, len(self.text[j])]) + tmpital.append([i[2], 0, i[3]]) + if width == 0: return self.text for n, i in enumerate(self.text): - # findsect = re.search(r"(?<= \(#).*?(?=\) )", i) + startline = len(text) # findsect = re.search(r"(?<= \(#).*?(?=\) )", i) # if findsect is not None and findsect.group() in self.sects: # i = i.replace(" (#" + findsect.group() + ") ", "") @@ -540,7 +562,32 @@ class HTMLtoLines(HTMLParser): text += [" "+j for j in wraptmp] + [""] else: text += textwrap.wrap(i, width) + [""] - return text, self.imgs, sect + + # TODO + endline = len(text) # -1 + tmp_filtered = [j for j in tmpital if j[0] == n] + for j in tmp_filtered: + tmp_count = 0 + # for k in text[startline:endline]: + for k in range(startline, endline): + if tmp_count <= j[1]: + tmp_start = [k, j[1]-tmp_count] + if tmp_count <= j[1]+j[2]: + tmp_end = [k, j[1]+j[2]-tmp_count] + tmp_count += len(text[k]) + 1 + if tmp_start[0] == tmp_end[0]: + formatting["italic"].append(tmp_start + [tmp_end[1]-tmp_start[1]]) + elif tmp_start[0] == tmp_end[0]-1: + formatting["italic"].append(tmp_start + [len(text[tmp_start[0]])-tmp_start[1]+1]) + formatting["italic"].append([tmp_end[0], 0, tmp_end[1]]) + # elif tmp_start[0]-tmp_end[1] > 1: + else: + formatting["italic"].append(tmp_start + [len(text[tmp_start[0]])-tmp_start[1]+1]) + for l in range(tmp_start[0]+1, tmp_end[0]): + formatting["italic"].append([l, 0, len(text[l])]) + formatting["italic"].append([tmp_end[0], 0, tmp_end[1]]) + + return text, self.imgs, sect, formatting class Board: @@ -558,6 +605,13 @@ class Board: def feed(self, textlist): self.text = textlist + def feed_format(self, formatting): + self.formatting = formatting + + def format(self): + for i in self.formatting["italic"]: + self.pad.chgat(i[0], i[1], i[2], curses.A_ITALIC) + def getch(self): return self.pad.getch() @@ -1002,7 +1056,7 @@ def input_prompt(prompt): curses.echo(0) safe_curs_set(0) return init_text - elif ipt in {8, curses.KEY_BACKSPACE}: + elif ipt in {8, 127, curses.KEY_BACKSPACE}: init_text = init_text[:-1] elif ipt == curses.KEY_RESIZE: stat.clear() @@ -1235,6 +1289,7 @@ def searching(pad, src, width, y, ch, tot): SEARCHPATTERN = None for i in found: pad.chgat(i[0], i[1], i[2], pad.getbkgd()) + pad.format() SCREEN.clear() SCREEN.refresh() return y @@ -1391,7 +1446,7 @@ def reader(ebook, index, width, y, pctg, sect): # except: # pass - src_lines, imgs, toc_secid = parser.get_lines(width) + src_lines, imgs, toc_secid, formatting = parser.get_lines(width) totlines = len(src_lines) + 1 # 1 extra line for suffix if y < 0 and totlines <= rows: @@ -1403,6 +1458,7 @@ def reader(ebook, index, width, y, pctg, sect): pad = Board(totlines, width) pad.feed(src_lines) + pad.feed_format(formatting) # this make curses.A_REVERSE not working # put before paint_text @@ -1410,6 +1466,7 @@ def reader(ebook, index, width, y, pctg, sect): pad.bkgd(SCREEN.getbkgd()) pad.paint_text(0) + pad.format() LOCALPCTG = [] for i in src_lines: |