本文整理汇总了Python中docutils.core.Publisher.set_destination方法的典型用法代码示例。如果您正苦于以下问题:Python Publisher.set_destination方法的具体用法?Python Publisher.set_destination怎么用?Python Publisher.set_destination使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docutils.core.Publisher
的用法示例。
在下文中一共展示了Publisher.set_destination方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_sphinx
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def get_sphinx():
sphinx = getattr(local_data, 'sphinx', None)
if sphinx is None:
sphinx = Sphinx(tempdir, tempdir, tempdir,
tempdir, 'json', status=None, warning=None)
sphinx.builder.translator_class = CustomHTMLTranslator
sphinx.env.patch_lookup_functions()
sphinx.env.temp_data['docname'] = 'text'
sphinx.env.temp_data['default_domain'] = 'py'
pub = Publisher(reader=None,
parser=None,
writer=HTMLWriter(sphinx.builder),
source_class=io.StringInput,
destination_class=io.NullOutput)
pub.set_components('standalone', 'restructuredtext', None)
pub.process_programmatic_settings(None, sphinx.env.settings, None)
pub.set_destination(None, None)
sphinx.publisher = pub
local_data.sphinx = sphinx
return sphinx, sphinx.publisher
示例2: publish
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def publish(writer_name):
reader=None
reader_name='standalone'
parser=None
parser_name='restructuredtext'
writer=None
settings=None
settings_spec=None
settings_overrides=options[writer_name]
config_section=None
enable_exit=1
argv=[]
usage=default_usage
pub = Publisher(reader, parser, writer, settings=settings)
pub.set_components(reader_name, parser_name, writer_name)
settings = pub.get_settings(settings_spec=settings_spec,
config_section=config_section)
if settings_overrides:
settings._update(settings_overrides, 'loose')
source = file(source_path)
pub.set_source(source, source_path)
destination_path = 'pg1.' + extensions[writer_name]
destination = file(destination_path, 'w')
pub.set_destination(destination, destination_path)
pub.publish(argv, usage, description, settings_spec, settings_overrides,
config_section=config_section, enable_exit=enable_exit)
示例3: _docutils_rest_to
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def _docutils_rest_to(rest, writer):
""" Uses docutils to convert a ReST string to HTML. Returns a tuple
containg the HTML string and the list of warning nodes that were
removed from the HTML.
"""
# Make sure any Sphinx polution of docutils has been removed.
if Sphinx is not None:
for key, value in docutils_roles.items():
if value.__module__.startswith('sphinx'):
docutils_roles.pop(key)
pub = Publisher(source_class=docutils.io.StringInput,
destination_class=docutils.io.StringOutput)
pub.set_reader('standalone', None, 'restructuredtext')
pub.set_writer(writer)
pub.writer.default_stylesheet_path=''
pub.get_settings() # Get the default settings
pub.settings.halt_level = 6 # Don't halt on errors
pub.settings.warning_stream = StringIO()
pub.set_source(rest)
pub.set_destination()
pub.document = pub.reader.read(pub.source, pub.parser, pub.settings)
pub.apply_transforms()
# Walk the node structure of a docutils document and remove 'problematic'
# and 'system_message' nodes. Save the system_message nodes.
warning_nodes = []
for node in pub.document.traverse(docutils.nodes.problematic):
node.parent.replace(node, node.children[0])
for node in pub.document.traverse(docutils.nodes.system_message):
warning_nodes.append(node)
node.parent.remove(node)
return pub.writer.write(pub.document, pub.destination), warning_nodes
示例4: init_publisher
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def init_publisher():
from docutils.core import Publisher
from docutils.io import StringOutput
p = Publisher(destination_class=StringOutput,writer=g_writer)
p.get_settings()
p.set_components('standalone', 'restructuredtext', 'html')
p.set_destination(None, None)
return p
示例5: pypi_render
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def pypi_render(source):
"""
Copied (and slightly adapted) from pypi.description_tools
"""
ALLOWED_SCHEMES = '''file ftp gopher hdl http https imap mailto mms news
nntp prospero rsync rtsp rtspu sftp shttp sip sips snews svn svn+ssh
telnet wais irc'''.split()
settings_overrides = {
"raw_enabled": 0, # no raw HTML code
"file_insertion_enabled": 0, # no file/URL access
"halt_level": 2, # at warnings or errors, raise an exception
"report_level": 5, # never report problems with the reST code
}
# capture publishing errors, they go to stderr
old_stderr = sys.stderr
sys.stderr = s = StringIO.StringIO()
parts = None
try:
# Convert reStructuredText to HTML using Docutils.
document = publish_doctree(source=source,
settings_overrides=settings_overrides)
for node in document.traverse():
if node.tagname == '#text':
continue
if node.hasattr('refuri'):
uri = node['refuri']
elif node.hasattr('uri'):
uri = node['uri']
else:
continue
o = urlparse.urlparse(uri)
if o.scheme not in ALLOWED_SCHEMES:
raise TransformError('link scheme not allowed')
# now turn the transformed document into HTML
reader = readers.doctree.Reader(parser_name='null')
pub = Publisher(reader, source=io.DocTreeInput(document),
destination_class=io.StringOutput)
pub.set_writer('html')
pub.process_programmatic_settings(None, settings_overrides, None)
pub.set_destination(None, None)
pub.publish()
parts = pub.writer.parts
except:
pass
sys.stderr = old_stderr
# original text if publishing errors occur
if parts is None or len(s.getvalue()) > 0:
return None
else:
return parts['body']
示例6: process_description
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def process_description(source, output_encoding='unicode'):
"""Given an source string, returns an HTML fragment as a string.
The return value is the contents of the <body> tag.
Parameters:
- `source`: A multi-line text string; required.
- `output_encoding`: The desired encoding of the output. If a Unicode
string is desired, use the default value of "unicode" .
"""
# Dedent all lines of `source`.
source = trim_docstring(source)
settings_overrides = {
'raw_enabled': 0, # no raw HTML code
'file_insertion_enabled': 0, # no file/URL access
'halt_level': 2, # at warnings or errors, raise an exception
'report_level': 5, # never report problems with the reST code
}
parts = None
# Convert reStructuredText to HTML using Docutils.
document = publish_doctree(source=source,
settings_overrides=settings_overrides)
for node in document.traverse():
if node.tagname == '#text':
continue
if node.hasattr('refuri'):
uri = node['refuri']
elif node.hasattr('uri'):
uri = node['uri']
else:
continue
o = urlparse(uri)
if o.scheme not in ALLOWED_SCHEMES:
raise TransformError('link scheme not allowed: {0}'.format(uri))
# now turn the transformed document into HTML
reader = readers.doctree.Reader(parser_name='null')
pub = Publisher(reader, source=io.DocTreeInput(document),
destination_class=io.StringOutput)
pub.set_writer('html')
pub.process_programmatic_settings(None, settings_overrides, None)
pub.set_destination(None, None)
pub.publish()
parts = pub.writer.parts
output = parts['body']
if output_encoding != 'unicode':
output = output.encode(output_encoding)
return output
示例7: __init__
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def __init__(self):
# publisher is used for html generation
pub = Publisher(None, None, None, settings=None,
source_class=StringInput,
destination_class=StringOutput)
pub.set_components('standalone', 'restructuredtext', 'html')
pub.process_programmatic_settings(None, None, None)
pub.set_destination(None, None)
self.pub = pub
示例8: convert
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def convert(infilename, outfilename):
print "converting %s to %s" % (infilename, outfilename)
pub = Publisher()
pub.set_components('standalone', # reader
'restructuredtext', # parser
'latex') # writer (arg, will be discarded)
pub.reader = StandaloneReader()
pub.writer = VerseLaTeXWriter()
pub.process_programmatic_settings(None, None, None)
pub.set_source(source_path=infilename)
pub.set_destination(destination_path=outfilename)
pub.publish()
示例9: get_html
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def get_html(self, body_only=True, content_only=False, noclasses=False):
import sys
import pygments_rest
from docutils.core import Publisher
from docutils.io import StringInput, StringOutput
from cStringIO import StringIO
settings = {'doctitle_xform' : 1,
'pep_references' : 1,
'rfc_references' : 1,
'footnote_references': 'superscript',
'output_encoding' : 'unicode',
'report_level' : 2, # 2=show warnings, 3=show only errors, 5=off (docutils.utils
}
if content_only:
post_rst = self.get_rst(noclasses=noclasses)
else:
post_rst = render_to('post_single.rst',
post=self,
noclasses=noclasses)
pub = Publisher(reader=None,
parser=None,
writer=None,
settings=None,
source_class=StringInput,
destination_class=StringOutput)
pub.set_components(reader_name='standalone',
parser_name='restructuredtext',
writer_name='html')
pub.process_programmatic_settings(settings_spec=None,
settings_overrides=settings,
config_section=None)
pub.set_source(post_rst,source_path=self.module_path)
pub.set_destination(None, None)
errors_io = StringIO()
real_stderr = sys.stderr
sys.stderr = errors_io
try:
html_full = pub.publish(enable_exit_status=False)
html_body = ''.join(pub.writer.html_body)
finally:
sys.stderr = real_stderr
errors = errors_io.getvalue()
self._process_rest_errors(errors)
errors_io.close()
return html_body if body_only else html_full
示例10: render_readme_like_pypi
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def render_readme_like_pypi(source, output_encoding='unicode'):
"""
Render a ReST document just like PyPI does.
"""
# Dedent all lines of `source`.
source = trim_docstring(source)
settings_overrides = {
'raw_enabled': 0, # no raw HTML code
'file_insertion_enabled': 0, # no file/URL access
'halt_level': 2, # at warnings or errors, raise an exception
'report_level': 5, # never report problems with the reST code
}
parts = None
# Convert reStructuredText to HTML using Docutils.
document = publish_doctree(source=source,
settings_overrides=settings_overrides)
for node in document.traverse():
if node.tagname == '#text':
continue
if node.hasattr('refuri'):
uri = node['refuri']
elif node.hasattr('uri'):
uri = node['uri']
else:
continue
o = urlparse(uri)
if o.scheme not in ALLOWED_SCHEMES:
raise TransformError('link scheme not allowed: {0}'.format(uri))
# now turn the transformed document into HTML
reader = readers.doctree.Reader(parser_name='null')
pub = Publisher(reader, source=io.DocTreeInput(document),
destination_class=io.StringOutput)
pub.set_writer('html')
pub.process_programmatic_settings(None, settings_overrides, None)
pub.set_destination(None, None)
pub.publish()
parts = pub.writer.parts
output = parts['body']
if output_encoding != 'unicode':
output = output.encode(output_encoding)
return output
示例11: check_rst
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def check_rst(self, rst):
pub = Publisher(reader=None, parser=None, writer=None, settings=None,
source_class=io.StringInput,
destination_class=io.StringOutput)
pub.set_components(reader_name='standalone',
parser_name='restructuredtext',
writer_name='pseudoxml')
pub.process_programmatic_settings(
settings_spec=None,
settings_overrides={'output_encoding': 'unicode'},
config_section=None,
)
pub.set_source(rst, source_path=None)
pub.set_destination(destination=None, destination_path=None)
output = pub.publish(enable_exit_status=False)
self.assertLess(pub.document.reporter.max_level, 0)
return output, pub
示例12: render_map
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def render_map(builder, node):
if node:
doc = new_document(b('<partial node>'))
doc.append(node)
publisher = Publisher( source_class = DocTreeInput, destination_class=StringOutput)
publisher.set_components('standalone', 'restructuredtext', 'pseudoxml')
publisher.reader = DoctreeReader()
publisher.writer = DitaMapWriter(builder)
publisher.process_programmatic_settings(None, {'output_encoding': 'utf-8'}, None)
publisher.set_source(doc, None)
publisher.set_destination(None, None)
publisher.publish()
return publisher.writer.output
output = XML_HEAD
output += u"<map></map>"
return output
示例13: __init__
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
class HTMLGenerator:
'''Really simple HTMLGenerator starting from publish_parts.
It reuses the docutils.core.Publisher class, which means it is *not*
threadsafe.
'''
def __init__(self,
settings_spec=None,
settings_overrides=dict(report_level=5, halt_level=5),
config_section='general'):
self.pub = Publisher(reader=None, parser=None, writer=None,
settings=None,
source_class=io.StringInput,
destination_class=io.StringOutput)
self.pub.set_components(reader_name='standalone',
parser_name='restructuredtext',
writer_name='html')
# hack: JEP-0071 does not allow HTML char entities, so we hack our way
# out of it.
# — == u"\u2014"
# a setting to only emit charater entities in the writer would be nice
# FIXME: several are emitted, and they are explicitly forbidden
# in the JEP
# == u"\u00a0"
self.pub.writer.translator_class.attribution_formats['dash'] = (
u'\u2014', '')
self.pub.process_programmatic_settings(settings_spec,
settings_overrides,
config_section)
def create_xhtml(self, text,
destination=None,
destination_path=None,
enable_exit_status=None):
''' Create xhtml for a fragment of IM dialog.
We can use the source_name to store info about
the message.'''
self.pub.set_source(text, None)
self.pub.set_destination(destination, destination_path)
output = self.pub.publish(enable_exit_status=enable_exit_status)
# kludge until we can get docutils to stop generating (rare)
# entities
return u'\u00a0'.join(self.pub.writer.parts['fragment'].strip().split(
' '))
示例14: aoeuaoeuaoeu_docs
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def aoeuaoeuaoeu_docs(f):
'''
I couldn't figure out how to use the docutils docstring parser, so I wrote
my own. Can somebody show me the right way to do this?
'''
raise NotImplementedError
from docutils.core import Publisher
from docutils.io import StringInput
pub = Publisher(None, None, None, settings = settings,
source_class = StringInput,
destination_class = destination_class)
pub.set_components('standalone', 'restructuredtext', 'pseudoxml')
pub.process_programmatic_settings(
settings_spec, settings_overrides, config_section)
pub.set_source(f.__doc__, f.__name__)
pub.set_destination(None, f.__name__)
output = pub.publish(enable_exit_status = False)
return output, pub
return publish_parts(f.__doc__, source_class = StringInput, source_path = f.__name__)
示例15: parts_from_doctree
# 需要导入模块: from docutils.core import Publisher [as 别名]
# 或者: from docutils.core.Publisher import set_destination [as 别名]
def parts_from_doctree(document, destination_path=None,
writer=Writer(), writer_name='html4css2',
settings=None, settings_spec=None,
settings_overrides=None, config_section=None,
enable_exit_status=None):
"""
A version of :func:`docutils.core.publish_from_doctree` that supplies
the "parts" rather than a regular string representation.
"""
reader = Reader(parser_name='null')
pub = Publisher(reader, None, writer,
source=io.DocTreeInput(document),
destination_class=io.StringOutput, settings=settings)
if not writer and writer_name:
pub.set_writer(writer_name)
pub.process_programmatic_settings(
settings_spec, settings_overrides, config_section)
pub.set_destination(None, destination_path)
output = pub.publish()
return pub.writer.parts