planning for image converter

This commit is contained in:
q
2016-06-18 23:47:35 +03:00
parent 1f8cfc5a12
commit 7a63b46cab

View File

@@ -58,6 +58,8 @@ class slide_reader:
self.width=None
self.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()
@@ -86,7 +88,7 @@ class slide_reader:
# if first slide havent been found yet:
if not first_slide_found:
continue
new_page.extend(self.launch(row))
new_page.extend(self.generate_content(row))
if len(new_page)>0:
self.data.append(new_page)
@@ -158,28 +160,60 @@ class slide_reader:
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):
def generate_content(self,s):
""" Check for launchable items, or converted images """
if self.opts.execute_read:
if s.find("`>")>-1:
command=self.re_command.match(s)
if command !=None:
return self.launch(command)
image=self.re_image_convert.match(s)
if image != None:
return self.convert_image(image)
return [s]
def launch(self,command):
""" 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]
#~ 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")
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 convert_image(self,image):
""" comnvert image using tags ![]()>
Remove empty lines from beginning and end of stdout.
"""
#~ 2=title
#~ 3=image command
output = subprocess.check_output("convert %s JPEG:- | jp2a --colors --width=70 -"%image.group(3),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=[image.group(1)]
return_value.extend(output)
#~ return_value.append(image.group(4))
return return_value
# return [s]
def get_interactive_help_text():
return ''' left/right,page up/down,home,end
@@ -202,8 +236,9 @@ 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!
* Execute shell: ` command -switch `! Beware of malicious code!
* Execute and print output: ` command `> Beware of malicious code!
* Convert images to ASCII, with jp2a: ![](file.jpg)
Keyboard shortcuts:
'''+get_interactive_help_text()
@@ -211,35 +246,43 @@ Keyboard shortcuts:
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,
content = parser.add_argument_group('content')
execution = parser.add_argument_group('execution')
control = parser.add_argument_group('controls')
content.add_argument("--dc",action="store_true",dest="dark_colors",default=False,
help="Use dark colorscheme, better for white background terminals.")
content.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,
content.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,
execution.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!")
execution.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!")
control.add_argument("--exit",action="store_true",dest="exit_last",default=False,
help="Exit after last slide.")
control.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,
control.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,
content.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,
content.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,
content.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,
content.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,