-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.lua
46 lines (44 loc) · 1.65 KB
/
script.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function character_replacer(teststring,characterin,characterout) --replaces a given character sequence occurring on a string
initialposition, finalposition = string.find(teststring, characterout)
if type(initialposition)=="number" and type(finalposition)=="number"then
return character_replacer(teststring:sub(1,initialposition-1) .. characterin .. teststring:sub(finalposition+1,#teststring),characterin,characterout)
else
return teststring
end
end
function csv_to_tabular(filename) --picks a file and converts it to LaTeX's tabular format
local str = ""
local filein = assert(io.open(filename,"r"))
local linenumber = 1
for line in filein:lines() do
if linenumber > 1 then
str = str .. "\\\\%\n"
end
str = str .. character_replacer(line," & ",",")
linenumber = linenumber + 1
end
filein:close()
str = str .. "%" --adds a final % sign to comment extra spaces
local fileout = assert(io.open("output.tex","w"))
fileout:write(str)
fileout:close()
--return str
end
function tsv_to_tabular(filename) --picks a file and converts it to LaTeX's tabular format
local str = ""
local filein = assert(io.open(filename,"r"))
local linenumber = 1
for line in filein:lines() do
if linenumber > 1 then
str = str .. "\\\\%\n"
end
str = str .. character_replacer(line," & ","\t")
linenumber = linenumber + 1
end
filein:close()
str = str .. "%" --adds a final % sign to comment extra spaces
local fileout = assert(io.open("output.tex","w"))
fileout:write(str)
fileout:close()
--return str
end