Class for safely making an HTML representation of a Python object.
| 536 | # -------------------------------------------- HTML documentation generator |
| 537 | |
| 538 | class HTMLRepr(Repr): |
| 539 | """Class for safely making an HTML representation of a Python object.""" |
| 540 | def __init__(self): |
| 541 | Repr.__init__(self) |
| 542 | self.maxlist = self.maxtuple = 20 |
| 543 | self.maxdict = 10 |
| 544 | self.maxstring = self.maxother = 100 |
| 545 | |
| 546 | def escape(self, text): |
| 547 | return replace(text, '&', '&', '<', '<', '>', '>') |
| 548 | |
| 549 | def repr(self, object): |
| 550 | return Repr.repr(self, object) |
| 551 | |
| 552 | def repr1(self, x, level): |
| 553 | if hasattr(type(x), '__name__'): |
| 554 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
| 555 | if hasattr(self, methodname): |
| 556 | return getattr(self, methodname)(x, level) |
| 557 | return self.escape(cram(stripid(repr(x)), self.maxother)) |
| 558 | |
| 559 | def repr_string(self, x, level): |
| 560 | test = cram(x, self.maxstring) |
| 561 | testrepr = repr(test) |
| 562 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
| 563 | # Backslashes are only literal in the string and are never |
| 564 | # needed to make any special characters, so show a raw string. |
| 565 | return 'r' + testrepr[0] + self.escape(test) + testrepr[0] |
| 566 | return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)', |
| 567 | r'<span class="repr">\1</span>', |
| 568 | self.escape(testrepr)) |
| 569 | |
| 570 | repr_str = repr_string |
| 571 | |
| 572 | def repr_instance(self, x, level): |
| 573 | try: |
| 574 | return self.escape(cram(stripid(repr(x)), self.maxstring)) |
| 575 | except: |
| 576 | return self.escape('<%s instance>' % x.__class__.__name__) |
| 577 | |
| 578 | repr_unicode = repr_string |
| 579 | |
| 580 | class HTMLDoc(Doc): |
| 581 | """Formatter class for HTML documentation.""" |