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