| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
import docutils |
| 17 |
from docutils import frontend, nodes, utils |
| 18 |
from docutils.writers import html4css1 |
| 19 |
|
| 20 |
|
| 21 |
class Writer(html4css1.Writer): |
| 22 |
|
| 23 |
default_stylesheet = 'pep.css' |
| 24 |
|
| 25 |
default_stylesheet_path = utils.relative_path( |
| 26 |
os.path.join(os.getcwd(), 'dummy'), |
| •27 |
os.path.join(docutils._datadir(__file__), default_stylesheet)) |
| 28 |
|
| 29 |
default_template = 'template.txt' |
| 30 |
|
| 31 |
default_template_path = utils.relative_path( |
| 32 |
os.path.join(os.getcwd(), 'dummy'), |
| •33 |
os.path.join(docutils._datadir(__file__), default_template)) |
| 34 |
|
| 35 |
settings_spec = html4css1.Writer.settings_spec + ( |
| 36 |
'PEP/HTML Writer 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 Writer Options above.' |
| 40 |
% (default_stylesheet_path, default_template_path), |
| 41 |
(('Python\'s home URL. Default is "https://www.python.org".', |
| 42 |
['--python-home'], |
| 43 |
{'default': 'https://www.python.org', 'metavar': '<URL>'}), |
| 44 |
('Home URL prefix for PEPs. Default is "." (current directory).', |
| 45 |
['--pep-home'], |
| 46 |
{'default': '.', 'metavar': '<URL>'}), |
| 47 |
|
| 48 |
(frontend.SUPPRESS_HELP, |
| 49 |
['--no-random'], |
| 50 |
{'action': 'store_true', 'validator': frontend.validate_boolean}),)) |
| 51 |
|
| 52 |
settings_default_overrides = {'stylesheet_path': default_stylesheet_path, |
| 53 |
'template': default_template_path} |
| 54 |
relative_path_settings = ('template',) |
| 55 |
config_section = 'pep_html writer' |
| 56 |
config_section_dependencies = ('writers', 'html writers', |
| 57 |
'html4css1 writer') |
| 58 |
|
| 59 |
def __init__(self) -> None: |
| 60 |
html4css1.Writer.__init__(self) |
| 61 |
self.translator_class = HTMLTranslator |
| 62 |
|
| 63 |
def interpolation_dict(self): |
| 64 |
subs = html4css1.Writer.interpolation_dict(self) |
| 65 |
settings = self.document.settings |
| 66 |
pyhome = settings.python_home |
| 67 |
subs['pyhome'] = pyhome |
| 68 |
subs['pephome'] = settings.pep_home |
| 69 |
if pyhome == '..': |
| 70 |
subs['pepindex'] = '.' |
| 71 |
else: |
| 72 |
subs['pepindex'] = pyhome + '/dev/peps' |
| 73 |
index = self.document.first_child_matching_class(nodes.field_list) |
| 74 |
header = self.document[index] |
| 75 |
self.pepnum = header[0][1].astext() |
| 76 |
subs['pep'] = self.pepnum |
| 77 |
if settings.no_random: |
| 78 |
subs['banner'] = 0 |
| 79 |
else: |
| 80 |
import random |
| 81 |
subs['banner'] = random.randrange(64) |
| 82 |
try: |
| 83 |
subs['pepnum'] = '%04i' % int(self.pepnum) |
| 84 |
except ValueError: |
| 85 |
subs['pepnum'] = self.pepnum |
| 86 |
self.title = header[1][1].astext() |
| 87 |
subs['title'] = self.title |
| 88 |
subs['body'] = ''.join( |
| 89 |
self.body_pre_docinfo + self.docinfo + self.body) |
| 90 |
return subs |
| 91 |
|
| 92 |
def assemble_parts(self) -> None: |
| 93 |
html4css1.Writer.assemble_parts(self) |
| 94 |
self.parts['title'] = [self.title] |
| 95 |
self.parts['pepnum'] = self.pepnum |
| 96 |
|
| 97 |
|
| 98 |
class HTMLTranslator(html4css1.HTMLTranslator): |
| 99 |
|
| 100 |
def depart_field_list(self, node) -> None: |
| 101 |
html4css1.HTMLTranslator.depart_field_list(self, node) |
| 102 |
if 'rfc2822' in node['classes']: |
| 103 |
self.body.append('<hr />\n') |