Monday, November 08, 2010 at 12:05 AM.
system.verbs.builtins.searchEngine.createPreview
on createPreview (s, title, url, pageID, adrPreviews, lastModified=nil) {
<<Create a preview for a page. Return the address of the preview.
<<s is the text of the page.
<<title is the title of the page.
<<url is the url of the page.
<<pageID is the page's identifier -- odb address, url, or filespec.
<<adrPreviews is the address of the previews table.
<<lastModified is when the page was last modified.
local (preview = "");
on add (s) {
preview = preview + s};
preview = preview + ("<b>" + html.getLink (title, url) + "</b><br>\r");
s = string.replaceAll (s, "***", "");
s = string.replaceAll (s, " ", " ");
s = string.replaceAll (s, " ", " ");
if string.lower (s) contains "<body " { //is this a raw page?
s = html.getOneTagValue (s, "body")}; //just get the body text
loop { //get preview text from page minus directives
if not (s beginsWith "#") {
s = searchEngine.stripMarkup (s);
local (i);
local (wordsToGet = 15);
local (numFields = string.countFields (s, ' '));
if numFields < wordsToGet {
wordsToGet = numFields};
local (origString = s);
s = "";
for i = 1 to wordsToGet { //get 15 words
s = s + " " + string.nthField (origString, ' ', i)};
s = string.trimWhiteSpace (s);
s = s + "...";
break};
ix = string.patternMatch ("\r", s);
if ix < 1 {
s = searchEngine.stripMarkup (s) + "...";
break};
s = string.delete (s, 1, ix)};
add (s); //add text to preview
add ("\r<br>\r");
if lastModified == nil {
try {
add ("Last-modified: " + timeModified (pageID) + "<br>")}
else {
lastModified = clock.now ()}}
else {
add ("Last-modified: " + lastModified + "<br>")};
add ("<a href=\"" + url + "\">" + url + "</a>");
adrPreviews^.[pageID] = preview;
return (@adrPreviews^.[pageID])} //return address of now-cached preview
This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.