· 7 years ago · Dec 29, 2018, 05:10 AM
1-- htmlTableMaker0.8.lua
2
3-- Creates a table in html format, with
4-- a tiny bit of css to set the table width.
5-- Output is sent to Table.html file,
6-- which can stand alone or be
7-- used as copypasta into a larger project.
8
9-- I created two different functions;
10-- one to build the header row, and
11-- a loop to create table data/rows.
12
13-- I did not use "th" tags for the header because
14-- they default to center text and that never looks
15-- right to me. I just used bolded td's.
16
17-- ========================================
18-- functions:
19-- file_exists() createHdr() createRow()
20-- =======================================
21
22function createHdr()
23
24 for x = 1, numCols do
25
26 print("Column "..x.." Name: " )
27
28 -- Put user input into a table
29 th[x] = io.read()
30
31 -- combine elements of table into a delimited string
32 -- we'll use html tags as the separator
33 entry = (table.concat(th, "</b></td><td><b>"))
34
35 end
36
37 -- Next, take *that* string and add the beginning
38 -- and ending html row tags...
39
40 table.insert(line, io.write(string.format("<tr><td><b>%s</b></td></tr>\n", entry)))
41end
42
43
44function createRow() -- Format is the same as above but with no bold td's
45
46
47 for x = 1, numCols do
48
49 print(th[x]..": " )
50 td[x] = io.read()
51 entry = (table.concat(td, "</td><td>"))
52
53 end
54
55 table.insert(line, io.write(string.format("<tr><td>%s</td></tr>\n", entry)))
56
57end
58
59function file_exists(file) -- Thanks to lua-users.org
60
61 local f = io.open(file, "rb")
62
63 if f then
64 f:close()
65 end
66
67 return f ~= nil
68
69end
70
71-- ===================================================================
72
73th = { }
74td = { }
75line = { }
76
77io.write("\n-----------------------------------------------------------\n")
78io.write(" htmlTableMaker0.8.lua \n")
79io.write(" Creates an html table (\"Table.html\") which can \n")
80io.write(" stand alone or be pasted into a larger file. \n")
81io.write("-----------------------------------------------------------\n")
82
83if file_exists("Table.html")
84 then io.write("\nFile Table.html exists in the working directory.\nPlease delete, rename, or move this file before continuing.\n")
85 os.exit()
86end
87
88
89io.write("How many columns for your table? ")
90numCols = io.read()
91
92io.write("How many rows for your table? ")
93numRows = io.read()
94
95io.write("How wide is the table (pixels)? ")
96width = io.read()
97
98-- Set up a file for writing. After this, all writes will go to the file, until the file is closed
99
100 htmlFile = "Table.html"
101 file = io.open(htmlFile, "a+")
102 io.output(file)
103
104
105io.write(string.format("<table style = \"width: %d\">\n", width)) -- Create the <table> markup, get user-defined width.
106
107createHdr()
108
109for x = 1, numRows do
110 print("\nRow "..x.."----------------------")
111 createRow()
112end
113
114
115io.write("</table>") -- Table closure tag
116
117file:write()
118file:close()