Copyright | (c) Nathan Bloomfield 2017 |
---|---|
License | GPL-3 |
Maintainer | nbloomf@gmail.com |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
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:
- Define a type
t
which represents the state of a shortcode. Typicallyt
will be a record type, and the fields should all be Maybes or Lists of instances of theValidate
class. - Define a default instance of your shortcode state. This may
consist of a bunch of
Nothing
s and[]
s, or maybe you prefer nontrivial defaults. This is theemptycode
method of theShortcode
class. - 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 theShortcode
class. - Provide a map
t -> String
, to be used to convert abstract shortcodes to their expanded form. This is theembedcode
method of theShortcode
class. - 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.
Documentation
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.