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