Well, I still don't see what line number has to do with anything, I gather though that you intend that if it is replacing "Internet" with "blah blah" you intend that it be restricted to a line that looks like:
<item> Internet </item>
in order to avoid unwanted replacements other places.
function Initialize()
filePath = SELF:GetOption('FilePath')
end
function Update()
end
function TextReplace(oldText, newText)
filePath = SKIN:MakePathAbsolute(filePath)
local inFile = io.open(filePath, 'r')
if not inFile then
print('LineReplace: unable to open file at ' .. filePath)
return
end
inText = inFile:read('*a')
io.close(inFile)
outText = string.gsub(inText, '<item> '..oldText..' </item>', '<item> '..newText..' </item>')
local outFile = io.open(filePath, 'w')
outFile:write(outText)
io.close(outFile)
end
LineReplace_1.0.rmskin
You do not have the required permissions to view the files attached to this post.
Probably the only thing about this that makes me a hair nervous is that if the "write" action fails for some reason, and I'm not sure what could possibly cause that, but if your system crashed in the middle of the write for instance, the .inc file containing your information could be corrupted or even lost. I'd be temped to create a ".bak" copy of the file in Lua first, and ensure that succeeded, before opening the original for "write".
There are tons of ways you might make this disaster-proof, you might double check that you successfully read the file, by looking for </grammar> in the result or something, you might only write the output file if the string value of "inText" and "outText" are different, and outText contains </grammar>, probably others. Not sure you need to get too anal about it though, this is a straightforward and REALLY fast operation that is unlikely to fail.
One other thing to keep in mind with this, is that the text file you are reading / writing must be UTF-8 w/o BOM Unicode or ANSI encoding. Lua just can't properly deal with UTF-16 Unicode files.