Skip to content

Commit facf84c

Browse files
OmikhleiaDidier Willis
authored andcommitted
feat(packages): Add new styling hooks to the indexer
1 parent 691bfe2 commit facf84c

1 file changed

Lines changed: 25 additions & 8 deletions

File tree

packages/indexer/init.lua

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ local function _isNotSamePage (p1, p2)
1919
end
2020

2121
-- Group pages into ranges of consecutive pages.
22-
-- @tparam table pages A list of pages with pageno and link fields.
22+
-- @tparam table pages A list of pages with pageno (counter) and link (internal string) fields.
2323
-- @treturn table A list of ranges, each containing a list of pages.
2424
local function _groupPageRanges(pages)
2525
local ret = {}
@@ -200,19 +200,21 @@ function package:registerCommands ()
200200
SU.collatedSort(sortedIndex)
201201
SILE.call("bigskip")
202202
for _, k in ipairs(sortedIndex) do
203-
local pageno = self:formatPages(index[k])
204-
SILE.call("index:item", { pageno = pageno }, { k })
203+
local pages = self:formatPages(index[k])
204+
SILE.call("index:item", { pages = pages, index = options.index }, { k })
205205
end
206206
end, "Print the index")
207207

208208
self:registerCommand("index:item", function (options, content)
209-
-- Unconventional: options.pageno is an AST
209+
-- Unconventional: options.pages is an AST
210+
local pages = options.pages
211+
options.pages = nil
210212
SILE.settings:temporarily(function ()
211213
if self.config.filler ~= "comma" then
212214
SILE.settings:set("typesetter.parfillskip", SILE.types.node.glue())
213215
end
214216
SILE.settings:set("current.parindent", SILE.types.node.glue())
215-
SILE.call("code", {}, content)
217+
SILE.call("index:entry:style", options, content)
216218
if self.config.filler == "dotfill" then
217219
SILE.call("dotfill")
218220
elseif self.config.filler == "fill" then
@@ -222,10 +224,19 @@ function package:registerCommands ()
222224
else
223225
SU.error("Unknown filler: " .. self.config.filler)
224226
end
225-
SILE.process(options.pageno)
227+
SILE.call("index:pages:style", options, pages)
226228
SILE.call("smallskip")
227229
end)
228-
end, "Output an index item")
230+
end, "Output an index item (normally an internal command)")
231+
232+
-- Hooks for styling the index
233+
self:registerCommand("index:entry:style", function (_, content)
234+
SILE.process(content)
235+
end, "Hook for styling an index entry")
236+
237+
self:registerCommand("index:pages:style", function (_, content)
238+
SILE.process(content)
239+
end, "Hook for styling index pages")
229240
end
230241

231242
package.documentation = [[
@@ -254,12 +265,18 @@ Possible values are:
254265
255266
This package provides the \autodoc:command{\indexentry} command, which can be called as either \autodoc:command{\indexentry[label=<text>]} or \autodoc:command{\indexentry{<text>}} (so that it can be called from a macro).
256267
Index entries are collated at the end of each page, and the command \autodoc:command{\printindex} will deposit them in a list.
257-
The entry can be styled using the \autodoc:command{\index:item} command.
258268
259269
Multiple indexes are available and an index can be selected by passing the \autodoc:parameter{index=<name>} parameter to \autodoc:command{\indexentry} and \autodoc:command{\printindex}.
260270
261271
If the \autodoc:package{pdf} package is loaded, then pages in the index will be hyperlinked to the relevant references.
262272
273+
The following commands just process their content by default, but can be overridden to style the index at your convenience:
274+
\begin{itemize}
275+
\item{\autodoc:command{\index:entry:style}: Hook for styling the index entry.}
276+
\item{\autodoc:command{\index:pages:style}: Hook for styling the page numbers.}
277+
\end{itemize}
278+
When called, they are being passed, as parameters, the index name and the content to be styled.
279+
263280
\end{document}
264281
]]
265282

0 commit comments

Comments
 (0)