Recent Changes - Search:

Cookbook

PmWiki

pmwiki.org

Excerpts

Summary: Shows fragment of page around given word(s).
Version: 2007-09-30 - v1.2
Prerequisites: PmWiki2
Status: beta
Maintainer: MateuszCzaplinski
Categories: Searching

Questions answered by this recipe

Can I show (for example in my search results) a fragment of a page around given word, with the word shown in bold, as it's done in Google search results?

Description

After installation, two new markups are available:

Single excerpt

(:excerpt Group.Page "search terms" :) or
(:excerpt Group.Page term :)

This syntax displays one line, showing where in specified page is the "search terms" phrase, or "term" word.

Example:

 (:excerpt PmWiki.InitialSetupTasks "when you":)
- will find words around the first occurence of "when you" phrase in the PmWiki.InitialSetupTasks page, which will show as:

...n file (''local/config.php'') When you first install PmWiki, the ''l...

A pack of excerpts

(:excerpts Group.Page first_term second_term ... :)

The plural syntax (note the 's' in 'excerpts') allows to display several excerpts of the same page using one directive. It creates a single excerpt for each specified term.

Example:

 (:excerpts PmWiki.WikiGroup foo bar:)
- will find an excerpt for each of "foo" and "bar":

...e a default page in the group Foo, create a link to [=[[Foo/]]=...
....]]=]). To make a page called Bar in the group Foo, create a li...

Installation

Installation is standard: download excerpts.phpΔ, save it in your cookbook/ directory, add include_once("$FarmD/cookbook/excerpts.php"); to your local/config.php file.

Configuration parameters (optional):

$ExcerptSideWidth = 30;
How many characters of the page are included at each side of the matched fragment.

Notes

  • It's case insensitive as intended, but not for UTF-8 characters. I haven't yet came with a good solution; see source code for details. If you want to improve the code, please feel welcome. I'm also open for suggestions - please put them in comments, below.
  • Todo: The recipe should first try to match whole words, and match parts of the words only if that failed.
  • Todo: Test if the (:excerpts:) directive works properly both with single (') and double quotes ("). Then test Cookbook:SortByScore about the same (or doesn't the latter need to work with singles?).
  • Todo: The Markup() regexp should require whitespaces between (:excerpt or (:excerpts and characters written immediately afterwards.

Release Notes

  • 2007-09-30 - v1.2
    • removed unused ExcertpsToupper() function
    • added $ExcerptSideWidth
  • 2007-09-26 - v1.1 - bug fix, which helps to make Cookbook:SortByScore's lists cleaner when excerpts not required
  • 2006-12-21 - v0.9 - initial release, by MateuszCzaplinski

Comments

  • Is there any way to hide (or process) markup in the excerpts? Francis March 02, 2007, at 03:51 AM
    • There is no simple way for that currently. I wanted to do this the way you suggest, but problems arose and I found no simple solution yet. MateuszCzaplinski September 26, 2007, at 01:50 PM
  • In more details:
    • To parse markup correctly, I think at least one line of page text should be included. But that can result in a veeery long excerpt.
    • This recipe is designed to work with Cookbook:SortByScore. The latter currently searches whole page text (as does PmWiki's normal search). And if markup was processed, some matches (like 'include') would disappear, and that might lead reader to confusion.
  • That said, I don't claim that's completely impossible. I suppose that might be a good goal for some 1.5 or 2.0 version.

I wonder if there is a solution to the difficulty I have in that the search text may appear more than once on a page and I would like to show a line for each occurrence in the search results. Is there a switch or setting to cause either of the methods to show all of the hits on the same page?

See Also

Contributors

Edit - History - Print - Recent Changes - Search
Page last modified on February 05, 2008, at 08:17 AM