heonian-conversation/converter.lua

258 lines
5.9 KiB
Lua

require "r2h2_modified"
function add_word(str)
local add = true
for _, v in pairs(word_list) do
if v == str then
add = false
end
end
if add then
table.insert(word_list,str)
end
end
function strip_symbols(str)
local symbol_table = "'~()!?:></.,\t"
for i=1, #symbol_table do
while string.find(str, "%"..string.sub(symbol_table,i,i)) do
str = string.gsub(str, "%"..string.sub(symbol_table,i,i),"")
end
end
return str
end
function replace_symbols(str)
while string.find(str, "%-") do
str = string.gsub(str, "%-"," ")
end
return str
end
function find_n_replace(str,find,repl)
while string.find(str, find) do
str = string.gsub(str, find, repl)
end
return str
end
function revert_verbs(str)
str = find_n_replace(str, "kanya","ku")
str = find_n_replace(str, "kome","ku")
str = find_n_replace(str, "kupash","ku")
str = find_n_replace(str, "kudash","ku")
str = find_n_replace(str, "kubash","ku")
str = find_n_replace(str, "kutash","ku")
str = find_n_replace(str, "faku","ku")
return str
end
function apply_html(path)
-- copy
local file = io.open(path,"r")
local html = file:read("*all")
file:close()
-- paste
apply(html)
end
function apply(text)
--print(text)
end
function convo_image(string,alt_text)
if string then
apply_html("html/convo/header/image/start.html")
apply(string)
apply_html("html/convo/header/image/middle.html")
apply(alt_text)
apply_html("html/convo/header/image/end.html")
end
end
function convo_notes(string)
apply_html("html/convo/middle.html")
if string then
apply_html("html/convo/notes/start.html")
apply(string)
apply_html("html/convo/notes/end.html")
end
end
function convo_date(string)
if string then
apply_html("html/convo/header/date/start.html")
apply(string)
apply_html("html/convo/header/date/end.html")
end
end
function convo_header_start()
if not header then
header = true
apply_html("html/convo/header/start.html")
end
end
function convo_header_end()
if header then
header = false
apply_html("html/convo/header/end.html")
end
end
function convo_start(color)
convo_header_end()
if not convo then
if color then
apply_html("html/convo/start_green.html")
else
apply_html("html/convo/start_gray.html")
end
convo = true
end
end
function convo_end()
convo_header_end()
if convo then
if not notes then
apply_html("html/convo/middle.html")
end
notes = false
apply_html("html/convo/end.html")
convo = false
end
end
-- lets make the html
apply_html("html/start.html")
-- get text
local file = io.open("input.txt","r")
local text = file:read("*all")
file:close()
-- first analyze text
-- (this is the fun part)
local content = ""
local p = 0
local s = 0
local convo = false
local header = false
local notes = false
local green = true
local skip = 1
local user = {}
while p ~= nil do
s = s + 1
local np = string.find(text,"\n",p+1)
if np and p - np ~= 1 then
if p then
skip = 1
if string.sub(text,p+1,p+1) == "\n"
or string.sub(text,p+1,p+1) == "/" then
-- end of new bubble
convo_end()
elseif string.sub(text,p+1,p+1) == "\t" then
-- its tabbed so its spoken?
convo_start()
apply_html("html/convo/text/start_roman.html")
apply(string.sub(text,p+2,np-1))
content = content .. " " .. string.sub(text,p+2,np-1)
apply_html("html/convo/text/end.html")
-- heonian
apply_html("html/convo/text/start_heonian.html")
apply(mod_convertToHeonian(string.sub(text,p+2,np-1)).."")
apply_html("html/convo/text/end.html")
elseif string.sub(text,p+1,p+1) == "h" then
-- spoken but forced to h
convo_start()
apply_html("html/convo/text/start_raw.html")
apply(mod_convertToHeonian(string.sub(text,p+3,np-1)).."")
apply_html("html/convo/text/end.html")
elseif string.sub(text,p+1,p+1) == "r" then
-- spoken but forced to r
convo_start()
apply_html("html/convo/text/start_raw.html")
apply(string.sub(text,p+3,np-1))
apply_html("html/convo/text/end.html")
elseif string.sub(text,p+1,p+1) == "i" then -- independant image, no username
convo_start(green)
convo_header_start()
convo_image(string.sub(text,p+3,np-1))
elseif string.sub(text,p+1,p+1) == "!" then -- independant image, no username
convo_notes(string.sub(text,p+3,np-1))
notes = true
else
-- new user name
convo_end()
if green then
green = false
else
green = true
end
convo_start(green)
-- this is a header section
convo_header_start()
-- lets check for images
local nl = string.find(text,"\n",p+1)
local nlp = string.find(text,"\n",nl+1)
current_user = string.sub(text,p+1,np-1)
user[current_user] = user[current_user] or {image = nil}
if nlp and string.sub(text,nl+1,nl+1) == "i" then
user[current_user].image = string.sub(text,nl+3,nlp-1)
skip = skip + 1
nl = string.find(text,"\n",nl+1)
nlp = string.find(text,"\n",nlp+1)
end
convo_image(user[current_user].image)
-- print name
apply_html("html/convo/header/name/start.html")
apply(string.sub(text,p+1,np-1))
apply_html("html/convo/header/name/end.html")
-- lets check for dates
if nlp and string.sub(text,nl+1,nl+1) == "d" then
convo_date(string.sub(text,nl+3,nlp-1))
skip = skip + 1
else
convo_date("")
end
header = true
end
end
while skip > 0 do
p = string.find(text,"\n",p+1)
skip = skip - 1
end
else
break
end
end
apply_html("html/convo/end.html")
-- fourth lets make word lists
-- process contents
content = strip_symbols(content)
content = replace_symbols(content)
content = revert_verbs(content)
word_list = {}
while string.find(content, " ") do
add_word(string.sub(content, 0, string.find(content, " ")))
content = string.sub(content, string.find(content, " ")+1)
--print(string.find(content, " "))
end
for i=1, #word_list do
print(word_list[i])
end
-- lets end the html
apply_html("html/end.html")