Commit 00d4bdb3 authored by Marco Cavalli's avatar Marco Cavalli
Browse files

feat: ~ can be used to prevent some text to become a link

parent 135f6e41
Loading
Loading
Loading
Loading
+52 −42
Original line number Diff line number Diff line
@@ -60,7 +60,9 @@ function Substitute(el, word)
        -- 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
            if not precedentChar:match("~") then
                if number then --create link
                    first_character = number:sub(1, 1)
                    newContent:insert(pandoc.Link(elem.text:sub(1, endIndex), "clause-" .. first_character .. ".html#" .. number))
@@ -110,6 +112,7 @@ function Substitute(el, word)
                    end
                end
            end
        end
        --append the other elements normally
        newContent:insert(elem)
        ::continue::
@@ -130,6 +133,13 @@ function MultipleClauses(el)
        if elem.t == "Str" then
            --this may be the start of a list of multiple clauses
            if elem.text:lower():find("clauses") then
                local wordStartIndex = elem.text:lower():find("clauses")
                local precedentChar = elem.text:sub(wordStartIndex - 1, wordStartIndex - 1)
                if precedentChar:match("~") then
                    --this is a negated clauses, do nothing
                    newContent:insert(elem)
                    goto continue
                end
                clausesFound = true
                -- Replace plain "clauses" text with a span containing it
                local clausesSpan = pandoc.Span({pandoc.Str(elem.text)})
+46 −5
Original line number Diff line number Diff line
@@ -118,10 +118,43 @@ function CustomTagsToLinks(el, prefix)
  return el
end

function RemoveLeadingTilde(el)
  local new_content = pandoc.List()
  local children = el.content
  local words = {"clause", "table", "figure", "annex"}

  for _, child in ipairs(children) do
    if child.t == "Str" then
      local text = child.text
      for _, word in ipairs(words) do
        local pattern = "~" .. word
        local start_index, end_index = text:lower():find(pattern)
        if start_index then
          if start_index > 1 then
            new_content:insert(pandoc.Str(text:sub(1, start_index - 1)))
          end
          local remaining_text = text:sub(end_index + 1)
          new_content:insert(pandoc.Str(word .. remaining_text))
          goto continue
        end
      end
      new_content:insert(child)
    else
      new_content:insert(child)
    end
    ::continue::
  end

  -- Replace the original content with the new one
  el.content = new_content
  return el
end

function Para(el)
    el = CustomTagsToLinks(el, "Table")
    el = CustomTagsToLinks(el, "Figure")
    el = CustomTagsToLinks(el, "Clause")
    el = RemoveLeadingTilde(el)
    return el
end

@@ -129,6 +162,7 @@ function Div(el)
    el = CustomTagsToLinks(el, "Table")
    el = CustomTagsToLinks(el, "Figure")
    el = CustomTagsToLinks(el, "Clause")
    el = RemoveLeadingTilde(el)
    return el
end

@@ -136,6 +170,7 @@ function Plain(el)
  el = CustomTagsToLinks(el, "Table")
  el = CustomTagsToLinks(el, "Figure")
  el = CustomTagsToLinks(el, "Clause")
  el = RemoveLeadingTilde(el)
  return el
end

@@ -143,18 +178,22 @@ function Strong(el)
  el = CustomTagsToLinks(el, "Table")
  el = CustomTagsToLinks(el, "Figure")
  el = CustomTagsToLinks(el, "Clause")
  return el
end

-- function Emph(el)
function Emph(el)
  -- el = CustomTagsToLinks(el, "Table")
  -- el = CustomTagsToLinks(el, "Figure")
  -- el = CustomTagsToLinks(el, "Clause")
-- end
  el = RemoveLeadingTilde(el)
  return el
end

function Underline(el)
  el = CustomTagsToLinks(el, "Table")
  el = CustomTagsToLinks(el, "Figure")
  el = CustomTagsToLinks(el, "Clause")
  el = RemoveLeadingTilde(el)
  return el
end

@@ -162,4 +201,6 @@ function Span(el)
  el = CustomTagsToLinks(el, "Table")
  el = CustomTagsToLinks(el, "Figure")
  el = CustomTagsToLinks(el, "Clause")
  el = RemoveLeadingTilde(el)
  return el
end
 No newline at end of file