Example #1
0
 def get(self, id, format):
     wk = Workspace.get_by_id(int(id))
     if not wk:
         self.response.out.write("No such workspace")
         return
     
     # Check access
     if self.request.get("key") and wk.sharing_key == self.request.get("key"):
         pass
     elif wk.demo:
         pass
     else:
         user = users.get_current_user()
         if not user or wk.user.google_id != user.user_id():
             self.response.out.write("Not logged in as the right user")
             return
     
     x = xhtml.parse(wk.content_xml(), wk, format)
     w = writer.Writer(wk)
     xhtml.recurse(x.documentElement, w, format, wk)
     html = w.to_html()
     
     # Re-encode the whole thing, in case out-of-range characters were in
     # elements names or attributes.
     html = encoding.encode_html(html, wk.encoding)
     
     if format == "text":
         self.response.headers["Content-Type"] = "text/plain; charset=%s" % wk.encoding
         self.response.out.write(html)
     else:
         path = 'templates/%s.html' % format
         template = Template(file=path)
         template.wk = wk      
         if format == "code": html = cgi.escape(html)
         if format != "text": html = xhtml.expand_annotations(html)
         template.html = html
         template.id = id
         self.response.headers["Content-Type"] = "text/html; charset=%s" % wk.encoding
         if wk.encoding == "utf-8":
             self.response.out.write(unicode(template))
         else:
             self.response.out.write(unicode(template).encode(wk.encoding))
Example #2
0
    def append_text(self, string):
        # XML-safe
        string = string.replace("&", "&")
        # SGML-safe
        string = string.replace("<", "&lt;").replace(">", "&gt;")
        # Straighten curly quotes
        if self.workspace.straighten_curly_quotes:
          string = string.replace(u"“", "\"").replace(u"”", "\"").replace(u"’", "'").replace(u"‘", "'")
        # Encode
        string = encoding.encode_html(string, self.workspace.encoding)

        if self.workspace.smarty_pants:
            string = string.replace("--", u"—").replace("...", u"…")
            string = re.sub(r"\b'", u"’", string)
            string = re.sub(r"'\b", u"‘", string)
            string = re.sub(r"\B'\B", u"‘", string)
            string = re.sub(r"\b\"", u"”", string)
            string = re.sub(r"\"\b", u"“", string)
        if self.last_type == "block_end":
            self.lines.append(self.workspace.indent_string() * self.indentation)
        elif self.last_type == "break":
            if self.workspace.indent_style == "block":
                if not self.contents_indented[-1]: self.indent()
            indent_adjustment = -1 if self.workspace.indent_style == "inline" else 0
            self.lines.append(self.workspace.indent_string() * (self.indentation + indent_adjustment))

        lpad = re.match(r"\s", string)
        if lpad: self.lines[-1] += " "
        for i, word in enumerate(string.split()):
            if i > 0:
                self.lines[-1] += " "
            self.lines[-1] += word
            self.wrap()
        if re.search(r"\s$", string):
            self.lines[-1] += " "
        self.last_type = "text"