@header@
Permalink Providers

Permalink providers are a type of Pebble plugin that allow you to extend the way that permalinks are generated and handled. The following implementations are included in the distribution, all of which can be found in the pebble.permalink package.

  • pebble.permalink.DefaultPermalinkProvider - generates permalinks of the form /yyyy/mm/dd/time-in-millis.html (this is the default)
  • pebble.permalink.ShortPermalinkProvider - generates permalinks of the form /time-in-millis.html
  • pebble.permalink.TitlePermalinkProvider - generates permalinks of the form /yyyy/mm/dd/each_word_in_the_title.html

To write your own peramlink provider, you need to write a Java class that implements the pebble.permalink.PermalinkProvider interface. A partial implementation is also available to subclass from called pebble.permalink.PermalinkProviderSupport.

The code for the ShortPermalinkProvider is shown here as an example.

 package pebble.permalink;

  import pebble.blog.BlogEntry;
  import pebble.blog.SimpleBlog;

  /**
   * Generates permalinks using the pattern <time-in-millis>.
   *
   * @author Simon Brown
   */
  public class ShortPermalinkProvider extends PermalinkProviderSupport {

    /** the regex used to check for a blog entry permalink */
    private static final String BLOG_ENTRY_PERMALINK_REGEX = "/\\d*.html";

    /**
     * Gets the permalink for a blog entry.
     *
     * @return  a URI as a String
     */
    public String getPermalink(BlogEntry blogEntry) {
      return "/" + blogEntry.getId() + ".html";
    }

    /**
     * Determines whether the specified URI is a blog entry permalink.
     *
     * @param uri   a relative URI
     * @return      true if the URI represents a permalink to a blog entry,
     *              false otherwise
     */
    public boolean isBlogEntryPermalink(String uri) {
      if (uri != null) {
        return uri.matches(BLOG_ENTRY_PERMALINK_REGEX);
      } else {
        return false;
      }
    }

    /**
     * Gets the blog entry referred to by the specified URI.
     *
     * @param uri   a relative URI
     * @return  a BlogEntry instance, or null if one can't be found
     */
    public BlogEntry getBlogEntry(String uri) {
      // uri is of the form /1234567890123.html, so extract the 13-digit ID
      // and use it to find the correct blog entry
      SimpleBlog simpleBlog = (SimpleBlog)getBlog();
      return simpleBlog.getBlogEntry(uri.substring(1, 14));
    }

  }

@footer@