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={})
| 775 | return '<a href="file:%s">%s</a>' % (url, path) |
| 776 | |
| 777 | def markup(self, text, escape=None, funcs={}, classes={}, methods={}): |
| 778 | """Mark up some plain text, given a context of symbols to look for. |
| 779 | Each context dictionary maps object names to anchor names.""" |
| 780 | escape = escape or self.escape |
| 781 | results = [] |
| 782 | here = 0 |
| 783 | pattern = re.compile(r'\b((http|https|ftp)://\S+[\w/]|' |
| 784 | r'RFC[- ]?(\d+)|' |
| 785 | r'PEP[- ]?(\d+)|' |
| 786 | r'(self\.)?(\w+))') |
| 787 | while match := pattern.search(text, here): |
| 788 | start, end = match.span() |
| 789 | results.append(escape(text[here:start])) |
| 790 | |
| 791 | all, scheme, rfc, pep, selfdot, name = match.groups() |
| 792 | if scheme: |
| 793 | url = escape(all).replace('"', '"') |
| 794 | results.append('<a href="%s">%s</a>' % (url, url)) |
| 795 | elif rfc: |
| 796 | url = 'https://www.rfc-editor.org/rfc/rfc%d.txt' % int(rfc) |
| 797 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 798 | elif pep: |
| 799 | url = 'https://peps.python.org/pep-%04d/' % int(pep) |
| 800 | results.append('<a href="%s">%s</a>' % (url, escape(all))) |
| 801 | elif selfdot: |
| 802 | # Create a link for methods like 'self.method(...)' |
| 803 | # and use <strong> for attributes like 'self.attr' |
| 804 | if text[end:end+1] == '(': |
| 805 | results.append('self.' + self.namelink(name, methods)) |
| 806 | else: |
| 807 | results.append('self.<strong>%s</strong>' % name) |
| 808 | elif text[end:end+1] == '(': |
| 809 | results.append(self.namelink(name, methods, funcs, classes)) |
| 810 | else: |
| 811 | results.append(self.namelink(name, classes)) |
| 812 | here = end |
| 813 | results.append(escape(text[here:])) |
| 814 | return ''.join(results) |
| 815 | |
| 816 | # ---------------------------------------------- type-specific routines |
| 817 |
no test coverage detected