X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=webber.py;h=07136de40733244f7f26a0ce75918834d02c012b;hb=69d1d9d06d799ea551c90fbe5e9e7bc9e7fc1c01;hp=98b1106c48d4cfcc30da7cfa7abdd23df5cabf05;hpb=0de356a7eb42440f875243206ceebc1207ac6a8d;p=webber.git diff --git a/webber.py b/webber.py index 98b1106..07136de 100644 --- a/webber.py +++ b/webber.py @@ -11,21 +11,21 @@ from config import Holder __all__ = [ # Globals - "cfg", # configuration from webber.ini - "directories", # global hash of directories, by rel_path - "files", # global hash of files, by rel_path - "functions", # all exported template functions + "cfg", # configuration from webber.ini + "directories", # global hash of directories, by rel_path + "files", # global hash of files, by rel_path + "functions", # all exported template functions # Functions - "set_hook", # decorator for hook-functions - "set_macro", # define macro + "set_hook", # decorator for hook-functions + "set_macro", # define macro "set_function", # define functions for the template "get_file_for", "get_link_from", "get_current_file", # because mako-called functions cannot access the - # current File object + # current File object "get_program_directory", - "log", # misc logging functions + "log", # misc logging functions "info", "warning", "error", @@ -63,6 +63,7 @@ class File(Holder): Holder.__init__(self, **kw) files[kw["rel_path"]] = self self.render = None + self.contents = None mtime = os.stat(self.path)[stat.ST_MTIME] self.mtime = mtime self.ctime = mtime @@ -80,7 +81,7 @@ class File(Holder): if read_keywords: s = s.strip() #print "kwd:", s - if s==terminate_line: + if s == terminate_line: read_keywords = False continue @@ -108,10 +109,15 @@ class File(Holder): #print "txt:", s.rstrip().encode("iso-8859-1") txt.append(s) + # Warn about a bogus time entries if self.mtime < self.ctime: log('%s: modification time cannot be before creation time' % self.rel_path) self.ctime = self.mtime + # Warn about long titles / long linktitles + if len(self.linktitle) > 20: + log('%s: define a shorter linktitle' % self.rel_path) + self.contents = "".join(txt) @@ -119,7 +125,7 @@ _get_file_for_cache = {} def get_file_for(name): """webber.files is an hash of File objects, but keyed on the real file name. This function returns a File object for a specific linktitle.""" - + try: return _get_file_for_cache[name] except: @@ -223,7 +229,7 @@ def get_link_from(source, dest): if rel_path.startswith("./"): rel_path = rel_path[2:] #print " from path:", source.out_path - #print " to path: ", out_path + #print " to path: ", out_path #print " rel path: ", rel_path return rel_path @@ -249,14 +255,14 @@ def get_program_directory(): # # Logging # -# 1 Error -# 2 Warning -# 3 Info -# 4 Log +# 1 Error +# 2 Warning +# 3 Info +# 4 Log # 5... Debug # def log(s, level=4): - if level>4: + if level > 4: indent = " " * (level-4) else: indent = "" @@ -282,43 +288,43 @@ def info(s): # IkiWiki does something like this: # At startup: -# getopt modify ARGV -# checkconfig check configuration -# refresh allow plugins to build source files +# getopt modify ARGV +# checkconfig check configuration +# refresh allow plugins to build source files # While scanning files: -# needsbuild detect if page needs to be rebuild -# filter arbitrary changes -# scan collect metadata +# needsbuild detect if page needs to be rebuild +# filter arbitrary changes +# scan collect metadata # While rendering files: -# filter arbitrary changes -# preprocess execute macros -# linkify change wikilinks into links -# htmlize turns text into html -# sanitize sanitize html -# templatefile allows changing of the template on a per-file basis -# pagetemplate fill template with page -# format similar to sanitize, but act on whole page body +# filter arbitrary changes +# preprocess execute macros +# linkify change wikilinks into links +# htmlize turns text into html +# sanitize sanitize html +# templatefile allows changing of the template on a per-file basis +# pagetemplate fill template with page +# format similar to sanitize, but act on whole page body # At the end: -# savestate plugins can save their state +# savestate plugins can save their state # # # We do something like this: # # At startup: -# addoptions allow plugins to add command-line options -# checkconfig check configuration -# start +# addoptions allow plugins to add command-line options +# checkconfig check configuration +# start # While reading files: -# read ask any reader (plugins!) to read the file -# filter ask anybody to filter the contents +# read ask any reader (plugins!) to read the file +# filter ask anybody to filter the contents # While scanning files: -# scan called per file, let plugins act on file data -# scan_done Allows post-processing of scanned data +# scan called per file, let plugins act on file data +# scan_done Allows post-processing of scanned data # While rendering files: -# htmlize turns text into html-part -# linkify convert link macros to HTML -# pagetemplate ask template engine (plugin!) to generate HTML out -# of template and body part +# htmlize turns text into html-part +# linkify convert link macros to HTML +# pagetemplate ask template engine (plugin!) to generate HTML out +# of template and body part # At the end: # finish # @@ -331,13 +337,11 @@ hooks = {} def load_plugins(): """Loads all plugins in the plugins directory.""" sys.path.append(os.path.join(get_program_directory(), "plugins")) + if cfg.has_key("plugin_dirs"): + for s in cfg.plugin_dirs: + sys.path.append(s) for s in cfg.plugins: - #print "import:", s - #try: exec "import %s" % s - #except: - # print "Could not import plugin '%s'" % s - # sys.exit(1) def set_hook(name, last=False): @@ -429,7 +433,7 @@ def iso_to_time(val): try: t = time.strptime(val, "%Y-%m-%d") except ValueError: - warning("%s: wrong ISO format in '%s'" % (self.rel_path, s)) + warning("wrong ISO format in '%s'" % val) return int(time.mktime(t)) @set_function("format_date") @@ -501,7 +505,7 @@ def walk_tree(dirpath): direc.inheritFrom(cfg) if not rel_path: rel_path = "." - log("reading directory %s" % rel_path, level=4) + log("reading directory %s" % rel_path, level=5) for s in os.listdir(dirpath): full_path = os.path.join(dirpath, s) @@ -539,7 +543,7 @@ def walk_tree(dirpath): ) file.inheritFrom(direc) read_file(direc, file) - + walk(dirpath) @@ -550,14 +554,14 @@ def walk_tree(dirpath): # reMacro = re.compile(r''' - \[\[\! # Begin of macro + \[\[\! # Begin of macro \s* - ([^\s\]]+) # Macro name + ([^\s\]]+) # Macro name (?: - \s+ # optional space - ([^\]]+) # optional argumens + \s+ # optional space + ([^\]]+) # optional argumens )? - \]\] # End of macro + \]\] # End of macro ''', re.VERBOSE) reMacroArgs = re.compile(r''' ([-_\w]+) # parameter name @@ -566,9 +570,9 @@ reMacroArgs = re.compile(r''' = \s* (?: - "([^"]*)" # single-quoted + "([^"]*)" # single-quoted | - (\S+) # unquoted + (\S+) # unquoted ) )? ''', re.VERBOSE) @@ -596,21 +600,27 @@ def run_macros(file, contents): s = reMacro.sub(do_macro, contents) #print s return s - + def scan_files(): info("Scanning files ...") for s in files: file = files[s] - try: - # Just check if the file has contents - contents = file.contents - except: + if not file.has_key("contents"): continue +# try: +# # Just check if the file has contents +# contents = file.contents +# except: +# continue direc = directories[file.direc] + # Output-Filename "berechnen" + if file.render and file.render == "html": + file.out_path = os.path.splitext(s)[0] + ".html" + run_hooks("scan", direc=direc, file=file) @@ -656,9 +666,6 @@ def render_files(): continue file.contents = contents - # Output-Filename "berechnen" - file.out_path = os.path.splitext(fname_in)[0] + ".html" - for fname_in in files: file = files[fname_in] current_file = file @@ -667,14 +674,13 @@ def render_files(): continue direc = directories[file.direc] - contents = run_hooks("linkify", + run_hooks("linkify", direc=direc, file=file, - return_holder=False) + return_holder=True) #print "contents after 'linkify':", contents - if not contents: + if not file.contents: continue - file.contents = contents # TODO: einige Fragmente sollen u.U. in eine andere # Webseite eingebaut werden und sollten daher nicht in @@ -734,7 +740,7 @@ def addoptions(params): return parser - + @set_hook("checkconfig", last=True) def checkconfig(params): # Ensure absolute paths that end in '/'.