def html_to_rst(html, indent=0, indent_first=False): """ Use bcdoc to convert html to rst. :type html: string :param html: Input HTML to be converted :type indent: int :param indent: Number of spaces to indent each line :type indent_first: boolean :param indent_first: Whether to indent the first line :rtype: string """ doc = ReSTDocument() # TODO: Remove me, temp workaround to fix doc building # because of smart quotes that aren't currently supported. html = html.replace(u'\u2019', "'") html = html.replace(u'\u2014', '-') doc.include_doc_string(html) rst = doc.getvalue().decode('utf-8') if indent: rst = '\n'.join([(' ' * indent) + line for line in rst.splitlines()]) if not indent_first: rst = rst.strip() return rst
def test_remove_doc_string(self): doc = ReSTDocument() doc.writeln('foo') doc.include_doc_string('<p>this is a <code>test</code></p>') doc.remove_last_doc_string() self.assertEqual(doc.getvalue(), six.b('foo\n'))
def test_include_doc_string(self): doc = ReSTDocument() doc.include_doc_string('<p>this is a <code>test</code></p>') self.assertEqual(doc.getvalue(), six.b('\n\nthis is a ``test`` \n\n'))