本文整理汇总了Python中docutils.parsers.rst.Parser类的典型用法代码示例。如果您正苦于以下问题:Python Parser类的具体用法?Python Parser怎么用?Python Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
def parse(self, source, document):
""" Parse ``source``, write results to ``document``.
"""
# This is lame, but seems to be required for python 2
source = CODING.sub("", source)
env = document.settings.env
filename = env.doc2path(env.docname) # e.g. full path to docs/user_guide/examples/layout_vertical
# This code splits the source into two parts: the docstring (or None if
# there is not one), and the remaining source code after
m = ast.parse(source)
docstring = ast.get_docstring(m)
if docstring is not None:
lines = source.split("\n")
lineno = m.body[0].lineno # assumes docstring is m.body[0]
source = "\n".join(lines[lineno:])
js_name = "bokeh-plot-%s.js" % hashlib.md5(env.docname.encode('utf-8')).hexdigest()
(script, js, js_path, source) = _process_script(source, filename, env.bokeh_plot_auxdir, js_name)
env.bokeh_plot_files[env.docname] = (script, js, js_path, source)
rst = PLOT_PAGE.render(source=source,
filename=basename(filename),
docstring=docstring,
script=script)
document['bokeh_plot_include_bokehjs'] = True
# can't use super, Sphinx Parser classes don't inherit object
Parser.parse(self, rst, document)
示例2: run
def run(self):
indexnode, node = super(DjangoAdminModel, self).run()
sig = self.arguments[0]
lst = []
if not 'noautodoc' in self.options:
exclude = [
a.strip() for a in self.options.get('exclude', '').split(',')
]
app_label, model_name = sig.split('.')
for name, opts in model_attributes(app_label, model_name).items():
if name in exclude:
continue
lst.append(".. djangoadmin:attribute:: %s.%s" % (sig, name))
lst.append('')
lst.append(" %s" % unicode(opts['description']))
lst.append('')
text = '\n'.join(lst)
new_doc = new_document('temp-string', self.state.document.settings)
parser = Parser()
parser.parse(text, new_doc)
container = nodes.container()
container.extend(new_doc.children)
node[1].extend(container)
return [indexnode, node]
示例3: _check_rst_data
def _check_rst_data(self, data):
"""Returns warnings when the provided data doesn't compile."""
source_path = StringIO()
parser = Parser()
settings = frontend.OptionParser().get_default_values()
settings.tab_width = 4
settings.pep_references = None
settings.rfc_references = None
reporter = SilentReporter(source_path,
settings.report_level,
settings.halt_level,
stream=settings.warning_stream,
debug=settings.debug,
encoding=settings.error_encoding,
error_handler=settings.error_encoding_error_handler)
document = nodes.document(settings, reporter, source=source_path)
document.note_source(source_path, -1)
try:
parser.parse(data, document)
except AttributeError:
reporter.messages.append((-1, 'Could not finish the parsing.',
'', {}))
return reporter.messages
示例4: parse_
def parse_(rst):
document = utils.new_document(b'test data', settings)
document['file'] = 'dummy'
parser = RstParser()
parser.parse(rst, document)
for msg in document.traverse(nodes.system_message):
if msg['level'] == 1:
msg.replace_self([])
return document
示例5: parse
def parse(filehandle):
""" Parse a document read from the given filehandle into a
:class:`dmr.data.Document` object.
The document must contain:
* A top-level title, the resume owner's name;
* A :class:`docutils.nodes.line_block` containing contact
information for the resume, to be parsed with
:func:`dmr.data.Contact.parse`; and
* Any number of subsections that conform to the restrictions of
the various :class:`dmr.data.Section` subclasses.
:param filehandle: The file-like object to parse the document from.
:type filehandle: file
:returns: :class:`dmr.data.Document`
"""
parser = Parser()
settings = OptionParser(components=(Parser,)).get_default_values()
logger.info("Parsing document from %s" % filehandle.name)
document = new_document(filehandle.name, settings)
try:
parser.parse(filehandle.read(), document)
except IOError:
fatal("Could not parse %s: %s" % (filehandle.name, sys.exc_info()[1]))
top = None
options = dict()
for child in document.children:
if isinstance(child, docutils.nodes.Structural):
if top:
fatal("Document must have exactly one top-level heading")
top = child
elif isinstance(child, docutils.nodes.comment):
contents = child_by_class(child, docutils.nodes.Text)
if contents and contents.startswith("options"):
opts = contents.splitlines()
try:
# see if this is a format-specific option block
ofmt = opts[0].split("=")[1]
logger.debug("Found document options for %s: %s" %
(ofmt, opts[1:]))
except IndexError:
ofmt = None
logger.debug("Found default document options: %s" %
opts[1:])
options[ofmt] = opts[1:]
else:
logger.info("Skipping unknown node %s" % child)
for ofmt in [None, config.format]:
if ofmt in options:
parse_document_options(options[ofmt])
doc = Document.parse(top)
doc.source = document
return doc
示例6: run
def run(self, cmd, code):
"""Attempt to parse code as reStructuredText."""
import docutils
from docutils.nodes import Element
from docutils.parsers.rst import Parser
# Generate a new parser
parser = Parser()
settings = docutils.frontend.OptionParser(
components=(docutils.parsers.rst.Parser,)
).get_default_values()
document = docutils.utils.new_document(None, settings=settings)
document.reporter.stream = None
document.reporter.halt_level = 5
# Collect errors via an observer
def error_collector(data):
# Mutate the data since it was just generated
data.type = data['type']
data.level = data['level']
data.message = Element.astext(data.children[0])
data.full_message = Element.astext(data)
# Save the error
errors.append(data)
errors = []
document.reporter.attach_observer(error_collector)
parser.parse(code, document)
for data in errors:
message = data.message.replace("\n", " ")
if 'Unknown directive type' in message:
continue
if 'Unknown interpreted text role' in message:
continue
if 'Substitution definition contains illegal element' in message:
# there will be error message for the contents it
# self so let's ignore it.
continue
if data.level >= 3:
error_type = highlight.ERROR
else:
error_type = highlight.WARNING
if data.level <= 1:
return
line = data['line'] - 1
self.highlight.range(line, 0, error_type=error_type)
self.error(line, 0, message, error_type)
示例7: make_citation
def make_citation(label, text, settings):
name = fully_normalize_name(label)
citation = nodes.citation(text)
citation += nodes.label('', label)
new_doc = new_document('temp-string', settings)
parser = Parser()
parser.parse(text, new_doc)
citation['names'].append(name)
citation += new_doc.children
return citation
示例8: main
def main():
# process cmdline arguments:
inputFile, outputFile, outputFormat, optargs = getArgs()
settings = OptionParser(components=(Parser,)).get_default_values()
settings.debug = optargs['debug']
parser = Parser()
input = inputFile.read()
document = new_document(inputFile.name, settings)
parser.parse(input, document)
output = format(outputFormat, input, document, optargs)
outputFile.write(output)
if optargs['attributes']:
import pprint
pprint.pprint(document.__dict__)
示例9: parse_rst_content
def parse_rst_content(filename, source_content):
settings = OptionParser(components=(Parser,)).get_default_values()
parser = Parser()
document = new_document(filename, settings)
parser.parse(source_content, document)
def _walk(node):
if type(node) is Text:
yield node
if not isinstance(node, ignored_node_types):
for child in node.children:
for n in _walk(child):
yield n
return ' '.join(n for n in _walk(document))
示例10: run
def run(self):
rawtext = """
.. image:: /../images/examples/%s.jpg
:width: %d
**Running the Example**::
openrave.py --example %s
"""%(self.arguments[0],self.options.get('image-width',640),self.arguments[0])
parser = Parser()
document = docutils.utils.new_document("<partial node>")
document.settings = self.state.document.settings
parser.parse(rawtext,document)
return document.children
示例11: parse_rst
def parse_rst(rst_string):
from abjad.tools import abjadbooktools
parser = Parser()
directives.register_directive(
'abjad', abjadbooktools.AbjadDirective,
)
directives.register_directive(
'import', abjadbooktools.ImportDirective,
)
directives.register_directive('shell', abjadbooktools.ShellDirective)
settings = OptionParser(components=(Parser,)).get_default_values()
document = new_document('test', settings)
parser.parse(rst_string, document)
document = parser.document
return document
示例12: run
def run(self): # check if there are spaces in the notebook name
md_path = self.arguments[0]
if ' ' in md_path: raise ValueError(
"Due to issues with docutils stripping spaces from links, white "
"space is not allowed in notebook filenames '{0}'".format(md_path))
# check if raw html is supported
if not self.state.document.settings.raw_enabled:
raise self.warning('"%s" directive disabled.' % self.name)
# get path to markdown file
md_filename = self.arguments[0]
md_dir = os.path.join(setup.confdir, '..')
md_abs_path = os.path.abspath(os.path.join(md_dir, md_filename))
with open(md_abs_path) as file:
source = file.read()
ptype = self.pandoc_from
if 'from' in self.options:
ptype = self.options['from']
if ptype is '':
ptype = 'markdown'
# if ptype != '':
# arglist = ['pandoc', '--from=' + ptype, '--to=rst', md_abs_path]
# else:
# arglist = ['pandoc', '--to=rst', md_abs_path]
#
# p = subprocess.Popen(arglist,
# stdout=subprocess.PIPE,
# stderr=subprocess.PIPE
# )
#
# out, err = p.communicate()
#
# print(out)
out = pandoc(source, ptype, 'rst')
settings = OptionParser(components=(Parser,)).get_default_values()
parser = Parser()
document = new_document('DOC', settings)
parser.parse(out, document)
return [node for node in document]
示例13: rest_document
def rest_document(filename):
"""Return an reST document.
"""
from docutils.parsers.rst import Parser
file = open(filename)
try:
text = file.read()
finally:
file.close()
parser = Parser()
docroot = docutils.utils.new_document()
parser.parse(text,docroot)
return docroot
示例14: auto_code_block
def auto_code_block(self, node):
"""Try to automatically generate nodes for codeblock syntax.
Parameters
----------
node : nodes.literal_block
Original codeblock node
Returns
-------
tocnode: docutils node
The converted toc tree node, None if conversion is not possible.
"""
assert isinstance(node, nodes.literal_block)
original_node = node
if 'language' not in node:
return None
self.state_machine.reset(self.document,
node.parent,
self.current_level)
content = node.rawsource.split('\n')
language = node['language']
if language == 'math':
if self.config['enable_math']:
return self.state_machine.run_directive(
'math', content=content)
elif language == 'eval_rst':
if self.config['enable_eval_rst']:
# allow embed non section level rst
node = nodes.section()
self.state_machine.state.nested_parse(
StringList(content, source=original_node.source),
0, node=node, match_titles=False)
return node.children[:]
else:
match = re.search('[ ]?[\w_-]+::.*', language)
if match:
parser = Parser()
new_doc = new_document(None, self.document.settings)
newsource = u'.. ' + match.group(0) + '\n' + node.rawsource
parser.parse(newsource, new_doc)
return new_doc.children[:]
else:
return self.state_machine.run_directive(
'code-block', arguments=[language],
content=content)
return None
示例15: parse_entries
def parse_entries(input_file):
global all_months, all_entries
file = codecs.open(input_file, 'r', 'utf-8')
try:
text = file.read()
finally:
file.close()
parser = Parser()
settings = OptionParser(
components=(Parser,
docutils.writers.html4css1.Writer)).get_default_values()
docroot = docutils.utils.new_document(file.name, settings)
parser.parse(text, docroot)
for i in docroot.traverse(condition=docutils.nodes.section):
try:
date_string = re.findall(r'(\d{4}-\d{1,2}-\d{1,2})',
str(i.children[0]))[0]
logging.debug("Found entry: %s" % date_string)
date = datetime.strptime(date_string, "%Y-%m-%d")
except IndexError:
sys.stderr.write("can not parse section : %s\n" %
str(i.children[0]))
sys.exit(1)
translator = HTMLTranslator(docroot)
i.walkabout(translator)
body = ''.join(translator.body)
entry = Entry(date, body)
all_months.add(entry.month)
all_entries[entry.month].append(entry)
all_months = sorted(all_months, reverse=True)