hakyll-shortcode-0.0.2: A shortcode extension module for Hakyll

Copyright(c) Nathan Bloomfield 2017
Safe HaskellNone



Do you want to make more shortcodes? Of course, we all do.

This module demonstrates how to implement a type-safe shortcode. The basic steps are as follows:

  1. Define a type t which represents the state of a shortcode. Typically t will be a record type, and the fields should all be Maybes or Lists of instances of the Validate class.
  2. Define a default instance of your shortcode state. This may consist of a bunch of Nothings and []s, or maybe you prefer nontrivial defaults. This is the emptycode method of the Shortcode class.
  3. Declare the usable keys for your shortcode by giving a list of 'ShortcodeAttribute t's. This type lets us define our keys in a declarative way, sort of like GetOpt. This is the attributes method of the Shortcode class.
  4. Provide a map t -> String, to be used to convert abstract shortcodes to their expanded form. This is the embedcode method of the Shortcode class.
  5. Last but not least, declare which tag will be used to name your shortcode.

And that's it! The shortcode API and type library will take care of parsing, validation, and sanitization for you, giving you a function String -> String that expands your new shortcode while making sure only input conforming to your type model is allowed.

Now let's walk through a sample shortcode module. To read along, view the source of this module.



expandExampleShortcodes :: String -> String Source #

This is the function that finds and replaces example shortcodes. It is just a type-specific version of the expandShortcodes function, that takes a shortcode specification and generates a find-and-replace function for it.