| 1 | # $Id: __init__.py 4564 2006-05-21 20:44:42Z wiemann $ |
|---|
| 2 | # Author: David Goodger <goodger@python.org> |
|---|
| 3 | # Copyright: This module has been placed in the public domain. |
|---|
| 4 | |
|---|
| 5 | """ |
|---|
| 6 | BEP HTML Writer. |
|---|
| 7 | """ |
|---|
| 8 | |
|---|
| 9 | __docformat__ = 'reStructuredText' |
|---|
| 10 | |
|---|
| 11 | bthome = "http://www.bittorrent.org" |
|---|
| 12 | |
|---|
| 13 | import sys |
|---|
| 14 | import os |
|---|
| 15 | import os.path |
|---|
| 16 | import codecs |
|---|
| 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 = 'bep.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 | 'BEP/HTML-Specific Options', |
|---|
| 38 | 'For the BEP/HTML writer, the default value for the --stylesheet-path ' |
|---|
| 39 | 'option is "%s", and the default value for --template is "%s". ' |
|---|
| 40 | 'See HTML-Specific Options above.' |
|---|
| 41 | % (default_stylesheet_path, default_template_path), |
|---|
| 42 | (('Python\'s home URL. Default is "http://www.python.org".', |
|---|
| 43 | ['--python-home'], |
|---|
| 44 | {'default': 'http://www.python.org', 'metavar': '<URL>'}), |
|---|
| 45 | ('Home URL prefix for BEPs. Default is "." (current directory).', |
|---|
| 46 | ['--bep-home'], |
|---|
| 47 | {'default': '.', 'metavar': '<URL>'}), |
|---|
| 48 | # For testing. |
|---|
| 49 | (frontend.SUPPRESS_HELP, |
|---|
| 50 | ['--no-random'], |
|---|
| 51 | {'action': 'store_true', 'validator': frontend.validate_boolean}),)) |
|---|
| 52 | |
|---|
| 53 | settings_default_overrides = {'stylesheet_path': default_stylesheet_path, |
|---|
| 54 | 'template': default_template_path,} |
|---|
| 55 | |
|---|
| 56 | relative_path_settings = (html4css1.Writer.relative_path_settings |
|---|
| 57 | + ('template',)) |
|---|
| 58 | |
|---|
| 59 | config_section = 'bep_html writer' |
|---|
| 60 | config_section_dependencies = ('writers', 'html4css1 writer') |
|---|
| 61 | |
|---|
| 62 | def __init__(self): |
|---|
| 63 | html4css1.Writer.__init__(self) |
|---|
| 64 | self.translator_class = HTMLTranslator |
|---|
| 65 | |
|---|
| 66 | def interpolation_dict(self): |
|---|
| 67 | subs = html4css1.Writer.interpolation_dict(self) |
|---|
| 68 | settings = self.document.settings |
|---|
| 69 | pyhome = settings.python_home |
|---|
| 70 | subs['pyhome'] = pyhome |
|---|
| 71 | subs['bthome'] = bthome |
|---|
| 72 | subs['bephome'] = settings.bep_home |
|---|
| 73 | if pyhome == '..': |
|---|
| 74 | subs['bepindex'] = '.' |
|---|
| 75 | else: |
|---|
| 76 | subs['bepindex'] = bthome + '/beps' |
|---|
| 77 | index = self.document.first_child_matching_class(nodes.field_list) |
|---|
| 78 | header = self.document[index] |
|---|
| 79 | self.bepnum = header[0][1].astext() |
|---|
| 80 | subs['bep'] = self.bepnum |
|---|
| 81 | if settings.no_random: |
|---|
| 82 | subs['banner'] = 0 |
|---|
| 83 | else: |
|---|
| 84 | import random |
|---|
| 85 | subs['banner'] = random.randrange(64) |
|---|
| 86 | try: |
|---|
| 87 | subs['bepnum'] = '%04i' % int(self.bepnum) |
|---|
| 88 | except ValueError: |
|---|
| 89 | subs['bepnum'] = bepnum |
|---|
| 90 | self.title = header[1][1].astext() |
|---|
| 91 | subs['title'] = self.title |
|---|
| 92 | subs['body'] = ''.join( |
|---|
| 93 | self.body_pre_docinfo + self.docinfo + self.body) |
|---|
| 94 | return subs |
|---|
| 95 | |
|---|
| 96 | def assemble_parts(self): |
|---|
| 97 | html4css1.Writer.assemble_parts(self) |
|---|
| 98 | self.parts['title'] = [self.title] |
|---|
| 99 | self.parts['bepnum'] = self.bepnum |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | class HTMLTranslator(html4css1.HTMLTranslator): |
|---|
| 103 | |
|---|
| 104 | def depart_field_list(self, node): |
|---|
| 105 | html4css1.HTMLTranslator.depart_field_list(self, node) |
|---|
| 106 | if 'rfc2822' in node['classes']: |
|---|
| 107 | self.body.append('<hr />\n') |
|---|