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

feat: support sequences of above and below special links

parent e704c035
Loading
Loading
Loading
Loading
+19 −6
Original line number Diff line number Diff line
@@ -21,7 +21,13 @@ end

function countPlusChars(str)
    local count = 0
    str:gsub("%+", function() count = count + 1 end)
    str:gsub("%+%+%+", function() count = count + 1 end)
    return count
end

function countWordChars(text, word)
    local count = 0
    text:gsub(word, function() count = count + 1 end)
    return count
end

@@ -32,17 +38,24 @@ function CustomTagsToLinks(el, prefix)
  for _, child in ipairs(children) do
    if child.t == "Str" and child.text:find(prefix .. "+++") then

      if child.text:find("Clause") and (child.text:find("below") or child.text:find("above")) then
      if child.text:find("Clause") and (child.text:find("+++below") or child.text:find("+++above")) then
        -- flash an error and stop execution, they are only allowed when there are only 2 parts
        error(string.format("%s\nInvalid reference: 'below' and 'above' are only allowed for Tables and Figures.", pandoc.utils.stringify(child)))
      end

      if child.text:find("+++below") or child.text:find("+++above") then
        local plusCount = countPlusChars(child.text)
      if plusCount > 3 and (child.text:find("below") or child.text:find("above")) then
        if plusCount > 1 and countWordChars(child.text, "+++below") ~= plusCount and countWordChars(child.text, "+++above") ~= plusCount then
          -- flash an error and stop execution, they are only allowed when there are only 2 parts
        error(string.format("%s\nInvalid reference: 'below' and 'above' are only allowed within the same file, for custom references that have the (Table|Figure)+++(below|above) format.", pandoc.utils.stringify(child)))
          error(string.format("%s\nInvalid reference: 'below' and 'above' are only allowed within the same file, for custom references that have the (Table|Figure)+++(below|above)[+++(below|above)]+ format. You can't mix up below and after in the same link.", pandoc.utils.stringify(child)))
        end

        local html_link = string.format('<a href="#%s">%s</a>', child.text, child.text)
        new_content:insert(pandoc.RawInline('html', html_link))
        goto continue
      end


      local remaining_text = child.text

      -- check if there is some text before the link (punctuation or words) and keep it
+22 −14
Original line number Diff line number Diff line
@@ -504,15 +504,19 @@ def fix_custom_tags(soup: BeautifulSoup):
    for a in a_tags:
        href = a.get("href", "")
        if href.endswith("below"):
            count_below = href.count("+++below")
            is_table = "Table" in href
            class_name = "TH" if is_table else "FL"
            next_element = a.find_next("div", class_=class_name, id=True)
            if next_element:
                prefix = "Table_" if is_table else "Figure_"
                string_to_be_replaced = f"{prefix}below"
                new_a_text = next_element["id"].replace(prefix, "")
                a["href"] = href.replace(string_to_be_replaced, next_element["id"])
                a.string = a.string.replace("below", new_a_text)
            next_nth_element = a
            for _ in range(count_below):
                next_nth_element = next_nth_element.find_next("div", class_=class_name, id=True)
            if next_nth_element:
                prefix = "Table" if is_table else "Figure"
                postfix = "+++below" * count_below
                string_to_be_replaced = f"{prefix}{postfix}"
                new_a_text = next_nth_element["id"].replace(prefix + "_", " ")
                a["href"] = href.replace(string_to_be_replaced, next_nth_element["id"])
                a.string = a.string.replace(postfix, new_a_text)
            else:
                # flash an error
                print(
@@ -525,15 +529,19 @@ def fix_custom_tags(soup: BeautifulSoup):
                )
                os._exit(1)
        elif href.endswith("above"):
            count_above = href.count("+++above")
            is_table = "Table" in href
            class_name = "TH" if is_table else "FL"
            previous_element = a.find_previous("div", class_=class_name, id=True)
            if previous_element:
                prefix = "Table_" if is_table else "Figure_"
                string_to_be_replaced = f"{prefix}above"
                new_a_text = previous_element["id"].replace(prefix, "")
                a["href"] = href.replace(string_to_be_replaced, previous_element["id"])
                a.string = a.string.replace("above", new_a_text)
            next_nth_element = a
            for _ in range(count_above):
                next_nth_element = next_nth_element.find_previous("div", class_=class_name, id=True)
            if next_nth_element:
                prefix = "Table" if is_table else "Figure"
                postfix = "+++above" * count_above
                string_to_be_replaced = f"{prefix}{postfix}"
                new_a_text = next_nth_element["id"].replace(prefix + "_", " ")
                a["href"] = href.replace(string_to_be_replaced, next_nth_element["id"])
                a.string = a.string.replace(postfix, new_a_text)
            else:
                # flash an error
                print(