Multicell Glossary Items
Status: Implemented in v0.2.0.0
1 Objective
Allow defining glossary term definitions (data:GlossaryTerm) with more than just 1 paragraph cell.
2 Background
Orgmode allows defining glossary terms with the :: syntax in list elements, like this:
- a :: 1
- b :: 2Lilac parses the above as a mixture of data:ListMarker cells and data:GlossaryTerm cells. However for the example below
- a :: 1
Foo
- b :: 2The "Foo" paragraph will not be part of the a glossary term, because currently we only allow a glossary term to be defined with just 1 paragraph. For example, see the "offsets" glossary term at Archives, which uses 2 paragraphs (but where currently the second paragraph is left out of the glossary definition).
3 Problem
Because Lilac limits glossary terms to just 1 paragraph, this limits the richness with which we can define glossary terms. This limitation is not present in vanilla Orgmode where you can use other elements too (everything in the list element itself, including children, are considered part of the definition).
4.1 No deviation from Orgmode syntax for defining glossary terms
We want to continue using the double-colon (::) syntax for unordered list items to define glossary terms.
4.2 Avoid weaving the list structure elements for glossary terms
Although we use the unordered list to define glossary terms, we don't want to weave these list structures because it's redundant. That is because in HTML we already put the glossary terms in an HTML description list.
So when we have cells like
CListItem [LUnorderedStart,LItemStart]
GlossaryTerm
Paragraph
CListItem [LItemEnd,LItemStart]
GlossaryTerm
Paragraph
Paragraph
Paragraph
CListItem [LItemEnd,LUnorderedEnd]we want to avoid weaving the ListMarker values because we want the glossary terms to be in the same (single) description list.
5.1 New data structures
We just need to record two new bits of information:
The LCIs of when a glossary term starts and ends. Let's call this the glossary term boundary. These are bounds that enclose a single glossary term, no matter how long its definition (in cells).
Locations of where glossary groups (contiguous ranges of one or more glossary terms and their definitions) start and end. We can use the existing pattern around unordered/ordered list item markers, to create new list markers which we can decorate around glossary terms and definitions. This way, weaving the glossary items becomes trivial because we just weave the new list markers. Parsing will take care of accurately preparing these new markers.
5.2.1 Be stricter during parsing
When parsing we can construct these boundaries as we enter and leave list indentation that have glossary terms. We can use a insideGlossaryTermDefinitions boolean to check whether we should have a stricter parser (disallowing headings, disallowing nesting glossary term definitions, etc).
5.2.2 Uniformity of list items that define glossary terms
We can have a rule that says if a list level begins without a glossary term, no glossary terms are allowed at that same level for the remainder of that level. Conversely if the list level begins with a glossary term, all subsequent items at that level must be a glossary term.
- x
- a :: 1 (invalid because 'x' started above without a glossary term)
- b :: 2 - c :: 3
- b (invalid because this must be a glossary term) - a
- x :: 0
- y :: 1 (OK because this is in its own level)
- b
- c
- z :: 2 (also OK)Let's call this the uniformity rule. It's similar to the rule we already have about no mixing of ordered and unordered lists (without intervening cells) at the same level.
The point of this rule is to encourage (well, force) the user to adopt a uniform style.
In terms of implementation, we'd have to remember some state at each level. Whenever we enter a new level (f:listItemNester), we can designate it as a Nothing (undefined) state. Then when we parse the next cell, if it's a glossary term we can set it to Just True to mean "glossary only", and if it's anything else we can set it to Just False to mean "non-glossary list level". This state can be a single field embedded inside the existing data:Level type. Then whenever we create a new list item (for the same level), we can check whether the level expects the first cell of the list item to be a glossary item or not.
5.2.3 New list markers for glossary terms
The aforementioned stricter parsing rules simplifies things for us, because we don't have to worry about edge cases. Simply put, whenever we see a glossary item, we know that we can be in a description list (of possibly more glossary items).
The description list in HTML uses this construct:
<dl>
<dt>...</dt>
<dd>...</dd>
<dt>...</dt>
<dd>...</dd>
...
</dl>Each glossary term will have a term (<dt>) and a definition (<dd>). Even though the HTML treats these as siblings, for us we will want to enclose them inside a single entity, as LGlossaryItemStart and LGlossaryItemEnd.
| HTML | New List Marker | Purpose |
|---|---|---|
| <dl> | LGlossaryGroupStart | Marks beginning of (contiguous) glossary items. |
| </dl> | LGlossaryGroupEnd | Marks end of (contiguous) glossary items. |
| N/A | LGlossaryItemStart | Marks the start of a single glossary item. |
| N/A | LGlossaryItemEnd | Marks the end of a single glossary item. |
| <dt> | N/A | No need to have a new list marker for this. |
| </dt> | N/A | No need to have a new list marker for this. |
| <dd> | N/A | No need to have a new list marker for this. |
| </dd> | N/A | No need to have a new list marker for this. |
We don't need new list item markers for <dt> and <dd> because they can be derived by looking at the contents of an existing data:GlossaryTerm.
With these new list markers, the example from ex:list-markers simply becomes
CListItem [LGlossaryGroupStart,LGlossaryItemStart]
GlossaryTerm
Paragraph
CListItem [LGlossaryItemEnd,LGlossaryItemStart]
GlossaryTerm
Paragraph
Paragraph
Paragraph
CListItem [LGlossaryItemEnd,LGlossaryGroupEnd]And now we no longer have to worry about avoiding to parse regular list item markers, because we use the LGlossary* variants instead. Also there is just a single LGlossaryGroup*, so we can easily weave these as a single description list.
6.1 Use LCI pairs to record glossary group ranges
We considered storing ranges of LCI values, to denote groups of glossary terms, which will encode the boundaries of contiguous groups of glossary terms. This can be used to determine when the overall description list (which encompasses one or more glossary terms) can be created/closed. It can also be used to know when we can skip weaving of any intervening list markers we may have parsed between glossary terms.
Using LCIs (integers) is lightweight and doesn't require major changes to how we parse cells.
To understand whether we are inside a glossary definition boundary or group boundary, we can convert the tuples into a Map and use lookupLE, like this:
import qualified Data.Map.Strict as M
type GlossaryGroupBoundaries = M.Map Int Int -- Map Start End
-- Find if an LCI (x) falls into any boundary (assuming the boundaries are
-- non-overlapping).
checkBoundary :: Int -> GlossaryGroupBoundaries -> Maybe GlossaryPosition
checkBoundary x m = case M.lookupLE x m of
-- Here "start" is <= x, but there is no guarantee that x <= end
Just (start, end)
| x == start && x == end -> Just Singleton
| x == start -> Just Start
| x < end -> Just Inside
| x == end -> Just End
| otherwise -> Nothing
Nothing -> NothingThen when we weave we can know to skip those list markers that fall inside a range.
However, this approach was rejected because during weaving we would have to check every single cell index against this checkBoundary function. And weaving gets even more complicated because we have to avoid weaving list item markers around the glossary items (where they are defined), all using just this range information. It's much easier if we just have glossary list item markers instead, because then we don't have to do any lookups.
6.2 Use new BCell type to create a subset of cell values allowed inside glossary terms
So, we need to create a new BCell (to mean "body" cell), which only has a subset of cells in it. This way we can guarantee that a glossary term's definition cannot be nested with another glossary term.
type BCell :: Type
data BCell
= BCListItem [ListMarker]
| BCParagraph Prose
| BCMathFragment MathFragment
| BCTable Table
deriving stock (Eq, Ord, Generic, Show)
deriving anyclass (FromJSON, ToJSON)
type GlossaryTerm :: Type
data GlossaryTerm = GlossaryTerm
{ term :: Prose, -- 1 ←
definition :: [BCell]
}
deriving stock (Eq, Ord, Generic, Show)
deriving anyclass (FromJSON, ToJSON)There is some room for "convention" here. Vanilla Orgmode will silently remove the terms if the first list item is not a term. It will also let you nest terms (which probably doesn't work well in practice). We will enforce a rule that says glossary term lists must only have such glossary terms (and also no nesting of term definitions is allowed, via the allowed constructors of the BCell type).
One consequence of using more than 1 cell for the glossary is that we have to tweak the glossary field in data:OrgDoc-glossary. Currently that just references a single cell number (inserted by f:assertUniqueGlossaryTerm, and woven by f:weaveCollectedGlossaryTerm). Instead we need to start referencing the start and end cell indices (or, at least the number of cells we need to weave after the starting cell).
There are some new concerns because a glossary term is no longer a single cell (paragraph):
1 is a part of the paragraph. However the definition is a list of separate cells. So this sort of breaks the notion we've had so far of having a blank line as a cell boundary.
We treat the CGlossaryTerm as a single Cell in data:OrgDoc-cells, but because it is composed of different cells inside it, we have to now call f:weaveCell on it. That one expects an LCI, but now this is wrong because CGlossaryTerm is just one cell.
Because we can have the parser complain loudly whenever we encounter an invalid cell, we can guarantee that the glossary term only has the allowed cells in it, even if we don't use the BCell type. So the BCell type is not strictly necessary.
There is probably no need to store the contents of a glossary term's child cells into the glossary term value at all (it is rather repetitive to do so) --- instead we can just store the LCIs of the first and last cells that make up the glossary term's definition.
For these reasons, we've abandoned this approach.
6.3 Include a size field for the GlossaryTerm type
Add a size field to encode how many cells (that follow) are part of the definition of the glossary term:
type GlossaryTerm :: Type
data GlossaryTerm = GlossaryTerm
{ term :: Prose,
definition :: Prose,
size :: Int
}
deriving stock (Eq, Ord, Generic, Show)
deriving anyclass (FromJSON, ToJSON)Here the size is just the total number of extra cells that comes after the initial paragraph cell. Now we don't have to change data:OrgDoc-glossary because that can continue to store the starting cell index (and once we have that, we can do a lookup inside the GlossaryTerm to figure out its size). Now we have enough info to grab all glossary terms and their definitions even if their definitions go beyond just the required paragraph.
One big downside now though is that the weaving should probably be stateful. That's because after we go past the first GlossaryTerm cell, we have to still know whether we've finished with the glossary or not (we need to know when to put in a raw </div> to enclose a single glossary term). We have to keep a running counter and figure out when we've hit the size, in order to know that we're at the end of the glossary term definition.
Keeping state during weaving is an anti-pattern (weaving shouldn't need to be stateful), so this approach has been rejected.