Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Last active July 5, 2020 20:57
Show Gist options
  • Save tmtmtmtm/cac95c54661441230e16df93007f0cde to your computer and use it in GitHub Desktop.
Save tmtmtmtm/cac95c54661441230e16df93007f0cde to your computer and use it in GitHub Desktop.
Making a Listeria table look nicer

In https://www.youtube.com/watch?v=q2iTKemFrq4, Jan Ainali and Tiago Lubiana use Listeria to put together a table on Swedish Wikipedia of current Members of the Riksdag.

I’d like to make that look much more similar to the other tables on the page, particularly in having the colour-coded cells beside each party.

party colour example

The relevant colours for each party can be stored in Wikidata, using P465 “sRGB colour hex triplet”, and thankfully they’re all already filled in for each of the parties currently represented in the Riksdag (e.g. https://www.wikidata.org/wiki/Q110832#P465), so there’s no preparatory data entry step needed here. So now we need to add that to our table:

Step 1: Adjust the query to also fetch the colours

This requires extending the existing

OPTIONAL {
  ?parl pq:P4100 ?parlamentsgrupp .
}

to also get the colour for that group. As the group is itself an optional qualifier (as there are a couple of members with no group), and we also need to be able to cope with there being a group with no sRGB value set at some later point, we need a nested doubly optional value!:

OPTIONAL {
  ?parl pq:P4100 ?parlamentsgrupp .
  OPTIONAL { ?parlamentsgrupp wdt:P465 ?colour }
}

That gives us a query like: https://w.wiki/W9K

Step 2: Update our Listeria table to include this column

To make the table nicer, we first have to make it uglier! When we add a column for the colour, it will simply show the raw hex codes for each party:

rawhex example

But adding it like this lets us know that everything looks OK, and so we can move on to Step 3…

Step 3: Adding a custom row template

By default Listeria simply displays each value in a plain cell. But we can specify our own template that should get used in every row, by adding an extra parameter:

|row_template=Wikidata:WikiProject every politician/Sweden/Current member list/rowtemplate

Then we can supply our own template to change how each cell works.

To start with, it’s easiest to simply replicate what Listeria is already doing, with a very simple template that checks whether each parameter was passed, and if so, displays it in a table cell:

|-
| {{#if:{{{label|}}} | {{{label}}} |}}
| {{#if:{{{colour|}}} | {{{colour}}} |}}
| {{#if:{{{parlamentsgrupp|}}} | {{{parlamentsgrupp}}} |}}
| {{#if:{{{valkrets|}}} | {{{valkrets}}} |}}
| {{#if:{{{start|}}} | {{{start}}} |}}
| {{#if:{{{ersatter|}}} | {{{ersatter}}} |}}

But once we’ve verified that that works, we can switch the 'colour' cell to fill in the background according to the RGB value, rather than simply displaying it:

|style="background: #{{#if:{{{colour|}}} | {{{colour}}} | 000}} |

That gives us a table like:

coloured cell example

This is still quite ugly, but we’re heading in the right direction.

Step 4: Adding a custom header template

To more accurately reflect the existing Wikipedia tables, we need to “hide” the heading for the coloured cells. That requires also adding a custom header template.

That works pretty much the same as the row template:

|header_template=Wikidata:WikiProject every politician/Sweden/Current member list/headertemplate

No parameters get passed to this one: we just arrange things how we want them: {| class="wikitable sortable jquery-tablesorter" style="text-align: center" ! Ledamot !colspan=2 | Parti ! Valkrets ! Tillträdde ! Ersättare för

And now we have a table that looks much closer to the others on the Wikipedia page:

looks like Wikipedia

Step 5: Switch to human readable dates

Now the main difference between our auto-generated table and the existing hand-rolled ones is the date formatting. Listeria spits out dates like “2020-05-04”, but what we really want is “4 maj 2020”.

On Swedish Wikipedia this is fairly simple: it has a localised version of English Wikipedia’s {{date}} template, which we can call as:

| {{#if:{{{start|}}} | {{date|{{{start}}}}} |}}

Unfortunately, Wikidata doesn’t have this template (and I can’t find an equivalent), so I can’t demonstrate this in action there! For that I need to wait for someone to take my work and copy everything across to Swedish Wikipedia…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment