diff --git a/lib/panela/__init__.py b/lib/panela/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py index 00bf58ba..a9d646bf 100644 --- a/lib/panela/panela_colors.py +++ b/lib/panela/panela_colors.py @@ -4,7 +4,6 @@ import sys import colored import itertools -from globals import MYDIR """ @@ -13,13 +12,12 @@ There are several features that not yet implemented (see ___doc___ in Panela) TODO: - * html output * png output """ from wcwidth import wcswidth -from colors import find_nearest_color, HEX_TO_ANSI, rgb_from_str +from .colors import find_nearest_color, HEX_TO_ANSI, rgb_from_str import pyte # http://stackoverflow.com/questions/19782975/convert-rgb-color-to-the-nearest-color-in-palette-web-safe-color @@ -566,6 +564,14 @@ def __init__(self): self.code = [] self.panela = None + self.width = 0 + self.height = 0 + + # [x, y, char, color, bg_color], ... + # colors are in #000000 format or None + # char is None for the end of line + self.data = [] + self._colors = { 'A': '#00cc00', 'B': '#00cc00', @@ -608,21 +614,71 @@ def read(self, filename): elif self._mode == 'code': self.mask.append(line) - def apply_mask(self): + self.width = max([len(line) for line in self.page]) + self.height = len(self.page) + def parse(self): lines = self.page - x_size = max([len(x) for x in lines]) - y_size = len(lines) + self.data = [] + + for y, line in enumerate(lines): + x = 0 + for x, char in enumerate(line): + color = None + bg_color = None + if y < len(self.mask): + if x < len(self.mask[y]): + m = self.mask[y][x] + color = self._colors.get(m) + bg_color = self._bg_colors.get(m) + + self.data.append([x, y, char, color, bg_color]) + # end of line + self.data.append([x, y, None, None, None]) + + def render_html(self, colorize=True, writer=sys.stdout): + + prev_style = (None, None) + + for x, y, char, color, bg_color in self.data: + style = (color, bg_color) + if char == None: # end of line + if colorize: + if prev_style != (None, None): + writer.write("") + prev_style = (None, None) + writer.write("\n") + else: + if colorize: + if style != prev_style: + if prev_style != (None, None): + writer.write("") + if style != (None, None): + fore = style[0] if style[0] else 'unset' + back = style[1] if style[1] else 'unset' + writer.write("" % back) + prev_style = style + + if char == '<': + writer.write("<") + elif char == '>': + writer.write(">") + elif char == '&': + writer.write("&") + else: + writer.write(char) - self.panela = Panela(x=x_size, y=y_size) + + def apply_mask(self): + + self.panela = Panela(x=self.width, y=self.height) self.panela.read_ansi("".join("%s\n" % x for x in self.page)) - for i, line in enumerate(self.mask): - for j, char in enumerate(line): - if char in self._colors or char in self._bg_colors: - color = self._colors.get(char) - bg_color = self._bg_colors.get(char) - self.panela.put_point(j, i, color=color, background=bg_color) + for x, y, _, color, bg_color in self.data: + if color or bg_color: + self.panela.put_point(x, y, color=color, background=bg_color) def show(self): @@ -634,9 +690,12 @@ def show(self): def main(): "Only for experiments" + from globals import MYDIR + pagepath = os.path.join(MYDIR, "share/firstpage-v2.pnl") template = Template() template.read(pagepath) + template.parse() template.apply_mask() sys.stdout.write(template.show()) diff --git a/lib/pnl2html.py b/lib/pnl2html.py new file mode 100755 index 00000000..cc6d3357 --- /dev/null +++ b/lib/pnl2html.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +import sys +import os + +head = '''\ + +cheat.sh - pnl2html + +
+'''
+
+foot = '''
+
+''' + + +if __name__ == '__main__': + if not sys.argv[1:]: + sys.exit('usage: pnl2html.py ') + pnlfile = sys.argv[1] + if not os.path.isfile(pnlfile): + sys.exit("error: " + pnlfile + " does not exist") + + from panela.panela_colors import Template + + print(head) + + pnl = Template() + pnl.read(pnlfile) + pnl.parse() + pnl.render_html() + + #pnl.apply_mask() + #print(pnl.show()) + + + print(foot) + diff --git a/requirements.txt b/requirements.txt index af1981cd..00c36143 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,7 @@ PyICU pycld2 colorama pyyaml + +# panela reqiurements +wcwidth +pyte