This file is indexed.

/usr/share/pyshared/zope/structuredtext/docbook.py is in python-zope.structuredtext 3.5.1-0ubuntu3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
##############################################################################
#
# Copyright (c) 2001 Zope Foundation and Contributors.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
#
##############################################################################
""" Render STX document as docbook.
"""

__metaclass__ = type

class DocBook:
    """ Structured text document renderer for Docbook.
    """
    element_types = {
       '#text': '_text',
       'StructuredTextDocument': 'document',
       'StructuredTextParagraph': 'paragraph',
       'StructuredTextExample': 'example',
       'StructuredTextBullet': 'bullet',
       'StructuredTextNumbered': 'numbered',
       'StructuredTextDescription': 'description',
       'StructuredTextDescriptionTitle': 'descriptionTitle',
       'StructuredTextDescriptionBody': 'descriptionBody',
       'StructuredTextSection': 'section',
       'StructuredTextSectionTitle': 'sectionTitle',
       'StructuredTextLiteral': 'literal',
       'StructuredTextEmphasis': 'emphasis',
       'StructuredTextStrong': 'strong',
       'StructuredTextLink': 'link',
       'StructuredTextXref': 'xref',
       'StructuredTextSGML': 'sgml',
       }

    def dispatch(self, doc, level, output):
        getattr(self, self.element_types[doc.getNodeName()]
               )(doc, level, output)

    def __call__(self, doc, level=1):
        r=[]
        self.dispatch(doc, level-1, r.append)
        return ''.join(r)

    def _text(self, doc, level, output):
        if doc.getNodeName() == 'StructuredTextLiteral':
            output(doc.getNodeValue())
        else:
            output(doc.getNodeValue().lstrip())

    def document(self, doc, level, output):
        output('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n')
        output('<book>\n')
        children=doc.getChildNodes()
        if (children and
            children[0].getNodeName() == 'StructuredTextSection'):
            output('<title>%s</title>'
                % children[0].getChildNodes()[0].getNodeValue())
        for c in children:
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</book>\n')

    def section(self, doc, level, output):
        output('\n<section>\n')
        children=doc.getChildNodes()
        for c in children:
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level+1, output)
        output('\n</section>\n')

    def sectionTitle(self, doc, level, output):
        output('<title>')
        for c in doc.getChildNodes():
            try:
                getattr(self, self.element_types[c.getNodeName()]
                       )(c, level, output)
            except:
                print "failed", c.getNodeName(), c
        output('</title>\n')

    def description(self, doc, level, output):
        p=doc.getPreviousSibling()
        if p is None or  p.getNodeName() is not doc.getNodeName():
            output('<variablelist>\n')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        n=doc.getNextSibling()
        if n is None or n.getNodeName() is not doc.getNodeName():
            output('</variablelist>\n')

    def descriptionTitle(self, doc, level, output):
        output('<varlistentry><term>\n')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</term>\n')

    def descriptionBody(self, doc, level, output):
        output('<listitem><para>\n')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</para></listitem>\n')
        output('</varlistentry>\n')

    def bullet(self, doc, level, output):
        p=doc.getPreviousSibling()
        if p is None or p.getNodeName() is not doc.getNodeName():
            output('<itemizedlist>\n')
        output('<listitem><para>\n')

        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        n=doc.getNextSibling()
        output('</para></listitem>\n')
        if n is None or n.getNodeName() is not doc.getNodeName():
            output('</itemizedlist>\n')

    def numbered(self, doc, level, output):
        p=doc.getPreviousSibling()
        if p is None or p.getNodeName() is not doc.getNodeName():
            output('<orderedlist>\n')
        output('<listitem><para>\n')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        n=doc.getNextSibling()
        output('</para></listitem>\n')
        if n is None or n.getNodeName() is not doc.getNodeName():
            output('</orderedlist>\n')

    def example(self, doc, level, output):
        i=0
        for c in doc.getChildNodes():
            if i==0:
                output('<programlisting>\n<![CDATA[\n')
                ##
                ## eek.  A ']]>' in your body will break this...
                ##
                output(prestrip(c.getNodeValue()))
                output('\n]]></programlisting>\n')
            else:
                getattr(self, self.element_types[c.getNodeName()])(
                   c, level, output)

    def paragraph(self, doc, level, output):
        output('<para>\n\n')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()])(
               c, level, output)
        output('</para>\n\n')

    def link(self, doc, level, output):
        output('<ulink url="%s">' % doc.href)
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</ulink>')

    def emphasis(self, doc, level, output):
        output('<emphasis>')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</emphasis> ')

    def literal(self, doc, level, output):
        output('<literal>')
        for c in doc.getChildNodes():
            output(c.getNodeValue())
        output('</literal>')

    def strong(self, doc, level, output):
        output('<emphasis>')
        for c in doc.getChildNodes():
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</emphasis>')

    def xref(self, doc, level, output):
        output('<xref linkend="%s"/>' % doc.getNodeValue())

    def sgml(self, doc, level, output):
        output(doc.getNodeValue())


def prestrip(v):
    v=v.replace( '\r\n', '\n')
    v=v.replace( '\r', '\n')
    v=v.replace( '\t', '        ')
    lines=v.split('\n')
    indent=len(lines[0])
    for line in lines:
        if not len(line): continue
        i=len(line)-len(line.lstrip())
        if i < indent:
            indent=i
    nlines=[]
    for line in lines:
        nlines.append(line[indent:])
    return '\n'.join(nlines)

class DocBookChapter(DocBook):

    def document(self, doc, level, output):
        output('<chapter>\n')
        children=doc.getChildNodes()
        if (children and
            children[0].getNodeName() == 'StructuredTextSection'):
            output('<title>%s</title>'
                    % children[0].getChildNodes()[0].getNodeValue())
        for c in children[0].getChildNodes()[1:]:
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</chapter>\n')

class DocBookChapterWithFigures(DocBookChapter):

    element_types = DocBook.element_types
    element_types.update({'StructuredTextImage': 'image'})

    def image(self, doc, level, output):
        if hasattr(doc, 'key'):
            output('<figure id="%s"><title>%s</title>\n'
                        % (doc.key, doc.getNodeValue()) )
        else:
            output('<figure><title>%s</title>\n' % doc.getNodeValue())
        output('<graphic fileref="%s"></graphic>\n</figure>\n' % doc.href)

class DocBookArticle(DocBook):

    def document(self, doc, level, output):
        output('<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n')
        output('<article>\n')
        children=doc.getChildNodes()
        if (children and
            children[0].getNodeName() == 'StructuredTextSection'):
            output('<articleinfo>\n<title>%s</title>\n</articleinfo>\n' %
                   children[0].getChildNodes()[0].getNodeValue())
        for c in children:
            getattr(self, self.element_types[c.getNodeName()]
                   )(c, level, output)
        output('</article>\n')


class DocBookBook:

    def __init__(self, title=''):
        self.title = title
        self.chapters = []

    def addChapter(self, chapter):
        self.chapters.append(chapter)

    def read(self):
        out = ('<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">\n'
               '<book>\n')
        out = out + '<title>%s</title>\n' % self.title
        for chapter in self.chapters:
            out = out + chapter + '\n</book>\n'

        return out

    def __str__(self):
        return self.read()