def test_is_textstruct(self): """Tests :func:`hkutils.is_textstruct`.""" self.assertTrue(hkutils.is_textstruct('')) self.assertTrue(hkutils.is_textstruct('text')) self.assertTrue(hkutils.is_textstruct([])) self.assertTrue(hkutils.is_textstruct(['text1', ('2', ['3']), '4'])) # Testing something that is not a TextStruct self.assertFalse(hkutils.is_textstruct(0))
def section(self, id, title, content, flat=False): """Encloses the given HTML text into a section. **Arguments:** - `id` (str) -- The unique identifier of the section (``'section_'`` + id will be used as an id in the HTML). - `title` (str) -- The title of the section. - `content` (|HtmlText|) -- The HTML text to be in the section. - `flat` (bool) -- The section contains flat post items. **Returns:** |HtmlText| """ assert hkutils.is_textstruct(content), "Parameter is not a valid text structure:\n%s\n" % (content,) if flat: content = self.enclose(content, "table", "flatlist", newlines=True) return (self.section_begin("section_%s" % (id,), title), content, self.section_end())
def print_issue_summary(self, id, title, content, flat=False): assert hkutils.is_textstruct(content), \ 'Parameter is not a valid text structure:\n%s\n' % (content,) if flat: raw_headers = ('State', 'Description', 'Type', 'Tags', 'ID', 'Priority', 'Assigned to', 'Manhour', 'Date') header = [self.enclose(h, 'th', id='th_%d' % (n,), newlines=True) for n,h in enumerate(raw_headers)] content_theader = self.enclose(header, 'theader', 'flatlist', newlines=True) content_tbody = \ self.enclose(content, 'tbody', 'flatlist', newlines=True) content_all = (content_theader, content_tbody) content = \ self.enclose(content_all, 'table', 'flatlist', newlines=True) return (self.section_begin('section_%s' % (id,), title), content, self.section_end())
def enclose( self, content, tag="span", class_=None, newlines=False, id=None, comment=None, closing_comment=False, title=None, skip_empty=False, attributes="", ): """Encloses the given content into a tag. **Arguments:** - `content` (|HtmlText|) -- The content to be placed between the opening and closing tags. - `tag` (|HtmlText|) -- The name of the tag to be printed. - `class_` (|HtmlText| | ``None``) -- The ``class`` of the tag. If ``None``, the tag will not have a ``class`` attribute. - `newlines` (bool) -- If ``True``, a newline character will be placed after both the opening and the tags. - `id` (|HtmlText| | ``None``) -- The ``id`` of the tag. If ``None``, the tag will not have an ``id`` attribute. - `comment` (|HtmlText| | ``None``) -- Comment that should be written after the enclosing tags. - `closing_comment` (bool) -- Whether a comment should be written after the closing tag about the class of the matching tag. - `title` (|HtmlText| | ``None``) -- Text for the ``title`` attribute of the tag. - `skip_empty` (bool) -- If ``True`` and ``content `` is an empty string or list, the function returns an empty string. (Note: ``content`` may be empty without being an empty string or list: e.g. the ``[[], []]`` text structure will be converted to an empty string, but this function will not consider it as empty. This is due to efficiency reasons.) - `attributes` (str) -- Additional attributes. **Returns:** |HtmlText| **Example:** :: >>> generator = hkgen.BaseGenerator(postdb()) >>> text = generator.enclose('mycontent', 'div', 'chapter') >>> print hkutils.textstruct_to_str(text) <div class="chapter">mycontent</div> >>> text = generator.enclose( ... 'mycontent\\n', ... class_='chapter', ... newlines=True, ... id="chap-10", ... comment="Chapter 10", ... closing_comment=True) >>> print hkutils.textstruct_to_str(text) <span class="chapter" id="chap-10"><!-- Chapter 10 --> mycontent </span><!-- Chapter 10 --><!-- chapter --> """ assert hkutils.is_textstruct(content), "Parameter is not a valid text structure:\n%s\n" % (content,) if skip_empty and (content == [] or content == ""): return "" newline = "\n" if newlines else "" classstr = (' class="', class_, '"') if class_ != None else "" idstr = (' id="', id, '"') if id != None else "" title_str = (' title="', title, '"') if title != None else "" comment_str = self.print_comment(comment) if comment != None else "" if closing_comment and class_ is not None: closing_comment_str = self.print_comment(class_) else: closing_comment_str = "" # If attributes is not empty and does not begin with a space character, # we have to add one. if (attributes == "") or (attributes[0] == " "): attributes_str = attributes else: attributes_str = " " + attributes return ( "<", tag, classstr, idstr, title_str, attributes_str, ">", comment_str, newline, content, "</", tag, ">", comment_str, closing_comment_str, newline, )