| | 284 | |
|---|
| | 285 | # ISearchSource methods |
|---|
| | 286 | def get_search_filters(self, req): |
|---|
| | 287 | if not req.perm.has_permission('IRCLOGS_VIEW'): |
|---|
| | 288 | return [] |
|---|
| | 289 | return [('irclogs', 'IRC Logs', True),] |
|---|
| | 290 | |
|---|
| | 291 | def _find_match(self, text, terms): |
|---|
| | 292 | # fixme: why can't pyndexter give us the position(s) where it |
|---|
| | 293 | # found the terms? |
|---|
| | 294 | for line in text.splitlines(): |
|---|
| | 295 | for term in terms: |
|---|
| | 296 | if term.lower() in line.lower(): |
|---|
| | 297 | return line |
|---|
| | 298 | |
|---|
| | 299 | def get_search_results(self, req, terms, filters): |
|---|
| | 300 | """Return a list of search results matching each search term in `terms`. |
|---|
| | 301 | |
|---|
| | 302 | The `filters` parameters is a list of the enabled filters, each item |
|---|
| | 303 | being the name of the tuples returned by `get_search_events`. |
|---|
| | 304 | |
|---|
| | 305 | The events returned by this function must be tuples of the form |
|---|
| | 306 | `(href, title, date, author, excerpt).` |
|---|
| | 307 | """ |
|---|
| | 308 | if not 'irclogs' in filters: |
|---|
| | 309 | return |
|---|
| | 310 | |
|---|
| | 311 | framework = pyndexter.Framework( |
|---|
| | 312 | indexer=self.indexer, stemmer='porter://', mode=pyndexter.READONLY) |
|---|
| | 313 | framework.add_source('file://%s?include=%s' % ( |
|---|
| | 314 | pyndexter.util.quote(self.path), self._get_file_glob())) |
|---|
| | 315 | |
|---|
| | 316 | for hit in framework.search(' '.join(terms)): |
|---|
| | 317 | file = hit.uri.path.lstrip(self.path) |
|---|
| | 318 | fileinfo = self._get_file_re().search(file).groupdict() |
|---|
| | 319 | |
|---|
| | 320 | url = "/irclogs/%(year)s/%(month)s/%(day)s" % fileinfo |
|---|
| | 321 | fragment = '' |
|---|
| | 322 | |
|---|
| | 323 | line = self._find_match(hit.current.content, terms) |
|---|
| | 324 | if line: |
|---|
| | 325 | lineinfo = self._line_re.search(line).groupdict() |
|---|
| | 326 | fragment = "#%(time)s" % lineinfo |
|---|
| | 327 | timestamp = parse_date("%(date)sT%(time)s" % lineinfo) |
|---|
| | 328 | else: |
|---|
| | 329 | timestamp = parse_date("%(year)s-%(month)s-%(day)s" % fileinfo) |
|---|
| | 330 | |
|---|
| | 331 | title = "irclogs for %s" % format_date(timestamp) |
|---|
| | 332 | |
|---|
| | 333 | yield (req.href(url)+fragment, title, timestamp, 'irclog', unicode(hit.excerpt(terms))) |
|---|
| | 334 | |