def print_gallery(request, photos): config = request.getConfiguration() http = request.getHttp() encoding = config['blog_encoding'] template_dir = config['photo_template'] #throws error if not set head = file(pathjoin(template_dir, 'head.html'), 'r').read() gallery = file(pathjoin(template_dir, 'photo-gallery.html'), 'r').read() foot = file(pathjoin(template_dir, 'foot.html'), 'r').read() num = get_numphotos(gallery) page = get_pagenum(http) start = num * page end = start + num filelist = photos[start:end] config['next_page'] = config['prev_page'] = "%s%s" %\ (config['base_url'], http['PATH_INFO']) if page-1 >= 0: pp = page - 1 else: pp = page if end < len(photos): np = page + 1 else: np = page config['prev_page'] = "%s?page=%s" % (config['prev_page'], pp) config['next_page'] = "%s?page=%s" % (config['next_page'], np) config['blog_title'] = "%s : %s" % (config['blog_title'], http['PATH_INFO']) print tools.parse(request, encoding, config, head) print photo_parse(gallery, filelist, config, request, encoding) print tools.parse(request, encoding, config, foot)
def print_gallery(request, photos): config = request.getConfiguration() http = request.getHttp() encoding = config['blog_encoding'] template_dir = config['photo_template'] #throws error if not set head = file(pathjoin(template_dir, 'head.html'), 'r').read() gallery = file(pathjoin(template_dir, 'photo-gallery.html'), 'r').read() foot = file(pathjoin(template_dir, 'foot.html'), 'r').read() num = get_numphotos(gallery) page = get_pagenum(http) start = num * page end = start + num filelist = photos[start:end] config['next_page'] = config['prev_page'] = "%s%s" %\ (config['base_url'], http['PATH_INFO']) if page - 1 >= 0: pp = page - 1 else: pp = page if end < len(photos): np = page + 1 else: np = page config['prev_page'] = "%s?page=%s" % (config['prev_page'], pp) config['next_page'] = "%s?page=%s" % (config['next_page'], np) config['blog_title'] = "%s : %s" % (config['blog_title'], http['PATH_INFO']) print tools.parse(request, encoding, config, head) print photo_parse(gallery, filelist, config, request, encoding) print tools.parse(request, encoding, config, foot)
def test_functions_with_args_that_have_commas(self): env = {"foo": lambda req, vd, x: (x + "A"), "foo2": lambda req, vd, x, y: (y + x)} for mem in (('$foo("ba,ar")', "ba,arA"), ('$foo2("a,b", "c,d")', "c,da,b")): self.eq_(tools.parse(req, env, mem[0]), mem[1])
def _processContent(self): """ Processes the content for the story portion of a page. @returns: the content string @rtype: string """ data = self._request.getData() outputbuffer = [] if callable(self._content): # if the content is a callable function, then we just spit out # whatever it returns as a string outputbuffer.append(self._content()) elif isinstance(self._content, dict): # if the content is a dict, then we parse it as if it were an # entry--except it's distinctly not an EntryBase derivative self._content.update(data) output = tools.parse(self._request, self._encoding, self._content, self.flavour['story']) outputbuffer.append(output) elif isinstance(self._content, list): current_date = '' for entry in self._content: output, current_date = self._processEntry(entry, current_date) outputbuffer.append(output) return self.write(u"".join(outputbuffer))
def test_delimited(self): env = {"foo": "FOO", "country": "España"} for mem in (("foo $(foo) foo", "foo FOO foo"), ("foo $(foor) foo", "foo foo"), ("foo $(country) foo", "foo España foo")): self.eq_(tools.parse(req, env, mem[0]), mem[1])
def test_simple(self): env = {"foo": "FOO", "country": "España"} for mem in (("foo foo foo", "foo foo foo"), ("foo $foo foo", "foo FOO foo"), ("foo $foor foo", "foo foo"), ("foo $country foo", "foo España foo")): self.eq_(tools.parse(req, env, mem[0]), mem[1])
def test_delimited(self): env = {"foo": "FOO", "country": "España"} for mem in ( ("foo $(foo) foo", "foo FOO foo"), ("foo $(foor) foo", "foo foo"), ("foo $(country) foo", "foo España foo"), ): self.eq_(tools.parse(req, env, mem[0]), mem[1])
def photo_parse(gallery, photos, config, request, encoding): output = cStringIO.StringIO() gallery = gallery.split('<photo>') #first one can't be a photo output.write(tools.parse(request, encoding, config, gallery.pop(0))) for line in gallery: try: cur_photo = photos.pop(0) except IndexError: p = line.find('</photo>') line = line[p+8:] output.write(tools.parse(request, encoding, config, line)) else: p = line.find('</photo>') line = line.replace('</photo>', '') mdict = merge_dicts(cur_photo.getData(), config) output.write(tools.parse(request, encoding, mdict, line[:p])) if p < len(line): output.write(tools.parse(request, encoding, config, line[p:])) return output.getvalue()
def test_simple(self): env = {"foo": "FOO", "country": "España"} for mem in ( ("foo foo foo", "foo foo foo"), ("foo $foo foo", "foo FOO foo"), ("foo $foor foo", "foo foo"), ("foo $country foo", "foo España foo"), ): self.eq_(tools.parse(req, env, mem[0]), mem[1])
def photo_parse(gallery, photos, config, request, encoding): output = cStringIO.StringIO() gallery = gallery.split('<photo>') #first one can't be a photo output.write(tools.parse(request, encoding, config, gallery.pop(0))) for line in gallery: try: cur_photo = photos.pop(0) except IndexError: p = line.find('</photo>') line = line[p + 8:] output.write(tools.parse(request, encoding, config, line)) else: p = line.find('</photo>') line = line.replace('</photo>', '') mdict = merge_dicts(cur_photo.getData(), config) output.write(tools.parse(request, encoding, mdict, line[:p])) if p < len(line): output.write(tools.parse(request, encoding, config, line[p:])) return output.getvalue()
def print_dirs(request, subdirs): config = request.getConfiguration() http = request.getHttp() encoding = config['blog_encoding'] template_dir = config['photo_template'] num_dirs = config['num_dirs'] page = get_pagenum(http) start = num_dirs * page end = start + num_dirs filelist = subdirs[start:end] head = file(pathjoin(template_dir, 'head.html'), 'r').read() dirs = file(pathjoin(template_dir, 'photo-dirs.html'), 'r').read() foot = file(pathjoin(template_dir, 'foot.html'), 'r').read() config['next_page'] = config['prev_page'] = "%s%s" %\ (config['base_url'], http['PATH_INFO']) if page - 1 >= 0: pp = page - 1 else: pp = page if end < len(subdirs): np = page + 1 else: np = page config['prev_page'] = "%s?page=%s" % (config['prev_page'], pp) config['next_page'] = "%s?page=%s" % (config['next_page'], np) config['blog_title'] = "%s : %s" % (config['blog_title'], http['PATH_INFO']) print tools.parse(request, encoding, config, head) for d in subdirs: mdict = merge_dicts(d.getData(), config) print tools.parse(request, encoding, mdict, dirs) print tools.parse(request, encoding, config, foot)
def print_dirs(request, subdirs): config = request.getConfiguration() http = request.getHttp() encoding = config['blog_encoding'] template_dir = config['photo_template'] num_dirs = config['num_dirs'] page = get_pagenum(http) start = num_dirs * page end = start + num_dirs filelist = subdirs[start:end] head = file(pathjoin(template_dir, 'head.html'), 'r').read() dirs = file(pathjoin(template_dir, 'photo-dirs.html'), 'r').read() foot = file(pathjoin(template_dir, 'foot.html'), 'r').read() config['next_page'] = config['prev_page'] = "%s%s" %\ (config['base_url'], http['PATH_INFO']) if page-1 >= 0: pp = page - 1 else: pp = page if end < len(subdirs): np = page + 1 else: np = page config['prev_page'] = "%s?page=%s" % (config['prev_page'], pp) config['next_page'] = "%s?page=%s" % (config['next_page'], np) config['blog_title'] = "%s : %s" % (config['blog_title'], http['PATH_INFO']) print tools.parse(request, encoding, config, head) for d in subdirs: mdict = merge_dicts(d.getData(), config) print tools.parse(request, encoding, mdict, dirs) print tools.parse(request, encoding, config, foot)
def test_functions(self): for mem in (({"foo": lambda req, vd: "FOO"}, "foo foo foo", "foo foo foo"), ({"foo": lambda req, vd: "FOO"}, "foo $foo() foo", "foo FOO foo"), ({"foo": lambda req, vd, z: z}, "foo $foo('a') foo", "foo a foo"), ({"foo": lambda req, vd, z: z}, "foo $foo(1) foo", "foo 1 foo"), ({"foo": lambda req, vd, z: z}, "foo $foo($money) foo", "foo $money foo"), ({"foo": lambda req, vd, z: z, "bar": "BAR"}, "foo $foo(bar) foo", "foo BAR foo"), ({"foo": lambda req, vd, z: z, "bar": "BAR"}, "foo $foo($bar) foo", "foo BAR foo"), ({"lang": lambda req, vd: "español"}, "foo $(lang) foo", "foo español foo"), # Note: functions can return unicode which will get # converted to blog_encoding ({"lang": lambda req, vd: u"español"}, "español $(lang)", "español español")): self.eq_(tools.parse(req, mem[0], mem[1]), mem[2])
def test_functions(self): for mem in ( ({"foo": lambda req, vd: "FOO"}, "foo foo foo", "foo foo foo"), ({"foo": lambda req, vd: "FOO"}, "foo $foo() foo", "foo FOO foo"), ({"foo": lambda req, vd, z: z}, "foo $foo('a') foo", "foo a foo"), ({"foo": lambda req, vd, z: z}, "foo $foo(1) foo", "foo 1 foo"), ({"foo": lambda req, vd, z: z}, "foo $foo($money) foo", "foo $money foo"), ({"foo": lambda req, vd, z: z, "bar": "BAR"}, "foo $foo(bar) foo", "foo BAR foo"), ({"foo": lambda req, vd, z: z, "bar": "BAR"}, "foo $foo($bar) foo", "foo BAR foo"), ({"lang": lambda req, vd: "español"}, "foo $(lang) foo", "foo español foo"), # Note: functions can return unicode which will get # converted to blog_encoding ({"lang": lambda req, vd: u"español"}, "español $(lang)", "español español"), ): self.eq_(tools.parse(req, mem[0], mem[1]), mem[2])
def _printTemplate(self, entry, template): """ @param entry: either a dict or the Entry object @type entry: dict or Entry @param template: the template string @type template: string @returns: the content string @rtype: string """ if template: template = unicode(template) finaltext = tools.parse(self._request, self._encoding, entry, template) return finaltext.replace(r'\$', '$') return ""
def render_template(self, entry, template_name, override=0): """ Find the flavour template for template_name, run any blosxom callbacks, substitute entry into it and render the template. If the entry has a ``template_name`` property and override is True (this happens in the story template), then we'll use that template instead. :param entry: the entry/variable-dict to use for expanding variables :param template_name: template name (gets looked up in self.flavour) :param override: whether (True) or not (False) this template can be overriden with the ``template_name`` value in the entry """ template = "" if override: # here we do a quick override... if the entry has a # template field we use that instead of the template_name # argument passed in. actual_template_name = entry.get("template_name", template_name) template = self.flavour.get(actual_template_name, '') if not template: template = self.flavour.get(template_name, '') # we run this through the regular callbacks args = self._run_callback(template_name, { "entry": entry, "template": template }) template = args["template"] # FIXME - the finaltext.replace(...) below causes \$ to get # unescaped in title and body text which is wrong. this # fix alleviates that somewhat, but there are still edge # cases regarding function data. need a real template # engine with a real parser here. entry = dict(args["entry"]) for k, v in entry.items(): if isinstance(v, basestring): entry[k] = v.replace(r"\$", r"\\$") finaltext = tools.parse(self._request, entry, template) return finaltext.replace(r'\$', '$')
def render_template(self, entry, template_name, override=0): """ Find the flavour template for template_name, run any blosxom callbacks, substitute entry into it and render the template. If the entry has a ``template_name`` property and override is True (this happens in the story template), then we'll use that template instead. :param entry: the entry/variable-dict to use for expanding variables :param template_name: template name (gets looked up in self.flavour) :param override: whether (True) or not (False) this template can be overriden with the ``template_name`` value in the entry """ template = "" if override: # here we do a quick override... if the entry has a # template field we use that instead of the template_name # argument passed in. actual_template_name = entry.get("template_name", template_name) template = self.flavour.get(actual_template_name, '') if not template: template = self.flavour.get(template_name, '') # we run this through the regular callbacks args = self._run_callback(template_name, {"entry": entry, "template": template}) template = args["template"] # FIXME - the finaltext.replace(...) below causes \$ to get # unescaped in title and body text which is wrong. this # fix alleviates that somewhat, but there are still edge # cases regarding function data. need a real template # engine with a real parser here. entry = dict(args["entry"]) for k, v in entry.items(): if isinstance(v, basestring): entry[k] = v.replace(r"\$", r"\\$") finaltext = tools.parse(self._request, entry, template) return finaltext.replace(r'\$', '$')
def _outputFlavour(self, entry, template_name): """ Find the flavour template for template_name, run any blosxom callbacks, substitute vars into it and write the template to the output @param entry: the EntryBase object @type entry: L{Pyblosxom.entries.base.EntryBase} @param template_name: - name of the flavour template @type template_name: string """ template = self.flavour[template_name] args = self._run_callback(template_name, { "entry": entry, "template": template }) template = args["template"] entry = args["entry"] self.write(tools.parse(self._request, self._encoding, entry, template))
def _processContent(self): """ Processes the content for the story portion of a page. @returns: the content string @rtype: string """ config = self._request.getConfiguration() data = self._request.getData() outputbuffer = [] content_type = type(self._content) if callable(self._content): # if the content is a callable function, then we just spit out # whatever it returns as a string outputbuffer.append(self._content()) elif content_type is type({}): # if the content is a dict, then we parse it as if it were an # entry--except it's distinctly not an EntryBase derivative self._content.update(data) output = tools.parse(self._content, self.flavour['story']) outputbuffer.append(output) elif content_type is type([]): current_date = '' maxcount = config['num_entries'] if maxcount and len(self._content) > maxcount: self._content = self._content[:maxcount] for entry in self._content: # FIXME - commented this next line out # data.update(entry) output, current_date = self._processEntry(entry, current_date) outputbuffer.append(output) # FIXME what's this do? # if entry['pi_yr'] == '': # break return self.write(u"".join(outputbuffer))
def test_functions_with_var_args(self): def pt(d, t): return tools.parse(req, d, t) vd = {"foo": lambda req, vd, x: (x + "A"), "bar": "BAR", "lang": "Español", "ulang": u"Español"} for mem in ( # this bar is a string ("foo $foo('bar') foo", "foo barA foo"), # this bar is also a string ('foo $foo("bar") foo', "foo barA foo"), # this bar is an identifier which we lookup in the # var_dict and pass into the foo function ("foo $foo(bar) foo", "foo BARA foo"), # variables that have utf-8 characters ("foo $foo(lang) foo", "foo EspañolA foo"), ("foo $foo(ulang) foo", "foo EspañolA foo"), ): self.eq_(tools.parse(req, vd, mem[0]), mem[1])
def _processContent(self): """ Processes the content for the story portion of a page. @returns: the content string @rtype: string """ config = self._request.getConfiguration() data = self._request.getData() outputbuffer = [] content_type = type(self._content) if callable(self._content): # if the content is a callable function, then we just spit out # whatever it returns as a string outputbuffer.append(self._content()) elif content_type is type({}): # if the content is a dict, then we parse it as if it were an # entry--except it's distinctly not an EntryBase derivative self._content.update(data) output = tools.parse(self._request, self._encoding, self._content, self.flavour['story']) outputbuffer.append(output) elif content_type is type([]): current_date = '' maxcount = config['num_entries'] if maxcount and len(self._content) > maxcount: self._content = self._content[:maxcount] for entry in self._content: # FIXME - commented this next line out # data.update(entry) output, current_date = self._processEntry(entry, current_date) outputbuffer.append(output) return self.write(u"".join(outputbuffer))
def render_medium(request, photo): config = request.getConfiguration() root = config['root_photodir'] base = config['photo_base'] thumb_size = config['thumb_size'] encoding = config['blog_encoding'] template_dir = config['photo_template'] p = PhotoEntry(photo, config) head = file(pathjoin(template_dir, 'head.html'), 'r').read() medium = file(pathjoin(template_dir, 'photo-medium.html'), 'r').read() foot = file(pathjoin(template_dir, 'foot.html'), 'r').read() print tools.parse(request, encoding, config, head) mdict = merge_dicts(p.getData(), config) print tools.parse(request, encoding, mdict, medium) print tools.parse(request, encoding, config, foot)
def test_functions_with_var_args(self): def pt(d, t): return tools.parse(req, d, t) vd = {"foo": lambda req, vd, x: (x + "A"), "bar": "BAR", "lang": "Español", "ulang": u"Español"} for mem in ( # this bar is a string ("foo $foo('bar') foo", "foo barA foo"), # this bar is also a string ('foo $foo("bar") foo', "foo barA foo"), # this bar is an identifier which we lookup in the # var_dict and pass into the foo function ("foo $foo(bar) foo", "foo BARA foo"), # variables that have utf-8 characters ("foo $foo(lang) foo", "foo EspañolA foo"), ("foo $foo(ulang) foo", "foo EspañolA foo")): self.eq_(tools.parse(req, vd, mem[0]), mem[1])
def render_content(self, content): """ Processes the content for the story portion of a page. :param content: the content to be rendered :returns: the content string """ data = self._request.get_data() outputbuffer = [] if callable(content): # if the content is a callable function, then we just spit out # whatever it returns as a string outputbuffer.append(content()) elif isinstance(content, dict): # if the content is a dict, then we parse it as if it were an # entry--except it's distinctly not an EntryBase derivative var_dict = self.get_parse_vars() var_dict.update(content) output = tools.parse(self._request, var_dict, self.flavour['story']) outputbuffer.append(output) elif isinstance(content, list): if len(content) > 0: current_date = content[0]["date"] if current_date and "date_head" in self.flavour: parse_vars = self.get_parse_vars() parse_vars.update({"date": current_date, "yr": content[0]["yr"], "mo": content[0]["mo"], "da": content[0]["da"]}) outputbuffer.append( self.render_template(parse_vars, "date_head")) for entry in content: if entry["date"] and entry["date"] != current_date: if "date_foot" in self.flavour: parse_vars = self.get_parse_vars() parse_vars.update({"date": current_date, "yr": content[0]["yr"], "mo": content[0]["mo"], "da": content[0]["da"]}) outputbuffer.append( self.render_template(parse_vars, "date_foot")) if "date_head" in self.flavour: current_date = entry["date"] parse_vars = self.get_parse_vars() parse_vars.update({"date": current_date, "yr": content[0]["yr"], "mo": content[0]["mo"], "da": content[0]["da"]}) outputbuffer.append( self.render_template(parse_vars, "date_head")) if data['content-type'] == 'text/plain': s = tools.Stripper() s.feed(entry.get_data()) s.close() p = [' ' + line for line in s.gettext().split('\n')] entry.set_data('\n'.join(p)) parse_vars = self.get_parse_vars() parse_vars.update(entry) outputbuffer.append( self.render_template(parse_vars, "story", override=1)) args = {"entry": parse_vars, "template": ""} args = self._run_callback("story_end", args) outputbuffer.append(args["template"]) if current_date and "date_foot" in self.flavour: parse_vars = self.get_parse_vars() parse_vars.update({"date": current_date}) outputbuffer.append( self.render_template(parse_vars, "date_foot")) return outputbuffer
def pt(d, t): ret = tools.parse(req, d, t) # print ret return ret
def pt(d, t): return tools.parse(req, d, t)
def test_functions_old_behavior(self): # test the old behavior that allowed for functions that have no # arguments--in this case we don't pass a request object in self.eq_(tools.parse(req, {"foo": (lambda: "FOO")}, "foo $foo() foo"), "foo FOO foo")
def test_functions_old_behavior(self): # test the old behavior that allowed for functions that have no # arguments--in this case we don't pass a request object in self.eq_(tools.parse(req, {"foo": (lambda : "FOO")}, "foo $foo() foo"), "foo FOO foo")
def pt(d, t): return tools.parse(self._get_req(), "iso-8859-1", d, t)