From 101671ae44e1686680c80cd07b452aabeb88fb63 Mon Sep 17 00:00:00 2001 From: goodger Date: Sat, 20 Apr 2002 03:01:52 +0000 Subject: Initial revision git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@18 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 85 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 docutils/transforms/components.py (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py new file mode 100644 index 000000000..2cfe4d2a8 --- /dev/null +++ b/docutils/transforms/components.py @@ -0,0 +1,85 @@ +#! /usr/bin/env python +""" +:Authors: David Goodger, Ueli Schlaepfer +:Contact: goodger@users.sourceforge.net +:Revision: $Revision$ +:Date: $Date$ +:Copyright: This module has been placed in the public domain. + +Transforms related to document components. + +- `Contents`: Used to build a table of contents. +""" + +__docformat__ = 'reStructuredText' + + +import re +from docutils import nodes, utils +from docutils.transforms import TransformError, Transform + + +class Contents(Transform): + + """ + This transform generates a table of contents from the entire document tree + or from a single branch. It locates "section" elements and builds them + into a nested bullet list, which is placed within a "topic". A title is + either explicitly specified, taken from the appropriate language module, + or omitted (local table of contents). The depth may be specified. + Two-way references between the table of contents and section titles are + generated (requires Writer support). + + This transform requires a startnode, which which contains generation + options and provides the location for the generated table of contents (the + startnode is replaced by the table of contents "topic"). + """ + + def transform(self): + topic = nodes.topic(CLASS='contents') + title = self.startnode.details['title'] + if self.startnode.details.has_key('local'): + startnode = self.startnode.parent + # @@@ generate an error if the startnode (directive) not at + # section/document top-level? Drag it up until it is? + while not isinstance(startnode, nodes.Structural): + startnode = startnode.parent + if not title: + title = [] + else: + startnode = self.doctree + if not title: + title = nodes.title('', self.language.labels['contents']) + contents = self.build_contents(startnode) + if len(contents): + topic += title + topic += contents + self.startnode.parent.replace(self.startnode, topic) + else: + self.startnode.parent.remove(self.startnode) + + def build_contents(self, node, level=0): + level += 1 + sections = [] + i = len(node) - 1 + while i >= 0 and isinstance(node[i], nodes.section): + sections.append(node[i]) + i -= 1 + sections.reverse() + entries = [] + for section in sections: + title = section[0] + reference = nodes.reference('', '', refid=section['id'], + *title.getchildren()) + entry = nodes.paragraph('', '', reference) + item = nodes.list_item('', entry) + itemid = self.doctree.set_id(item) + title['refid'] = itemid + if (not self.startnode.details.has_key('depth')) \ + or level < self.startnode.details['depth']: + subsects = self.build_contents(section, level) + item += subsects + entries.append(item) + if entries: + entries = nodes.bullet_list('', *entries) + return entries -- cgit v1.2.1 From 9f85d5f3a15e46003ea3228608373d639a2e67e3 Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 5 May 2002 15:24:19 +0000 Subject: renamed parts.py from old components.py git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@76 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 85 --------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 docutils/transforms/components.py (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py deleted file mode 100644 index 2cfe4d2a8..000000000 --- a/docutils/transforms/components.py +++ /dev/null @@ -1,85 +0,0 @@ -#! /usr/bin/env python -""" -:Authors: David Goodger, Ueli Schlaepfer -:Contact: goodger@users.sourceforge.net -:Revision: $Revision$ -:Date: $Date$ -:Copyright: This module has been placed in the public domain. - -Transforms related to document components. - -- `Contents`: Used to build a table of contents. -""" - -__docformat__ = 'reStructuredText' - - -import re -from docutils import nodes, utils -from docutils.transforms import TransformError, Transform - - -class Contents(Transform): - - """ - This transform generates a table of contents from the entire document tree - or from a single branch. It locates "section" elements and builds them - into a nested bullet list, which is placed within a "topic". A title is - either explicitly specified, taken from the appropriate language module, - or omitted (local table of contents). The depth may be specified. - Two-way references between the table of contents and section titles are - generated (requires Writer support). - - This transform requires a startnode, which which contains generation - options and provides the location for the generated table of contents (the - startnode is replaced by the table of contents "topic"). - """ - - def transform(self): - topic = nodes.topic(CLASS='contents') - title = self.startnode.details['title'] - if self.startnode.details.has_key('local'): - startnode = self.startnode.parent - # @@@ generate an error if the startnode (directive) not at - # section/document top-level? Drag it up until it is? - while not isinstance(startnode, nodes.Structural): - startnode = startnode.parent - if not title: - title = [] - else: - startnode = self.doctree - if not title: - title = nodes.title('', self.language.labels['contents']) - contents = self.build_contents(startnode) - if len(contents): - topic += title - topic += contents - self.startnode.parent.replace(self.startnode, topic) - else: - self.startnode.parent.remove(self.startnode) - - def build_contents(self, node, level=0): - level += 1 - sections = [] - i = len(node) - 1 - while i >= 0 and isinstance(node[i], nodes.section): - sections.append(node[i]) - i -= 1 - sections.reverse() - entries = [] - for section in sections: - title = section[0] - reference = nodes.reference('', '', refid=section['id'], - *title.getchildren()) - entry = nodes.paragraph('', '', reference) - item = nodes.list_item('', entry) - itemid = self.doctree.set_id(item) - title['refid'] = itemid - if (not self.startnode.details.has_key('depth')) \ - or level < self.startnode.details['depth']: - subsects = self.build_contents(section, level) - item += subsects - entries.append(item) - if entries: - entries = nodes.bullet_list('', *entries) - return entries -- cgit v1.2.1 From 82bf628c5790a1cc17855199c88d07094b4dbbfa Mon Sep 17 00:00:00 2001 From: goodger Date: Sun, 5 May 2002 15:26:09 +0000 Subject: Docutils component-related transforms. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@77 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 docutils/transforms/components.py (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py new file mode 100644 index 000000000..b0d1ae48f --- /dev/null +++ b/docutils/transforms/components.py @@ -0,0 +1,45 @@ +#! /usr/bin/env python +""" +:Author: David Goodger +:Contact: goodger@users.sourceforge.net +:Revision: $Revision$ +:Date: $Date$ +:Copyright: This module has been placed in the public domain. + +Docutils component-related transforms. +""" + +__docformat__ = 'reStructuredText' + +import sys, os, re, time +from docutils import nodes, utils, ApplicationError, DataError +from docutils.transforms import Transform, TransformError + + +class Filter(Transform): + + """ + Include or exclude elements which depend on a specific Docutils component. + + For use with `nodes.pending` elements. A "pending" element's dictionary + attribute ``details`` must contain a key matching the dependency component + type (e.g. ``details['writer']`` for a "pending" element whose ``stage`` + attribute is 'last writer'). The value is the name of a specific format + or context of that component (e.g. ``details['writer'] = 'html'``). If + the Docutils component which called this transform supports that format or + context, the "pending" element is replaced by the nodes in + ``details['nodes']``; otherwise, the "pending" element is removed. + + For example, the reStructuredText "meta" directive creates a "pending" + element containing a "meta" element. Only writers supporting the "html" + format will include the "meta" element; it will be deleted from the output + of all other writers. + """ + + def transform(self): + pending = self.startnode + component_name = pending.details[pending.stage.split()[-1]] + if self.component.supports(component_name): + pending.parent.replace(pending, pending.details['nodes']) + else: + pending.parent.remove(pending) -- cgit v1.2.1 From 05e4910c4b36bdcc1534594160644f2bb2fec9c4 Mon Sep 17 00:00:00 2001 From: goodger Date: Wed, 22 May 2002 04:19:32 +0000 Subject: docstrings git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@136 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index b0d1ae48f..71473bea9 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -27,18 +27,20 @@ class Filter(Transform): attribute is 'last writer'). The value is the name of a specific format or context of that component (e.g. ``details['writer'] = 'html'``). If the Docutils component which called this transform supports that format or - context, the "pending" element is replaced by the nodes in - ``details['nodes']``; otherwise, the "pending" element is removed. + context, the "pending" element is replaced by the contents of + ``details['nodes']`` (a list of nodes); otherwise, the "pending" element + is removed. For example, the reStructuredText "meta" directive creates a "pending" - element containing a "meta" element. Only writers supporting the "html" - format will include the "meta" element; it will be deleted from the output - of all other writers. + element containing a "meta" element (in ``pending.details['nodes']``). + Only writers supporting the "html" format will include the "meta" element; + it will be deleted from the output of all other writers. """ def transform(self): pending = self.startnode - component_name = pending.details[pending.stage.split()[-1]] + component_type = pending.stage.split()[-1] # 'reader' or 'writer' + component_name = pending.details[component_type] if self.component.supports(component_name): pending.parent.replace(pending, pending.details['nodes']) else: -- cgit v1.2.1 From 6e398dc95eb60a5b81047b899954503ce780f1db Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 30 May 2002 02:30:40 +0000 Subject: Cleaned up imports git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@160 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index 71473bea9..dee54a215 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -11,8 +11,12 @@ Docutils component-related transforms. __docformat__ = 'reStructuredText' -import sys, os, re, time -from docutils import nodes, utils, ApplicationError, DataError +import sys +import os +import re +import time +from docutils import nodes, utils +from docutils import ApplicationError, DataError from docutils.transforms import Transform, TransformError -- cgit v1.2.1 From 58036bb54a184a823272be8ddf5f52c619d4f83c Mon Sep 17 00:00:00 2001 From: goodger Date: Tue, 8 Oct 2002 01:35:57 +0000 Subject: updated git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@774 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index dee54a215..6b0c2648c 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -1,11 +1,10 @@ -#! /usr/bin/env python -""" -:Author: David Goodger -:Contact: goodger@users.sourceforge.net -:Revision: $Revision$ -:Date: $Date$ -:Copyright: This module has been placed in the public domain. +# Author: David Goodger +# Contact: goodger@users.sourceforge.net +# Revision: $Revision$ +# Date: $Date$ +# Copyright: This module has been placed in the public domain. +""" Docutils component-related transforms. """ -- cgit v1.2.1 From afeedfb343c2904e9357997d2a50f8f3cabb2568 Mon Sep 17 00:00:00 2001 From: goodger Date: Fri, 18 Oct 2002 04:55:21 +0000 Subject: Refactored names (options -> settings; .transform() -> .apply(); etc.); updated. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@825 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index 6b0c2648c..7bb4e959d 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -40,7 +40,7 @@ class Filter(Transform): it will be deleted from the output of all other writers. """ - def transform(self): + def apply(self): pending = self.startnode component_type = pending.stage.split()[-1] # 'reader' or 'writer' component_name = pending.details[component_type] -- cgit v1.2.1 From 1ecb8a1bf993d41cb606a26ca5168809bd75412b Mon Sep 17 00:00:00 2001 From: goodger Date: Thu, 24 Oct 2002 00:51:10 +0000 Subject: Completed transform reform; updated. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@853 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index 7bb4e959d..8f4a267e1 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -25,26 +25,30 @@ class Filter(Transform): Include or exclude elements which depend on a specific Docutils component. For use with `nodes.pending` elements. A "pending" element's dictionary - attribute ``details`` must contain a key matching the dependency component - type (e.g. ``details['writer']`` for a "pending" element whose ``stage`` - attribute is 'last writer'). The value is the name of a specific format - or context of that component (e.g. ``details['writer'] = 'html'``). If - the Docutils component which called this transform supports that format or - context, the "pending" element is replaced by the contents of + attribute ``details`` must contain the keys "component" and "format". The + value of ``details['component']`` must match the type name of the + component the elements depend on (e.g. "writer"). The value of + ``details['format']`` is the name of a specific format or context of that + component (e.g. "html"). If the matching Docutils component supports that + format or context, the "pending" element is replaced by the contents of ``details['nodes']`` (a list of nodes); otherwise, the "pending" element is removed. For example, the reStructuredText "meta" directive creates a "pending" element containing a "meta" element (in ``pending.details['nodes']``). - Only writers supporting the "html" format will include the "meta" element; - it will be deleted from the output of all other writers. + Only writers (``pending.details['component'] == 'writer'``) supporting the + "html" format (``pending.details['format'] == 'html'``) will include the + "meta" element; it will be deleted from the output of all other writers. """ + default_priority = 780 + def apply(self): pending = self.startnode - component_type = pending.stage.split()[-1] # 'reader' or 'writer' - component_name = pending.details[component_type] - if self.component.supports(component_name): + component_type = pending.details['component'] # 'reader' or 'writer' + format = pending.details['format'] + component = self.document.transformer.components[component_type] + if component.supports(format): pending.parent.replace(pending, pending.details['nodes']) else: pending.parent.remove(pending) -- cgit v1.2.1 From 469baf29f163924dca499b0591ca6f2addba6b35 Mon Sep 17 00:00:00 2001 From: wiemann Date: Sun, 11 Sep 2005 21:53:49 +0000 Subject: replaced node.parent.replace(node, new) constructs with node.substitute(new) git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3870 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index 8f4a267e1..a31f09fb5 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -49,6 +49,6 @@ class Filter(Transform): format = pending.details['format'] component = self.document.transformer.components[component_type] if component.supports(format): - pending.parent.replace(pending, pending.details['nodes']) + pending.substitute(pending.details['nodes']) else: pending.parent.remove(pending) -- cgit v1.2.1 From 69fadff9ca4f067b3042b6383989ce638ff4d42d Mon Sep 17 00:00:00 2001 From: wiemann Date: Mon, 26 Sep 2005 18:17:31 +0000 Subject: renamed Element.substitute to Element.replace_self git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@3909 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/transforms/components.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docutils/transforms/components.py') diff --git a/docutils/transforms/components.py b/docutils/transforms/components.py index a31f09fb5..1c3ecbef6 100644 --- a/docutils/transforms/components.py +++ b/docutils/transforms/components.py @@ -49,6 +49,6 @@ class Filter(Transform): format = pending.details['format'] component = self.document.transformer.components[component_type] if component.supports(format): - pending.substitute(pending.details['nodes']) + pending.replace_self(pending.details['nodes']) else: pending.parent.remove(pending) -- cgit v1.2.1