Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test(self, data):
content_type = data.get("long_description_content_type", None)
if content_type in ("text/plain", "text/markdown"):
# These can't fail. Markdown will just assume everything
# it doesn't understand is plain text.
return True
# This should be ReStructuredText
source = data.get("long_description", "")
stream = io.StringIO()
settings = {"warning_stream": stream}
try:
publish_parts(
source=source, writer_name="html4css1", settings_overrides=settings
)
except SystemMessage as e:
self._message = e.args[0]
errors = stream.getvalue().strip()
if not errors:
return True
self._message = "\n" + errors
return False
def rst_to_html_fragment(source):
parts = docutils.core.publish_parts(source=source, writer_name="html")
return parts["body_pre_docinfo"] + parts["fragment"]
def convert_doc(rst_string, view_file_url=None):
""" Utility to load an RST file and turn it into fancy HTML. """
rst = modify_rst(rst_string, view_file_url)
overrides = {"report_level": "quiet"}
try:
html = docutils.core.publish_parts(
source=rst, writer_name="html", settings_overrides=overrides
)
except Exception:
return "<pre>%s</pre>" % jinja2.escape(rst)
else:
html_string = html["html_body"]
html_string = modify_html(html_string)
html_string = markupsafe.Markup(html_string)
return html_string
def render_markup(markup, raw_data):
data = raw_data
if markup == Article.MARKUP_MARKDOWN:
data = markdown(data)
elif markup == Article.MARKUP_RST:
data = publish_parts(source=data, writer_name="html4css1")["fragment"]
elif markup == Article.MARKUP_TEXTILE:
data = textile(data)
data = html_parser(data)
return data
(backref, message)
for backref in backrefs)))
else:
span = ('<span title="%s" class="system-message">?</span>' %
message)
self.body.append(span)
def depart_system_message(self, node):
pass
writer = html4css1.Writer()
writer.translator_class = TracHTMLTranslator
inliner = rst.states.Inliner()
inliner.trac = (self.env, context)
parser = rst.Parser(inliner=inliner)
content = content_to_unicode(self.env, content, mimetype)
parts = publish_parts(content, writer=writer, parser=parser,
settings_overrides={'halt_level': 6,
'warning_stream': False,
'file_insertion_enabled': 0,
'raw_enabled': 0,
'warning_stream': False})
return parts['html_body']
def apply_restructuretext(content, page_msg):
try:
from docutils.core import publish_parts
except ImportError:
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", {}
)
parts = publish_parts(
source=content, writer_name="html4css1",
settings_overrides=docutils_settings
)
return parts["fragment"]
rst_input = '\n'.join(rst_input)
rst_error = None
parsing_error = None
settings_overrides = {
'initial_header_level': '2',
'traceback': True,
'warning_stream': StringIO(),
'embed_stylesheet': False,
'dump_settings': False,
'halt_level': 3,
}
try:
return publish_parts(
settings_overrides=settings_overrides,
writer=Writer(),
source=rst_input,
)
except SystemMessage as e:
rst_error = e
# parse docutils.utils.SystemMessage and re-raise SystemMessage
# on parsing error
try:
result = system_message_re.search(rst_error.args[0])
result = result.groupdict()
result['level'] = int(result['level'])
result['line'] = int(result['line'])
result_file = "%s.html" % basename
with codecs.open(result_file, 'w', encoding='utf-8') as result:
#print "%s => %s" % (rst_file, result_file)
path_to_root = normalize(os.path.dirname(os.path.relpath(main_path, result_file)))
path_to_root = "./" if path_to_root == '' else path_to_root
#print "path_to_root: %s" % path_to_root
path_to_css = normalize(os.path.dirname(os.path.relpath(__thisfile__, result_file)))
path_to_css = "./" if path_to_css == '' else path_to_css
#print "path_to_css: %s" % path_to_css
# get the rst file contents and render it into parts
rst = open(rst_file).read()
parts = publish_parts(source=rst, writer_name='html')
# get the main template's content and render it
template = open('templates/layout.html').read()
t = Template(template)
# sub-page navigation
navpy_path = os.path.join(os.path.dirname(result_file), PAGE_NAV_FILE)
subpage_nav = None
if os.path.exists(navpy_path):
navpy = eval(open(navpy_path).read().replace("\r",''))
nav_vars = {
'this_page': os.path.relpath(result_file, os.path.dirname(navpy_path)),
'nav': navpy,
}
subpage_nav = Template(open('templates/nav.html').read()).render(nav_vars)
def _format_rest(self, doc):
try:
from docutils.core import publish_parts
except ImportError:
raise DataError("reST format requires 'docutils' module to be installed.")
parts = publish_parts(doc, writer_name='html',
settings_overrides={'syntax_highlight': 'short'})
return self._format_html(parts['html_body'])
summary = ''
finally:
fp.close()
description = None
filename = docs.get('meta.description', 'DESCRIPTION').strip()
if filename and _os.path.isfile(filename):
fp = open(filename)
try:
description = fp.read().rstrip()
finally:
fp.close()
if summary is None and description:
from docutils import core
summary = core.publish_parts(
source=description,
source_path=filename,
writer_name='html',
)['title'].encode('utf-8')
return summary, description