In the last posts, I talked about wanting to avoid using the HTML editor and 5 parts of the report that cause me pain. I’ve taken care of 3 of those 5. Let’s get another one checked off the list.
Here’s a different snippet of the last Beer Pairing Dinner report I wrote, now with a new area to work on:
|Unknown words:||1 (mizuna)|
This is the final grade area (remember, I said this is one of the problem areas). The biggest problem isn’t that it’s complicated, rather it’s a table and tables aren’t well supported. Add to that lack of support that I’m really picky about the format and voila trouble.
Let’s dig into that information – there’s a section header (“Final Grades”) and a table with 4 rows. As usual, I’m not sure what I want to do. It seems like I could do the section header with [course], except that it doesn’t have the grade part that [course] would expect. So that needs something different.
The table has 4 rows – 3 of these are grades and the 4th one is a recap of the unknown words that get called out in the report. All of the rows have 2 columns, but the difference here from the case of [pairgrade] is that the information on each row is related – the left column has the name of the grade and the right column has the grade. That’s a hint toward the structure of the shortcode (Oh, I didn’t mention that yet – the shortcode thing is working quite nicely, and it will work again here).
The first thing I implemented is one code, [finalgrade], with 3 parameters – one each for the food, beer, and pairings grades. Here’s that code as it would be for the above snippet.
[finalgrade beer="A-/B+" food="A" pairings="A-/B+"]
Hmm. There’s no parameter for unknown words, and the parameters are very highly tied to writing a report about beer pairing dinners. It’s simple (which is good for making it easy to write the code) and I made it automatically create the row for unknown words. However, it’s not very useful for people writing about wine pairing dinners that don’t care about unknown words. That’s a problem, so let’s take a look at that a little further.
Changing this away from beer dinners is not too big of a problem, but it does require work. The first thing to do is create tags for each of the rows – %#91;finalgrade] is going to change into this row grade, and that means that the current code has to change, but I don’t know what it will change to. For discussion purposes, let’s just use [finals].
The new [finalgrade] code has 2 parameters – a grade name (name) and a grade (grade). I still want to force the “Final” prefix on the name as well as force the name part to be capitalized. Here’s an example for the beer row:
[finalgrade name="beer" grade="A-/B+"]
It’s obvious how that structure can be repeated for each of the grade rows, but what about that pesky unknown words row. One option is to add another shortcode for that, but a simpler way of doing it is to add it as a parameter for the [finals] shortcode. Here’s the final shortcode version of the table:
[finalgrade name="beer" grade="A-/B+"]
[finalgrade name="food" grade="A"]
[finalgrade name="pairings" grade="A-/B+"]
The code that implements these shortcodes is very similar to the code that implements the [pairing]/[pairgrade] shortcodes, except with one big difference – for [pairing], the code that prints the rows has to be there because of the column vs row problem. Here though, because each of the [finalgrade] entries represents one row, the row printing can occur in the [finalgrade] handler. The [finals] handler still starts and ends the table, and outputs a row for the unknown words if ANY value is provided for the unknowns parameter.
Hey, speaking of those unknown words, how exactly does it output that row? Since all of the unknown words are provided by a shortcode, that shortcode can enter the words into an array. That array can then be used to output the list of unknown words. I thought about sorting the list in alphabetical order, but right now it’s staying in the order they show up in the post. That required an adjustment to the handler code for the [foodword] code to populate that array, which looks similar to what was done for [pairgrade].
That’s been implemented and tested, and is available as Ron’s Beer Report Plugin v0.4.
(PS: If you dig into that code, you’ll see that I cheated and left the beer/food/pairings keywords in the code. That’s because it’s easier for ME. That’s one of the two permanent rules the Ron CCB runs under: a) If it’s easy, I’ll probably do it; b) If I want it, I’ll probably do it.)