markslide moved, testing tgz installers

This commit is contained in:
Q
2021-10-30 10:46:14 +03:00
parent ecd8a10c5d
commit 533a663eaf
14 changed files with 42 additions and 47 deletions

View File

@@ -14,12 +14,13 @@ help: ## *:・゚✧*:・゚✧ This help *:・゚✧*:・゚✧
'
clean: ## Clean build and .egg folder in py modules
rm -vr */build/ */*.egg-info
find . -depth -type d -name __pycache__ -exec rm -vr \{\} \;
find . -depth -type d -name build -exec rm -vr \{\} \;
find . -depth -type d -name '*.egg-info' -exec rm -vr \{\} \;
pipx: ## Install all packages with pipx
for module in *; do if [[ -f $module/setup.py ]]; then pipx install $module; fi; done
for module in *; do if [ -f $$module/setup.py ]; then pipx install $$module; fi; done
foo:
pass
package: clean ## Create packages for every module
for module in *; do if [ -f $$module/setup.py ]; then tar czf $$module.tgz $$module; fi; done

Binary file not shown.

BIN
py-packages/TSVFilter.tgz Normal file

Binary file not shown.

BIN
py-packages/markslider.tgz Normal file

Binary file not shown.

View File

@@ -0,0 +1,197 @@
# Python library for ansi colorization
import re, sys
class code:
K = "\033[1;30m"
R = "\033[1;31m"
G = "\033[1;32m"
B = "\033[1;34m"
Y = "\033[1;33m"
M = "\033[1;35m"
C = "\033[1;36m"
W = "\033[1;37m"
k = "\033[0;30m"
r = "\033[0;31m"
g = "\033[0;32m"
b = "\033[0;34m"
y = "\033[0;33m"
m = "\033[0;35m"
c = "\033[0;36m"
w = "\033[0;37m"
bk = "\033[40m"
br = "\033[41m"
bg = "\033[42m"
by = "\033[43m"
bb = "\033[44m"
bm = "\033[45m"
bc = "\033[46m"
bw = "\033[47m"
S = "\033[1m" # strong
s = "\033[2m" # strong off
U = "\033[4m" # underline
u = "\033[24m" # underline off
Z = "\033[0m" # zero colors
ic = "\033[7m" # inverse colors
io = "\033[27m" # inverse off
st = "\033[9m" # strike on
so = "\033[29m" # strike off
CLR = "\033[2J"
CLREND = "\033[K"
CLRBEG = "\033[1K"
CLRSCR = CLR + "\033[0;0H"
color_keys = "K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLR,CLREND,CLRBEG,CLRSCR".split(
","
)
color_list = [
K,
R,
G,
B,
Y,
M,
C,
W,
k,
r,
g,
b,
y,
m,
c,
w,
S,
s,
U,
u,
Z,
ic,
io,
st,
so,
bk,
br,
bg,
by,
bb,
bm,
bc,
bw,
CLR,
CLREND,
CLRBEG,
CLRSCR,
]
custom_match = re.compile(r"(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})")
def pos(self, y, x):
""" Go to absolute position """
return "\033[" + str(y) + ";" + str(x) + "H"
def column(self, x):
""" Go to absolute column """
return "\033[" + str(x) + "G"
def posprint(self, y, x, s):
""" Print string at a location """
sys.stdout.write(self.pos(y, x) + str(s))
self.flush()
def clear(self):
sys.stdout.write(self.CLRSCR + self.pos(0, 0))
def clear_to_end(self):
sys.stdout.write(self.CLREND)
def clear_to_beginning(self):
sys.stdout.write(self.CLRBEG)
def up(self, n=1):
sys.stdout.write("\033[" + str(n) + "A")
def down(self, n=1):
sys.stdout.write("\033[" + str(n) + "B")
def right(self, n=1):
sys.stdout.write("\033[" + str(n) + "C")
def left(self, n=1):
sys.stdout.write("\033[" + str(n) + "D")
def up_line(self, n=1):
sys.stdout.write("\033[" + str(n) + "F")
def down_line(self, n=1):
sys.stdout.write("\033[" + str(n) + "E")
def save(self):
""" Save cursor position """
sys.stdout.write("\033[s")
def restore(self):
""" Restore cursor position """
sys.stdout.write("\033[u")
def color_string(self, s):
for i, c in enumerate(self.color_keys):
s = s.replace("${" + c + "}", self.color_list[i])
return self.custom_color(s)
def nocolor_string(self, s):
for i, c in enumerate(self.color_keys):
s = s.replace("${" + c + "}", "")
return self.custom_nocolor(s)
def custom_color(self, s):
return self.custom_match.sub("\033[\\2", s)
def custom_nocolor(self, s):
return self.custom_match.sub("", s)
def get_keys(self):
return self.color_keys
def flush(self):
sys.stdout.flush()
def demo():
""" Print all control sequences """
c = code()
unformatted = """${S}ANSI CODES
==========${Z}
${S}Fo${U}rm${st}at${u}ti${ic}ng${Z}
${S}==========${Z}
0 Z Clear format
1 S ${S}Strong ${Z} 2 s ${s}Off${Z}
4 U ${U}Underline${Z} 24 u Off
7 ic ${ic}Inverse${Z} 27 io Off
9 st ${st}Strike${Z} 29 so Off
${R}Co${G}lo${B}rs${Z}
${S}======${Z}
30 k ${k}Black ${Z}1 K ${K}Strong ${Z}40 bk ${bk}Background${Z}
31 r ${r}Red ${Z}1 R ${R}Strong ${Z}41 br ${br}Background${Z}
32 g ${g}Green ${Z}1 G ${G}Strong ${Z}42 bg ${bg}Background${Z}
33 y ${y}Yellow ${Z}1 Y ${Y}Strong ${Z}43 by ${by}Background${Z}
34 b ${b}Blue ${Z}1 B ${B}Strong ${Z}44 bb ${bb}Background${Z}
35 m ${m}Magenta ${Z}1 M ${M}Strong ${Z}45 bm ${bm}Background${Z}
36 c ${c}Cyan ${Z}1 C ${C}Strong ${Z}46 bc ${bc}Background${Z}
37 w ${w}White ${Z}1 W ${W}Strong ${Z}47 bw ${bw}Background${Z}
${S}Clearing and movement
${S}=====================${Z}
2J CLR Clear screen
2J ;H CLRSCR .clear() Clear screen and cursor to upper left
K CLREND Clear to end of line
1K CLRBEG Clear to beginning of line
s .save() Save location u .restore() Restore location
A .up() Up E .up_line() Up line
B .down() Down F .down_line() Down line
C .right() Right y;xH .pos() Absolute Position
D .left() Left """
return c.color_string(unformatted)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,420 @@
#!/usr/bin/env python3
import sys, os, re
from argparse import ArgumentParser, RawDescriptionHelpFormatter
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansicodes as ansi
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.3"
""" Rules modified from mistune project """
def setup_options():
bc = ansi.code()
""" Create command line options """
usage = (
"""
Markdown syntax color in ansi codes.
Special syntaxes:
- Colors: insert string e.g. ${C}.
- Any ANSI control code: ${3A}, ${1;34;42m}, see the table..
"""
+ ansi.demo()
)
parser = ArgumentParser(
formatter_class=RawDescriptionHelpFormatter,
description=usage,
epilog=__author__,
)
parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument(
"-D", action="store_true", dest="debug", default=False, help="Debug mode"
)
parser.add_argument(
"--dc",
action="store_true",
dest="dark_colors",
default=False,
help="Use dark colorscheme, better for white background terminals.",
)
parser.add_argument(
"--no-color",
"-n",
action="store_false",
dest="color",
default=True,
help="Disable color.",
)
parser.add_argument(
"--print-template",
action="store_true",
dest="print_template",
default=False,
help="Print customizable color template.",
)
parser.add_argument(
"--template",
action="store",
type=str,
dest="template",
default=None,
help="Use a custom template file for colorization.",
)
parser.add_argument(
"-z",
action="store_true",
dest="zero",
default=False,
help="Reset coloring at the end of each line.",
)
parser.add_argument(
"-Z",
action="store_false",
dest="zero_final",
default=True,
help="Disable reset of colors at the end of file.",
)
parser.add_argument(
"filename", type=str, nargs="?", help="File to show, - for stdin"
)
opts = parser.parse_args()
return opts
def parse(data):
data = [[None, row] for row in data]
block = "text"
new_block = "text"
multiline_block = False
# Parse styles
for i, line in enumerate(data):
row = line[1]
if line[0] is not None:
# Previous lines have set the style already
continue
for match in blocks:
if block_match[match]["re"].match(row):
new_block = match
if match.startswith("multiline"):
if multiline_block:
multiline_block = False
else:
multiline_block = match
break
if multiline_block:
new_block = multiline_block
# Lists must end with empty line
if new_block not in ("empty", "list_bullet") and block.startswith("list_"):
new_block = "list_loose"
if "mod" in block_match[match]:
# Style sets block in previous or next lines
data[i + block_match[match]["mod"]["pos"]][0] = block_match[match]["mod"][
"name"
]
data[i][0] = new_block
if new_block != block:
block = new_block
return data
def colorize(data, remove_colors=False, dark_colors=False, debug=False):
# Start inserting colors, and printing
bc = ansi.code()
colorized = []
cs = "dc" if dark_colors else "bc"
csc = cs + "c"
for i, line in enumerate(data):
row = line[1]
block = line[0]
multiline_block = block.startswith("multiline")
if multiline_block:
row = block_match[block][csc] + row
if block_match[block][cs]:
row = block_match[block]["re"].sub(block_match[block][cs], row)
# No coloring inside block_code, nor multiline
if not (multiline_block or block == "block_code"):
for match in inlines:
if inline_match[match]["re"].search(row):
row = inline_match[match]["re"].sub(
inline_match[match][cs] + block_match[block][csc], row
)
if remove_colors:
colored = bc.nocolor_string(row)
else:
colored = bc.color_string(row)
if debug:
multistr = "*" if multiline_block else " "
colored = "{:<18}{:}:".format(data[i][0], multistr) + colored
colorized.append(colored)
return colorized
def md_re_compile(d):
""" Returns a re.compiled dict """
n = {}
for t in d:
n[t] = {}
for i in d[t]:
n[t][i] = d[t][i]
try:
if n[t]["re"]:
n[t]["re"] = re.compile(n[t]["re"])
except err:
print("Error compiling: %s" % (n[t]["re"]))
sys.exit(1)
return n
def read_data2(fp):
data = []
# Read data
for row in f:
if not row:
continue
row = row.decode("utf-8").rstrip("\n\r ")
data.append(row)
return data
def read_data3(fp):
data = []
# Read data
for row in f:
if not row:
continue
row = row.rstrip("\n\r ")
data.append(row)
return data
def write_colored2(colored, opts):
for c in colored:
sys.stdout.write(c.encode("utf-8"))
if opts.zero:
sys.stdout.write(bc.Z)
sys.stdout.write("\n")
if opts.zero_final:
sys.stdout.write(bc.Z.encode("utf-8"))
def write_colored3(colored, opts):
for c in colored:
sys.stdout.write(c)
if opts.zero:
sys.stdout.write(bc.Z)
sys.stdout.write("\n")
if opts.zero_final:
sys.stdout.write(bc.Z)
# re: regular expression, bc: bright colors, bcc: continue with this color after inline
# dc: dark colors, dcc: continued color after inline
block_match_str = {
"block_code": {
"re": "^( {4}[^*])(.*)$",
"bc": "${Z}${c}\\1\\2",
"bcc": "${Z}${c}",
"dc": "${Z}${m}\\1\\2",
"dcc": "${Z}${m}",
}, # code
"multiline_code": {
"re": "^ *(`{3,}|~{3,}) *(\S*)",
"bc": "${Z}${c}\\1\\2",
"bcc": "${Z}${c}",
"dc": "${Z}${m}\\1\\2",
"dcc": "${Z}${m}",
}, # ```lang
"block_quote": {
"re": "^(>[ >]* )",
"bc": "${K}\\1${Z}",
"bcc": "${Z}",
"dc": "${Y}\\1${Z}",
"dcc": "${Z}",
}, # > > quote
"hrule": {
"re": "^ {0,3}[-*_]([-*_]){2,}$",
"bc": "False",
"bcc": "${Z}",
"dc": "False",
"dcc": "${Z}",
}, # ----
"heading": {
"re": "^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)",
"bc": "${W}\\1 ${U}\\2${Z}",
"bcc": "${W}${U}",
"dc": "${B}\\1 ${U}\\2${Z}",
"dcc": "${B}${U}",
}, # # heading
"lheading": {
"re": "^(=+|-+)$",
"bc": "${W}\\1",
"bcc": "${W}",
"dc": "${B}\\1",
"dcc": "${B}",
"mod": {"pos": -1, "name": "lheading.mod"},
}, # ======= under header
"lheading.mod": {
"re": "^([^\n]+)",
"bc": "${W}\\1",
"bcc": "${W}",
"dc": "${B}\\1",
"dcc": "${B}",
}, # over the ======= under header
"list_bullet": {
"re": "^( *)([*+-]|[\d\.]+)( +)",
"bc": "\\1${y}\\2${Z}\\3",
"bcc": "${Z}",
"dc": "\\1${r}\\2${Z}\\3",
"dcc": "${Z}",
}, # * or 1.
"list_loose": {
"re": "None",
"bc": "False",
"bcc": "${Z}",
"dc": "False",
"dcc": "${Z}",
},
"text": {
"re": "^([^\n]+)",
"bc": "${Z}\\1",
"bcc": "${Z}",
"dc": "${Z}\\1",
"dcc": "${Z}",
},
"empty": {
"re": "(^$)",
"bc": "${Z}\\1",
"bcc": "${Z}",
"dc": "${Z}\\1",
"dcc": "${Z}",
},
"preformatted": {
"re": "a^", # Never matches anything
"bc": "",
"bcc": "",
"dc": "",
"dcc": "",
},
}
block_match = md_re_compile(block_match_str)
blocks = [
"block_quote",
"multiline_code",
"hrule",
"heading",
"lheading",
"list_bullet",
"block_code",
"text",
"empty",
]
inline_match_str = {
"bold1": {
"re": r"(^| |})(_[^_]+_)",
"bc": "\\1${W}\\2",
"dc": "\\1${W}\\2",
}, # _bold_
"bold2": {
"re": r"(^| |})(\*{1,2}[^\*]+\*{1,2})",
"bc": "\\1${W}\\2",
"dc": "\\1${W}\\2",
}, # **bold**
"code": {"re": r"([`]+[^`]+[`]+)", "bc": "${c}\\1", "dc": "${m}\\1"}, # `code`
"code_special": {
"re": r"([`]+[^`]+[`]+)([!>])",
"bc": "${c}\\1${g}\\2",
"dc": "${m}\\1${r}\\2",
}, # `code`! or `code`> for markslider
"link": {
"re": r"(\[)([^\]]+)(\])\(([^\)]+)\)",
"bc": "${B}\\1${Z}\\2${B}\\3(${U}\\4${u})",
"dc": "${b}\\1${Z}\\2${b}\\3(${U}\\4${u})",
}, # [text](link)
"image": {
"re": r"(!\[[^\]]+\]\([^\)]+\))",
"bc": "${r}\\1",
"dc": "${g}\\1",
}, # ![text](image)
"underline": {
"re": r"(^|\W)(__)([^_]+)(__)",
"bc": "\\1\\2${U}\\3${Z}\\4",
"dc": "\\1\\2${U}\\3${Z}\\4",
}, # __underline__
"strikethrough": {
"re": r"(~~)([^~]+)(~~)",
"bc": "\\1${st}\\2${so}\\3",
"dc": "\\1${st}\\2${so}\\3",
}, # ~~strike~~
"checkbox": {"re": r"(\[[x ]\])", "bc": "${y}\\1", "dc": "${r}\\1"}, # [x] [ ]
}
inline_match = md_re_compile(inline_match_str)
inlines = [
"bold1",
"bold2",
"code_special",
"code",
"image",
"link",
"underline",
"strikethrough",
"checkbox",
]
if __name__ == "__main__":
opts = setup_options()
if opts.print_template:
import json
print(
json.dumps(
{
"about": 're: regular expression, bc: bright colors, bcc: continue with this color after inline, dc: dark colors, dcc: continued color after inline. "blocks" and "inlines" list keys of matchers. ',
"blocks": blocks,
"block_match": block_match_str,
"inline_match": inline_match_str,
"inlines": inlines,
},
indent=2,
sort_keys=True,
)
)
sys.exit(0)
if opts.template:
import json
template = json.load(open(opts.template, "r"))
if "inlines" in template:
inlines = template["inlines"]
if "blocks" in template:
blocks = template["blocks"]
if "block_match" in template:
block_match_str = template["block_match"]
block_match = md_re_compile(block_match_str)
if "inline_match" in template:
inline_match_str = template["inline_match"]
inline_match = md_re_compile(inline_match_str)
bc = ansi.code()
if opts.filename == "-" or opts.filename == None:
f = sys.stdin
else:
f = open(opts.filename, "r")
if sys.version_info > (3, 0):
data = read_data3(f)
else:
data = read_data2(f)
data = parse(data)
colored = colorize(data, not opts.color, opts.dark_colors, opts.debug)
if sys.version_info > (3, 0):
write_colored3(colored, opts)
else:
write_colored2(colored, opts)

View File

@@ -0,0 +1,23 @@
from distutils.core import setup
def version_reader(path):
for line in open(path, "rt").read(1024).split("\n"):
if line.startswith("__version__"):
return line.split("=")[1].strip().replace('"', "")
version = version_reader(os.path.join("markslider", "markslider.py"))
setup(
name="markslider",
packages=["markslider"],
version=version,
description="View markdown files as slides in terminal",
author="Ville Rantanen",
author_email="ville.q.rantanen@gmail.com",
keywords=["Markdown", "Slideshow"],
entry_points = {'console_scripts': 'markslider = markslider.markslider:main'},
install_requires = [
'pygments'
]
)