heonian-conversation/converter.lua
2022-05-05 08:22:53 +02:00

187 lines
4.2 KiB
Lua

require "R2H2"
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 reverse_verbs(str)
while string.find(str, "kanya") do
str = string.gsub(str, "kanya","ku")
end
while string.find(str, "kome") do
str = string.gsub(str, "kome","ku")
end
return str
end
function apply_html(path)
-- copy
local file = io.open(path,"r")
local html = file:read("*all")
file:close()
-- paste
print(html)
end
function convo_image(string,alt_text)
if string then
apply_html("html/convo/header/image/start.html")
print(string)
apply_html("html/convo/header/image/middle.html")
print(alt_text)
apply_html("html/convo/header/image/end.html")
end
end
function convo_date(string)
if string then
apply_html("html/convo/header/date/start.html")
print(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
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 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")
print(string.sub(text,p+2,np-1))
--content = content .. " " .. reverse_verbs(strip_symbols(string.sub(text,p+2,np-1)))
--print("\n"..reverse_verbs(strip_symbols(string.sub(text,p+2,np-1))))
apply_html("html/convo/text/end.html")
-- heonian
apply_html("html/convo/text/start_heonian.html")
print(convertToHeonian(string.sub(text,p+2,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))
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")
print(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
-- lets end the html
apply_html("html/end.html")
-- print(content)