Debian Codegraph @

docutils/writers/pep_html/__init__.py

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