| [13393] | 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006 Alec Thomas <alec@swapoff.org> |
|---|
| [14148] | 4 | # Copyright (C) 2013,2014 Steffen Hoffmann <hoff.st@web.de> |
|---|
| [14157] | 5 | # Copyright (C) 2014 Ryan J Ollos <ryan.j.ollos@gmail.com> |
|---|
| [18140] | 6 | # Copyright (C) 2021 Cinc |
|---|
| [13393] | 7 | # |
|---|
| 8 | # This software is licensed as described in the file COPYING, which |
|---|
| 9 | # you should have received as part of this distribution. |
|---|
| 10 | # |
|---|
| 11 | |
|---|
| 12 | import re |
|---|
| [14950] | 13 | from functools import partial |
|---|
| [13393] | 14 | |
|---|
| [14157] | 15 | from trac.test import Mock, MockPerm |
|---|
| [15559] | 16 | from trac.web.api import _RequestArgs |
|---|
| [14157] | 17 | |
|---|
| [13393] | 18 | _TAG_SPLIT = re.compile('[,\s]+') |
|---|
| 19 | |
|---|
| 20 | |
|---|
| [14157] | 21 | # DEVEL: This needs monitoring for possibly varying endpoint requirements. |
|---|
| [15559] | 22 | MockReq = partial(Mock, args=_RequestArgs(), authname='anonymous', |
|---|
| [16943] | 23 | perm=MockPerm(), session=dict(), |
|---|
| 24 | is_authenticated=lambda a: a != 'anonymous') |
|---|
| [14148] | 25 | |
|---|
| [13393] | 26 | |
|---|
| [14157] | 27 | def query_realms(query, all_realms): |
|---|
| 28 | realms = [] |
|---|
| 29 | for realm in all_realms: |
|---|
| 30 | if re.search('(^|\W)realm:%s(\W|$)' % realm, query): |
|---|
| 31 | realms.append(realm) |
|---|
| 32 | return realms |
|---|
| 33 | |
|---|
| [15559] | 34 | |
|---|
| [14157] | 35 | def split_into_tags(text): |
|---|
| 36 | """Split plain text into tags.""" |
|---|
| 37 | return set(filter(None, [tag.strip() for tag in _TAG_SPLIT.split(text)])) |
|---|
| [18140] | 38 | |
|---|
| 39 | class JTransformer(object): |
|---|
| 40 | """Class modelled after the Genshi Transformer class. Instead of an xpath it uses a |
|---|
| 41 | selector usable by jQuery. |
|---|
| 42 | You may use cssify (https://github.com/santiycr/cssify) to convert a xpath to a selector.""" |
|---|
| 43 | |
|---|
| 44 | def __init__(self, xpath): |
|---|
| 45 | self.css = xpath # xpath must be a css selector for jQuery |
|---|
| 46 | |
|---|
| 47 | def after(self, html): |
|---|
| 48 | return {'pos': 'after', 'css': self.css, 'html': html} |
|---|
| 49 | |
|---|
| 50 | def before(self, html): |
|---|
| 51 | return {'pos': 'before', 'css': self.css, 'html': html} |
|---|
| 52 | |
|---|
| 53 | def prepend(self, html): |
|---|
| 54 | return {'pos': 'prepend', 'css': self.css, 'html': html} |
|---|
| 55 | |
|---|
| 56 | def append(self, html): |
|---|
| 57 | return {'pos': 'append', 'css': self.css, 'html': html} |
|---|
| 58 | |
|---|
| 59 | def remove(self): |
|---|
| 60 | return {'pos': 'remove', 'css': self.css, 'html': ''} |
|---|
| 61 | |
|---|
| 62 | def replace(self, html): |
|---|
| 63 | return {'pos': 'replace', 'css': self.css, 'html': html} |
|---|