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={})
| 730 | return '<a href="file:%s">%s</a>' % (url, path) |
| 731 | |
| 732 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 733 | """Mark up some plain text, given a context of symbols to look for. |
| 734 | Each context dictionary maps object names to anchor names.""" |
| 735 | escape = escape or self.escape |
| 736 | results = [] |
| 737 | here = 0 |
| 738 | pattern = re.compile(r'\b((http|https|ftp)://\S+[\w/]|' |
| 739 | r'RFC[- ]?(\d+)|' |
| 740 | r'PEP[- ]?(\d+)|' |
| 741 | r'(self\.)?(\w+))') |
| 742 | while match := pattern.search(text, here): |
| 743 | start, end = match.span() |
| 744 | results.append(escape(text[here:start])) |
| 745 | |
| 746 | all, scheme, rfc, pep, selfdot, name = match.groups() |
| 747 | if scheme: |
| 748 | url = escape(all).replace('"', '"') |
| 749 | results.append('<a href="%s">%s</a>' % (url, url)) |
| 750 | elif rfc: |
| 751 | url = 'https://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 752 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 753 | elif pep: |
| 754 | url = 'https://peps.python.org/pep-%04d/' % int(pep) |
| 755 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 756 | elif selfdot: |
| 757 | # Create a link for methods like 'self.method(...)' |
| 758 | # and use <strong> for attributes like 'self.attr' |
| 759 | if text[end:end+1] == '(': |
| 760 | results.append('self.' + self.namelink(name, methods)) |
| 761 | else: |
| 762 | results.append('self.<strong>%s</strong>' % name) |
| 763 | elif text[end:end+1] == '(': |
| 764 | results.append(self.namelink(name, methods, funcs, classes)) |
| 765 | else: |
| 766 | results.append(self.namelink(name, classes)) |
| 767 | here = end |
| 768 | results.append(escape(text[here:])) |
| 769 | return ''.join(results) |
| 770 | |
| 771 | # ---------------------------------------------- type-specific routines |
| 772 |
no test coverage detected