[Zope-Checkins] CVS: Zope/lib/python/docutils/parsers/rst -
roles.py:1.1.4.1 __init__.py:1.2.10.4 states.py:1.2.10.4
tableparser.py:1.2.10.4
Christian 'Tiran' Heimes
heimes at faho.rwth-aachen.de
Thu May 13 12:20:29 EDT 2004
Update of /cvs-repository/Zope/lib/python/docutils/parsers/rst
In directory cvs.zope.org:/tmp/cvs-serv4939/lib/python/docutils/parsers/rst
Modified Files:
Tag: Zope-2_7-branch
__init__.py states.py tableparser.py
Added Files:
Tag: Zope-2_7-branch
roles.py
Log Message:
Merge from tiran-restfixing-branch
=== Added File Zope/lib/python/docutils/parsers/rst/roles.py ===
# Author: Edward Loper
# Contact: edloper at gradient.cis.upenn.edu
# Revision: $Revision: 1.1.4.1 $
# Date: $Date: 2004/05/13 16:19:58 $
# Copyright: This module has been placed in the public domain.
"""
This module defines standard interpreted text role functions, a registry for
interpreted text roles, and an API for adding to and retrieving from the
registry.
The interface for interpreted role functions is as follows::
def role_fn(name, rawtext, text, lineno, inliner,
options={}, content=[]):
code...
# Set function attributes for customization:
role_fn.options = ...
role_fn.content = ...
Parameters:
- ``name`` is the local name of the interpreted text role, the role name
actually used in the document.
- ``rawtext`` is a string containing the entire interpreted text construct.
Return it as a ``problematic`` node linked to a system message if there is a
problem.
- ``text`` is the interpreted text content.
- ``lineno`` is the line number where the interpreted text beings.
- ``inliner`` is the Inliner object that called the role function.
It defines the following useful attributes: ``reporter``,
``problematic``, ``memo``, ``parent``, ``document``.
- ``options``: A dictionary of directive options for customization, to be
interpreted by the role function. Used for additional attributes for the
generated elements and other functionality.
- ``content``: A list of strings, the directive content for customization
("role" directive). To be interpreted by the role function.
Function attributes for customization, interpreted by the "role" directive:
- ``options``: A dictionary, mapping known option names to conversion
functions such as `int` or `float`. ``None`` or an empty dict implies no
options to parse. Several directive option conversion functions are defined
in the `directives` module.
All role functions implicitly support the "class" option, unless disabled
with an explicit ``{'class': None}``.
- ``content``: A boolean; true if content is allowed. Client code must handle
the case where content is required but not supplied (an empty content list
will be supplied).
Note that unlike directives, the "arguments" function attribute is not
supported for role customization. Directive arguments are handled by the
"role" directive itself.
Interpreted role functions return a tuple of two values:
- A list of nodes which will be inserted into the document tree at the
point where the interpreted role was encountered (can be an empty
list).
- A list of system messages, which will be inserted into the document tree
immediately after the end of the current inline block (can also be empty).
"""
__docformat__ = 'reStructuredText'
from docutils import nodes
from docutils.parsers.rst import directives
from docutils.parsers.rst.languages import en as _fallback_language_module
DEFAULT_INTERPRETED_ROLE = 'title-reference'
"""
The canonical name of the default interpreted role. This role is used
when no role is specified for a piece of interpreted text.
"""
_role_registry = {}
"""Mapping of canonical role names to role functions. Language-dependent role
names are defined in the ``language`` subpackage."""
_roles = {}
"""Mapping of local or language-dependent interpreted text role names to role
functions."""
def role(role_name, language_module, lineno, reporter):
"""
Locate and return a role function from its language-dependent name, along
with a list of system messages. If the role is not found in the current
language, check English. Return a 2-tuple: role function (``None`` if the
named role cannot be found) and a list of system messages.
"""
normname = role_name.lower()
messages = []
msg_text = []
if _roles.has_key(normname):
return _roles[normname], messages
if role_name:
canonicalname = None
try:
canonicalname = language_module.roles[normname]
except AttributeError, error:
msg_text.append('Problem retrieving role entry from language '
'module %r: %s.' % (language_module, error))
except KeyError:
msg_text.append('No role entry for "%s" in module "%s".'
% (role_name, language_module.__name__))
else:
canonicalname = DEFAULT_INTERPRETED_ROLE
# If we didn't find it, try English as a fallback.
if not canonicalname:
try:
canonicalname = _fallback_language_module.roles[normname]
msg_text.append('Using English fallback for role "%s".'
% role_name)
except KeyError:
msg_text.append('Trying "%s" as canonical role name.'
% role_name)
# The canonical name should be an English name, but just in case:
canonicalname = normname
# Collect any messages that we generated.
if msg_text:
message = reporter.info('\n'.join(msg_text), line=lineno)
messages.append(message)
# Look the role up in the registry, and return it.
if _role_registry.has_key(canonicalname):
role_fn = _role_registry[canonicalname]
register_local_role(normname, role_fn)
return role_fn, messages
else:
return None, messages # Error message will be generated by caller.
def register_canonical_role(name, role_fn):
"""
Register an interpreted text role by its canonical name.
:Parameters:
- `name`: The canonical name of the interpreted role.
- `role_fn`: The role function. See the module docstring.
"""
set_implicit_options(role_fn)
_role_registry[name] = role_fn
def register_local_role(name, role_fn):
"""
Register an interpreted text role by its local or language-dependent name.
:Parameters:
- `name`: The local or language-dependent name of the interpreted role.
- `role_fn`: The role function. See the module docstring.
"""
set_implicit_options(role_fn)
_roles[name] = role_fn
def set_implicit_options(role_fn):
"""
Add customization options to role functions, unless explicitly set or
disabled.
"""
if not hasattr(role_fn, 'options') or role_fn.options is None:
role_fn.options = {'class': directives.class_option}
elif not role_fn.options.has_key('class'):
role_fn.options['class'] = directives.class_option
def register_generic_role(canonical_name, node_class):
"""For roles which simply wrap a given `node_class` around the text."""
role = GenericRole(canonical_name, node_class)
register_canonical_role(canonical_name, role)
class GenericRole:
"""
Generic interpreted text role, where the interpreted text is simply
wrapped with the provided node class.
"""
def __init__(self, role_name, node_class):
self.name = role_name
self.node_class = node_class
def __call__(self, role, rawtext, text, lineno, inliner,
options={}, content=[]):
return [self.node_class(rawtext, text, **options)], []
class CustomRole:
"""
Wrapper for custom interpreted text roles.
"""
def __init__(self, role_name, base_role, options={}, content=[]):
self.name = role_name
self.base_role = base_role
self.options = None
if hasattr(base_role, 'options'):
self.options = base_role.options
self.content = None
if hasattr(base_role, 'content'):
self.content = base_role.content
self.supplied_options = options
self.supplied_content = content
def __call__(self, role, rawtext, text, lineno, inliner,
options={}, content=[]):
opts = self.supplied_options.copy()
opts.update(options)
cont = list(self.supplied_content)
if cont and content:
cont += '\n'
cont.extend(content)
return self.base_role(role, rawtext, text, lineno, inliner,
options=opts, content=cont)
def generic_custom_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
""""""
# Once nested inline markup is implemented, this and other methods should
# recursively call inliner.nested_parse().
return [nodes.inline(rawtext, text, **options)], []
generic_custom_role.options = {'class': directives.class_option}
######################################################################
# Define and register the standard roles:
######################################################################
register_generic_role('abbreviation', nodes.abbreviation)
register_generic_role('acronym', nodes.acronym)
register_generic_role('emphasis', nodes.emphasis)
register_generic_role('literal', nodes.literal)
register_generic_role('strong', nodes.strong)
register_generic_role('subscript', nodes.subscript)
register_generic_role('superscript', nodes.superscript)
register_generic_role('title-reference', nodes.title_reference)
def pep_reference_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
try:
pepnum = int(text)
if pepnum < 0 or pepnum > 9999:
raise ValueError
except ValueError:
msg = inliner.reporter.error(
'PEP number must be a number from 0 to 9999; "%s" is invalid.'
% text, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
# Base URL mainly used by inliner.pep_reference; so this is correct:
ref = inliner.pep_url % pepnum
return [nodes.reference(rawtext, 'PEP ' + text, refuri=ref, **options)], []
register_canonical_role('pep-reference', pep_reference_role)
def rfc_reference_role(role, rawtext, text, lineno, inliner,
options={}, content=[]):
try:
rfcnum = int(text)
if rfcnum <= 0:
raise ValueError
except ValueError:
msg = inliner.reporter.error(
'RFC number must be a number greater than or equal to 1; '
'"%s" is invalid.' % text, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
# Base URL mainly used by inliner.rfc_reference, so this is correct:
ref = inliner.rfc_url % rfcnum
node = nodes.reference(rawtext, 'RFC ' + text, refuri=ref, **options)
return [node], []
register_canonical_role('rfc-reference', rfc_reference_role)
######################################################################
# Register roles that are currently unimplemented.
######################################################################
def unimplemented_role(role, rawtext, text, lineno, inliner, attributes={}):
msg = inliner.reporter.error(
'Interpreted text role "%s" not implemented.' % role, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
register_canonical_role('index', unimplemented_role)
register_canonical_role('named-reference', unimplemented_role)
register_canonical_role('anonymous-reference', unimplemented_role)
register_canonical_role('uri-reference', unimplemented_role)
register_canonical_role('footnote-reference', unimplemented_role)
register_canonical_role('citation-reference', unimplemented_role)
register_canonical_role('substitution-reference', unimplemented_role)
register_canonical_role('target', unimplemented_role)
# This should remain unimplemented, for testing purposes:
register_canonical_role('restructuredtext-unimplemented-role',
unimplemented_role)
=== Zope/lib/python/docutils/parsers/rst/__init__.py 1.2.10.3 => 1.2.10.4 ===
=== Zope/lib/python/docutils/parsers/rst/states.py 1.2.10.3 => 1.2.10.4 ===
--- Zope/lib/python/docutils/parsers/rst/states.py:1.2.10.3 Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/states.py Thu May 13 12:19:58 2004
@@ -107,13 +107,14 @@
import sys
import re
-from docutils import roman
+import roman
from types import TupleType
from docutils import nodes, statemachine, utils, urischemes
from docutils import ApplicationError, DataError
from docutils.statemachine import StateMachineWS, StateWS
from docutils.nodes import fully_normalize_name as normalize_name
-from docutils.parsers.rst import directives, languages, tableparser
+from docutils.nodes import whitespace_normalize_name
+from docutils.parsers.rst import directives, languages, tableparser, roles
from docutils.parsers.rst.languages import en as _fallback_language_module
@@ -274,7 +275,7 @@
state_machine.unlink()
new_offset = state_machine.abs_line_offset()
# No `block.parent` implies disconnected -- lines aren't in sync:
- if block.parent:
+ if block.parent and (len(block) - block_length) != 0:
# Adjustment for block if modified in nested parse:
self.state_machine.next_line(len(block) - block_length)
return new_offset
@@ -475,46 +476,6 @@
Parse inline markup; call the `parse()` method.
"""
- _interpreted_roles = {
- # Values of ``None`` mean "not implemented yet":
- 'title-reference': 'generic_interpreted_role',
- 'abbreviation': 'generic_interpreted_role',
- 'acronym': 'generic_interpreted_role',
- 'index': None,
- 'subscript': 'generic_interpreted_role',
- 'superscript': 'generic_interpreted_role',
- 'emphasis': 'generic_interpreted_role',
- 'strong': 'generic_interpreted_role',
- 'literal': 'generic_interpreted_role',
- 'named-reference': None,
- 'anonymous-reference': None,
- 'uri-reference': None,
- 'pep-reference': 'pep_reference_role',
- 'rfc-reference': 'rfc_reference_role',
- 'footnote-reference': None,
- 'citation-reference': None,
- 'substitution-reference': None,
- 'target': None,
- 'restructuredtext-unimplemented-role': None}
- """Mapping of canonical interpreted text role name to method name.
- Initializes a name to bound-method mapping in `__init__`."""
-
- default_interpreted_role = 'title-reference'
- """The role to use when no explicit role is given.
- Override in subclasses."""
-
- generic_roles = {'abbreviation': nodes.abbreviation,
- 'acronym': nodes.acronym,
- 'emphasis': nodes.emphasis,
- 'literal': nodes.literal,
- 'strong': nodes.strong,
- 'subscript': nodes.subscript,
- 'superscript': nodes.superscript,
- 'title-reference': nodes.title_reference,}
- """Mapping of canonical interpreted text role name to node class.
- Used by the `generic_interpreted_role` method for simple, straightforward
- roles (simple wrapping; no extra processing)."""
-
def __init__(self, roles=None):
"""
`roles` is a mapping of canonical role name to role function or bound
@@ -525,17 +486,6 @@
"""List of (pattern, bound method) tuples, used by
`self.implicit_inline`."""
- self.interpreted_roles = {}
- """Mapping of canonical role name to role function or bound method.
- Items removed from this mapping will be disabled."""
-
- for canonical, method in self._interpreted_roles.items():
- if method:
- self.interpreted_roles[canonical] = getattr(self, method)
- else:
- self.interpreted_roles[canonical] = None
- self.interpreted_roles.update(roles or {})
-
def init_customizations(self, settings):
"""Setting-based customizations; run when parsing begins."""
if settings.pep_references:
@@ -546,6 +496,8 @@
self.rfc_reference))
def parse(self, text, lineno, memo, parent):
+ # Needs to be refactored for nested inline markup.
+ # Add nested_parse() method?
"""
Return 2 lists: nodes (text and inline elements), and system_messages.
@@ -601,11 +553,12 @@
non_whitespace_after = r'(?![ \n])'
# Alphanumerics with isolated internal [-._] chars (i.e. not 2 together):
simplename = r'(?:(?!_)\w)+(?:[-._](?:(?!_)\w)+)*'
- # Valid URI characters (see RFC 2396 & RFC 2732):
- uric = r"""[-_.!~*'()[\];/:@&=+$,%a-zA-Z0-9]"""
+ # Valid URI characters (see RFC 2396 & RFC 2732);
+ # final \x00 allows backslash escapes in URIs:
+ uric = r"""[-_.!~*'()[\];/:@&=+$,%a-zA-Z0-9\x00]"""
# Last URI character; same as uric but no punctuation:
urilast = r"""[_~/a-zA-Z0-9]"""
- emailc = r"""[-_!~*'{|}/#?^`&=+$%a-zA-Z0-9]"""
+ emailc = r"""[-_!~*'{|}/#?^`&=+$%a-zA-Z0-9\x00]"""
email_pattern = r"""
%(emailc)s+(?:\.%(emailc)s+)* # name
@ # at
@@ -659,10 +612,10 @@
embedded_uri=re.compile(
r"""
(
- [ \n]+ # spaces or beginning of line
+ (?:[ \n]+|^) # spaces or beginning of line/string
< # open bracket
%(non_whitespace_after)s
- ([^<>\0]+) # anything but angle brackets & nulls
+ ([^<>\x00]+) # anything but angle brackets & nulls
%(non_whitespace_before)s
> # close bracket w/o whitespace before
)
@@ -823,26 +776,11 @@
return self.phrase_ref(string[:matchstart], string[textend:],
rawsource, escaped, text)
else:
- try:
- return self.interpreted(
- string[:rolestart], string[textend:],
- rawsource, text, role, lineno)
- except UnknownInterpretedRoleError, detail:
- msg = self.reporter.error(
- 'Unknown interpreted text role "%s".' % role,
- line=lineno)
- text = unescape(string[rolestart:textend], 1)
- prb = self.problematic(text, text, msg)
- return (string[:rolestart], [prb], string[textend:],
- detail.args[0] + [msg])
- except InterpretedRoleNotImplementedError, detail:
- msg = self.reporter.error(
- 'Interpreted text role "%s" not implemented.' % role,
- line=lineno)
- text = unescape(string[rolestart:textend], 1)
- prb = self.problematic(text, text, msg)
- return (string[:rolestart], [prb], string[textend:],
- detail.args[0] + [msg])
+ rawsource = unescape(string[rolestart:textend], 1)
+ nodelist, messages = self.interpreted(rawsource, text, 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)
@@ -861,10 +799,13 @@
target = nodes.target(match.group(1), refuri=uri)
else:
raise ApplicationError('problem with URI: %r' % uri_text)
+ if not text:
+ text = uri
else:
target = None
refname = normalize_name(text)
- reference = nodes.reference(rawsource, text)
+ reference = nodes.reference(rawsource, text,
+ name=whitespace_normalize_name(text))
node_list = [reference]
if rawsource[-2:] == '__':
if target:
@@ -891,50 +832,18 @@
else:
return uri
- def interpreted(self, before, after, rawsource, text, role, lineno):
- role_function, canonical, messages = self.get_role_function(role,
- lineno)
- if role_function:
- nodelist, messages2 = role_function(canonical, rawsource, text,
- lineno)
- messages.extend(messages2)
- return before, nodelist, after, messages
- else:
- raise InterpretedRoleNotImplementedError(messages)
-
- def get_role_function(self, role, lineno):
- messages = []
- msg_text = []
- if role:
- name = role.lower()
+ 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:
- name = self.default_interpreted_role
- canonical = None
- try:
- canonical = self.language.roles[name]
- except AttributeError, error:
- msg_text.append('Problem retrieving role entry from language '
- 'module %r: %s.' % (self.language, error))
- except KeyError:
- msg_text.append('No role entry for "%s" in module "%s".'
- % (name, self.language.__name__))
- if not canonical:
- try:
- canonical = _fallback_language_module.roles[name]
- msg_text.append('Using English fallback for role "%s".'
- % name)
- except KeyError:
- msg_text.append('Trying "%s" as canonical role name.'
- % name)
- # Should be an English name, but just in case:
- canonical = name
- if msg_text:
- message = self.reporter.info('\n'.join(msg_text), line=lineno)
- messages.append(message)
- try:
- return self.interpreted_roles[canonical], canonical, messages
- except KeyError:
- raise UnknownInterpretedRoleError(messages)
+ msg = self.reporter.error(
+ 'Unknown interpreted text role "%s".' % role,
+ line=lineno)
+ return ([self.problematic(rawsource, rawsource, msg)],
+ messages + [msg])
def literal(self, match, lineno):
before, inlines, remaining, sysmessages, endstring = self.inline_obj(
@@ -1014,8 +923,9 @@
def reference(self, match, lineno, anonymous=None):
referencename = match.group('refname')
refname = normalize_name(referencename)
- referencenode = nodes.reference(referencename + match.group('refend'),
- referencename)
+ referencenode = nodes.reference(
+ referencename + match.group('refend'), referencename,
+ name=whitespace_normalize_name(referencename))
if anonymous:
referencenode['anonymous'] = 1
self.document.note_anonymous_ref(referencenode)
@@ -1104,44 +1014,6 @@
'_': reference,
'__': anonymous_reference}
- def generic_interpreted_role(self, role, rawtext, text, lineno):
- try:
- role_class = self.generic_roles[role]
- except KeyError:
- msg = self.reporter.error('Unknown interpreted text role: "%s".'
- % role, line=lineno)
- prb = self.problematic(text, text, msg)
- return [prb], [msg]
- return [role_class(rawtext, text)], []
-
- def pep_reference_role(self, role, rawtext, text, lineno):
- try:
- pepnum = int(text)
- if pepnum < 0 or pepnum > 9999:
- raise ValueError
- except ValueError:
- msg = self.reporter.error(
- 'PEP number must be a number from 0 to 9999; "%s" is invalid.'
- % text, line=lineno)
- prb = self.problematic(text, text, msg)
- return [prb], [msg]
- ref = self.pep_url % pepnum
- return [nodes.reference(rawtext, 'PEP ' + text, refuri=ref)], []
-
- def rfc_reference_role(self, role, rawtext, text, lineno):
- try:
- rfcnum = int(text)
- if rfcnum <= 0:
- raise ValueError
- except ValueError:
- msg = self.reporter.error(
- 'RFC number must be a number greater than or equal to 1; '
- '"%s" is invalid.' % text, line=lineno)
- prb = self.problematic(text, text, msg)
- return [prb], [msg]
- ref = self.rfc_url % rfcnum
- return [nodes.reference(rawtext, 'RFC ' + text, refuri=ref)], []
-
class Body(RSTState):
@@ -1472,15 +1344,15 @@
def field_marker(self, match, context, next_state):
"""Field list item."""
- fieldlist = nodes.field_list()
- self.parent += fieldlist
+ field_list = nodes.field_list()
+ self.parent += field_list
field, blank_finish = self.field(match)
- fieldlist += field
+ field_list += field
offset = self.state_machine.line_offset + 1 # next line
newline_offset, blank_finish = self.nested_list_parse(
self.state_machine.input_lines[offset:],
input_offset=self.state_machine.abs_line_offset() + 1,
- node=fieldlist, initial_state='FieldList',
+ node=field_list, initial_state='FieldList',
blank_finish=blank_finish)
self.goto_line(newline_offset)
if not blank_finish:
@@ -1492,14 +1364,15 @@
lineno = self.state_machine.abs_line_number()
indented, indent, line_offset, blank_finish = \
self.state_machine.get_first_known_indented(match.end())
- fieldnode = nodes.field()
- fieldnode.line = lineno
- fieldnode += nodes.field_name(name, name)
- fieldbody = nodes.field_body('\n'.join(indented))
- fieldnode += fieldbody
+ field_node = nodes.field()
+ field_node.line = lineno
+ name_nodes, name_messages = self.inline_text(name, lineno)
+ field_node += nodes.field_name(name, '', *name_nodes)
+ field_body = nodes.field_body('\n'.join(indented), *name_messages)
+ field_node += field_body
if indented:
- self.parse_field_body(indented, line_offset, fieldbody)
- return fieldnode, blank_finish
+ self.parse_field_body(indented, line_offset, field_body)
+ return field_node, blank_finish
def parse_field_marker(self, match):
"""Extract & return field name from a field marker match."""
@@ -1876,39 +1749,59 @@
raise MarkupError('malformed hyperlink target.', lineno)
del block[:blockindex]
block[0] = (block[0] + ' ')[targetmatch.end()-len(escaped)-1:].strip()
+ target = self.make_target(block, blocktext, lineno,
+ targetmatch.group('name'))
+ return [target], blank_finish
+
+ def make_target(self, block, block_text, lineno, target_name):
+ target_type, data = self.parse_target(block, block_text, lineno)
+ if target_type == 'refname':
+ target = nodes.target(block_text, '', refname=normalize_name(data))
+ target.indirect_reference_name = data
+ self.add_target(target_name, '', target, lineno)
+ self.document.note_indirect_target(target)
+ return target
+ elif target_type == 'refuri':
+ target = nodes.target(block_text, '')
+ self.add_target(target_name, data, target, lineno)
+ return target
+ else:
+ return data
+
+ def parse_target(self, block, block_text, lineno):
+ """
+ Determine the type of reference of a target.
+
+ :Return: A 2-tuple, one of:
+
+ - 'refname' and the indirect reference name
+ - 'refuri' and the URI
+ - 'malformed' and a system_message node
+ """
if block and block[-1].strip()[-1:] == '_': # possible indirect target
reference = ' '.join([line.strip() for line in block])
refname = self.is_reference(reference)
if refname:
- target = nodes.target(blocktext, '', refname=refname)
- target.line = lineno
- self.add_target(targetmatch.group('name'), '', target)
- self.document.note_indirect_target(target)
- return [target], blank_finish
- nodelist = []
+ return 'refname', refname
reference = ''.join([line.strip() for line in block])
- if reference.find(' ') != -1:
+ if reference.find(' ') == -1:
+ return 'refuri', unescape(reference)
+ else:
warning = self.reporter.warning(
'Hyperlink target contains whitespace. Perhaps a footnote '
'was intended?',
- nodes.literal_block(blocktext, blocktext), line=lineno)
- nodelist.append(warning)
- else:
- unescaped = unescape(reference)
- target = nodes.target(blocktext, '')
- target.line = lineno
- self.add_target(targetmatch.group('name'), unescaped, target)
- nodelist.append(target)
- return nodelist, blank_finish
+ nodes.literal_block(block_text, block_text), line=lineno)
+ return 'malformed', warning
def is_reference(self, reference):
match = self.explicit.patterns.reference.match(
- normalize_name(reference))
+ whitespace_normalize_name(reference))
if not match:
return None
return unescape(match.group('simple') or match.group('phrase'))
- def add_target(self, targetname, refuri, target):
+ def add_target(self, targetname, refuri, target, lineno):
+ target.line = lineno
if targetname:
name = normalize_name(unescape(targetname))
target['name'] = name
@@ -1988,17 +1881,18 @@
return [msg], blank_finish
def directive(self, match, **option_presets):
+ """Returns a 2-tuple: list of nodes, and a "blank finish" boolean."""
type_name = match.group(1)
directive_function, messages = directives.directive(
type_name, self.memo.language, self.document)
self.parent += messages
if directive_function:
- return self.parse_directive(
+ return self.run_directive(
directive_function, match, type_name, option_presets)
else:
return self.unknown_directive(type_name)
- def parse_directive(self, directive_fn, match, type_name, option_presets):
+ def run_directive(self, directive_fn, match, type_name, option_presets):
"""
Parse a directive then run its directive function.
@@ -2020,13 +1914,6 @@
Returns a 2-tuple: list of nodes, and a "blank finish" boolean.
"""
- arguments = []
- options = {}
- argument_spec = getattr(directive_fn, 'arguments', None)
- if argument_spec and argument_spec[:2] == (0, 0):
- argument_spec = None
- option_spec = getattr(directive_fn, 'options', None)
- content_spec = getattr(directive_fn, 'content', None)
lineno = self.state_machine.abs_line_number()
initial_line_offset = self.state_machine.line_offset
indented, indent, line_offset, blank_finish \
@@ -2034,6 +1921,30 @@
strip_top=0)
block_text = '\n'.join(self.state_machine.input_lines[
initial_line_offset : self.state_machine.line_offset + 1])
+ try:
+ arguments, options, content, content_offset = (
+ self.parse_directive_block(indented, line_offset,
+ directive_fn, option_presets))
+ except MarkupError, detail:
+ error = self.reporter.error(
+ 'Error in "%s" directive:\n%s.' % (type_name, detail),
+ nodes.literal_block(block_text, block_text), line=lineno)
+ return [error], blank_finish
+ result = directive_fn(type_name, arguments, options, content, lineno,
+ content_offset, block_text, self,
+ self.state_machine)
+ return (result,
+ blank_finish or self.state_machine.is_next_line_blank())
+
+ def parse_directive_block(self, indented, line_offset, directive_fn,
+ option_presets):
+ arguments = []
+ options = {}
+ argument_spec = getattr(directive_fn, 'arguments', None)
+ if argument_spec and argument_spec[:2] == (0, 0):
+ argument_spec = None
+ option_spec = getattr(directive_fn, 'options', None)
+ content_spec = getattr(directive_fn, 'content', None)
if indented and not indented[0].strip():
indented.trim_start()
line_offset += 1
@@ -2055,24 +1966,15 @@
while content and not content[0].strip():
content.trim_start()
content_offset += 1
- try:
- if option_spec:
- options, arg_block = self.parse_directive_options(
- option_presets, option_spec, arg_block)
- if argument_spec:
- arguments = self.parse_directive_arguments(argument_spec,
- arg_block)
- if content and not content_spec:
- raise MarkupError('no content permitted')
- except MarkupError, detail:
- error = self.reporter.error(
- 'Error in "%s" directive:\n%s.' % (type_name, detail),
- nodes.literal_block(block_text, block_text), line=lineno)
- return [error], blank_finish
- result = directive_fn(
- type_name, arguments, options, content, lineno, content_offset,
- block_text, self, self.state_machine)
- return result, blank_finish or self.state_machine.is_next_line_blank()
+ if option_spec:
+ options, arg_block = self.parse_directive_options(
+ option_presets, option_spec, arg_block)
+ if argument_spec:
+ arguments = self.parse_directive_arguments(
+ argument_spec, arg_block)
+ if content and not content_spec:
+ raise MarkupError('no content permitted')
+ return (arguments, options, content, content_offset)
def parse_directive_options(self, option_presets, option_spec, arg_block):
options = option_presets.copy()
@@ -2251,38 +2153,14 @@
return [], next_state, []
def anonymous_target(self, match):
+ lineno = self.state_machine.abs_line_number()
block, indent, offset, blank_finish \
= self.state_machine.get_first_known_indented(match.end(),
until_blank=1)
blocktext = match.string[:match.end()] + '\n'.join(block)
- if block and block[-1].strip()[-1:] == '_': # possible indirect target
- reference = escape2null(' '.join([line.strip()
- for line in block]))
- refname = self.is_reference(reference)
- if refname:
- target = nodes.target(blocktext, '', refname=refname,
- anonymous=1)
- self.document.note_anonymous_target(target)
- self.document.note_indirect_target(target)
- return [target], blank_finish
- nodelist = []
- reference = escape2null(''.join([line.strip() for line in block]))
- if reference.find(' ') != -1:
- lineno = self.state_machine.abs_line_number() - len(block) + 1
- warning = self.reporter.warning(
- 'Anonymous hyperlink target contains whitespace. Perhaps a '
- 'footnote was intended?',
- nodes.literal_block(blocktext, blocktext),
- line=lineno)
- nodelist.append(warning)
- else:
- target = nodes.target(blocktext, '', anonymous=1)
- if reference:
- unescaped = unescape(reference)
- target['refuri'] = unescaped
- self.document.note_anonymous_target(target)
- nodelist.append(target)
- return nodelist, blank_finish
+ block = [escape2null(line) for line in block]
+ target = self.make_target(block, blocktext, lineno, '')
+ return [target], blank_finish
def line(self, match, context, next_state):
"""Section title overline or transition marker."""
@@ -2660,20 +2538,29 @@
"""Return a list of nodes."""
indented, indent, offset, blank_finish = \
self.state_machine.get_indented()
- nodelist = []
while indented and not indented[-1].strip():
indented.trim_end()
- if indented:
- data = '\n'.join(indented)
- nodelist.append(nodes.literal_block(data, data))
- if not blank_finish:
- nodelist.append(self.unindent_warning('Literal block'))
- else:
- nodelist.append(self.reporter.warning(
- 'Literal block expected; none found.',
- line=self.state_machine.abs_line_number()))
+ if not indented:
+ return self.quoted_literal_block()
+ nodelist = []
+ data = '\n'.join(indented)
+ nodelist.append(nodes.literal_block(data, data))
+ if not blank_finish:
+ nodelist.append(self.unindent_warning('Literal block'))
return nodelist
+ def quoted_literal_block(self):
+ abs_line_offset = self.state_machine.abs_line_offset()
+ offset = self.state_machine.line_offset
+ parent_node = nodes.Element()
+ new_abs_offset = self.nested_parse(
+ self.state_machine.input_lines[offset:],
+ input_offset=abs_line_offset, node=parent_node, match_titles=0,
+ state_machine_kwargs={'state_classes': (QuotedLiteralBlock,),
+ 'initial_state': 'QuotedLiteralBlock'})
+ self.goto_line(new_abs_offset)
+ return parent_node.children
+
def definition_list_item(self, termline):
indented, indent, line_offset, blank_finish = \
self.state_machine.get_indented()
@@ -2687,8 +2574,8 @@
definitionlistitem += definition
if termline[0][-2:] == '::':
definition += self.reporter.info(
- 'Blank line missing before literal block? Interpreted as a '
- 'definition list item.', line=line_offset + 1)
+ 'Blank line missing before literal block (after the "::")? '
+ 'Interpreted as a definition list item.', line=line_offset+1)
self.nested_parse(indented, input_offset=line_offset, node=definition)
return definitionlistitem, blank_finish
@@ -2888,6 +2775,77 @@
self.state_machine.previous_line(lines)
context[:] = []
raise statemachine.StateCorrection('Body', 'text')
+
+
+class QuotedLiteralBlock(RSTState):
+
+ """
+ Nested parse handler for quoted (unindented) literal blocks.
+
+ Special-purpose. Not for inclusion in `state_classes`.
+ """
+
+ patterns = {'initial_quoted': r'(%(nonalphanum7bit)s)' % Body.pats,
+ 'text': r''}
+ initial_transitions = ('initial_quoted', 'text')
+
+ def __init__(self, state_machine, debug=0):
+ RSTState.__init__(self, state_machine, debug)
+ self.messages = []
+ self.initial_lineno = None
+
+ def blank(self, match, context, next_state):
+ if context:
+ raise EOFError
+ else:
+ return context, next_state, []
+
+ def eof(self, context):
+ if context:
+ text = '\n'.join(context)
+ literal_block = nodes.literal_block(text, text)
+ literal_block.line = self.initial_lineno
+ self.parent += literal_block
+ else:
+ self.parent += self.reporter.warning(
+ 'Literal block expected; none found.',
+ line=self.state_machine.abs_line_number())
+ self.state_machine.previous_line()
+ self.parent += self.messages
+ return []
+
+ def indent(self, match, context, next_state):
+ assert context, ('QuotedLiteralBlock.indent: context should not '
+ 'be empty!')
+ self.messages.append(
+ self.reporter.error('Unexpected indentation.',
+ line=self.state_machine.abs_line_number()))
+ self.state_machine.previous_line()
+ raise EOFError
+
+ def initial_quoted(self, match, context, next_state):
+ """Match arbitrary quote character on the first line only."""
+ self.remove_transition('initial_quoted')
+ quote = match.string[0]
+ pattern = re.compile(re.escape(quote))
+ # New transition matches consistent quotes only:
+ self.add_transition('quoted',
+ (pattern, self.quoted, self.__class__.__name__))
+ self.initial_lineno = self.state_machine.abs_line_number()
+ return [match.string], next_state, []
+
+ def quoted(self, match, context, next_state):
+ """Match consistent quotes on subsequent lines."""
+ context.append(match.string)
+ return context, next_state, []
+
+ def text(self, match, context, next_state):
+ if context:
+ self.messages.append(
+ self.reporter.error('Inconsistent literal block quoting.',
+ line=self.state_machine.abs_line_number()))
+ self.state_machine.previous_line()
+ raise EOFError
state_classes = (Body, BulletList, DefinitionList, EnumeratedList, FieldList,
=== Zope/lib/python/docutils/parsers/rst/tableparser.py 1.2.10.3 => 1.2.10.4 ===
More information about the Zope-Checkins
mailing list