This is version . It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]

This plugin will return a random single line from any Wiki page. I use this to implement 'fortune cookies' on my wiki (See the bottom of the page).

The format for using this plugin is

  • for FortuneCookies.jar:
   [{INSERT com.bradfordholcombe.JSPWiki.plugins.FortuneCookiePlugin WHERE page='Sayings'}]
  • for the other version, FortuneCookiePluginv2.0.52.jar, use:
   [{INSERT net.colbadhombre.JSPWiki.plugins.FortuneCookiePlugin WHERE page='Sayings'}]

The source page should be a list of newline delimited sayings. Optionally, the first line can be a number that represents the number of sayings on the page. Specifying this number allows the plugin to skip parsing the whole page once to count the number of lines. Also, lines starting with '//' or whitespace followed by '//' will be ignored.

This plugin was developed under 2.1.82 CVS/JDK1.4.2, and I haven't tried it anywhere else. Let me know if I should make any changes for compatability reasons.

I use JSPWiki for my home site, so I have a lot of these plugins doing things like reporting the local weather, what is playing on my SliMP3, and even the level of the river that runs in front of my house! Let me know if any of that sounds fun, and I'll post it.

The source code is below and a JAR is attached at the bottom. The source has no copyright. Feel free to use it whereever and however you'd like.

Thanks to Janne for JSPWiki.

05OCT2004 11:28am UPDATE
I have added code to remove list wiki markup from each line. This allows the original page to use the wiki list syntax, so the fortune line looks better when rendered in its own page. Thanks to suggestions from Janne, I have added a call to the wiki tranlation engine so that each line (minus its original list markup) renders any wiki markup in the returned fortune. The new source is above, and the JAR reflects these changes.

-- Bradford Holcombe


Source:
package net.colbadhombre.JSPWiki.plugins;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.WikiEngine;
import com.ecyrd.jspwiki.WikiPage;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Map;

public class FortuneCookiePlugin implements WikiPlugin
{
   public String execute(WikiContext context, Map params) throws PluginException
   {
      String pageName = (String)params.get("page");
      WikiEngine engine = context.getEngine();
      WikiPage page = engine.getPage( pageName );
      String pageData = engine.getPureText( page );

      String line = "";
      BufferedReader br = null;
      try
      {
         br = new BufferedReader(new StringReader(pageData));

         line = br.readLine();
         if (line == null)
         {
            return "Fortune file empty (" + pageName + ")";
         }

         int lineCount = -1;
         try
         {
            // first line is a count of the number of fortunes in the file, if it is a number.
            lineCount = Integer.parseInt(line);
         }
         catch (NumberFormatException nfe)
         {
            // not a number, gotta count.
            lineCount = 0;
            while (line != null)
            {
               if (line.length() > 0 && !line.trim().startsWith("//"))
               {
                  lineCount++;
               }
               line = br.readLine();


            }

            br.close();

            br = new BufferedReader(new StringReader(pageData));
         }

         // pick the line to insert
         int chosenLine = (int)Math.floor(lineCount * Math.random());

         lineCount = 0;
         line = br.readLine();
         while (lineCount != chosenLine && line != null)
         {
            line = br.readLine();
            if (line.length() > 0 && !line.trim().startsWith("//"))
            {
               lineCount++;
            }
         }

         br.close();
      }
      catch (IOException e)
      {
         return "IO exception with fortune file.";
      }
      finally
      {
         if (br != null)
         {
            try
            {
               br.close();
            }
            catch (IOException e)
            {
               //
            }
         }
      }

      if( line.startsWith( "*" ) )
      {
         line = line.substring( 1 );
      }
      if( line.endsWith( "\\" ) )
      {
         line = line.substring( 0, line.length() - 2 );
      }

      // invoke the wiki markup translator
      TranslatorReader reader = new TranslatorReader( context, new StringReader( line ) );
      int c;
      StringBuffer wikiLine = new StringBuffer();
      try
      {
         while( ( c = reader.read() ) != -1 )
         {
            wikiLine.append( (char)c );
         }
      }
      catch( IOException ioe )
      {
         // nothing
      }

      return wikiLine.toString();
   }
}


This plugin was not working for the 2.0.52 stable version. Also attached to this page is the JAR for this version.
HTH
--MBarcia


ContributedPlugins

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
java
FortuneCookie.java 4.0 kB 1 25-Sep-2008 04:35 Maik Scheibler
java
FortuneCookiePlugin.java 3.2 kB 1 25-Sep-2008 14:02 Maik Scheibler moved source code from page to attachment
jar
FortuneCookiePlugin_v2.6.4.jar 3.3 kB 1 25-Sep-2008 04:14 Maik Scheibler
jar
FortuneCookiePluginv2.0.52.jar 2.3 kB 1 18-Mar-2005 22:41 Mbarcia
jar
FortuneCookies.jar 3.1 kB 2 05-Oct-2004 18:27 BradfordHolcombe
« This particular version was published on 26-Sep-2007 23:21 by JanneJalkanen.