Mark up some plain text, given a context of symbols to look for. Each context dictionary maps object names to anchor names.
(self, text, escape=None, funcs={}, classes={}, methods={})
| 711 | return '<a href="file:%s">%s</a>' % (url, path) |
| 712 | |
| 713 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 714 | """Mark up some plain text, given a context of symbols to look for. |
| 715 | Each context dictionary maps object names to anchor names.""" |
| 716 | escape = escape or self.escape |
| 717 | results = [] |
| 718 | here = 0 |
| 719 | pattern = re.compile(r'\b((http|https|ftp)://\S+[\w/]|' |
| 720 | r'RFC[- ]?(\d+)|' |
| 721 | r'PEP[- ]?(\d+)|' |
| 722 | r'(self\.)?(\w+))') |
| 723 | while True: |
| 724 | match = pattern.search(text, here) |
| 725 | if not match: break |
| 726 | start, end = match.span() |
| 727 | results.append(escape(text[here:start])) |
| 728 | |
| 729 | all, scheme, rfc, pep, selfdot, name = match.groups() |
| 730 | if scheme: |
| 731 | url = escape(all).replace('"', '"') |
| 732 | results.append('<a href="%s">%s</a>' % (url, url)) |
| 733 | elif rfc: |
| 734 | url = 'https://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 735 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 736 | elif pep: |
| 737 | url = 'https://peps.python.org/pep-%04d/' % int(pep) |
| 738 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 739 | elif selfdot: |
| 740 | # Create a link for methods like 'self.method(...)' |
| 741 | # and use <strong> for attributes like 'self.attr' |
| 742 | if text[end:end+1] == '(': |
| 743 | results.append('self.' + self.namelink(name, methods)) |
| 744 | else: |
| 745 | results.append('self.<strong>%s</strong>' % name) |
| 746 | elif text[end:end+1] == '(': |
| 747 | results.append(self.namelink(name, methods, funcs, classes)) |
| 748 | else: |
| 749 | results.append(self.namelink(name, classes)) |
| 750 | here = end |
| 751 | results.append(escape(text[here:])) |
| 752 | return ''.join(results) |
| 753 | |
| 754 | # ---------------------------------------------- type-specific routines |
| 755 |
no test coverage detected