From d2eecf1b5f3301d46cab90fa9f0fbabfe5fc5204 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 15:01:49 +0300 Subject: [PATCH 01/10] Converting panela to a module for colored HTML render --- lib/panela/__init__.py | 0 lib/panela/panela_colors.py | 3 ++- lib/pnl2html.py | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/panela/__init__.py create mode 100755 lib/pnl2html.py 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..6b8c0470 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 """ @@ -634,6 +633,8 @@ 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) diff --git a/lib/pnl2html.py b/lib/pnl2html.py new file mode 100755 index 00000000..a3528f96 --- /dev/null +++ b/lib/pnl2html.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +import sys +import os + +head = '''\ + +
+'''
+
+foot = '''
+
+''' + + +if __name__ == '__main__': + if not sys.argv[1:]: + sys.exit('usage: pnl2html.py ') + + from panela import panela_colors + + with open(sys.argv[1], 'rb') as pnlfile: + print(head) + print("... TODO panela html render ...") + print(foot) + From 02aad7e1dd6678a988c420c81f9b1895d5706fe2 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 18:50:14 +0300 Subject: [PATCH 02/10] Fix panela color import, add missing requirements --- lib/panela/panela_colors.py | 2 +- requirements.txt | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py index 6b8c0470..644ca194 100644 --- a/lib/panela/panela_colors.py +++ b/lib/panela/panela_colors.py @@ -18,7 +18,7 @@ """ 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 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 From cb7273c0fae61b6e2439020a2b0f9d099e0d60b9 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 19:04:34 +0300 Subject: [PATCH 03/10] pnl2html: Check params and run panela render ag -l | entr pipenv run ./pnl2html.py ../share/firstpage-v2.pnl --- lib/pnl2html.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/pnl2html.py b/lib/pnl2html.py index a3528f96..c7ec5053 100755 --- a/lib/pnl2html.py +++ b/lib/pnl2html.py @@ -16,11 +16,18 @@ 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 import panela_colors + from panela.panela_colors import Template - with open(sys.argv[1], 'rb') as pnlfile: - print(head) - print("... TODO panela html render ...") - print(foot) + print(head) + + pnl = Template() + pnl.read(pnlfile) + pnl.apply_mask() + print(pnl.show()) + + print(foot) From f8a9568e65fe9bdbd1cb8ddb830fb948c9fce10c Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 19:06:08 +0300 Subject: [PATCH 04/10] Set Template width and height after reading file --- lib/panela/panela_colors.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py index 644ca194..9d0966ae 100644 --- a/lib/panela/panela_colors.py +++ b/lib/panela/panela_colors.py @@ -565,6 +565,9 @@ def __init__(self): self.code = [] self.panela = None + self.width = 0 + self.height = 0 + self._colors = { 'A': '#00cc00', 'B': '#00cc00', @@ -607,13 +610,12 @@ 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) - lines = self.page - x_size = max([len(x) for x in lines]) - y_size = len(lines) + def apply_mask(self): - self.panela = Panela(x=x_size, y=y_size) + 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): From 37309cb3a32a3cc6ce4ccd7537ca0ed667ea462c Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 19:48:35 +0300 Subject: [PATCH 05/10] Parse .pnl file into data structure [x, y, char, color, bg_color], ... This allows to use Template object for different kind of output --- lib/panela/panela_colors.py | 30 ++++++++++++++++++++++++------ lib/pnl2html.py | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py index 9d0966ae..1b1b49c6 100644 --- a/lib/panela/panela_colors.py +++ b/lib/panela/panela_colors.py @@ -568,6 +568,10 @@ def __init__(self): self.width = 0 self.height = 0 + # [x, y, char, color, bg_color], ... + # colors are in #000000 format or None + self.data = [] + self._colors = { 'A': '#00cc00', 'B': '#00cc00', @@ -613,17 +617,30 @@ def read(self, filename): self.width = max([len(line) for line in self.page]) self.height = len(self.page) + def parse(self): + lines = self.page + self.data = [] + + for y, line in enumerate(lines): + 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]) + 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): @@ -640,6 +657,7 @@ def main(): 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 index c7ec5053..81062346 100755 --- a/lib/pnl2html.py +++ b/lib/pnl2html.py @@ -26,6 +26,7 @@ pnl = Template() pnl.read(pnlfile) + pnl.parse() pnl.apply_mask() print(pnl.show()) From a8bf48dc98153a456065ab63bdea263d40dd2262 Mon Sep 17 00:00:00 2001 From: Anatoli Babenia Date: Sun, 10 Mar 2019 23:23:41 +0300 Subject: [PATCH 06/10] Render .pnl as .html (without background colors for now) --- lib/panela/panela_colors.py | 35 +++++++++++++++++++++++++++++++++++ lib/pnl2html.py | 13 +++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py index 1b1b49c6..047185dd 100644 --- a/lib/panela/panela_colors.py +++ b/lib/panela/panela_colors.py @@ -570,6 +570,7 @@ def __init__(self): # [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 = { @@ -622,6 +623,7 @@ def parse(self): self.data = [] for y, line in enumerate(lines): + x = 0 for x, char in enumerate(line): color = None bg_color = None @@ -632,6 +634,39 @@ def parse(self): 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): + + prev_color = None + prev_bg = None + + for x, y, char, color, bg_color in self.data: + if char == None: # end of line + if colorize: + if prev_color != None: + sys.stdout.write("") + prev_color = None + sys.stdout.write("\n") + else: + if colorize: + if color != prev_color: + if prev_color != None: + sys.stdout.write("") + if color != None: + sys.stdout.write("" % color) + prev_color = color + + if char == '<': + sys.stdout.write("<") + elif char == '>': + sys.stdout.write(">") + elif char == '&': + sys.stdout.write("&") + else: + sys.stdout.write(char) + def apply_mask(self): diff --git a/lib/pnl2html.py b/lib/pnl2html.py index 81062346..5bcef5fb 100755 --- a/lib/pnl2html.py +++ b/lib/pnl2html.py @@ -5,6 +5,12 @@ head = '''\ +
 '''
 
@@ -27,8 +33,11 @@
     pnl = Template()
     pnl.read(pnlfile)
     pnl.parse()
-    pnl.apply_mask()
-    print(pnl.show())
+    pnl.render_html()
+
+    #pnl.apply_mask()
+    #print(pnl.show())
+
 
     print(foot)
 

From 924deb0a2bb88fb8efcaa881b3e8234e98a909d8 Mon Sep 17 00:00:00 2001
From: Anatoli Babenia 
Date: Mon, 11 Mar 2019 08:54:27 +0300
Subject: [PATCH 07/10] Add background color for html output on Template

---
 lib/panela/panela_colors.py | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py
index 047185dd..bfd302f9 100644
--- a/lib/panela/panela_colors.py
+++ b/lib/panela/panela_colors.py
@@ -12,7 +12,6 @@
 There are several features that not yet implemented (see ___doc___ in Panela)
 
 TODO:
-    * html output
     * png output
 
 """
@@ -639,24 +638,26 @@ def parse(self):
 
     def render_html(self, colorize=True):
 
-        prev_color = None
-        prev_bg = None
+        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_color != None:
+                    if prev_style != (None, None):
                         sys.stdout.write("")
-                    prev_color = None
+                    prev_style = (None, None)
                 sys.stdout.write("\n")
             else:
                 if colorize:
-                    if color != prev_color:
-                        if prev_color != None:
+                    if style != prev_style:
+                        if prev_style != (None, None):
                             sys.stdout.write("")
-                        if color != None:
-                            sys.stdout.write("" % color)
-                        prev_color = color
+                        if style != (None, None):
+                            sys.stdout.write("" % style[1])
+                        prev_style = style
 
                 if char == '<':
                     sys.stdout.write("<")

From e4a83de8244a7a85d708c0aab40de41a54f5df9c Mon Sep 17 00:00:00 2001
From: Anatoli Babenia 
Date: Mon, 11 Mar 2019 09:20:52 +0300
Subject: [PATCH 08/10] Use valid CSS value `unset` instead of `None`

---
 lib/panela/panela_colors.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/lib/panela/panela_colors.py b/lib/panela/panela_colors.py
index bfd302f9..0c5cfc16 100644
--- a/lib/panela/panela_colors.py
+++ b/lib/panela/panela_colors.py
@@ -654,9 +654,11 @@ def render_html(self, colorize=True):
                         if prev_style != (None, None):
                             sys.stdout.write("")
                         if style != (None, None):
+                            fore = style[0] if style[0] else 'unset'
+                            back = style[1] if style[1] else 'unset'
                             sys.stdout.write("" % style[1])
+                                    "color: %s; " % fore +
+                                    "background-color: %s\">" % back)
                         prev_style = style
 
                 if char == '<':

From 30d04a2702a98fef2ca758856a86978ed69634b5 Mon Sep 17 00:00:00 2001
From: Anatoli Babenia 
Date: Mon, 11 Mar 2019 09:28:24 +0300
Subject: [PATCH 09/10] Add doctype and title to pass HTML validation

---
 lib/pnl2html.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/pnl2html.py b/lib/pnl2html.py
index 5bcef5fb..cc6d3357 100755
--- a/lib/pnl2html.py
+++ b/lib/pnl2html.py
@@ -4,7 +4,8 @@
 import os
 
 head = '''\
-
+
+cheat.sh - pnl2html