Commit d7dcd70c authored by Marco Cavalli's avatar Marco Cavalli
Browse files

fix: wrong annex links

parent ba8870dc
Loading
Loading
Loading
Loading
+35 −8
Original line number Diff line number Diff line
@@ -51,6 +51,10 @@ end
function AnnexLink(text, number)
    text = text:gsub("%-", "‑") --always use non-breaking hyphens for links
    first_character = number:sub(1, 1)
    -- if first character is a letter and it is lowercase, make it uppercase and replace the number
    if first_character:match("%a") and first_character == first_character:lower() then
        number = first_character:upper() .. number:sub(2)
    end
    return pandoc.Link(text, "annex-" .. first_character:lower() .. ".html#" .. number)
end

@@ -58,19 +62,19 @@ end
function Substitute(el, word)
    local newContent = pandoc.List({})
    local pattern = "(%w*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d+%-?%d*)"
    -- if word == "annex" then
    --     pattern = "(%u)"
    -- end
    local annexPattern = "(%a%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*)"

    local i = 1
    while el.content[i] do
        local elem = el.content[i]
        -- logfile:write("DEBUG: Paragrafo con contenuto -> " .. pandoc.utils.stringify(elem) .. "\n")
        -- logfile:flush()
        if elem.t == "Str" and elem.text:lower():find(word) and not elem.text:find("+++") then --check the next strings to see if we need to link it
            local wordStartIndex = elem.text:lower():find(word)
            local precedentChar = elem.text:sub(wordStartIndex - 1, wordStartIndex - 1)
            local startIndex, endIndex, number = elem.text:gsub("‑", "-"):find(pattern) --check if number is in the same Str elem
            local textWithoutWord = elem.text:sub(wordStartIndex + #word)
            local startIndex, endIndex, number = textWithoutWord:gsub("‑", "-"):find(pattern) --check if number is in the same Str elem
            if not number then
                startIndex, endIndex, number = textWithoutWord:gsub("‑", "-"):find(annexPattern) --check if number is in the same Str elem
            end
            if not precedentChar:match("~") then
                if number then --create link
                    first_character = number:sub(1, 1)
@@ -84,6 +88,9 @@ function Substitute(el, word)
                local succ = el.content[i + 2] --next string if it exists should be number
                if succ and succ.t == "Str" then
                    number = succ.text:gsub("‑", "-"):match(pattern)
                    if not number then
                        number = succ.text:gsub("‑", "-"):match(annexPattern)
                    end
                    if succ.t == "Str" and number then                                        --we continue searching
                        local succ_succ = el.content
                            [i + 4]                                                           -- next next string if it exists and is of could link to other documents
@@ -100,13 +107,23 @@ function Substitute(el, word)
                            elseif word == "table" then
                                newContent:insert(TableLink(elem.text .. " " .. number, number))
                            elseif word == "annex" then
                                local lastNumberChar = number:sub(-1)
                                if lastNumberChar == "." or lastNumberChar == "," then
                                    number = number:sub(1, -2) --remove the last char if it is a dot or comma
                                end
                                newContent:insert(AnnexLink(elem.text .. " " .. number, number))
                                if lastNumberChar == "." or lastNumberChar == "," then
                                    newContent:insert(pandoc.Str(lastNumberChar)) --reinsert the dot or comma if it was there
                                end
                            else
                                if debug then print("Unkown behavior for " .. word .. " or link does not exists") end
                            end
                            if debug then print(succ.text, number) end
                            local text, substitutions = succ.text:gsub("‑", "-") --if we substituted we need to account for this, because nbh is not acii anf thus is more then one byte
                            local startIndex, endIndex = text:find(pattern)
                            if not startIndex then
                                startIndex, endIndex = text:find(annexPattern)
                            end
                            if endIndex + substitutions * 2 < #succ.text then -- if something remains (like a comma or a bracket), append it too
                                newContent:insert(pandoc.Str(succ.text:sub(endIndex + substitutions * 2 + 1, -1)))
                            end
@@ -132,7 +149,8 @@ end

function MultipleClauses(el)
    local newContent = pandoc.List({})
    local pattern = "(%w*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d+%-?%d*)"
    local pattern = "(%w*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d+)"
    local annexPattern = "(%a%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*%.?%d*)"
    local clausesFound = false --this is true when we found the word "clauses" and remain true until we habe clauses numbers following

    local i = 1
@@ -160,6 +178,10 @@ function MultipleClauses(el)
            if clausesFound then
                --check if we found a clause number
                local startIndex, endIndex, number = elem.text:find(pattern)
                if number == nil then
                    --check if it is an annex clause number
                    startIndex, endIndex, number = elem.text:find(annexPattern)
                end

                if elem.text:match("^,$") or elem.text:match("and") then
                    --this is just a comma or the word 'and' separating the clauses
@@ -167,7 +189,12 @@ function MultipleClauses(el)
                elseif number then
                    -- we did in fact find a clause number
                    --substitute with linkS
                    -- if first letter is a letter it is an annex
                    if elem.text:sub(1, 1):match("%a") then
                        newContent:insert(AnnexLink(elem.text:sub(startIndex, endIndex), number))
                    else
                        newContent:insert(ClauseLink(elem.text:sub(startIndex, endIndex), number))
                    end
                    if endIndex < #elem.text then
                        --add the rest of the string too
                        newContent:insert(pandoc.Str(elem.text:sub(endIndex + 1, -1)))
+7 −0
Original line number Diff line number Diff line
@@ -107,14 +107,21 @@ function CustomTagsToLinks(el, prefix)
        local clause_number = filename_numbers_mapping[filename]
        -- check that number is not a letter to differentiate between clauses and annexes
        if type(clause_number) ~= "number" then
          -- replace the first char with a uppercase letter
          clause_number = clause_number:sub(1,1):upper() .. clause_number:sub(2)
          html_filename = "annex-" .. clause_number .. ".html"
          if id:find("root.") then
            id = id:gsub("root%.", clause_number .. ".")
          elseif id:match("root$") then
            id = id:gsub("root", clause_number)
          end
          prefix = prefix:gsub("Clause", "Annex"):gsub("clause", "annex")
        else
          html_filename = "clause-" .. math.floor(clause_number) .. ".html"
          if id:find("root.") then
            id = id:gsub("root%.", math.floor(clause_number) .. ".")
          elseif id:match("root$") then
            id = id:gsub("root", math.floor(clause_number))
          end
        end
      end