From 1ef6dad7c7b48ff7f6b1ae63c1a11a08eceefb25 Mon Sep 17 00:00:00 2001 From: ville rantanen Date: Mon, 22 Jun 2015 10:26:35 +0300 Subject: [PATCH] markslider toc --- reporting/markslider.py | 49 +++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/reporting/markslider.py b/reporting/markslider.py index a0ebd43..5f91e3e 100755 --- a/reporting/markslider.py +++ b/reporting/markslider.py @@ -111,7 +111,7 @@ class slide_reader: for row in f: if not row: continue - row=row.decode('utf-8').rstrip("\n\r") + row=row.decode('utf-8').rstrip("\n\r ") if row.startswith("#") and not row.startswith("##"): if len(new_page)>0: self.data.append(new_page) @@ -119,9 +119,10 @@ class slide_reader: new_page.append(row) if len(new_page)>0: self.data.append(new_page) + self.toc() self.pages=len(self.data) self.inc_page_no(0) - self.toc() + def get_data(self): return self.data @@ -162,16 +163,25 @@ class slide_reader: def toc(self): if self.opts.toc: TOC=["# "+self.opts.toc,""] - for h1,page in enumerate(self.data): + for h1,page in enumerate(self.data[(self.opts.toc_page-1):]): title=page[0].strip("# ") TOC.append("%d. %s"%(h1+1,title)) - h2=0 - for line in page: - if re.search("^##[^#]", line): - h2+=1 + subh=[0,0,0] + if self.opts.toc_depth>1: + for line in page: title=line.strip("# ") - TOC.append(" %d.%d. %s"%(h1+1,h2,title)) - self.data.insert(1,TOC) + 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) @@ -196,22 +206,27 @@ def setup_options(): formatter_class=argparse.RawDescriptionHelpFormatter, epilog=__author__) - parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True, - help="Disable color.") + parser.add_argument("-v","--version",action="version",version=__version__) - parser.add_argument("-m",action="store_true",dest="autocolor",default=False, - help="Color by markdown structure.") + parser.add_argument("--dc",action="store_true",dest="dark_colors",default=False, help="Use dark colorscheme, better for white background terminals.") - parser.add_argument("--toc",action="store_true",dest="toc",default=False, - help="Display TOC as 2nd slide.") + 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("-w",action="store_false",dest="wrap",default=True, help="Disable line wrapping. Cuts long lines.") - parser.add_argument("-t",action="store",dest="toc",default=False, + parser.add_argument("--toc",action="store",dest="toc",default=False, const="Table of contents", type=str, nargs='?', - help="Insert table of contents as page 2. Define the header, or use default: %(const)s") + 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()