Files
q-tools/reporting/markslider.py
2016-03-02 10:40:51 +02:00

578 lines
20 KiB
Python
Executable File

#!/usr/bin/env python
# coding=utf-8
#
# Copyright 2015 Ville Rantanen
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
'''Markslider: a slideshow engine based on markdown.'''
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.6"
import sys,os,argparse,re
from argparse import ArgumentParser
import traceback,tty,termios,subprocess,signal
HL=">"
EOS="# End of Slides"
class bc:
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[30m"
r="\033[31m"
g="\033[32m"
b="\033[34m"
y="\033[33m"
m="\033[35m"
c="\033[36m"
w="\033[37m"
S = '\033[1m'
U = '\033[4m'
Z = '\033[0m'
CLR = '\033[2J'
CLREND = '\033[K'
CLRBEG = '\033[1K'
color_keys="KRGBYMCWkrgbymcwSUZ"
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,U,Z]
def pos(self,y,x):
return "\033["+str(y)+";"+str(x)+"H"
def column(self,x):
return "\033["+str(x)+"G"
def posprint(self, y,x,s):
sys.stdout.write( self.pos(y,x) + str(s) )
def clear(self):
sys.stdout.write( self.CLR+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):
sys.stdout.write( "\033[s" )
def restore(self):
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 s
def nocolor_string(self,s):
for i,c in enumerate(self.color_keys):
s=s.replace("${"+c+"}","")
return s
class getch:
def get(self):
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class EndProgram( Exception ):
''' Nice exit '''
pass
class slide_reader:
""" Class for reading files. """
def __init__(self,filename,opts):
self.filename=filename
self.reader=None
self.opts=opts
self.pages=0
self.page=0
self.width=None
self.height=None
self.data=[]
#~ self.control_chars = ''.join(map(unichr, range(0,32) + range(127,160)))
#~ self.control_char_re = re.compile('[%s]' % re.escape(self.control_chars))
self.read()
def read(self):
''' Read a file, set pages and data '''
f=open(self.filename,'r')
self.pages=0
self.data=[]
new_page=[]
first_slide_found=False
for row in f:
if not row:
continue
row=row.decode('utf-8').rstrip("\n\r ")
# find end of show
if row==EOS:
break
# find header to start a new page
if row.startswith("#") and not row.startswith("##"):
first_slide_found=True
if len(new_page)>0:
self.data.append(new_page)
new_page=[]
# if first slide havent been found yet:
if not first_slide_found:
continue
new_page.extend(self.launch(row))
if len(new_page)>0:
self.data.append(new_page)
self.toc()
self.pages=len(self.data)
self.inc_page_no(0)
def get_data(self):
return self.data
def get_filename(self):
return self.filename
def get_page(self, page):
try:
return self.data[page]
except IndexError:
return None
def get_pages(self):
return self.pages
def get_current_page(self):
return self.data[self.page]
def get_page_no(self):
return self.page
def inc_page_no(self,inc=1,loop=False):
self.page+=inc
if self.page<0:
self.page=0
if loop:
self.page=self.pages-1
if self.page>=self.pages:
self.page=self.pages-1
if loop:
self.page=0
self.width=max([len(x) for x in self.data[self.page]])
self.height=len(self.data[self.page])
def last_page(self):
self.page=self.pages-1
def first_page(self):
self.page=0
def get_page_height(self):
return self.height
def get_page_width(self):
return self.width
def toc(self):
if self.opts.toc:
TOC=["# "+self.opts.toc,""]
for h1,page in enumerate(self.data[(self.opts.toc_page-1):]):
title=page[0].strip("# ")
TOC.append("%d. %s"%(h1+1,title))
subh=[0,0,0]
if self.opts.toc_depth>1:
for line in page:
title=line.strip("# ")
if re.search("^##[^#]", line):
subh=[ subh[0]+1, 0, 0 ]
TOC.append(" %d.%d. %s"%(h1+1,subh[0],title))
if self.opts.toc_depth==2: continue
if re.search("^###[^#]", line):
subh=[ subh[0], subh[1]+1, 0 ]
TOC.append(" %d.%d.%d. %s"%(h1+1,subh[0],subh[1],title))
if self.opts.toc_depth==3: continue
if re.search("^####[^#]", line):
subh=[ subh[0], subh[1], subh[2]+1 ]
TOC.append(" %d.%d.%d.%d. %s"%(h1+1,subh[0],subh[1],subh[2],title))
self.data.insert(self.opts.toc_page-1,TOC)
def launch(self,s):
""" Launch in a string using tags $!command$!
Remove empty lines from beginning and end of stdout.
"""
if not self.opts.execute_read:
return [s]
if s.find("$>")==-1:
return [s]
command=re.match("(.*)\$>(.*)\$>(.*)",s)
if command != None:
output = subprocess.check_output(command.group(2).strip(),shell=True).split("\n")
while len(output[0].strip())==0:
if len(output)==1: return [""]
del output[0]
while len(output[-1].strip())==0:
if len(output)==1: return [""]
del output[-1]
return_value=[command.group(1)]
return_value.extend(output)
return_value.append(command.group(3))
return return_value
return [s]
def get_interactive_help_text():
return ''' left/right,page up/down,home,end
change page
q exit browser
r reload the file
s toggle status bar
t toggle timer (reqs. --timer switch)
,/. scroll page
up/down move highlight
h help'''
def setup_options():
''' Create command line options '''
usage='''
MarkSlider: a markdown based slideshow engine
Special syntaxes:
* Colors: insert string ${C}, where C is one of KRGBYMCWkrgbymcwSUZ
* Text before first "# header" is not shown
* Text after a "# End of Slides" is not shown
* Execute shell: "$! command -switch $!" Beware of malicious code!
* Execute and print output: "$> command $>" Beware of malicious code!
Keyboard shortcuts:
'''+get_interactive_help_text()
parser=ArgumentParser(description=usage,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__author__)
parser.add_argument("-v","--version",action="version",version=__version__)
parser.add_argument("--dc",action="store_true",dest="dark_colors",default=False,
help="Use dark colorscheme, better for white background terminals.")
parser.add_argument("-e",action="store_true",dest="execute",default=False,
help="Execute commands in $! or $> tags at show time with Enter key. WARNING: Potentially very dangerous to run others' slides with this switch!")
parser.add_argument("-E",action="store_true",dest="execute_read",default=False,
help="Execute commands in $> tags at file read time. WARNING: Potentially very dangerous to run others' slides with this switch!")
parser.add_argument("-m",action="store_true",dest="autocolor",default=False,
help="Color by markdown structure.")
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
help="Disable color.")
parser.add_argument("-s",action="store_false",dest="menu",default=True,
help="Disable status bar.")
parser.add_argument("--timer",action="store",dest="slideTimer",default=False, type=int,
help="Timer for slideshow. If set, starts automatic slide changing.")
parser.add_argument("-w",action="store_false",dest="wrap",default=True,
help="Disable line wrapping. Cuts long lines.")
parser.add_argument("--toc",action="store",dest="toc",default=False,
const="Table of Contents", type=str, nargs='?',
help="Insert table of contents. Define the header, or use default: %(const)s")
parser.add_argument("--toc-page",action="store",dest="toc_page",default=2, type=int,
help="Insert table of contents on a chosen page. default: %(const)s")
parser.add_argument("--toc-depth",action="store",dest="toc_depth",default=2, type=int,
choices=xrange(1,5),
help="Table of contents display depth. default: %(const)s")
parser.add_argument("filename",type=str,
help="File to show")
opts=parser.parse_args()
opts.slideShow=not not opts.slideTimer
return opts
def page_print(reader,opts,offset):
''' Print a page '''
page=reader.get_current_page()
scrsize=opts.size
# clear page
bc.clear()
# Print header
if opts.dark_colors:
coloring="${U}${b}"
else:
coloring="${U}${Y}"
print(colorify(coloring+page[0],opts))
# Print page rows
r=0
for row in page[1+offset[0]:]:
if not opts.wrap:
row=cut_line(row,scrsize[1]-1)
row_lines=0
else:
row_lines=int(float(len(row))/scrsize[1])
colored=colorify(row,opts)
if offset[1]==r+1+offset[0]:
colored=add_highlight(row,opts)
sys.stdout.write(colored)
if r>=scrsize[0]-2:
break
r+=row_lines+1
sys.stdout.write("\n")
return
def menu_print(reader,opts):
bc.posprint( opts.size[0], 0,
colorify("${y}%d${Z}/%d %s|%s"%(
1+reader.get_page_no(),
reader.get_pages(),
reader.get_filename(),
"slideshow" if opts.slideShow else ""),
opts))
def print_help(reader,opts):
''' Create a window with help message '''
helptext=get_interactive_help_text().split('\n')
maxlen=max([len(x) for x in helptext])
bc.posprint(3,6,"+"+"-"*maxlen+"+")
bc.posprint(4,6,colorify("|${U}${Y}"+("{:^"+str(maxlen)+"}").format("Help")+"${Z}|",opts))
for y,row in enumerate(helptext):
bc.posprint(5+y,6,("|{:<"+str(maxlen)+"}|").format(row))
bc.posprint(6+y,6,"+"+"-"*maxlen+"+")
sys.stdout.write(bc.pos(opts.size[0], opts.size[1]))
inkey=getch.get()
def offset_change(opts,reader,offset,new_offset):
''' Change the display position of page '''
new_offset=(offset[0]+new_offset[0], offset[1]+new_offset[1])
offsety=min(reader.get_page_height()-1,new_offset[0])
offseth=min(reader.get_page_height(),new_offset[1])
return [max(0,o) for o in (offsety,offseth)]
def timeouthandler(sig,frame):
#~ print(sig,frame)
raise IOError("Input timeout")
def getkeypress():
try:
return ord(getch.get())
except:
return False
def browser(opts,filename):
''' Main function for printing '''
try:
reader=slide_reader(filename,opts)
except:
print "Error in reading the file:"
for o in sys.exc_info():
print(o)
sys.exit(1)
offset=(0,0)
try:
while 1:
opts.size=get_console_size()
page_print(reader,opts,offset)
if opts.menu:
menu_print(reader,opts)
sys.stdout.write(bc.pos(opts.size[0], opts.size[1]))
while 1:
if opts.slideTimer and opts.slideShow:
signal.signal(signal.SIGALRM, timeouthandler)
signal.alarm(opts.slideTimer)
inkey=getkeypress()
signal.alarm(0)
if not inkey and opts.slideShow:
reader.inc_page_no(1,True)
offset=(0,0)
break
#~ print(inkey)
if inkey in [113,3,120]:
#print('Exited in: ')
return
if inkey in [67,54,32]: # PGDN or space
reader.inc_page_no(1)
offset=(0, 0)
if inkey in [68,53,127]:
reader.inc_page_no(-1)
offset=(0, 0)
if inkey==72 or inkey==49: # HOME
reader.first_page()
offset=(0, 0)
if inkey==70 or inkey==52: # END
reader.last_page()
offset=(0, 0)
if inkey==ord('h'):
print_help(reader,opts)
if inkey==ord('s'):
opts.menu=not opts.menu
if inkey==ord('t'):
opts.slideShow=not opts.slideShow
if inkey==ord('r'):
reader.read()
offset=offset_change(opts,reader,offset,(0, 0))
if inkey==ord(','):
offset=offset_change(opts,reader,offset,(-1, 0))
if inkey==ord('.'):
offset=offset_change(opts,reader,offset,(1, 0))
if inkey==65:
offset=offset_change(opts,reader,offset,(0, -1))
if inkey==66:
offset=offset_change(opts,reader,offset,(0, 1))
if inkey==13:
launch(reader,opts,offset)
break
except IOError:
pass
except KeyboardInterrupt:
sys.exit(0)
except EndProgram:
pass
except:
print "Unexpected error:"
print traceback.format_exc()
sys.exit(1)
def get_console_size():
rows, columns = os.popen('stty size', 'r').read().split()
return (int(rows),int(columns))
def colorify(s,opts):
""" Add colors to string """
if not opts.color:
return bc.nocolor_string(s)
if opts.autocolor and not s.startswith("${"):
rules=["(^\s*\*.*)", # * bullets
"(^\s*[0-9]+\..*)", # 1. ordered
"(^#.*)", ## Header
"(^\s\s\s\s[^\*0-9\s].*)", # code block
"(\`[^\s]*[^\`]+\`)", # code inline
"(\$[>!].*\$[>!])", # code tags
"(\[[^]]+\]\([^\)]+\))", # [link](url)
"(\*{1,2}[^\s]*[^\*\s]+\*{1,2})", # *bold*
"(_[^\s]*[^_\s]+_)", # _bold_
"(<[^>]+>)"] # <Tags>
if opts.dark_colors:
colors=["${r}\\1", # * bullets
"${r}\\1", # 1. ordered
"${U}${b}\\1", ## Header
"${m}\\1", # code block
"${m}\\1${Z}", # code inline
"${m}\\1${Z}", # code tags
"${B}${U}\\1${Z}", # [link](url)
"${W}\\1${Z}", # *bold*
"${W}\\1${Z}", # _bold_
"${K}\\1${Z}"] # <Tags>
else:
colors=["${y}\\1", # * bullets
"${y}\\1", # 1. ordered
"${U}${Y}\\1", ## Header
"${c}\\1", # code block
"${c}\\1${Z}", # code inline
"${c}\\1${Z}", # code tags
"${B}${U}\\1${Z}", # [link](url)
"${W}\\1${Z}", # *bold*
"${W}\\1${Z}", # _bold_
"${K}\\1${Z}"] # <Tags>
for r in zip(rules,colors):
s=re.sub(r[0],r[1],s)
# Replace executable commands with $ only in the beginning
s=re.sub("\$[>!]","$",s,1)
s=re.sub("\$[>!]","",s)
c=bc.color_string(s)+bc.Z
return c
def cut_line(s,i):
""" cut a color tagged string, and remove control chars """
s=s[:i]
s=re.sub("\$$","",
re.sub("\$\{$","",
re.sub("\$\{.$","",
s)))
return s
def add_highlight(s,opts):
""" Add cursor position highlight """
if len(s.strip())==0:
cleaned=HL
else:
cleaned=bc.nocolor_string(s)
tagged="${Y}"+cleaned
return colorify(tagged,opts)
def launch(reader,opts,offset):
""" Launch in a string using tags $!command$! or $>command$>
Remove empty lines from beginning and end of stdout in $> commands.
Detects URLS and markdown images ![Alt text](/path/to/img.jpg)
"""
if not opts.execute:
return
s=reader.get_current_page()[offset[1]]
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', s)
images = re.findall('!\[[^]]+\]\([^\)]+\)', s)
if s.find("$!")==-1 and s.find("$>")==-1 and len(urls)==0 and len(images)==0:
return
run_command=re.match("(.*)\$!(.*)\$!(.*)",s)
show_command=re.match("(.*)\$>(.*)\$>(.*)",s)
if show_command != None:
output = subprocess.check_output(show_command.group(2).strip(),shell=True).split("\n")
while len(output[0].strip())==0:
if len(output)==1: return [""]
del output[0]
while len(output[-1].strip())==0:
if len(output)==1: return [""]
del output[-1]
for y,l in enumerate(output):
bc.posprint(y+offset[1]-offset[0]+2,0,' '*len(l))
bc.clear_to_end()
bc.posprint(y+offset[1]-offset[0]+2,0,l)
inkey=getch.get()
return
if run_command != None:
subprocess.call(run_command.group(2),
shell=True,executable="/bin/bash")
inkey=getch.get()
return
# Open URLS last
if len(urls)>0:
# Remove ) at the end of url: [name](link) markdown syntax
subprocess.call("xdg-open '%s' &"%(urls[0].rstrip(")"),),
stdout=subprocess.PIPE,stderr=subprocess.PIPE,
shell=True)
return
if len(images)>0:
image = re.sub('.*\(([^\)]+)\).*', "\\1",images[0])
subprocess.call("xdg-open '%s' &"%(image,),
stdout=subprocess.PIPE,stderr=subprocess.PIPE,
shell=True)
return
return
bc=bc()
getch=getch()
opts=setup_options()
browser(opts,opts.filename)