From 0df01800cfaf4cc2566f540619b23ec176bee79a Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Wed, 26 Dec 2012 13:21:40 +0100 Subject: [PATCH] Fixed a bug where directories with unknown type would show up in the menu --- pylog.py | 18 +++++- pylog_action_impl.py | 150 ++++++++++++++++++++++--------------------- pylog_config.py | 2 +- 3 files changed, 93 insertions(+), 77 deletions(-) diff --git a/pylog.py b/pylog.py index b9f814f..7cf89ca 100755 --- a/pylog.py +++ b/pylog.py @@ -15,14 +15,18 @@ import comment_section __version = "v0.1-(2012-03-15)" -def dir_entry(f,path,depth,hidden): +def get_dir_name_type(f): if '_' in os.path.basename(f): typ = f[f.rindex('_')+1:] name = os.path.basename(f)[:-(len(typ)+1)] else: typ = '' - name = os.path.basename(f) - + name = os.path.basename(f) + + return (name,typ) + +def dir_entry(f,path,depth,hidden): + name,typ = get_dir_name_type(f) if typ in dir_actions: dir_actions[typ](name,f,depth,hidden) @@ -39,6 +43,12 @@ def file_entry(f,path,depth,hidden): def process_dirs(path='.',disppath='.',depth=0,hidden=False): dirlist = os.listdir(path) if len(dirlist)!=0: + + # don't list directories with unknown action + name,typ = get_dir_name_type(path) + if not typ in dir_actions: + return + dir_begin(disppath,hidden,depth) dirs = [] files = [] @@ -54,6 +64,8 @@ def process_dirs(path='.',disppath='.',depth=0,hidden=False): for f in dirs+files: if os.path.isdir(f): dir_entry(f,disppath,depth,hidden) + # if the directory is a part of the path we want + # to show -> show it, if not -> hide it's contents if disppath[:len(f)]==f: process_dirs(f,disppath,depth+1,False) else: diff --git a/pylog_action_impl.py b/pylog_action_impl.py index 75b8709..efd34df 100644 --- a/pylog_action_impl.py +++ b/pylog_action_impl.py @@ -42,7 +42,84 @@ def disp_txt(path): print markdown.markdown(txt,extensions=cfg.markdown_extensions).encode(cfg.encoding) f.close() print '' + +def disp_python(path): + disp_source(path,"python",True) + +def disp_c(path): + disp_source(path,"c",True) + +def disp_source(path,lang,line_numbers): + print '
' + f = codecs.open(path, mode="r", encoding=cfg.encoding) + if line_numbers: + txt="\t#!"+lang+'\n' + else: + txt="\t:::"+lang+'\n' + for line in f.read().split('\n'): + txt += "\t"+line+'\n' + print markdown.markdown(txt,extensions=cfg.markdown_extensions).encode(cfg.encoding) + f.close() + print '
' + +def disp(path): + print '
' + f = codecs.open(path, mode="r", encoding=cfg.encoding)(path) + print f.read().encode(cfg.encoding) + f.close() + print '
' +def get_n_words(fi,n): + i=0 + txt="" + c=" " + space=False + while i<40 and len(c)==1: + c = fi.read(1) + txt+=c + if c==" " and not space: + i+=1 + space=True + else: + space=False + + if i==40: + return (txt,True) + return (txt,False) + +def prev(path): + fi = codecs.open(path, mode="r", encoding=cfg.encoding) + txt=get_n_words(fi,40) + print '
'+txt[0].encode(cfg.encoding) + if txt[1]: + print ' ...' + print '
' + fi.close() + +def prev_txt(path): + fi = codecs.open(path, mode="r", encoding=cfg.encoding) + txt=get_n_words(fi,40) + print '
'+markdown.markdown(txt[0],extensions=cfg.markdown_extensions).encode(cfg.encoding) + if txt[1]: + print ' ...' + print '
' + fi.close() + +def prev_source(path): + return + + + + + +# Legacy code ------------------------ +# using vim for syntax highlighting,could be usefull, +# old disp_source using vim +#def disp_source(path): +# print '
' +# print get_source_html(path).encode(cfg.encoding) +# #print source +# print '
' class BodyParser(HTMLParser): body=False src='' @@ -69,14 +146,6 @@ class BodyParser(HTMLParser): if self.body: self.src += '&'+ref+';' -# Legacy code used vim for syntax highlighting, as this could be usefull, -# I'll keep it here -# old disp_source using vim -#def disp_source(path): -# print '
' -# print get_source_html(path).encode(cfg.encoding) -# #print source -# print '
' def get_source_html(path): tmpdir = os.path.join(tempfile.gettempdir(),"pylog") cache=False @@ -125,69 +194,4 @@ def get_source_html(path): if not cache: os.remove(tmpfile) return src - -def disp_python(path): - disp_source(path,"python",True) - -def disp_c(path): - disp_source(path,"c",True) - -def disp_source(path,lang,line_numbers): - print '
' - f = codecs.open(path, mode="r", encoding=cfg.encoding) - if line_numbers: - txt="\t#!"+lang+'\n' - else: - txt="\t:::"+lang+'\n' - for line in f.read().split('\n'): - txt += "\t"+line+'\n' - print markdown.markdown(txt,extensions=cfg.markdown_extensions).encode(cfg.encoding) - f.close() - print '
' - -def disp(path): - print '
' - f = codecs.open(path, mode="r", encoding=cfg.encoding)(path) - print f.read().encode(cfg.encoding) - f.close() - print '
' - -def get_n_words(fi,n): - i=0 - txt="" - c=" " - space=False - while i<40 and len(c)==1: - c = fi.read(1) - txt+=c - if c==" " and not space: - i+=1 - space=True - else: - space=False - - if i==40: - return (txt,True) - return (txt,False) - -def prev(path): - fi = codecs.open(path, mode="r", encoding=cfg.encoding) - txt=get_n_words(fi,40) - print '
'+txt[0].encode(cfg.encoding) - if txt[1]: - print ' ...' - print '
' - fi.close() - -def prev_txt(path): - fi = codecs.open(path, mode="r", encoding=cfg.encoding) - txt=get_n_words(fi,40) - print '
'+markdown.markdown(txt[0],extensions=cfg.markdown_extensions).encode(cfg.encoding) - if txt[1]: - print ' ...' - print '
' - fi.close() - -def prev_source(path): - return diff --git a/pylog_config.py b/pylog_config.py index 0e94e96..0392328 100644 --- a/pylog_config.py +++ b/pylog_config.py @@ -38,7 +38,7 @@ encoding="utf-8" # format. See manual reference for codecs.open for # details -markdown_extensions = ['math','gallery','tables','footnotes','codehilite(guess_lang=False)','abbr'] +markdown_extensions = ['math','gallery','extra','codehilite(guess_lang=False)',] # Markdown extensions: # math .......... rich subset of latex formulas between $$ c^2=a^2+b^2 $$ # gallery ....... galleries support: