[Zope-Checkins] CVS: Zope/lib/python/docutils/parsers/rst/directives - __init__.py:1.2.10.5 admonitions.py:1.2.10.4 body.py:1.2.10.4 html.py:1.2.10.4 images.py:1.2.10.4 misc.py:1.2.10.4 parts.py:1.2.10.4 references.py:1.2.10.4

Christian 'Tiran' Heimes heimes at faho.rwth-aachen.de
Thu May 13 12:20:30 EDT 2004


Update of /cvs-repository/Zope/lib/python/docutils/parsers/rst/directives
In directory cvs.zope.org:/tmp/cvs-serv4939/lib/python/docutils/parsers/rst/directives

Modified Files:
      Tag: Zope-2_7-branch
	__init__.py admonitions.py body.py html.py images.py misc.py 
	parts.py references.py 
Log Message:
Merge from tiran-restfixing-branch


=== Zope/lib/python/docutils/parsers/rst/directives/__init__.py 1.2.10.4 => 1.2.10.5 ===
--- Zope/lib/python/docutils/parsers/rst/directives/__init__.py:1.2.10.4	Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/directives/__init__.py	Thu May 13 12:19:58 2004
@@ -102,6 +102,7 @@
       'epigraph': ('body', 'epigraph'),
       'highlights': ('body', 'highlights'),
       'pull-quote': ('body', 'pull_quote'),
+      'table': ('body', 'table'),
       #'questions': ('body', 'question_list'),
       'image': ('images', 'image'),
       'figure': ('images', 'figure'),
@@ -117,6 +118,7 @@
       'replace': ('misc', 'replace'),
       'unicode': ('misc', 'unicode_directive'),
       'class': ('misc', 'class_directive'),
+      'role': ('misc', 'role'),
       'restructuredtext-test-directive': ('misc', 'directive_test_function'),}
 """Mapping of directive name to (module name, function name).  The directive
 name is canonical & must be lowercase.  Language-dependent names are defined
@@ -165,23 +167,37 @@
     try:
         modulename, functionname = _directive_registry[canonicalname]
     except KeyError:
+        messages.append(document.reporter.error(
+            'Directive "%s" not registered (canonical name "%s").'
+            % (directive_name, canonicalname), line=document.current_line))
         return None, messages
     if _modules.has_key(modulename):
         module = _modules[modulename]
     else:
         try:
             module = __import__(modulename, globals(), locals())
-        except ImportError:
+        except ImportError, detail:
+            messages.append(document.reporter.error(
+                'Error importing directive module "%s" (directive "%s"):\n%s'
+                % (modulename, directive_name, detail),
+                line=document.current_line))
             return None, messages
     try:
         function = getattr(module, functionname)
         _directives[normname] = function
     except AttributeError:
+        messages.append(document.reporter.error(
+            'No function "%s" in module "%s" (directive "%s").'
+            % (functionname, modulename, directive_name),
+            line=document.current_line))
         return None, messages
     return function, messages
 
 def register_directive(name, directive):
-    """Register a nonstandard application-defined directive function."""
+    """
+    Register a nonstandard application-defined directive function.
+    Language lookups are not needed for such functions.
+    """
     _directives[name] = directive
 
 def flag(argument):
@@ -257,7 +273,10 @@
     """
     if argument is None:
         raise ValueError('argument required but none supplied')
