summaryrefslogtreecommitdiff
path: root/docutils/transforms/components.py
blob: 7bb4e959d8dc876351c000fac3f61a1d0282fe83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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
import os
import re
import time
from docutils import nodes, utils
from docutils import 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 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.
    """

    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):
            pending.parent.replace(pending, pending.details['nodes'])
        else:
            pending.parent.remove(pending)