#! lua-5.0.exe -- ChangeFile.lua -- -- Apply changes on a given file. -- -- Take one parameter: the file to convert, used as input and output. -- Note that the file is processed as binary file, to preserve its EOLs -- whatever the system default is. -- -- by Philippe Lhoste http://Phi.Lho.free.fr -- v. 1.0 -- 2003/10/19 -- Initial code local filenameIn = (arg and arg[1]) or "index.html" -- If absent, use default file local eol local fileHandle -- Process one given line. function ProcessLine(line) if line == nil then return nil end -- Move the link from the end of the header to the start line = string.gsub(line, "(%s*)

]+)>([^<]+)

", "%1

%3

") fileHandle:write(line .. eol) return nil end -- Get the end of line used in the given string and return it. -- Check only the first one, as we assume the string is consistent. function GetEol(str) local eol1, eol2, eol, b b, _, eol1 = string.find(str, "([\r\n])") if b == nil then return nil -- no EOL in this string end -- Care is taken in case the first line finishes with two EOLs eol2 = string.sub(str, b+1, b+1) if eol1 == '\r' then if eol2 == '\n' then -- Windows style eol = '\r\n' else -- Mac style eol = '\r' end else -- eol1 == '\n' -- Unix style eol = '\n' end return eol end function ConvertFile(filename) -- Read the whole file at once, to avoid clash with write -- Binary read, to preserve original EOLs, even if not in current system's style fileHandle = io.open(filename, "rb") if fileHandle == nil then return false, "open rb " .. filename end local file = fileHandle:read("*a") if file == nil then return false, "read " .. filename end fileHandle:close() -- Get the EOL kind for this file eol = GetEol(file) if eol == nil then -- Avoid to process the file, it can be binary or non-standard return false, "no EOL" end -- Prepare to write in the same file fileHandle = io.open(filename, "wb") if fileHandle == nil then return false, "open wb " .. filename end -- Loop on the lines and process them string.gsub(file, "(.-)" .. eol, ProcessLine) fileHandle:close() return true, nil end local result, op = ConvertFile(filenameIn) if not result then print("Error in operation: " .. (op or 'nil')) else print"Done!" end