| 64 | |
| 65 | |
| 66 | class ReSTStyle(BaseStyle): |
| 67 | def __init__(self, doc, indent_width=2): |
| 68 | BaseStyle.__init__(self, doc, indent_width) |
| 69 | self.do_p = True |
| 70 | self.a_href = None |
| 71 | self.list_depth = 0 |
| 72 | |
| 73 | def new_paragraph(self): |
| 74 | self.doc.write('\n\n%s' % self.spaces()) |
| 75 | |
| 76 | def new_line(self): |
| 77 | self.doc.write('\n%s' % self.spaces()) |
| 78 | |
| 79 | def _start_inline(self, markup): |
| 80 | self.doc.write(markup) |
| 81 | |
| 82 | def _end_inline(self, markup): |
| 83 | # Sometimes the HTML markup has whitespace between the end |
| 84 | # of the text inside the inline markup and the closing element |
| 85 | # (e.g. <b>foobar </b>). This trailing space will cause |
| 86 | # problems in the ReST inline markup so we remove it here |
| 87 | # by popping the last item written off the stack, striping |
| 88 | # the whitespace and then pushing it back on the stack. |
| 89 | last_write = self.doc.pop_write().rstrip(' ') |
| 90 | |
| 91 | # Sometimes, for whatever reason, a tag like <b/> is present. This |
| 92 | # is problematic because if we simply translate that directly then |
| 93 | # we end up with something like ****, which rst will assume is a |
| 94 | # heading instead of an empty bold. |
| 95 | if last_write == markup: |
| 96 | return |
| 97 | |
| 98 | self.doc.push_write(last_write) |
| 99 | self.doc.write(markup + ' ') |
| 100 | |
| 101 | def start_bold(self, attrs=None): |
| 102 | self._start_inline('**') |
| 103 | |
| 104 | def end_bold(self): |
| 105 | self._end_inline('**') |
| 106 | |
| 107 | def start_b(self, attrs=None): |
| 108 | self.doc.do_translation = True |
| 109 | self.start_bold(attrs) |
| 110 | |
| 111 | def end_b(self): |
| 112 | self.doc.do_translation = False |
| 113 | self.end_bold() |
| 114 | |
| 115 | def bold(self, s): |
| 116 | if s: |
| 117 | self.start_bold() |
| 118 | self.doc.write(s) |
| 119 | self.end_bold() |
| 120 | |
| 121 | def ref(self, title, link=None): |
| 122 | if link is None: |
| 123 | link = title |
no outgoing calls