|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- https://anggtwu.net/LUA/Unaccent1.lua.html
-- https://anggtwu.net/LUA/Unaccent1.lua
-- (find-angg "LUA/Unaccent1.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/Unaccent1.lua"))
-- (find-angg "LUA/lua50init.lua" "utf8tohtml")
-- (find-es "lua5" "tonumber")
-- (find-es "math" "leandro-cardoso")
-- (find-google-links "how do I write an ls using luaposix")
-- (find-google-links "how do I rename a file using luaposix")
load_posix()
ls = function (path) return VTable(ls0(path):ks()) end
ls0 = function (path) return ls00(path):del("."):del("..") end
ls00 = function (path)
local dir = require("posix.dirent").dir
return Set.from(dir(path))
end
-- (find-angg ".emacs.unibyte" "unaccent")
-- (find-angg "LUA/lua50init.lua" "u8c_to_l1")
-- (find-angg "LUA/lua50init.lua" "u8_to_l1")
accents_u8 = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýª°"
accents_no = "AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyao"
accents_l1 = u8_to_l1(accents_u8)
accents_l1_pat = "["..accents_l1.."]"
unaccent_table = VTable {}
for i=1,#accents_no do
unaccent_table[accents_l1:sub(i,i)] = accents_no:sub(i,i)
end
unaccent_l1 = function (str)
return (str:gsub(accents_l1_pat, unaccent_table))
end
hashU_to_l1_pat = "#U(00%x%x)"
hashU_to_l1_f = function (cccc) return string.char(tonumber(cccc,16)) end
hashU_to_l1 = function (str) return str:gsub(hashU_to_l1_pat, hashU_to_l1_f) end
unaccent = function (str) return unaccent_l1(hashU_to_l1(str)) end
-- (find-es "lua5" "luaposix-git")
-- (find-luaposixfile "ext/posix/stdio.c" "rename(oldpath, newpath)")
mv = function (oldfname,newfname) return posix.stdio.rename(oldfname,newfname) end
mv = function (oldfname,newfname) return posix.rename(oldfname,newfname) end
-- (find-sh "cd /tmp/LEANDRO/ && find * -type d | sort | tac")
subdirs = function (path)
local cmd = format("find '%s' -type d | sort | tac", path)
return VTable(splitlines(getoutput(cmd)))
end
unaccents = function (path)
chdir(path)
for _,oldfname in ipairs(ls(".")) do
local newfname = unaccent(oldfname)
if newfname ~= (oldfname) then
-- print(oldfname)
-- print(newfname)
printf(" %s/%s\n-> %s/%s\n", path, oldfname, path, newfname)
mv(oldfname, newfname)
end
end
end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Unaccent1.lua"
= ls "/tmp/LEANDRO"
fnames = ls "/tmp/LEANDRO"
fname = fnames[1]
= unaccent(fname)
= getoutput("cd /tmp/LEANDRO/ && find * -type d | sort | tac")
= subdirs("/tmp/LEANDRO")
fname_l1 = hashU_to_l1(fname)
= fname_l1
= unaccent_l1(hashU_to_l1(fname))
dirs = subdirs("/tmp/LEANDRO")
dir = dirs[13]
= dirs
= dir
unaccents(dir)
for _,dir in ipairs(dirs) do unaccents(dir) end
-- (find-fline "/tmp/LEANDRO/AVALIACOES/GABARITO/")
-- (find-fline "/tmp/LEANDRO/")
= posix
= posix.stdio
chdir(dir)
= ls"."
-- (find-sh "cd /tmp/LEANDRO/ && find * -type d | sort | tac")
= VTable(keys(posix))
= VTable(sortedkeys(posix))
= VTable(sortedkeys(posix.ctype))
= posix
= posix.stdio.rename
= fname_l1:match(accents_l1_pat)
= unaccent_table
for i=1,#accents_no do print(accents_l1:sub(i,i), accents_no:sub(i,i)) end
= string.sub(accents_no, 9,9)
return string.char(tonumber(cccc,16)) end
= fname:match(pat)
= fname:gsub(hashU_pat, hashU_f)
= ls0("/tmp/")
= ls0("/tmp/"):ksc("\n")
= ls0("/tmp/"):del("."):del(".."):ksc("\n")
dir = require("posix.dirent").dir
PPPV(dir("/tmp/"))
files = require("posix.dirent").files
= files("/tmp/")
= files()
= dir
PPV(dir)
= dir.dir("/tmp/")
= dir.dir("/tmp0")
= dir.dir("/tmp0/")
--]]
-- Local Variables:
-- coding: utf-8-unix
-- End: