lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Note that Lua patterns do not support alternatives with '|', but they support  '?' for 0 or 1 occurrence (not just '*' and '+' for 0+ and 1+ occurences).
Note also that you want to match the dot for the extension literally, not arbitrary characters, so you must use '%.', not '.'. The '#' does not need to be escaped by a '%' in the search pattern (however it still works if you do).
Just create a capture group for the optional anchor (*including* the leading '#').

function Link (link)
  link.target = link.target:gsub('(.+)%.md(#.+)?', '%1.html%2')
  return link
end

Le jeu. 21 janv. 2021 à 17:38, Peter Matulis <pmatulis@gmail.com> a écrit :
Hi, I am very new to Lua but I do have two tiny Pandoc Lua filters that work. They are extremely similar and I would like to combine them. I'm just not sure how to introduce the conditional aspect.

1. This script simply exchanges the '.md' file extension for the '.html' extension:


function Link (link)
  link.target = link.target:gsub('(.+)%.md', '%1.html')
  return link
end



2. This script does the same except it handles the case where the link contains an anchor:


function Link (link)
  link.target = link.target:gsub('(.+)%.md%#(.+)', '%1.html#%2')
  return link
end



/pm