I’m looking for a way to simplify a somewhat complex JS macro, and was wondering if anyone had advice.
I’m building a wiki that is meant to replace the collection of PDFs that serve as the Policy Manual for a local school district. It’s modeled on another project I did, which attempts to replace the single PDF town charter for my town with a TiddlyWiki version. Both documents contain links to various Connecticut statutes; in the originals, these are just mentioned by name, but here it’s helpful to offer actual links. The Charter, though, had fewer than twenty such links, easy enough to create manually. I’m guessing this one will have 500 or more. I don’t want to look those up by hand and add all those links. So I created a macro to do this.
There is a hierarchy of Volume > Title > Chapter > Section
, and the sections break down in many ways. But to cite them, you need only the title and section. Three are nearly 1100 chapters, each containing from a handful to many dozens of sections. But the citations need to know the chapter number as the sections are just anchors on chapter pages. For instance, School climate survey
is in Volume 1 > Title 10 (Education) > Chapter 170 (Boards of Education) > Section 222gg
. It’s cited as CGS 10-222gg
, but the url includes the chapter, https://www.cga.ct.gov/current/pub/chap_170.htm#sec_10-222gg
.
What I have built is a macro to take 10-222gg
and generate from it something like this:
<a class="cgs" href="https://www.cga.ct.gov/current/pub/chap_170.htm#sec_10-222gg" >10-222gg</a>
To do this, I first extracted a list of chapters and their start/end sections from all the links off the titles page of the state law webpage. I parsed this list to get a JSON object with records like this
{
"Title": "1",
"ChapterType": "Chapter",
"ChapterNumber": "1",
"FirstSection": "1-1",
"LastSection": "1-3b",
"Description": "Construction of Statutes"
}
But I only need the ChapterType
(mostly “Chapter”, but for a few, “Article”), the ChapterNumber
and the LastSection
in order to find the correct record and format the output. And since this is a lot of text, I reduce this to C|1|1-3b
to store in my macro.
All this was one-time setup. At startup, the macro expands those shortened forms back into {ChapterType, ChapterNumber, LastSection}
records. When it’s called with, say, 10-222gg
, it searches through them one by one until it finds one whose last section is greater than 10-222gg
. (This has a good deal of complexity on its own, as we have to break that apart into numeric sections, punctuation, and alphabetic sections, and the alphabetic ones need to sort a, b, c, ...z, aa, bb, cc, ...zz, aaa, bbb, ...
, but I have already written that code.) Once I have the right chapter, it’s a matter of some fairly simple formatting. Well, with some caveats about Chapter versus Article, and the different naming conventions for them: not hard, just annoying.
So that’s the macro. It’s got a long, ordered list of shortened formats of the 1100 chapters, which expand into more usable objects. When called, it searches those objects for the first one that will include the section parameter supplied, and then uses that object and the section parameter to format a link.
The process in convoluted, the code is fairly complex. I can’t even think about porting this to wikitext unless I can simplify things. So I’m looking for advice. Can anyone suggest ways to simplify this?
Compare, this if you will, to the quite simple procedure for federal citations. Here’s the whole thing:
\procedure usc(title, section)
<a class="usc" href=`https://www.law.cornell.edu/uscode/text/$(title)$/$(section)$`>
<<title>> U.S.C. § <<section>>
</a>
\end
This is helped by Cornell Law School taking it upon themselves to organize this in a logical way, with links that depend only upon the parts needed for citation: title
and section
. I would rather be able to cite official sources, but the hierarchy in federal law is even more complex, and I don’t think I could comfortably replicate it without pages and pages of code, if I could even do so at all. For instance, here’s the link to what’s cited as 10 U.S.C. § 7973
:
https://www.govinfo.gov/content/pkg/
USCODE-2023-title20/html/USCODE-2023-title20-chap70-subchapVIII-partF-subpart5-sec7973.htm
That includes the Year > Title > Chapter > Subchapter > Part > Subpart > Section
. I don’t even want to think about it!
In any case, you can see some sample calls to the two macros in Legal links demo.
Is there any good way to simplify this?