Exemplo n.º 1
0
    def execute(self, args):
        """Sets the post body.

        **Argument:**

        - `args` ({'post_id': |PrePostId|, 'new_body_text': str, 'new_count':
          int, 'new': object})

        **Returns:** {'error': str} | {'new_body_html': str} |
        {'new_post_summary': str, 'new_post_id': str}
        """

        postdb = self._postdb

        post_id = args.get('post_id')
        new = args.get('new')
        if new is None:
            post = postdb.post(post_id)
        else:
            parent = postdb.post(post_id)

            # Create new post, make it a child of the existing one.
            post = hklib.Post.from_str('')
            post.set_author(parent.author())
            post.set_subject(parent.subject())
            post.set_date(parent.date())
            post.set_tags(parent.tags())
            post.set_parent(parent.post_id_str())
            heap = parent.heap_id()
            prefix = 'hkweb_'
            hkshell.add_post_to_heap(post, prefix, heap)
            post_id = post.post_id()

        if post is None:
            return {'error': 'No such post: "%s"' % (post_id,)}

        newPostBodyText = args.get('new_body_text')
        if newPostBodyText is None:
            return {'error': 'No post body specified'}

        post.set_body(newPostBodyText)

        # Generating the HTML for the new body or new post summary.
        if new is None:
            generator = PostBodyGenerator(self._postdb)
            new_body_html = generator.print_post_body(post_id)
            new_body_html = hkutils.textstruct_to_str(new_body_html)
            return {'new_body_html': new_body_html}
        else:
            generator = PostPageGenerator(self._postdb)
            generator.set_post_id(post.post_id())
            postitem = hklib.PostItem('inner', post)
            postitem.print_post_body = True
            postitem.print_parent_post_id = True
            postitem.print_children_post_id = True
            new_post_summary = generator.print_postitems([postitem])
            new_post_summary = hkutils.textstruct_to_str(new_post_summary)
            return { 'new_post_summary': new_post_summary,
                     'new_post_id': post.post_id_str()}
Exemplo n.º 2
0
    def assertTextStructsAreEqual(self, text1, text2):
        """Asserts that the given text structures are equal.

        **Arguments:**

        - `text1` (|TextStruct|)
        - `text2` (|TextStruct|)
        """

        self.assertEqual(
            hkutils.textstruct_to_str(text1),
            hkutils.textstruct_to_str(text2))
Exemplo n.º 3
0
    def print_postitem_children_post_id_core(self, postitem):
        """Prints the core of the post id list of the children of the post.

        **Argument:**

        - `postitem` (|PostItem|)

        **Returns:** |HtmlText|
        """

        if hasattr(postitem, "print_children_post_id") and postitem.print_children_post_id:

            children = self._postdb.children(postitem.post)
            children_printed = []
            for child in children:
                child_postitem = hklib.PostItem("inner", child)
                child_printed = self.print_link(
                    self.print_postitem_link(child_postitem), (self.escape(child.post_id_str()))
                )
                child_printed = hkutils.textstruct_to_str(child_printed)
                children_printed.append(child_printed)

            if children_printed == []:
                children_printed_str = "-"
            else:
                children_printed_str = ", ".join(children_printed)

            return ("Children: ", children_printed_str)

        return ""
Exemplo n.º 4
0
    def escape(self, text):
        """Escapes the given text so that it will appear correctly when
        inserted into HTML.

        **Argument:**

        - `text` (|TextStruct|)

        **Returns:** |HtmlText|

        **Example:** ::

            >>> generator.escape('<text>')
            '&lt;text&gt;'
        """

        def escape_char(matchobject):
            """Escapes one character based on a match."""
            whole = matchobject.group(0)
            if whole == "<":
                return "&lt;"
            elif whole == ">":
                return "&gt;"
            elif whole == "&":
                return "&amp;"

        return re.sub(r"[<>&]", escape_char, hkutils.textstruct_to_str(text))
Exemplo n.º 5
0
    def test_textstruct_to_str(self):

        """Tests :func:`hkutils.textstruct_to_str`."""

        # Converting a string
        self.assertEqual(
            hkutils.textstruct_to_str('text'),
            'text')

        # Converting a structure that contains both lists and tuples
        self.assertEqual(
            hkutils.textstruct_to_str(['text1', ('2', ['3']), '4']),
            'text1234')

        # Trying to converting something that is not a TextStruct
        self.assertRaises(
            TypeError,
            lambda: hkutils.textstruct_to_str(0))
Exemplo n.º 6
0
    def escape_url(self, url):
        """Escapes a url by replacing the double quotes to ``%22``.

        **Argument:**

        - `url` (|HtmlText|)

        **Returns:** |HtmlText|
        """

        return hkutils.textstruct_to_str(url).replace('"', "%22")
Exemplo n.º 7
0
    def serve_html(self, content, generator):
        """Serves a HTML page that contains the given content.

        **Argument:**

        - `content` (|HtmlText|)
        - `generator` (|BaseGenerator|) -- Generator to be used for generating
           the HTML headers and footers.

        **Returns:** str
        """

        webpy.header('Content-type', 'text/html')
        webpy.header('Transfer-Encoding', 'chunked')
        page = generator.print_html_page(content)
        return hkutils.textstruct_to_str(page)
Exemplo n.º 8
0
    def execute(self, args):
        # Unused argument 'args' # pylint: disable=W0613
        """Gets the post body.

        **Argument:**

        - `args` ({'post_id': |PrePostId|})

        **Returns:** {'error': str} | {'body_html': str}
        """

        post_id = args.get('post_id')
        post = self._postdb.post(post_id)
        if post is None:
            return {'error': 'No such post: "%s"' % (post_id,)}

        # Generating the HTML for the new body
        generator = PostBodyGenerator(self._postdb)
        new_body_html = generator.print_post_body(post_id)
        new_body_html = hkutils.textstruct_to_str(new_body_html)

        return {'body_html': new_body_html}
Exemplo n.º 9
0
 def red(self, s):
     return hkutils.textstruct_to_str(self.enclose(s, class_='important'))