Commit 4c89010f authored by Marco Cavalli's avatar Marco Cavalli
Browse files

fix: prevent adding punctuation in links

parent 5008002f
Loading
Loading
Loading
Loading
+24 −16
Original line number Diff line number Diff line
@@ -44,23 +44,27 @@ function CustomTagsToLinks(el, prefix)
      end

      local remaining_text = child.text

      -- check if there is some text before the link (punctuation or words) and keep it
      local start_index = remaining_text:find(prefix .. "+++")
      -- logfile:write("DEBUG: Par with content -> " .. pandoc.utils.stringify(child) .. "\n")
      -- logfile:write("DEBUG: Par with content -> " .. prefix .. "+++" .. "\n")
      -- logfile:flush()

      if start_index > 1 then
        new_content:insert(pandoc.Str(child.text:sub(1, start_index - 1)))
        remaining_text = child.text:sub(start_index)
      end
      local last_char = remaining_text:sub(-1)
      if last_char:find("[ ,.;:!?)]") then
        extract_text = remaining_text:sub(start_index, #remaining_text - 1)
      else
        -- If no delimiter found, assume the reference goes to the end of the text
        end_index = #remaining_text + 1
        extract_text = remaining_text:sub(start_index)

      -- check if there is some text after the link (punctuation or words), and extract only the link part
      local end_index = #remaining_text
      for i = #remaining_text, 1, -1 do
        if remaining_text:sub(i, i):find("[%w]") then
          end_index = i
          break
        end
      end
      
      extract_text = remaining_text:sub(start_index, end_index)

      -- extract the individual parts of the link (up to 2 parts: optional filename, id)
      local parts = split(extract_text, "+++")

      local filename = nil
@@ -71,24 +75,24 @@ function CustomTagsToLinks(el, prefix)
      elseif #parts == 2 then
        id = parts[2]
      else
        -- skip, it is malformed
        new_content:insert(child)
        goto continue
        error(string.format("%s\nInvalid reference: '%s' does not have the correct format. It should be %s+++<id> or %s+++<filename>+++<id>", pandoc.utils.stringify(child), child.text, prefix, prefix))
      end

      local id_prefix = ""
      local html_filename = ""

      if prefix == "Figure" and #parts == 2  and (id:find("^[%w_-]+%.png$") or id:find("^[%w_-]+%.jpg$") or id:find("^[%w_-]+%.jpeg$") or id:find("^[%w_-]+%.svg$")) then
        id_prefix = "Figure+++"
      elseif prefix ~= "Clause" then
        id_prefix = prefix .. "_"
      end

      if filename then
        if filename_numbers_mapping[filename] == nil then
          error(string.format("Filename '%s' not found. Can't create a link for element %s", filename, pandoc.utils.stringify(child)))
        end
        local clause_number = filename_numbers_mapping[filename]
        -- check that number is not a letter
        -- check that number is not a letter to differentiate between clauses and annexes
        if type(clause_number) ~= "number" then
          html_filename = "annex-" .. clause_number .. ".html"
          if id:find("root.") then
@@ -101,11 +105,15 @@ function CustomTagsToLinks(el, prefix)
          end
        end
      end

      -- create the HTML link
      local html_link = string.format('<a href="%s#%s%s">%s</a>', html_filename, id_prefix, id, prefix:lower() .. " " .. id)
      new_content:insert(pandoc.RawInline('html', html_link))

      if last_char:find("[ ,.;:!?)]") then
        new_content:insert(pandoc.Str(last_char))
      -- if we extracted the link (end_index < #remaining_text), keep the remaining text after the link
      if end_index < #remaining_text then
        local after_text = remaining_text:sub(end_index + 1)
        new_content:insert(pandoc.Str(after_text))
      end
    else
      new_content:insert(child)