-    return nodes.make_id(argument)
+    class_name = nodes.make_id(argument)
+    if not class_name:
+        raise ValueError('cannot make "%s" into a class name' % argument)
+    return class_name
 
 def format_values(values):
     return '%s, or "%s"' % (', '.join(['"%s"' % s for s in values[:-1]]),


=== Zope/lib/python/docutils/parsers/rst/directives/admonitions.py 1.2.10.3 => 1.2.10.4 ===


=== Zope/lib/python/docutils/parsers/rst/directives/body.py 1.2.10.3 => 1.2.10.4 ===
--- Zope/lib/python/docutils/parsers/rst/directives/body.py:1.2.10.3	Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/directives/body.py	Thu May 13 12:19:58 2004
@@ -34,6 +34,7 @@
     title_text = arguments[0]
     textnodes, messages = state.inline_text(title_text, lineno)
     titles = [nodes.title(title_text, '', *textnodes)]
+    # sidebar uses this code
     if options.has_key('subtitle'):
         textnodes, more_messages = state.inline_text(options['subtitle'],
                                                      lineno)
@@ -120,3 +121,38 @@
     return [block_quote] + messages
 
 pull_quote.content = 1
+
+def table(name, arguments, options, content, lineno,
+          content_offset, block_text, state, state_machine):
+    if not content:
+        warning = state_machine.reporter.warning(
+            'Content block expected for the "%s" directive; none found.'
+            % name, nodes.literal_block(block_text, block_text),
+            line=lineno)
+        return [warning]
+    if arguments:
+        title_text = arguments[0]
+        text_nodes, messages = state.inline_text(title_text, lineno)
+        title = nodes.title(title_text, '', *text_nodes)
+    else:
+        title = None
+    node = nodes.Element()          # anonymous container for parsing
+    text = '\n'.join(content)
+    state.nested_parse(content, content_offset, node)
+    if len(node) != 1 or not isinstance(node[0], nodes.table):
+        error = state_machine.reporter.error(
+            'Error parsing content block for the "%s" directive: '
+            'exactly one table expected.'
+            % name, nodes.literal_block(block_text, block_text),
+            line=lineno)
+        return [error]
+    table_node = node[0]
+    if options.has_key('class'):
+        table_node.set_class(options['class'])
+    if title:
+        table_node.insert(0, title)
+    return [table_node]
+
+table.arguments = (0, 1, 1)
+table.options = {'class': directives.class_option}
+table.content = 1


=== Zope/lib/python/docutils/parsers/rst/directives/html.py 1.2.10.3 => 1.2.10.4 ===


=== Zope/lib/python/docutils/parsers/rst/directives/images.py 1.2.10.3 => 1.2.10.4 ===
--- Zope/lib/python/docutils/parsers/rst/directives/images.py:1.2.10.3	Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/directives/images.py	Thu May 13 12:19:58 2004
@@ -13,7 +13,8 @@
 
 import sys
 from docutils import nodes, utils
-from docutils.parsers.rst import directives
+from docutils.parsers.rst import directives, states
+from docutils.nodes import whitespace_normalize_name
 
 try:
     import Image                        # PIL
@@ -34,8 +35,23 @@
               nodes.literal_block(block_text, block_text), line=lineno)
         return [error]
     options['uri'] = reference
