#!/usr/bin/env python
# coding=utf-8
#
# Copyright 2016 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 .
#
'''Markslider: a slideshow engine based on markdown.'''
__author__ = "Ville Rantanen "
__version__ = "0.8"
import sys,os,argparse,re
from argparse import ArgumentParser
import traceback,tty,termios,subprocess,signal
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansi,md_color
HL=">"
EOS="# End of Slides"
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
M modify file with VIM
r reload the file
s toggle status bar
t toggle timer (reqs. --timer switch)
,/. scroll page
up/down move highlight
enter execute highlighted line
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("--exit",action="store_true",dest="exit_last",default=False,
help="Exit after last slide.")
parser.add_argument("--export",action="store",dest="screenshots",default=False,
type=str, help="Take screenshots of the slideshow in the given folder.")
parser.add_argument("-m",action="store_false",dest="autocolor",default=True,
help="Disable 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
if opts.screenshots:
opts.slideShow=True
opts.slideTimer=1
opts.exit_last=True
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="${b}${U}"
else:
coloring="${U}${Y}"
print(colorify(coloring+page[0],opts))
# Print page rows
parsed=md_color.parse(page)
if opts.autocolor:
colored=md_color.colorize(parsed,not opts.color,opts.dark_colors)
else:
if opts.color:
colored=[bc.color_string(row[1]) for row in parsed]
else:
colored=[bc.nocolor_string(row[1]) for row in parsed]
r=0
for row_i in range(len(page)):
if row_i==0: continue
if row_i=scrsize[0]-2:
break
r+=row_lines+1
sys.stdout.write("\n")
sys.stdout.flush()
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]))
sys.stdout.flush()
if opts.screenshots:
take_screenshot(reader,opts)
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:
if opts.exit_last:
if reader.page+1==reader.pages:
return
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
if opts.exit_last:
if reader.page+1==reader.pages:
return
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('M'):
modify_file(reader,offset)
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: # up
offset=offset_change(opts,reader,offset,(0, -1))
if inkey==66: # down
offset=offset_change(opts,reader,offset,(0, 1))
if inkey==13: # enter
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)
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 
"""
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 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
def modify_file(reader,offset):
row=1
for page in range(reader.page):
row+=len(reader.data[page])
subprocess.call("vim +%d -c 'exe \"normal! zt\"' -c %d %s"%(row,row+offset[1],reader.filename),
shell=True)
def take_screenshot(reader,opts):
out_file=os.path.join(opts.screenshots,"slide%03d.png"%(reader.page+1))
if not os.path.exists(opts.screenshots):
os.mkdir(opts.screenshots)
subprocess.call("sleep 0.5; import -window $WINDOWID '%s'"%(out_file,),
stdout=subprocess.PIPE,stderr=subprocess.PIPE,
shell=True)
bc=ansi.code()
getch=getch()
opts=setup_options()
browser(opts,opts.filename)