markslider python3 upgrades
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
'''Markslider: a slideshow engine based on markdown.'''
|
||||
|
||||
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
|
||||
__version__ = "0.9"
|
||||
__version__ = "1.0"
|
||||
|
||||
import sys,os,argparse,re,datetime
|
||||
from argparse import ArgumentParser
|
||||
@@ -45,25 +45,26 @@ class getch:
|
||||
|
||||
class EndProgram( Exception ):
|
||||
''' Nice exit '''
|
||||
print('')
|
||||
pass
|
||||
|
||||
class slide_reader:
|
||||
""" Class for reading files. """
|
||||
def __init__(self,files,opts):
|
||||
self.filename=files[0]
|
||||
self.files=files
|
||||
self.reader=None
|
||||
self.opts=opts
|
||||
self.pages=0
|
||||
self.page=0
|
||||
self.file_start_page=[]
|
||||
self.width=None
|
||||
self.height=None
|
||||
self.max_width=None
|
||||
self.max_height=None
|
||||
self.data=[]
|
||||
self.re_image_convert=re.compile("(.*)(!\[.*\])\((.*)\)>")
|
||||
self.re_command=re.compile("(.*)`(.*)`>(.*)")
|
||||
self.filename = files[0]
|
||||
self.files = files
|
||||
self.reader = None
|
||||
self.opts = opts
|
||||
self.pages = 0
|
||||
self.page = 0
|
||||
self.file_start_page = []
|
||||
self.width = None
|
||||
self.height = None
|
||||
self.max_width = None
|
||||
self.max_height = None
|
||||
self.data = []
|
||||
self.re_image_convert = re.compile("(.*)(!\[.*\])\((.*)\)>")
|
||||
self.re_command = re.compile("(.*)`(.*)`>(.*)")
|
||||
#~ 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()
|
||||
@@ -71,10 +72,10 @@ class slide_reader:
|
||||
def read(self):
|
||||
''' Read a file, set pages and data '''
|
||||
|
||||
self.pages=0
|
||||
self.data=[]
|
||||
self.file_start_page=[]
|
||||
first_slide_found=False
|
||||
self.pages = 0
|
||||
self.data = []
|
||||
self.file_start_page = []
|
||||
first_slide_found = False
|
||||
for fname in self.files:
|
||||
first_slide_found=False
|
||||
f=open(fname,'r')
|
||||
@@ -82,9 +83,9 @@ class slide_reader:
|
||||
for row in f:
|
||||
if not row:
|
||||
continue
|
||||
row=row.decode('utf-8').rstrip("\n\r ")
|
||||
row=row.rstrip("\n\r ")
|
||||
# find end of show
|
||||
if row==EOS:
|
||||
if row == EOS:
|
||||
break
|
||||
# find header to start a new page
|
||||
if row.startswith("#") and not row.startswith("##"):
|
||||
@@ -201,7 +202,7 @@ class slide_reader:
|
||||
command=self.re_command.match(s)
|
||||
if command !=None:
|
||||
return self.launch(command)
|
||||
image=self.re_image_convert.match(s)
|
||||
image = self.re_image_convert.match(s)
|
||||
if image != None:
|
||||
return self.convert_image(image)
|
||||
return [s]
|
||||
@@ -210,13 +211,13 @@ class slide_reader:
|
||||
""" 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=self.re_command.match(s)
|
||||
#~ if command != None:
|
||||
output = subprocess.check_output(command.group(2).strip(),shell=True).split("\n")
|
||||
output = subprocess.check_output(
|
||||
command.group(2).strip(),
|
||||
shell = True
|
||||
)
|
||||
if type(output) == bytes:
|
||||
output = output.decode('utf-8')
|
||||
output = output.split("\n")
|
||||
while len(output[0].strip())==0:
|
||||
if len(output)==1: return [""]
|
||||
del output[0]
|
||||
@@ -235,7 +236,13 @@ class slide_reader:
|
||||
"""
|
||||
#~ 2=title
|
||||
#~ 3=image command
|
||||
output = subprocess.check_output("convert %s JPEG:- | jp2a --colors --width=70 -"%image.group(3),shell=True).split("\n")
|
||||
output = subprocess.check_output(
|
||||
"convert %s JPEG:- | jp2a --colors --width=70 -"%( image.group(3),),
|
||||
shell=True
|
||||
)
|
||||
if type(output) == bytes:
|
||||
output = output.decode('utf-8')
|
||||
output = output.split("\n")
|
||||
while len(output[0].strip())==0:
|
||||
if len(output)==1: return [""]
|
||||
del output[0]
|
||||
@@ -273,7 +280,7 @@ Special syntaxes:
|
||||
* 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!
|
||||
* Convert images to ASCII, with jp2a: 
|
||||
* Convert images to ASCII, with jp2a: >
|
||||
|
||||
Keyboard shortcuts:
|
||||
'''+get_interactive_help_text()
|
||||
@@ -333,55 +340,67 @@ Keyboard shortcuts:
|
||||
def page_print(reader,opts,offset):
|
||||
''' Print a page '''
|
||||
|
||||
page=reader.get_current_page()
|
||||
scrsize=opts.size
|
||||
page = reader.get_current_page()
|
||||
scrsize = opts.size
|
||||
# clear page
|
||||
bc.clear()
|
||||
if opts.center: # Placeholder for 80x25 center alignment
|
||||
align_width=reader.get_max_width()
|
||||
align_x_offset=scrsize[1]/2-align_width/2
|
||||
align_pad=" "*align_x_offset
|
||||
align_y_offset=scrsize[0]/2-reader.get_max_height()/2
|
||||
align_width = reader.get_max_width()
|
||||
align_x_offset = scrsize[1]/2-align_width/2
|
||||
align_pad = " "*align_x_offset
|
||||
align_y_offset = scrsize[0]/2 - reader.get_max_height()/2
|
||||
bc.down_line(align_y_offset)
|
||||
else:
|
||||
align_pad=""
|
||||
align_pad = ""
|
||||
# Print header
|
||||
if opts.dark_colors:
|
||||
coloring="${b}${U}"
|
||||
coloring = "${b}${U}"
|
||||
else:
|
||||
coloring="${U}${Y}"
|
||||
print(align_pad+colorify(coloring+page[0],opts)+bc.Z)
|
||||
|
||||
coloring = "${U}${Y}"
|
||||
print(
|
||||
align_pad +
|
||||
colorify(
|
||||
coloring + page[0],
|
||||
opts) +
|
||||
bc.Z
|
||||
)
|
||||
if (sys.version_info < (3, 0)):
|
||||
# python2 magic
|
||||
page = [row.decode('utf-8') for row in page]
|
||||
# Print page rows
|
||||
if not opts.wrap:
|
||||
page=[cut_line(row,scrsize[1]-1) for row in page]
|
||||
parsed=md_color.parse(page)
|
||||
page = [cut_line(row,scrsize[1]-1) for row in page]
|
||||
parsed = md_color.parse(page)
|
||||
if opts.autocolor:
|
||||
colored=md_color.colorize(parsed,not opts.color,opts.dark_colors)
|
||||
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
|
||||
r = 0
|
||||
for row_i in range(len(page)):
|
||||
if row_i==0: continue
|
||||
if row_i<offset[0]: continue
|
||||
row=page[row_i]
|
||||
if row_i == 0: continue
|
||||
if row_i < offset[0]: continue
|
||||
row = page[row_i]
|
||||
#page[1+offset[0]:]:
|
||||
if opts.wrap:
|
||||
row_lines=int(float(len(row))/scrsize[1])
|
||||
row_lines = int(float(len(row))/scrsize[1])
|
||||
else:
|
||||
row_lines=0
|
||||
row_lines=0
|
||||
colored_row=colored[row_i]#=colorify(row,opts)
|
||||
if offset[1]==r+1+offset[0]:
|
||||
colored_row=add_highlight(row,opts)
|
||||
sys.stdout.write(align_pad+colored_row)
|
||||
row_lines = 0
|
||||
row_lines = 0
|
||||
colored_row = colored[row_i]#=colorify(row,opts)
|
||||
if offset[1] == r + 1 + offset[0]:
|
||||
colored_row = add_highlight(row,opts)
|
||||
sys.stdout.write(align_pad + colored_row)
|
||||
|
||||
if r>=scrsize[0]-2:
|
||||
if r >= scrsize[0] - 2:
|
||||
break
|
||||
r+=row_lines+1
|
||||
r += row_lines + 1
|
||||
sys.stdout.write("\n")
|
||||
sys.stdout.flush()
|
||||
return
|
||||
@@ -398,8 +417,17 @@ def print_menu(reader,opts):
|
||||
|
||||
def print_time(opts):
|
||||
now=datetime.datetime.now()
|
||||
bc.posprint( opts.size[0], opts.size[1]-5,
|
||||
colorify("%02d:%02d"%(now.hour,now.minute),opts))
|
||||
bc.posprint(
|
||||
opts.size[0],
|
||||
opts.size[1]-5,
|
||||
colorify(
|
||||
"%02d:%02d"%(
|
||||
now.hour,
|
||||
now.minute
|
||||
),
|
||||
opts
|
||||
)
|
||||
)
|
||||
|
||||
def print_help(reader,opts):
|
||||
''' Create a window with help message '''
|
||||
@@ -588,24 +616,35 @@ def launch(reader,opts,offset):
|
||||
Detects URLS and markdown images 
|
||||
"""
|
||||
if not opts.execute:
|
||||
bc.posprint(offset[1]-offset[0]+1,0,"Execution not enabled!")
|
||||
inkey=getch.get()
|
||||
bc.posprint(
|
||||
offset[1]-offset[0]+1,
|
||||
0,
|
||||
"Execution not enabled!"
|
||||
)
|
||||
inkey = getch.get()
|
||||
return
|
||||
s=reader.get_current_page()[offset[1]]
|
||||
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:
|
||||
# sanity check
|
||||
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")
|
||||
output = subprocess.check_output(
|
||||
show_command.group(2).strip(),
|
||||
shell = True
|
||||
)
|
||||
if type(output) == bytes:
|
||||
output = output.decode('utf-8')
|
||||
output = output.split("\n")
|
||||
while len(output[0].strip())==0:
|
||||
if len(output)==1: return [""]
|
||||
if len(output)==1: return
|
||||
del output[0]
|
||||
while len(output[-1].strip())==0:
|
||||
if len(output)==1: return [""]
|
||||
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))
|
||||
@@ -614,22 +653,37 @@ def launch(reader,opts,offset):
|
||||
inkey=getch.get()
|
||||
return
|
||||
if run_command != None:
|
||||
subprocess.call(run_command.group(2),
|
||||
shell=True,executable="/bin/bash")
|
||||
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)
|
||||
subprocess.call(
|
||||
"%s '%s' &"%(
|
||||
get_open_command(),
|
||||
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)
|
||||
subprocess.call(
|
||||
"%s '%s' &"%(
|
||||
get_open_command(),
|
||||
image,
|
||||
),
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
shell = True
|
||||
)
|
||||
return
|
||||
return
|
||||
|
||||
@@ -642,16 +696,32 @@ def modify_file(reader,offset):
|
||||
row+=len(reader.data[page])
|
||||
if (page+1) in row_restarts:
|
||||
row=1
|
||||
subprocess.call("vim +%d -c 'exe \"normal! zt\"' -c %d %s"%(row,row+offset[1],reader.get_current_filename()),
|
||||
shell=True)
|
||||
subprocess.call(
|
||||
"vim +%d -c 'exe \"normal! zt\"' -c %d %s"%(
|
||||
row,
|
||||
row + offset[1],
|
||||
reader.get_current_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)
|
||||
subprocess.call(
|
||||
"sleep 0.5; import -window $WINDOWID '%s'"%(out_file,),
|
||||
stdout = subprocess.PIPE,
|
||||
stderr = subprocess.PIPE,
|
||||
shell = True
|
||||
)
|
||||
|
||||
def get_open_command():
|
||||
if sys.platform.startswith("darwin"):
|
||||
return "open"
|
||||
else:
|
||||
return "xdg-open"
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user