def get_long_description(package_root, filename="README.creole", raise_errors=None): """ read README file and convert it on-the-fly to ReStructuredText. """ if raise_errors is None: raise_errors = should_raise_errors() if raise_errors: # raise a error if a unknown node found unknown_emit = raise_unknown_node else: # ignore unknown nodes unknown_emit = transparent_unknown_nodes filepath = os.path.join(package_root, filename) long_description_origin = "" try: # Read creole README f = codecs.open(filepath, "r", encoding="utf-8") long_description_origin = f.read().strip() f.close() # convert creole into html long_description_html = creole2html(long_description_origin) # convert html to ReSt long_description_rest_unicode = html2rest( long_description_html, emitter_kwargs={"unknown_emit":unknown_emit} ) if PY3: long_description_rest = long_description_rest_unicode else: long_description_rest = long_description_rest_unicode.encode("utf-8") except Exception: if raise_errors: raise # Don't raise the error e.g. in ./setup install process evalue = sys.exc_info()[1] long_description_rest = "[Error: %s]\n%s" % ( evalue, long_description_origin ) else: if raise_errors: # Test created ReSt code from creole.rest2html.clean_writer import rest2html try: rest2html(long_description_rest_unicode, traceback=True, enable_exit_status=1, exit_status_level=2) except SystemExit as e: print("Error creole2rest self test failed: rest2html() exist with status code: %s" % e.args[0]) raise return long_description_rest
def assert_rest2html(self, rest_string, html_string, \ strip_lines=False, debug=False, prepare_strings=True): # compare rest -> html if not REST_INSTALLED: warnings.warn("Skip ReSt test. Please install Docutils.") return if prepare_strings: rest_string = self._prepare_text(rest_string) html_string = self._prepare_text(html_string) html = rest2html(rest_string) if debug: print(rest_string) print(html_string) print(html) html = html.strip() # html = html.replace("<br />", "<br />\n") # html = tabs2spaces(html) if strip_lines: html = strip_html_lines(html, strip_lines) self.assertEqual(html, html_string, msg="rest2html")
def test_get_long_description_without_raise_errors(self): long_description = get_long_description(CREOLE_PACKAGE_ROOT, raise_errors=False) self.assertIn("=====\nabout\n=====\n\n", long_description) # Test created ReSt code from creole.rest2html.clean_writer import rest2html html = rest2html(long_description) self.assertIn("<h1>about</h1>\n", html)
def render(readme_info): """ Turns a readme file into HTML :param readme_info: The dict from ReadmeClient, containing the keys: `format` - one of `markdown`, `textile`, `creole`, `rst`, `txt` `contents` - unicode/str contents of readme :return: An HTML string """ contents = re.sub('\r\n', '\n', readme_info['contents']) if readme_info['format'] == 'markdown': contents = contents.replace("\t", ' ') output = _markdown(contents) elif readme_info['format'] == 'textile': output = textile.textile(contents, html_type='html') elif readme_info['format'] == 'creole': output = creole2html(contents) elif readme_info['format'] == 'rst': output = rest2html(contents, report_level=4) # Everything else is treated as txt else: output = contents for char, entity in _entities: output = output.replace(char, entity) output = output.replace('\n', '<br>\n') output = sanitize(output) if output.find('src=') != -1 or output.find('href=') != -1: url_dirname = os.path.dirname(readme_info['url']) + '/' src_dirname = url_dirname href_dirname = url_dirname github_match = re.match( 'https://raw.githubusercontent.com(/[^/]+/[^/]+/)(.*$)', url_dirname) if github_match is not None: href_dirname = 'https://github.com' + github_match.group( 1) + 'blob/' + github_match.group(2) output = re.sub('(<img\\s+[^>]*\\bsrc=["\'])(?!http://|https://|/)', '\\1' + src_dirname, output, 0, re.I) output = re.sub( '(<a\\s+[^>]*\\bhref=["\'])(?!http://|https://|/|mailto:|#)', '\\1' + href_dirname, output, 0, re.I) return output
def apply_restructuretext(content, page_msg): from creole.exceptions import DocutilsImportError try: from creole.rest2html.clean_writer import rest2html except DocutilsImportError: page_msg("Markup error: The Python docutils library isn't installed." " Download: http://docutils.sourceforge.net/") return fallback_markup(content) else: #docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}) rest = rest2html(content) return rest
def send(self, from_addr, recipients, data): # Ensure the message complies with RFC2822: use CRLF line endings message = data['msg'] data = data['data'] params = {} changes_body = data['changes_body'] if changes_body: params['changes_body'] = rest2html(changes_body) if data['ticket']['new']: params['ticket_new'] = True params['ticket_body_hdr'] = data['ticket_body_hdr'] params['ticket_link'] = data['ticket']['link'] params['ticket_reporter'] = data['ticket']['reporter'] params['ticket_owner'] = data['ticket']['owner'] params['ticket_description'] = self.wiki2html(data['ticket']['description']) params['ticket_type'] = data['ticket']['type'] params['ticket_status'] = data['ticket']['status'] params['ticket_priority'] = data['ticket']['priority'] params['ticket_milestone'] = data['ticket'].get('milestone') params['change_author'] = data['change'].get('author','') params['change_comment'] = self.wiki2html(data['change'].get('comment','')) params['project_name'] = data['project']['name'] params['project_url'] = data['project']['url'] merged_params = [] for k,v in params.items(): merged_params.append({'name': k, 'content':v}) mandrill_client = mandrill.Mandrill(self.smtp_password) message = {'auto_html': None, 'auto_text': None, 'from_email': from_addr, 'from_name': 'RedTurtle Team', 'headers': {'Reply-To': from_addr}, 'important': True, 'inline_css': True, 'global_merge_vars': merged_params, 'subject': unicode(message['subject']), 'to': [], } for rec in recipients: message['to'].append({'email':rec}) self.log.info("Sending notification through Mandril API to %s" % recipients) mandrill_client.messages.send_template(template_name='ticket', template_content=[], message=message)
def apply_restructuretext(content, page_msg): from creole.exceptions import DocutilsImportError try: from creole.rest2html.clean_writer import rest2html except DocutilsImportError: page_msg( "Markup error: The Python docutils library isn't installed." " Download: http://docutils.sourceforge.net/" ) return fallback_markup(content) else: #docutils_settings = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}) rest = rest2html(content) return rest
def render(readme_info): """ Turns a readme file into HTML :param readme_info: The dict from ReadmeClient, containing the keys: `format` - one of `markdown`, `textile`, `creole`, `rst`, `txt` `contents` - unicode/str contents of readme :return: An HTML string """ contents = re.sub("\r\n", "\n", readme_info["contents"]) if readme_info["format"] == "markdown": contents = contents.replace("\t", " ") output = _markdown(contents) elif readme_info["format"] == "textile": output = textile.textile(contents, html_type="html") elif readme_info["format"] == "creole": output = creole2html(contents) elif readme_info["format"] == "rst": output = rest2html(contents, report_level=4) # Everything else is treated as txt else: output = contents for char, entity in _entities: output = output.replace(char, entity) output = output.replace("\n", "<br>\n") output = sanitize(output) if output.find("src=") != -1 or output.find("href=") != -1: url_dirname = os.path.dirname(readme_info["url"]) + "/" src_dirname = url_dirname href_dirname = url_dirname github_match = re.match("https://raw.githubusercontent.com(/[^/]+/[^/]+/)(.*$)", url_dirname) if github_match is not None: href_dirname = "https://github.com" + github_match.group(1) + "blob/" + github_match.group(2) output = re.sub("(<img\\s+[^>]*\\bsrc=[\"'])(?!http://|https://|/)", "\\1" + src_dirname, output, 0, re.I) output = re.sub( "(<a\\s+[^>]*\\bhref=[\"'])(?!http://|https://|/|mailto:|#)", "\\1" + href_dirname, output, 0, re.I ) return output
def render(readme_info): """ Turns a readme file into HTML :param readme_info: The dict from ReadmeClient, containing the keys: `format` - one of `markdown`, `textile`, `creole`, `rst`, `txt` `contents` - unicode/str contents of readme :return: An HTML string """ contents = re.sub('\r\n', '\n', readme_info['contents']) if readme_info['format'] == 'markdown': contents = contents.replace("\t", ' ') output = _markdown(contents) elif readme_info['format'] == 'textile': output = textile.textile(contents, html_type='html') elif readme_info['format'] == 'creole': output = creole2html(contents) elif readme_info['format'] == 'rst': output = rest2html(contents, report_level=4) # Everything else is treated as txt else: output = contents for char, entity in _entities: output = output.replace(char, entity) output = output.replace('\n', '<br>\n') output = sanitize(output) if output.find('src=') != -1 or output.find('href=') != -1: url_dirname = os.path.dirname(readme_info['url']) + '/' output = re.sub('(<img\\s+[^>]*\\bsrc=["\'])(?!http://|https://|/)', '\\1' + url_dirname, output, 0, re.I) output = re.sub('(<a\\s+[^>]*\\bhref=["\'])(?!http://|https://|/)', '\\1' + url_dirname, output, 0, re.I) return output
# coding: utf-8 #https://github.com/jedie/python-creole #convert a rest file to HTML (for blog) import sys import os.path import re from creole.rest2html.clean_writer import rest2html if len(sys.argv) < 2: print "Usage: %s [filename]" % __file__ sys.exit() try: with open(sys.argv[1], "r") as text: path, name = os.path.split(sys.argv[1]) name = "%s.html" % name.split('.')[0] f = open(name, "w+") text = re.sub("tt>", "code>", rest2html(text.read())) f.write(text) f.close() print "Wrote new file %s" % name except IOError as e: print "File not found!" sys.exit()