'Convert text content'

def mobimlize_content(self, tag, text, bstate, istates):

    if (text or (tag != 'br')):
        bstate.content = True
    istate = istates[(-1)]
    para = bstate.para
    if ((tag in SPECIAL_TAGS) and (not text)):
        para = (para if (para is not None) else bstate.body)
    elif ((para is None) or (tag in ('td', 'th'))):
        body = bstate.body
        if bstate.pbreak:
            etree.SubElement(body, MBP('pagebreak'))
            bstate.pbreak = False
        bstate.istate = None
        bstate.anchor = None
        parent = (bstate.nested[(-1)] if bstate.nested else bstate.body)
        indent = istate.indent
        left = istate.left
        if isinstance(indent, basestring):
            indent = 0
        if ((indent < 0) and (abs(indent) < left)):
            left += indent
            indent = 0
        elif ((indent != 0) and (abs(indent) < self.profile.fbase)):
            indent = ((indent / abs(indent)) * self.profile.fbase)
        if ((tag in NESTABLE_TAGS) and (not istate.rendered)):
            para = wrapper = etree.SubElement(parent, XHTML(tag), attrib=istate.attrib)
            bstate.nested.append(para)
            if ((tag == 'li') and (len(istates) > 1)):
                istates[(-2)].list_num += 1
                para.attrib['value'] = str(istates[(-2)].list_num)
        elif ((tag in NESTABLE_TAGS) and istate.rendered):
            para = wrapper = bstate.nested[(-1)]
        elif ((not self.opts.mobi_ignore_margins) and (left > 0) and (indent >= 0)):
            ems = self.profile.mobi_ems_per_blockquote
            para = wrapper = etree.SubElement(parent, XHTML('blockquote'))
            para = wrapper
            emleft = (int(round((left / self.profile.fbase))) - ems)
            emleft = min((emleft, 10))
            while (emleft > (ems / 2.0)):
                para = etree.SubElement(para, XHTML('blockquote'))
                emleft -= ems
        else:
            para = wrapper = etree.SubElement(parent, XHTML('p'))
        bstate.inline = bstate.para = para
        vspace = (bstate.vpadding + bstate.vmargin)
        bstate.vpadding = bstate.vmargin = 0
        if (tag not in TABLE_TAGS):
            if ((tag in ('ul', 'ol')) and (vspace > 0)):
                wrapper.addprevious(etree.Element(XHTML('div'), height=self.mobimlize_measure(vspace)))
            else:
                wrapper.attrib['height'] = self.mobimlize_measure(vspace)
            para.attrib['width'] = self.mobimlize_measure(indent)
        elif ((tag == 'table') and (vspace > 0)):
            vspace = int(round((vspace / self.profile.fbase)))
            while (vspace > 0):
                wrapper.addprevious(etree.Element(XHTML('br')))
                vspace -= 1
        if ((istate.halign != 'auto') and isinstance(istate.halign, (str, unicode))):
            para.attrib['align'] = istate.halign
    istate.rendered = True
    pstate = bstate.istate
    if (tag in CONTENT_TAGS):
        bstate.inline = para
        pstate = bstate.istate = None
        try:
            etree.SubElement(para, XHTML(tag), attrib=istate.attrib)
        except:
            print 'Invalid    subelement:', para, tag, istate.attrib
            raise
    elif (tag in TABLE_TAGS):
        para.attrib['valign'] = 'top'
    if istate.ids:
        for id_ in istate.ids:
            anchor = etree.Element(XHTML('a'), attrib={'id': id_})
            if (tag == 'li'):
                try:
                    last = bstate.body[(-1)][(-1)]
                except:
                    break
                last.insert(0, anchor)
                anchor.tail = last.text
                last.text = None
            else:
                last = bstate.body[(-1)]
                if (barename(last.tag) in LEAF_TAGS):
                    last.addprevious(anchor)
                else:
                    last.append(anchor)
        istate.ids.clear()
    if (not text):
        return
    if ((not pstate) or (istate != pstate)):
        inline = para
        fsize = istate.fsize
        href = istate.href
        if (not href):
            bstate.anchor = None
        elif (pstate and (pstate.href == href)):
            inline = bstate.anchor
        else:
            inline = etree.SubElement(inline, XHTML('a'), href=href)
            bstate.anchor = inline
        if (fsize != 3):
            inline = etree.SubElement(inline, XHTML('font'), size=str(fsize))
        if (istate.family == 'monospace'):
            inline = etree.SubElement(inline, XHTML('tt'))
        if istate.italic:
            inline = etree.SubElement(inline, XHTML('i'))
        if istate.bold:
            inline = etree.SubElement(inline, XHTML('b'))
        if ((istate.bgcolor is not None) and (istate.bgcolor != 'transparent')):
            inline = etree.SubElement(inline, XHTML('span'), bgcolor=convert_color_for_font_tag(istate.bgcolor))
        if (istate.fgcolor != 'black'):
            inline = etree.SubElement(inline, XHTML('font'), color=convert_color_for_font_tag(istate.fgcolor))
        if istate.strikethrough:
            inline = etree.SubElement(inline, XHTML('s'))
        if istate.underline:
            inline = etree.SubElement(inline, XHTML('u'))
        bstate.inline = inline
    bstate.istate = istate
    inline = bstate.inline
    content = (self.preize_text(text, pre_wrap=istate.pre_wrap) if (istate.preserve or istate.pre_wrap) else [text])
    for item in content:
        if isinstance(item, basestring):
            if (len(inline) == 0):
                inline.text = ((inline.text or '') + item)
            else:
                last = inline[(-1)]
                last.tail = ((last.tail or '') + item)
        else:
            inline.append(item)
