What I want to achieve is a “colspan” Map for TiddlyWiki tables in wikitext
I want to create a “rowspan” Map in a similar manner but let’s start with the colspan Map since that appears to be easier
I start with a TiddlyWiki table and put it in a variable wikitextTable:
Its not clear to me where the 3 and 1 map values come from. I assume they relate to the intended size (or order?) of those columns.
Nonetheless, something to remember when working with your “map” lists: since they can contain multiple items with the same value, you will probably want to use enlist:raw[...] when handling those map lists, so that the duplicate map values will be preserved rather than collapsed into single values by the default “dominant append” filter handling.
where 0 stands for > and x stands for < (I could also let that one be <)
I try doing this in two steps.
Now I’d need a filter that accumulates consecutive or single 0, adds them to the next 1 as amount of consecutive 0 + 1 and replaces those 0 with a marker, say _ (so that later on I can use the produced list to get indexes correctly)
In a similar way and in a second step, I think I’d need to reverse the produced list and look for consecutive or single x and do the same as before…
Use of list widgets is just the quickest way (no fuss, no muss) to demonstrate a “process”. It is meant to be “descriptive”, not “prescriptive”.
That is how everybody should use any “solution”.
I expect any snippets I provide to be studied for the description of the process (the mental gymnastics of getting from “A” to “Z”), so that you can then implement your solution.
Well, life does have a way of occasionally confounding my expectations.
Here is some code based on functions which should do the job, even though it’s certainly not very robust to malformed wikitext tables…
I was heavily inspired by previous solutions in this thread, thank you all!
My main disappointment is I couldn’t use fancy unicode chars, because the solution relies on strings length and the result is wrong when the input contains unicode, be it with length[] or with split[]count[] filters.
<!-- Replace every cell value except ">" or "<" by "1" -->
\function fn.value-to-1()
[all[]]
:map[<currentTiddler>!regexp[>|<]then[1]else<currentTiddler>]
+[join[ ]]
\end
<!-- Counts the occurrences of "c" in string "s" -->
\function fn.nb(c,s)
[<s>split<c>count[]subtract[1]]
\end
<!-- Replaces sequences of "from" characters by "to" characters -->
\function fn.expand.cell(from,to)
[fn.nb<from>,<cell>!match[0]]
:map:flat[range<currentTiddler>]
:map[<to>]
+[join[]]
\end
<!-- Returns <<cell>> length -->
\function fn.cell.ln() [<cell>split[]count[]]
<!-- Computes current <<cell>> weight -->
\function fn.compute.cell(cell)
=[fn.expand.cell[>],[0|]]
=[fn.cell.ln[]]
=[fn.expand.cell[<],[|0]]
+[join[]]
\end
<!-- Computes current <<row>> weights -->
\function fn.compute.row(row)
=[<row>split[|]]
:map:flat[fn.compute.cell<currentTiddler>]
+[join[|]split[|]join[ ]]
\end
<!-- Sanitizes input (not perfect, but you get the idea... -->
\function fn.sanitize()
[all[]]
:map[<currentTiddler>search-replace:g[||],[|x|]]
\end
<!-- Replaces actual cell values by "1"s -->
\function fn.get.ones()
[all[]]
:map[<currentTiddler>split[|]butfirst[]butlast[]trim[]fn.value-to-1[]enlist-input:raw[]join[|]]
\end
<!-- Collapse cells (">|1" becomes ">1" -->
\function fn.collapse()
[all[]]
:map[<currentTiddler>search-replace:g[>|],[>]search-replace:g[|<],[<]]
\end
<!-- Maps a table to its colspans -->
\function row.map(table)
[<table>splitregexp[\n]]
:map[<currentTiddler>fn.sanitize[]]
:map[<currentTiddler>fn.get.ones[]]
:map[<currentTiddler>fn.collapse[]]
:map[fn.compute.row<currentTiddler>]
\end