File: Formatters.py

package info (click to toggle)
python-gendoc 0.73-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 312 kB
  • ctags: 845
  • sloc: python: 2,610; makefile: 124; sh: 26
file content (165 lines) | stat: -rw-r--r-- 5,334 bytes parent folder | download | duplicates (3)
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
#
# FILE            $Id: Formatters.py,v 1.4 1998/02/04 18:19:41 dlarsson Exp dlarsson $
#
# DESCRIPTION     Formatters for MIF and HTML manual formats.
#
# AUTHOR          SEISY/LKSB Daniel Larsson
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of ABB Industrial Systems
# not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior permission.
#
# ABB INDUSTRIAL SYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO
# THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS, IN NO EVENT SHALL ABB INDUSTRIAL SYSTEMS BE LIABLE
# FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# Copyright (C) ABB Industrial Systems AB, 1996
# Unpublished work.  All Rights Reserved.
#
# HISTORY:
# $Log: Formatters.py,v $
# Revision 1.4  1998/02/04 18:19:41  dlarsson
# Removed ni features.
#
# Revision 1.3  1997/01/23 20:43:58  omfadmin
# Added a warning message.
#
# Revision 1.2  1996/09/06  10:12:46  omfadmin
# Added 'try: ... except:' around importing formatters. Otherwise
# gendoc will fail if HTMLgen isn't present on the site.
#
# Revision 1.1  1996/06/24  10:07:57  omfadmin
# Initial revision
#
#

"""Format manual pages.

A manual page formatter translates a ManualPage into a concrete document
format, such as HTML, MIF, UNIX man or some other format.

A formatter needs to implement a number of methods, which are called by the
ManualPage class when rendering a document:

> # Example implementation of man formatter
> class Formatter:
>       file_ext = '.txt'
>
>	def _add_(self, txt):
>		# Add text to the document
>		self._text_ = self._text_ + txt
>
>	# -----   THESE ARE THE REQUIRED METHODS  -----
>	def __init__(self):
>		# String to collect document text
>		self._text_ = ''
>
>	def render_title(self, manpage, title, marker):
>		# **manpage** is a reference back to the manual page object.
>		# **title** is the title of the document.
>		# **marker** is a list of markers for the title. Markers
>		# are used to implement indices. See the MIFFormatter
>		# for how its used.
>		self._add_(title+'\\n\\n')
>
>	def render_section(self, manpage, lvl, section, marker):
>		# *lvl* is an integer indicating the nesting depth of
>		# the section. The title is at level 0, top level sections
>		# at level 1, and so on.
>		self._add_(section+'\\n\\n')
>
>	def render_paragraph(self, manpage, para):
>		# *para* is the paragraph text.
>		self._add_(para+'\\n\\n')
>
>	def render_code(self, manpage, code):
>		# *code* is the code snippet.
>		self._add_(code+'\\n\\n')
>
>	def render_ordered_list(self, manpage, list):
>		# *list* is a list of items.
>		code = ''
>		for i in range(len(list)):
>			code = code + '%d.  %s\\n' % (i+1, list[i])
>		self._add_(code+'\\n')
>
>	def render_list(self, manpage, list):
>		# *list* is a list of items.
>		code = ''
>		for i in list:
>			code = code + '*  %s\\n' % i
>		self._add_(code+'\\n')
>
>       # In this particular case (since nothing is done), these
>       # functions need not be implemented (render_bold, render_italic,
>       # render_underline, render_internal_link, render_external_link)
>       def render_bold(self, text):
>               return text
>
>       def render_italic(self, text):
>               return text
>
>       def render_underline(self, text):
>               return text
>
>       def render_internal_link(self, link, link_text):
>               return link_text
>
>       def render_external_link(self, link, link_text):
>               return link_text
>
>	def end(self):
>		# Called when the document is ready.
>		# IMPORTANT: The same formatter object is very likely
>		# to be used for more than one ManualPage, so we must
>		# remove the text also (alternatively, we might initialise
>		# self._text_ in 'render_title', since it is always called
>		# first).
>		text = self._text_
>		self._text_ = ''
>		return text
>
"""

__author__ = "Daniel Larsson"
__version__ = "$Revision: 1.4 $"

import os, sys

# Will contain all available formatters
formatters = {}

def main(path):
    files = reduce(lambda c, dir: c+os.listdir(dir), path, [])
    modules = filter(lambda file: file[-3:] == '.py', files)
    if path not in sys.path:
        sys.path.extend(path)

    cpy_modules = []
    for module in modules:
	if module not in ["Formatters.py", "__init__.py"]:
	    py_module = module[:-3]
	    try:
		cpy_modules.append(__import__(py_module))
	    except ImportError:
		sys.stderr.write('Warning: Failed to import %s (%s: %s)\n' % (py_module, sys.exc_type, sys.exc_value))

    global formatters
    from types import ClassType, ModuleType

    for object in cpy_modules:
        for mod_obj in object.__dict__.values():
            if type(mod_obj) == ClassType and hasattr(mod_obj, 'render_title'):
                formatters[mod_obj.__name__] = mod_obj