Class for safely making an HTML representation of a Python object.
| 600 | # -------------------------------------------- HTML documentation generator |
| 601 | |
| 602 | class HTMLRepr(Repr): |
| 603 | """Class for safely making an HTML representation of a Python object.""" |
| 604 | def __init__(self): |
| 605 | Repr.__init__(self) |
| 606 | self.maxlist = self.maxtuple = 20 |
| 607 | self.maxdict = 10 |
| 608 | self.maxstring = self.maxother = 100 |
| 609 | |
| 610 | def escape(self, text): |
| 611 | return replace(text, '&', '&', '<', '<', '>', '>') |
| 612 | |
| 613 | def repr(self, object): |
| 614 | return Repr.repr(self, object) |
| 615 | |
| 616 | def repr1(self, x, level): |
| 617 | if hasattr(type(x), '__name__'): |
| 618 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
| 619 | if hasattr(self, methodname): |
| 620 | return getattr(self, methodname)(x, level) |
| 621 | return self.escape(cram(stripid(repr(x)), self.maxother)) |
| 622 | |
| 623 | def repr_string(self, x, level): |
| 624 | test = cram(x, self.maxstring) |
| 625 | testrepr = repr(test) |
| 626 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
| 627 | # Backslashes are only literal in the string and are never |
| 628 | # needed to make any special characters, so show a raw string. |
| 629 | return 'r' + testrepr[0] + self.escape(test) + testrepr[0] |
| 630 | return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)', |
| 631 | r'<span class="repr">\1</span>', |
| 632 | self.escape(testrepr)) |
| 633 | |
| 634 | repr_str = repr_string |
| 635 | |
| 636 | def repr_instance(self, x, level): |
| 637 | try: |
| 638 | return self.escape(cram(stripid(repr(x)), self.maxstring)) |
| 639 | except: |
| 640 | return self.escape('<%s instance>' % x.__class__.__name__) |
| 641 | |
| 642 | repr_unicode = repr_string |
| 643 | |
| 644 | class HTMLDoc(Doc): |
| 645 | """Formatter class for HTML documentation.""" |