|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
#!/usr/bin/env lua5.1
-- This file: (find-angg "LUA/texcatcodes.lua")
-- See: (find-angg "LUA/oldglyphs.lua")
-- (find-LATEXfile "2009unilog-dnc.tex" "\\usepackage{edrx08}")
-- (find-angg "dednat4/examples/edrx08.sty" "glyphs")
-- (find-LATEXgrep "grep --color -nH --null -e raw-text *.tex")
-- (find-eevfile "eev-math-glyphs.el" "defun eev-math-glyphs-edrx")
-- (find-TH "glyphs")
-- http://angg.twu.net/glyphs.html
-- (find-LATEX "istanbulglyphs.el")
--
-- Most of my .tex files until 2014 were written in raw-text-unix with
-- one-byte glyphs. This file implements SOME functions to convert
-- files in that format to the new format with unibyte glyphs, but
-- this is a mess!
--
-- (find-LATEX "2009unilog-dnc.tex")
-- (find-LATEXfile "2009unilog-dnc.tex" "\\usepackage{edrx08}")
-- (find-angg "dednat4/examples/edrx08.sty" "glyphs")
--
-- (defun e () (interactive) (find-fline "~/LUA/texcatcodes.lua"))
--
-- (find-sh "./texcatcodes.lua arg1 arg2")
--
-- «.getdef» (to "getdef")
-- «.bslify» (to "bslify")
-- «.Triples» (to "Triples")
-- «.translation» (to "translation")
-- «getdef» (to ".getdef")
getdef = function (li)
if not li:match"^\\catcode" then return end
local c,def = li:match"\\def([^{}]+)(%b{})"
if not c then return end
return c, def:sub(2,-2)
end
getdefs = function (bigstr)
local A = {}
for li in bigstr:gmatch"([^\n]+)" do
if getdef(li) then table.insert(A, {getdef(li)}) end
end
return A
end
gendefs = function (bigstr)
return cow(function ()
for li in bigstr:gmatch"([^\n]+)" do
if getdef(li) then coy(getdef(li)) end
end
end)
end
-- «bslify» (to ".bslify")
-- backslashify
bslify1 = function (c) return format("\\%d", string.byte(c, 1)) end
bslify = function (str) return '"'..(str:gsub("[\1-\9\11-\31\127-\255]", bslify1))..'"' end
string.bslify = bslify
-- = bslify("\5")
-- «Triples» (to ".Triples")
-- (find-angg "LUA/lua50init.lua" "SetL")
Triples = SetL.new()
Triples.entry = function (def)
if not Triples:has(def) then Triples:add(def, {}) end
return Triples:val(def)
end
Triples.goodchars0 = function ()
local cs = Set.new()
for k,v in Triples:gen() do if v.old and v.new then cs:add(v.old) end end
return cs:ks()
end
Triples.goodchars = function () return table.concat(Triples.goodchars0()) end
Triples.substs = function ()
local ss = {}
for k,v in Triples:gen() do if v.old and v.new then ss[v.old] = v.new end end
return ss
end
Triples.getdefault = function ()
-- (find-LATEX "edrxchars.tex")
-- (find-angg "dednat4/examples/edrx08.sty")
bigstrnew = ee_readfile "~/LATEX/edrxchars.tex"
bigstrold = ee_readfile "~/dednat4/examples/edrx08.sty"
for c,def in gendefs(bigstrold) do Triples.entry(def).old = c end
for c,def in gendefs(bigstrnew) do Triples.entry(def).new = c end
-- Triples.entry("\\pi").old = "\21"
-- Triples.entry("\\epsilon").old = "\5"
--
oldchars = Triples.goodchars()
substs = Triples.substs()
end
Triples.subst = function (bigstr)
return bigstr:gsub("["..oldchars.."]", substs)
end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "texcatcodes.lua"
Triples.getdefault()
= Triples:ksc(" ")
= bslify(Triples:goodchars())
= bslify(oldchars)
-- PPV(Triples.substs())
PPV(Triples:val("\\sigma"))
PPV(Triples:val("\\pi"))
PPV(Triples.entry("\\pi"))
--]]
Translate = Class {
type = "Translate",
__index = {
translatables = function () end,
nontranslatables = function () end,
},
}
sortchars = function (str)
local cs = SetL.new()
for c in str:gmatch(".") do cs:add(c) end
return cs:ksc("")
end
non_translatable_chars_in = function (bigstr)
local bigstr = bigstr:gsub("[^\1-\8\11-\31\127-\255]", "") -- del non-ascii
local bigstr = bigstr:gsub("["..oldchars.."]", "") -- del translatables
return sortchars(bigstr)
end
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "texcatcodes.lua"
= sortchars "Hello! Bye!"
-- (find-angg "LATEX/2008hyp.tex")
bigstr = ee_readfile "~/LATEX/2008hyp.tex"
bigstr = ee_readfile "~/LATEX/2008notations.tex"
= non_translatable_chars_in(bigstr)
--]]
-- «translation» (to ".translation")
--
AB = {}
BC = {}
AC = {}
badAs = SetL.new()
goodAs = SetL.new()
BC["\\varepsilon"] = "ε"
BC["\\underline"] = "▁"
oldchars = "\4\5\14\16\17\18\20\21\23\28\31\130\162\164\165\166\167\168\169\172"..
"\173\174\175\179\180\183\184\197\198\203\204\206\207\208\209\215\216\219\221"..
"\222\229\238\240\241\247\253\254"
translation = {
[ "\4"] = "δ", [ "\5"] = "ε", [ "\14"] = "σ", [ "\16"] = "⟧",
[ "\17"] = "⟦", [ "\18"] = "ρ", [ "\20"] = "θ", [ "\21"] = "π",
[ "\23"] = "⊓", [ "\28"] = "τ", [ "\31"] = "∨", ["\130"] = "∞",
["\162"] = "∘", ["\164"] = "⊗", ["\165"] = "⊕", ["\166"] = "𝐛",
["\167"] = "⊤", ["\168"] = "⠆", ["\169"] = "≥", ["\172"] = "¬",
["\173"] = "▁", ["\174"] = "⊥", ["\175"] = "𝐫", ["\179"] = "¹",
["\180"] = "∧", ["\183"] = "·", ["\184"] = "π", ["\197"] = "∫",
["\198"] = "Σ", ["\203"] = "𝐭", ["\204"] = "∩", ["\206"] = "∃",
["\207"] = "ω", ["\208"] = "𝐬", ["\209"] = "⅋", ["\215"] = "×",
["\216"] = "Ω", ["\219"] = "ν", ["\221"] = "∈", ["\222"] = "Θ",
["\229"] = "Π", ["\238"] = "♮", ["\240"] = "λ", ["\241"] = "◻",
["\247"] = "⊔", ["\253"] = "∀", ["\254"] = "∪"
}
translation["\182"] = "<<limp>>"
translation["\182"] = "→"
translation["\171"] = "«"
translation["\187"] = "»"
oldchars2 = table.concat(sorted(keys(translation)), "")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "texcatcodes.lua"
= oldchars
= oldchars2
--]]
-- Command-line arguments:
a,b,c = ...
if a == "-trans" then
bigstrold = ee_readfile(b)
bigstrnew, nc = bigstrold:gsub("["..oldchars.."]", translation)
bigstrnew, nc = bigstrold:gsub("[\1-\8\11-\31\127-\255]", translation)
bigstrnew2,nu = bigstrnew:gsub("raw%-text%-unix", "utf-8-unix")
print(nc, nu)
ee_writefile(c, bigstrnew2)
end
-- (find-fline "~/LATEX/2010kockdiff.tex")
-- (find-sh "~/LUA/texcatcodes.lua -trans ~/LATEX/2010kockdiff.tex")
-- (find-sh0 "~/LUA/texcatcodes.lua -trans ~/LATEX/2010kockdiff.tex > /tmp/o")
-- (find-LATEXsh "~/LUA/texcatcodes.lua -trans 2010kockdiff.tex /tmp/foo")
-- (find-fline "/tmp/foo")
--[[
;; (find-eevfile "eev-math-glyphs.el" "920")
(find-einsert '(920 928 931 937 948 949 952 955 957 960 961 963 964
969 8868 8869 8743 8744 8835 8704 8707 9633 9744 8758 8712 9675
8745 8746 8747 8711 8734 8805 185 8851 8852 8523 8853 8855 8594
8888 9480 9838 9623 9629 8988 8989))
--]]
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "texcatcodes.lua"
OldCatcode = Class {
type = "OldCatcode",
new = function (c, d)
return OldCatcode {c=c, d=d}
end,
fromline = function (li)
if not li:match"^\\catcode" then return end
local c,def = li:match"\\def([^{}]+)(%b{})"
if not c then return end
return OldCatcode {c=c, d=def:sub(2,-2)}
end,
__index = {
tolatex = function (occ)
return format("\\catcode`%s=13 \\def%s{%s}", occ.c, occ.c, occ.d)
end,
},
}
= OldCatcode.new("@", "At"):tolatex()
--]]
-- (find-es "lua5" "cow-and-coy")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "texcatcodes.lua"
bigstrnew = ee_readfile "~/LATEX/edrxchars.tex"
bigstrold = ee_readfile "~/dednat4/examples/edrx08.sty"
for c,def in gendefs(bigstrnew) do BC[def] = c end
for c,def in gendefs(bigstrold) do
local a = c
if BC[def] then
table.insert(AB, {a, BC[def]})
goodAs:add(a)
AC[a] = BC[def]
else
badAs:add(a)
print("No translation for "..bslify(a).." -> "..def)
end
end
-- PPV(AB)
-- PPV(badAs)
= badAs:ksc(""):bslify()
= goodAs:ksc(""):bslify()
for c in goodAs:ksc(""):gmatch"." do
printf(" [%6s] = \"%s\",\n", c:bslify(), AC[c])
end
for c,def in gendefs(bigstrold) do print(c:bslify(), def) end
PPV(AC)
PPV(getdefs(bigstrnew))
PPV(getdefs(bigstrold))
for c,def in gendefs(bigstrold) do Triples.entry(def).old = c end
for c,def in gendefs(bigstrnew) do Triples.entry(def).new = c end
oldchars = Triples.goodchars()
substs = Triples.substs()
f = function (c) return format("\\%d", string.byte(c, 1)) end
= oldchars:gsub(".", f)
-- PPV(substs)
-- _____ ___ ____ ___
-- |_ _/ _ \| _ \ / _ \
-- | || | | | | | | | | |
-- | || |_| | |_| | |_| |
-- |_| \___/|____/ \___/
--
Translations = SetL.new()
-- (find-LATEXgrep "grep -niH -e kock *")
-- (find-fline "~/LATEX/edrxchars.tex")
-- (find-fline "~/dednat4/examples/")
-- (find-fline "~/dednat4/examples/edrx08.sty")
-- (find-sh0 "lua51 -e 'print(tonumber(\"2293\", 16))'")
-- (find-sh0 "lua51 -e 'print(tonumber(\"214B\", 16))'")
-- (insert 8523)
= f("@")
gcs = map(f, Triples.goodchars())
PPV(gcs)
= table.concat(gcs)
Triples.texs = function (f)
local ks = Set.new()
f = f or function (v) return true end
for k,v in Triples:gen() do if f(v) then ks:add(k) end end
return ks:ks()
end
PPV(map(f, Triples.goodchars()))
PPV(table.concat(map(f, Triples.goodchars())))
= Triples.keys
PPV(Triples.texs())
PPV(Triples.texs(function (v) return v.new end))
PPV(Triples.texs(function (v) return v.old end))
print("\64")
--]]