Class for safely making an HTML representation of a Python object.
| 555 | # -------------------------------------------- HTML documentation generator |
| 556 | |
| 557 | class HTMLRepr(Repr): |
| 558 | """Class for safely making an HTML representation of a Python object.""" |
| 559 | def __init__(self): |
| 560 | Repr.__init__(self) |
| 561 | self.maxlist = self.maxtuple = 20 |
| 562 | self.maxdict = 10 |
| 563 | self.maxstring = self.maxother = 100 |
| 564 | |
| 565 | def escape(self, text): |
| 566 | return replace(text, '&', '&', '<', '<', '>', '>') |
| 567 | |
| 568 | def repr(self, object): |
| 569 | return Repr.repr(self, object) |
| 570 | |
| 571 | def repr1(self, x, level): |
| 572 | if hasattr(type(x), '__name__'): |
| 573 | methodname = 'repr_' + '_'.join(type(x).__name__.split()) |
| 574 | if hasattr(self, methodname): |
| 575 | return getattr(self, methodname)(x, level) |
| 576 | return self.escape(cram(stripid(repr(x)), self.maxother)) |
| 577 | |
| 578 | def repr_string(self, x, level): |
| 579 | test = cram(x, self.maxstring) |
| 580 | testrepr = repr(test) |
| 581 | if '\\' in test and '\\' not in replace(testrepr, r'\\', ''): |
| 582 | # Backslashes are only literal in the string and are never |
| 583 | # needed to make any special characters, so show a raw string. |
| 584 | return 'r' + testrepr[0] + self.escape(test) + testrepr[0] |
| 585 | return re.sub(r'((\\[\\abfnrtv\'"]|\\[0-9]..|\\x..|\\u....)+)', |
| 586 | r'<span class="repr">\1</span>', |
| 587 | self.escape(testrepr)) |
| 588 | |
| 589 | repr_str = repr_string |
| 590 | |
| 591 | def repr_instance(self, x, level): |
| 592 | try: |
| 593 | return self.escape(cram(stripid(repr(x)), self.maxstring)) |
| 594 | except: |
| 595 | return self.escape('<%s instance>' % x.__class__.__name__) |
| 596 | |
| 597 | repr_unicode = repr_string |
| 598 | |
| 599 | class HTMLDoc(Doc): |
| 600 | """Formatter class for HTML documentation.""" |
no outgoing calls
no test coverage detected
searching dependent graphs…