Website: add template and basic stylesheet with menu. Update buildhtml.py (and theref...
[docutils.git] / sandbox / gitpull / web_stylesheet_and_menu / docutils / writers / pep_html / __init__.py
blob0ad79e01da02c1b106dc00a3910f3dcedfc2b61d
1 # $Id$
2 # Author: David Goodger <goodger@python.org>
3 # Copyright: This module has been placed in the public domain.
5 """
6 PEP HTML Writer.
7 """
9 __docformat__ = 'reStructuredText'
12 import sys
13 import os
14 import os.path
15 import codecs
16 import docutils
17 from docutils import frontend, nodes, utils, writers
18 from docutils.writers import html4css1
21 class Writer(html4css1.Writer):
23 default_stylesheet = 'pep.css'
25 default_stylesheet_path = utils.relative_path(
26 os.path.join(os.getcwd(), 'dummy'),
27 os.path.join(os.path.dirname(__file__), default_stylesheet))
29 default_template = 'template.txt'
31 default_template_path = utils.relative_path(
32 os.path.join(os.getcwd(), 'dummy'),
33 os.path.join(os.path.dirname(__file__), default_template))
35 settings_spec = html4css1.Writer.settings_spec + (
36 'PEP/HTML-Specific Options',
37 'For the PEP/HTML writer, the default value for the --stylesheet-path '
38 'option is "%s", and the default value for --template is "%s". '
39 'See HTML-Specific Options above.'
40 % (default_stylesheet_path, default_template_path),
41 (('Python\'s home URL. Default is "http://www.python.org".',
42 ['--python-home'],
43 {'default': 'http://www.python.org', 'metavar': '<URL>'}),
44 ('Home URL prefix for PEPs. Default is "." (current directory).',
45 ['--pep-home'],
46 {'default': '.', 'metavar': '<URL>'}),
47 # For testing.
48 (frontend.SUPPRESS_HELP,
49 ['--no-random'],
50 {'action': 'store_true', 'validator': frontend.validate_boolean}),))
52 settings_default_overrides = {'stylesheet_path': default_stylesheet_path,
53 'template': default_template_path,}
55 relative_path_settings = ('template',)
57 config_section = 'pep_html writer'
58 config_section_dependencies = ('writers', 'html4css1 writer')
60 def __init__(self):
61 html4css1.Writer.__init__(self)
62 self.translator_class = HTMLTranslator
64 def interpolation_dict(self):
65 subs = html4css1.Writer.interpolation_dict(self)
66 settings = self.document.settings
67 pyhome = settings.python_home
68 subs['pyhome'] = pyhome
69 subs['pephome'] = settings.pep_home
70 if pyhome == '..':
71 subs['pepindex'] = '.'
72 else:
73 subs['pepindex'] = pyhome + '/dev/peps'
74 index = self.document.first_child_matching_class(nodes.field_list)
75 header = self.document[index]
76 self.pepnum = header[0][1].astext()
77 subs['pep'] = self.pepnum
78 if settings.no_random:
79 subs['banner'] = 0
80 else:
81 import random
82 subs['banner'] = random.randrange(64)
83 try:
84 subs['pepnum'] = '%04i' % int(self.pepnum)
85 except ValueError:
86 subs['pepnum'] = self.pepnum
87 self.title = header[1][1].astext()
88 subs['title'] = self.title
89 subs['body'] = ''.join(
90 self.body_pre_docinfo + self.docinfo + self.body)
91 return subs
93 def assemble_parts(self):
94 html4css1.Writer.assemble_parts(self)
95 self.parts['title'] = [self.title]
96 self.parts['pepnum'] = self.pepnum
99 class HTMLTranslator(html4css1.HTMLTranslator):
101 def depart_field_list(self, node):
102 html4css1.HTMLTranslator.depart_field_list(self, node)
103 if 'rfc2822' in node['classes']:
104 self.body.append('<hr />\n')