Your own Trunk Notes settings page

Lua is a great way to really make your Trunk Notes wiki your own. If you have done some programming before then Lua is quite easy to pick up.

This week I had a question from someone wanting to create their own settings page. The page would have checkboxes which could then be read by a Lua script run on each page load.

Doing this is fairly straightforward. To start with create a new page containing your checkboxes. For example you might create a page call Globals which looks like:

My Trunk Notes settings:

* {{check Night Mode}}
* {{check Append Last Page}}

You can now turn on or off your settings on the page Globals.

Next you need to be able to read the settings in your Lua script. This is really easy. Here’s an example I’ve put in a page called ConfigurePage.lua:

-- Start by getting my Globals page
globals = wiki.get('Globals')

-- See if night mode is switched on
if globals.metadata['checked_nightmode'] == 'YES' then
    -- It is so return my night stylesheet
    return '{{stylesheet NightStyle}}'
    -- return my day stylesheet
    return '{{stylesheet DayStyle}}'

You can determine if a checkbox is on by looking in a pages metadata. Each checkbox is given a key. The key always starts checked_ and is followed by the label you gave to your checkbox with all spaces and other non alphanumeric characters removed, all turned into a lowercase string. If the checkbox is turned on this key in the metadata will have the value YES, otherwise the key doesn’t appear.

In my above example I’ve added…

{{lua ConfigurePage.lua}}

…to my Trunk Notes page Special:Header so that the Lua gets run on every page. It will then change the stylesheet used to render the page to either one suited for reading at night, or one better suited for the daytime. You can of course do any number of things here based on your own custom settings page. Feel free to share your ideas in the comments below!