I'm starring at these few (slightly modified) lines from luadoc that are obviously building a filename with a full path. But I simply don't get it what happens in line 5.
The parameter filename
could be something like "myfile.lua".
function out_file (filename)
local h = filename
h = string.gsub(h, "lua$", "tex")
h = string.gsub(h, "luadoc$", "tex")
h = options.output_dir .. string.gsub (h, "^.-([%w_]+%.tex)$", "%1")
return h
end
What does happen in line 5?
h = options.output_dir .. string.gsub (h, "^.-([%w_]+%.tex)$", "%1")
The pattern matches any string that begins with zero or more non-alphanumeric characters (i.e. whitespace, etc.) followed by one or more alphanumeric characters and underscores (probably a filename), a period and the string "tex" which then ends. It captures the filename + ".tex" for later use. Basically it's taking a filename with possible junk characters (whitespace) at the beginning and replacing it with the clean version before tacking the output directory to the front of it.
Now what's probably causing you confusion there is that .
matches any character. But when modified by a terminating -
that means "the shortest string of zero or more characters before the next match" -- i.e. a non-greedy search. It will match on any characters it finds from the beginning of the string until it finds something that matches the compound [%w_]
– alphanumeric or underscore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With