Skip to content

Commit

Permalink
fixed another locator number indexing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-h21 committed Dec 23, 2024
1 parent ee28a8b commit bb03301
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

- 2024/12/23

- fixed bug in the `make4ht-indexing` library. It didn't handle index entries
with the duplicate locator number that were broken over several lines correctly.
https://tex.stackexchange.com/a/733106/2891

- 2024/12/20

- remove duplicate index locators in the `make4ht-indexing` library.
Expand Down
18 changes: 17 additions & 1 deletion make4ht-indexing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,36 @@ local parse_idx = function(content)
end


local previous
-- replace numbers in .ind file with links back to text
local function replace_index_pages(rest, entries)
-- keep track of the previous page number
local previous
local count = 0
local delete_coma = false
return rest:gsub("(%s*%-*%s*)(,?%s*)(%{?)(%d+)(%}?)", function(dash, coma, lbrace, page, rbrace)
local entry = entries[tonumber(page)]
count = count + 1
if entry then
page = entry.locator or page
if delete_coma then
-- if the coma was marked for deletion, remove it. this may happen after line breaks in the index
coma = ""
end
-- if the page number is the same as the previous one, don't create a link
-- this can happen when we use section numbers as locators. for example,
-- we could get 1.1 -- 1.1, 1.1, so we want to keep only the first one
if page == previous then
previous = page
-- if the first page number on a line is the same as the previous one, we need to delete the coma,
-- otherwise the coma will be left in the output
if count == 1 then
delete_coma = true
end
return ""
else
previous = page
-- don't forget to reset the delete_coma flag after page change
delete_coma = false
-- construct link to the index entry
return dash .. coma.. lbrace .. "\\Link[" .. entry.file .."]{".. entry.dest .."}{}" .. page .."\\EndLink{}" .. rbrace
end
Expand All @@ -126,6 +140,8 @@ local fix_idx_pages = function(content, idxobj)
local entries = idxobj.map
for line in content:gmatch("([^\n]+)") do
local line, count = line:gsub("(%s*\\%a+.-%,)(.+)$", function(start,rest)
-- reset the previous page number
previous = nil
-- there is a problem when index term itself contains numbers, like Bible verses (1:2),
-- because they will be detected as page numbers too. I cannot find a good solution
-- that wouldn't break something else.
Expand Down

0 comments on commit bb03301

Please sign in to comment.