source: tagsplugin/trunk/tractags/util.py

Last change on this file was 18140, checked in by Cinc-th, 2 years ago

TagsPlugin: mo longer use Genshi Transformer and ITemplateStreamFilter for TagTimelineEventFilter.

Refs #13993

File size: 2.0 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006 Alec Thomas <alec@swapoff.org>
4# Copyright (C) 2013,2014 Steffen Hoffmann <hoff.st@web.de>
5# Copyright (C) 2014 Ryan J Ollos <ryan.j.ollos@gmail.com>
6# Copyright (C) 2021 Cinc
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
12import re
13from functools import partial
14
15from trac.test import Mock, MockPerm
16from trac.web.api import _RequestArgs
17
18_TAG_SPLIT = re.compile('[,\s]+')
19
20
21# DEVEL: This needs monitoring for possibly varying endpoint requirements.
22MockReq = partial(Mock, args=_RequestArgs(), authname='anonymous',
23                  perm=MockPerm(), session=dict(),
24                  is_authenticated=lambda a: a != 'anonymous')
25
26
27def 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
34
35def split_into_tags(text):
36    """Split plain text into tags."""
37    return set(filter(None, [tag.strip() for tag in _TAG_SPLIT.split(text)]))
38
39class 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}
Note: See TracBrowser for help on using the repository browser.