-    image_node = nodes.image(block_text, **options)
-    return [image_node]
+    if options.has_key('target'):
+        block = states.escape2null(options['target']).splitlines()
+        block = [line for line in block]
+        target_type, data = state.parse_target(block, block_text, lineno)
+        if target_type == 'refuri':
+            node_list = nodes.reference(refuri=data)
+        elif target_type == 'refname':
+            node_list = nodes.reference(
+                refname=data, name=whitespace_normalize_name(options['target']))
+            state.document.note_refname(node_list)
+        else:                           # malformed target
+            node_list = [data]          # data is a system message
+        del options['target']
+    else:
+        node_list = []
+    node_list.append(nodes.image(block_text, **options))
+    return node_list
 
 image.arguments = (1, 0, 1)
 image.options = {'alt': directives.unchanged,
@@ -43,6 +59,7 @@
                  'width': directives.nonnegative_int,
                  'scale': directives.nonnegative_int,
                  'align': align,
+                 'target': directives.unchanged_required,
                  'class': directives.class_option}
 
 def figure(name, arguments, options, content, lineno,


=== Zope/lib/python/docutils/parsers/rst/directives/misc.py 1.2.10.3 => 1.2.10.4 ===
--- Zope/lib/python/docutils/parsers/rst/directives/misc.py:1.2.10.3	Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/directives/misc.py	Thu May 13 12:19:59 2004
@@ -11,11 +11,15 @@
 import sys
 import os.path
 import re
-from urllib2 import urlopen, URLError
 from docutils import io, nodes, statemachine, utils
-from docutils.parsers.rst import directives, states
+from docutils.parsers.rst import directives, roles, states
 from docutils.transforms import misc
 
+try:
+    import urllib2
+except ImportError:
+    urllib2 = None
+
 
 def include(name, arguments, options, content, lineno,
             content_offset, block_text, state, state_machine):
@@ -97,9 +101,16 @@
         raw_file.close()
         attributes['source'] = path
     elif options.has_key('url'):
+        if not urllib2:
+            severe = state_machine.reporter.severe(
+                  'Problems with the "%s" directive and its "url" option: '
+                  'unable to access the required functionality (from the '
+                  '"urllib2" module).' % name,
+                  nodes.literal_block(block_text, block_text), line=lineno)
+            return [severe]
         try:
-            raw_file = urlopen(options['url'])
-        except (URLError, IOError, OSError), error:
+            raw_file = urllib2.urlopen(options['url'])
+        except (urllib2.URLError, IOError, OSError), error:
             severe = state_machine.reporter.severe(
                   'Problems with "%s" directive URL "%s":\n%s.'
                   % (name, options['url'], error),
@@ -209,7 +220,7 @@
         return [pending]
     else:
         error = state_machine.reporter.error(
-            'Invalid class attribute value for "%s" directive: %s'
+            'Invalid class attribute value for "%s" directive: "%s".'
             % (name, arguments[0]),
             nodes.literal_block(block_text, block_text), line=lineno)
         return [error]
@@ -217,8 +228,67 @@
 class_directive.arguments = (1, 0, 0)
 class_directive.content = 1
 
+role_arg_pat = re.compile(r'(%s)\s*(\(\s*(%s)\s*\)\s*)?$'
+                          % ((states.Inliner.simplename,) * 2))
+def role(name, arguments, options, content, lineno,
+         content_offset, block_text, state, state_machine):
+    """Dynamically create and register a custom interpreted text role."""
+    if content_offset > lineno or not content:
+        error = state_machine.reporter.error(
+            '"%s" directive requires arguments on the first line.'
+            % name, nodes.literal_block(block_text, block_text), line=lineno)
+        return [error]
+    args = content[0]
+    match = role_arg_pat.match(args)
+    if not match:
+        error = state_machine.reporter.error(
+            '"%s" directive arguments not valid role names: "%s".'
+            % (name, args), nodes.literal_block(block_text, block_text),
+            line=lineno)
+        return [error]
+    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, state_machine.language, lineno, state.reporter)
+        if base_role is None:
+            error = state.reporter.error(
+                'Unknown interpreted text role "%s".' % base_role_name,
+                nodes.literal_block(block_text, block_text), line=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).' % (name, base_role))
+    try:
+        (arguments, options, content, content_offset) = (
+            state.parse_directive_block(content[1:], content_offset, base_role,
+                                        option_presets={}))
+    except states.MarkupError, detail:
+        error = state_machine.reporter.error(
+            'Error in "%s" directive:\n%s.' % (name, detail),
+            nodes.literal_block(block_text, block_text), line=lineno)
+        return messages + [error]
+    if not options.has_key('class'):
+        try:
+            options['class'] = directives.class_option(new_role_name)
+        except ValueError, detail:
+            error = state_machine.reporter.error(
+                'Invalid argument for "%s" directive:\n%s.'
+                % (name, detail),
+                nodes.literal_block(block_text, block_text), line=lineno)
+            return messages + [error]
+    role = roles.CustomRole(new_role_name, base_role, options, content)
+    roles.register_local_role(new_role_name, role)
+    return messages
+
+role.content = 1
+
 def directive_test_function(name, arguments, options, content, lineno,
                             content_offset, block_text, state, state_machine):
+    """This directive is useful only for testing purposes."""
     if content:
         text = '\n'.join(content)
         info = state_machine.reporter.info(


=== Zope/lib/python/docutils/parsers/rst/directives/parts.py 1.2.10.3 => 1.2.10.4 ===
--- Zope/lib/python/docutils/parsers/rst/directives/parts.py:1.2.10.3	Sun Nov 30 11:05:23 2003
+++ Zope/lib/python/docutils/parsers/rst/directives/parts.py	Thu May 13 12:19:59 2004
@@ -10,7 +10,7 @@
 
 __docformat__ = 'reStructuredText'
 
-from docutils import nodes
+from docutils import nodes, languages
 from docutils.transforms import parts
 from docutils.parsers.rst import directives
 
@@ -27,17 +27,42 @@
 def contents(name, arguments, options, content, lineno,
              content_offset, block_text, state, state_machine):
     """Table of contents."""
+    document = state_machine.document
+    language = languages.get_language(document.settings.language_code)
+
     if arguments:
         title_text = arguments[0]
         text_nodes, messages = state.inline_text(title_text, lineno)
         title = nodes.title(title_text, '', *text_nodes)
     else:
         messages = []
-        title = None
-    pending = nodes.pending(parts.Contents, {'title': title}, block_text)
+        if options.has_key('local'):
+            title = None
+        else:
+            title = nodes.title('', language.labels['contents'])
+
+    topic = nodes.topic(CLASS='contents')
+
+    cls = options.get('class')
+    if cls:
+        topic.set_class(cls)
+
+    if title:
+        name = title.astext()
+        topic += title
+    else:
+        name = language.labels['contents']
+
+    name = nodes.fully_normalize_name(name)
+    if not document.has_name(name):
+        topic['name'] = name
+    document.note_implicit_target(topic)
+
+    pending = nodes.pending(parts.Contents, rawsource=block_text)
     pending.details.update(options)
-    state_machine.document.note_pending(pending)
-    return [pending] + messages
+    document.note_pending(pending)
+    topic += pending
+    return [topic] + messages
 
 contents.arguments = (0, 1, 1)
 contents.options = {'depth': directives.nonnegative_int,


=== Zope/lib/python/docutils/parsers/rst/directives/references.py 1.2.10.3 => 1.2.10.4 ===




More information about the Zope-Checkins mailing list