def render(self, variables, content_template, pretty=False): """ Works like lamson.view.render, but uses apply_styles to modify the HTML with the configured CSS before returning it to you. If you set the pretty=True then it will prettyprint the results, which is a waste of bandwidth, but helps when debugging. Remember that content_template is run through the template system, and then processed with self.wiki (defaults to markdown). This let's you do template processing and write the HTML contents like you would an email. You could also attach the content_template as a text version of the message for people without HTML. Simply set the .Body attribute of the returned lamson.mail.MailResponse object. """ content = self.wiki(view.render(variables, content_template)) lvars = variables.copy() lvars['content'] = content html = view.render(lvars, self.template) styled = self.apply_styles(html) if pretty: return styled.prettify() else: return str(styled)
def test_spelling(): message = {} original = {} for path in glob("app/templates/mail/*.msg"): template = "mail/" + os.path.basename(path) result = view.render(locals(), template) spelling(template, result)
def respond(self, variables, content, **kwd): """ Works like lamson.view.respond letting you craft a lamson.mail.MailResponse immediately from the results of a lamson.html.HtmlMail.render call. Simply pass in the From, To, and Subject parameters you would normally pass in for MailResponse, and it'll craft the HTML mail for you and return it ready to deliver. A slight convenience in this function is that if the Body kw parameter equals the content parameter, then it's assumed you want the raw markdown content to be sent as the text version, and it will produce a nice dual HTML/text email. """ assert content, "You must give a contents template." if kwd.get('Body', None) == content: kwd['Body'] = view.render(variables, content) for key in kwd: kwd[key] = kwd[key] % variables msg = mail.MailResponse(**kwd) msg.Html = self.render(variables, content) return msg
def POSTING(message, post_name=None, host=None): user, address = parseaddr(message['from']) user = user or address post_url = "posts/%s/%s.html" % (address, post_name) index_q = queue.Queue("run/indexed") post_keys = sorted(index_q.keys(), reverse=True) old_keys = post_keys[50:] del post_keys[50:] # find the old one and remove it posts = [] for key in post_keys: msg = index_q.get(key) if msg['x-post-url'] == post_url: # this is the old one, take it out index_q.remove(key) else: posts.append(msg) # update the index and our posts message['X-Post-URL'] = post_url index_q.push(message) posts.insert(0, message) # and generate the index with what we got now index = view.render(locals(), "web/index.html") f = open("app/data/index.html", "w") f.write(index.encode("utf-8")) f.close() # finally, zap all the old keys for old in old_keys: index_q.remove(old)
def test_HtmlMail_apply_styles(): hs = html.HtmlMail("style.css", "html_test.html") page = view.render(locals(), "html_test.html") styled = hs.apply_styles(page) assert "magenta" in str(styled) assert_not_equal(str(styled), str(page))
def load_css(self, css_template, variables): """ If you want to change the CSS, simply call this with the new CSS and variables. It will change internal state so that later calls to render or respond use the new CSS. """ self.css = view.render(variables, css_template) self.engine = clevercss.Engine(self.css) self.stylesheet = [] for selector, style in self.engine.evaluate(): attr = "; ".join("%s: %s" % (k,v) for k,v in style) selectors = selector[0].split() # root, path, attr self.stylesheet.append((selectors[0], selectors[1:], attr))
def load_css(self, css_template, variables): """ If you want to change the CSS, simply call this with the new CSS and variables. It will change internal state so that later calls to render or respond use the new CSS. """ self.css = view.render(variables, css_template) self.engine = clevercss.Engine(self.css) self.stylesheet = [] for selector, style in self.engine.evaluate(): attr = "; ".join("%s: %s" % (k, v) for k, v in style) selectors = selector[0].split() # root, path, attr self.stylesheet.append((selectors[0], selectors[1:], attr))
def post(post_name, user, host, message): user_dir = make_user_dir(user) user_id, domain = user.split("@") # make sure it's removed first if it existed delete(post_name, user) posting = open("%s/%s.html" % (user_dir, post_name), "w") content = markdown(message.body()) html = view.render(locals(), "web/post.html") posting.write(html.encode('utf-8')) post_q = get_user_post_queue(user_dir) post_q.push(message)
def COMMENTING(message, user_id=None, domain=None, post_name=None, host=None): address = user_id + '@' + domain user_dir = post.get_user_dir(address) if post.user_exists(address): # stuff it here for now, but we'll just build the file rolling comments = queue.Queue("%s/comments" % user_dir) comments.push(message) contents = markdown(message.body()) comment_file = "%s/%s-comments.html" % (user_dir, post_name) snippet = view.render(locals(), "web/comments.html") with open(comment_file, "a") as out: out.write(snippet) else: logging.warning("Attempt to post to user %r but user doesn't exist.", address)
def test_render(): # try with some empty vars text = view.render({}, "template.txt") assert text
def build_index(): lists = sorted(os.listdir(settings.ARCHIVE_BASE)) html = view.render(locals(), "web/list_index.html") open(os.path.join(settings.ARCHIVE_BASE, "lists.html"), "w").write(html)