~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

Viewing changes to src/zope/index/text/htmlsplitter.py

  • Committer: Sidnei da Silva
  • Date: 2010-07-05 21:07:01 UTC
  • Revision ID: sidnei.da.silva@canonical.com-20100705210701-zmqhqrbzad1mhzsl
- Reduce deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
##############################################################################
2
 
#
3
 
# Copyright (c) 2002 Zope Corporation and Contributors.
4
 
# All Rights Reserved.
5
 
#
6
 
# This software is subject to the provisions of the Zope Public License,
7
 
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
 
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
 
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
 
# FOR A PARTICULAR PURPOSE.
12
 
#
13
 
##############################################################################
14
 
"""HTML Splitter
15
 
 
16
 
$Id: htmlsplitter.py 100821 2009-06-10 23:48:01Z tseaver $
17
 
"""
18
 
import re
19
 
 
20
 
from zope.interface import implements
21
 
 
22
 
from zope.index.text.interfaces import ISplitter
23
 
 
24
 
MARKUP = re.compile(r"(<[^<>]*>|&[A-Za-z]+;)")
25
 
WORDS = re.compile(r"(?L)\w+")
26
 
GLOBS = re.compile(r"(?L)\w+[\w*?]*")
27
 
 
28
 
class HTMLWordSplitter(object):
29
 
 
30
 
    implements(ISplitter)
31
 
 
32
 
    def process(self, text):
33
 
        return self._apply(text, WORDS)
34
 
 
35
 
    def processGlob(self, text):
36
 
        # see Lexicon.globToWordIds()
37
 
        return self._apply(text, GLOBS)
38
 
 
39
 
    def _apply(self, text, pattern):
40
 
        result = []
41
 
        for chunk in text:
42
 
            result.extend(self._split(chunk, pattern))
43
 
        return result
44
 
 
45
 
    def _split(self, text, pattern):
46
 
        text = MARKUP.sub(' ', text.lower())
47
 
        return pattern.findall(text)