本文整理汇总了Python中docutils.parsers.rst.roles.role方法的典型用法代码示例。如果您正苦于以下问题:Python roles.role方法的具体用法?Python roles.role怎么用?Python roles.role使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类docutils.parsers.rst.roles
的用法示例。
在下文中一共展示了roles.role方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def run(self):
if not self.arguments:
if '' in roles._roles:
# restore the "default" default role
del roles._roles['']
return []
role_name = self.arguments[0]
role, messages = roles.role(role_name, self.state_machine.language,
self.lineno, self.state.reporter)
if role is None:
error = self.state.reporter.error(
'Unknown interpreted text role "%s".' % role_name,
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
roles._roles[''] = role
# @@@ should this be local to the document, not the parser?
return messages
示例2: run
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def run(self):
if not self.arguments:
if '' in roles._roles:
# restore the "default" default role
del roles._roles['']
return []
role_name = self.arguments[0]
role, messages = roles.role(role_name, self.state_machine.language,
self.lineno, self.state.reporter)
if role is None:
error = self.state.reporter.error(
'Unknown interpreted text role "%s".' % role_name,
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
roles._roles[''] = role
return messages
示例3: interpreted
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def interpreted(self, rawsource, text, role, lineno):
role_fn, messages = roles.role(role, self.language, lineno,
self.reporter)
if role_fn:
nodes, messages2 = role_fn(role, rawsource, text, lineno, self)
try:
nodes[0][0].rawsource = unescape(text, True)
except IndexError:
pass
return nodes, messages + messages2
else:
msg = self.reporter.error(
'Unknown interpreted text role "%s".' % role,
line=lineno)
return ([self.problematic(rawsource, rawsource, msg)],
messages + [msg])
示例4: interpreted
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def interpreted(self, rawsource, text, role, lineno):
role_fn, messages = roles.role(role, self.language, lineno,
self.reporter)
if role_fn:
nodes, messages2 = role_fn(role, rawsource, text, lineno, self)
return nodes, messages + messages2
else:
msg = self.reporter.error(
'Unknown interpreted text role "%s".' % role,
line=lineno)
return ([self.problematic(rawsource, rawsource, msg)],
messages + [msg])
示例5: run
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def run(self):
"""Dynamically create and register a custom interpreted text role."""
if self.content_offset > self.lineno or not self.content:
raise self.error('"%s" directive requires arguments on the first '
'line.' % self.name)
args = self.content[0]
match = self.argument_pattern.match(args)
if not match:
raise self.error('"%s" directive arguments not valid role names: '
'"%s".' % (self.name, args))
new_role_name = match.group(1)
base_role_name = match.group(3)
messages = []
if base_role_name:
base_role, messages = roles.role(
base_role_name, self.state_machine.language, self.lineno,
self.state.reporter)
if base_role is None:
error = self.state.reporter.error(
'Unknown interpreted text role "%s".' % base_role_name,
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
else:
base_role = roles.generic_custom_role
assert not hasattr(base_role, 'arguments'), (
'Supplemental directive arguments for "%s" directive not '
'supported (specified by "%r" role).' % (self.name, base_role))
try:
converted_role = convert_directive_function(base_role)
(arguments, options, content, content_offset) = (
self.state.parse_directive_block(
self.content[1:], self.content_offset, converted_role,
option_presets={}))
except states.MarkupError, detail:
error = self.state_machine.reporter.error(
'Error in "%s" directive:\n%s.' % (self.name, detail),
nodes.literal_block(self.block_text, self.block_text),
line=self.lineno)
return messages + [error]
示例6: reset
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def reset(self, document, parent, level):
"""Reset the state of state machine.
After reset, self and self.state can be used to
passed to docutils.parsers.rst.Directive.run
Parameters
----------
document: docutils document
Current document of the node.
parent: parent node
Parent node that will be used to interpret role and directives.
level: int
Current section level.
"""
self.language = languages.get_language(
document.settings.language_code)
# setup memo
self.memo.document = document
self.memo.reporter = document.reporter
self.memo.language = self.language
self.memo.section_level = level
# setup inliner
if self.memo.inliner is None:
self.memo.inliner = Inliner()
self.memo.inliner.init_customizations(document.settings)
inliner = self.memo.inliner
inliner.reporter = document.reporter
inliner.document = document
inliner.language = self.language
inliner.parent = parent
# setup self
self.document = document
self.reporter = self.memo.reporter
self.node = parent
self.state.runtime_init()
self.input_lines = document['source']
示例7: run_role
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def run_role(self, name,
options=None,
content=None):
"""Generate a role node.
options : dict
key value arguments.
content : content
content of the directive
Returns
-------
node : docutil Node
Node generated by the arguments.
"""
if options is None:
options = {}
if content is None:
content = []
role_fn, _ = role(name,
self.language,
self.node.line,
self.reporter)
vec, _ = role_fn(name,
rawtext=str(content),
text=str(content),
lineno=self.node.line,
inliner=self.memo.inliner,
options=options,
content=content)
assert len(vec) == 1, 'only support one list in role'
return vec[0]
示例8: interpreted_or_phrase_ref
# 需要导入模块: from docutils.parsers.rst import roles [as 别名]
# 或者: from docutils.parsers.rst.roles import role [as 别名]
def interpreted_or_phrase_ref(self, match, lineno):
end_pattern = self.patterns.interpreted_or_phrase_ref
string = match.string
matchstart = match.start('backquote')
matchend = match.end('backquote')
rolestart = match.start('role')
role = match.group('role')
position = ''
if role:
role = role[1:-1]
position = 'prefix'
elif self.quoted_start(match):
return (string[:matchend], [], string[matchend:], [])
endmatch = end_pattern.search(string[matchend:])
if endmatch and endmatch.start(1): # 1 or more chars
textend = matchend + endmatch.end()
if endmatch.group('role'):
if role:
msg = self.reporter.warning(
'Multiple roles in interpreted text (both '
'prefix and suffix present; only one allowed).',
line=lineno)
text = unescape(string[rolestart:textend], 1)
prb = self.problematic(text, text, msg)
return string[:rolestart], [prb], string[textend:], [msg]
role = endmatch.group('suffix')[1:-1]
position = 'suffix'
escaped = endmatch.string[:endmatch.start(1)]
rawsource = unescape(string[matchstart:textend], 1)
if rawsource[-1:] == '_':
if role:
msg = self.reporter.warning(
'Mismatch: both interpreted text role %s and '
'reference suffix.' % position, line=lineno)
text = unescape(string[rolestart:textend], 1)
prb = self.problematic(text, text, msg)
return string[:rolestart], [prb], string[textend:], [msg]
return self.phrase_ref(string[:matchstart], string[textend:],
rawsource, escaped, unescape(escaped))
else:
rawsource = unescape(string[rolestart:textend], 1)
nodelist, messages = self.interpreted(rawsource, escaped, role,
lineno)
return (string[:rolestart], nodelist,
string[textend:], messages)
msg = self.reporter.warning(
'Inline interpreted text or phrase reference start-string '
'without end-string.', line=lineno)
text = unescape(string[matchstart:matchend], 1)
prb = self.problematic(text, text, msg)
return string[:matchstart], [prb], string[matchend:], [msg]