Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --http://pastebin.com/u/guitarplayer616 this brilliant man came up with the original idea, i've just expanded upon it
- local debug = false
- local nextline = {1}
- local constructors = {"<!DOCTYPE html>", "<html>", "</html>", "<head>", "</head>", "<body(.-)>", "</body>", "<p(.-)>", "</p>", "<h1(.-)>", "</h1>", "<title>", "</title>"}
- local formats = {"background=", "text=", "align="}
- local w, h = term.getSize()
- local lua = ""
- local currentX = 1
- local currentY = 1
- local mode = nil
- local con = nil
- local lNum = 0
- local bakcol = "black"
- local txtcol = "white"
- local title = "Untitled"
- local defaultAlignment = "left"
- local alignment = defaultAlignment
- --an example code to run using this code can be found here http://pastebin.com/fwCkxy6G
- function addLuaLine(input) --adds lines to the lua loadstring
- lua = lua .. "\n" .. input
- end
- function addLuaFrontLine(input)
- lua = "\n" .. input .. lua
- end
- function bodyFormat(line) --used to format page body
- local vars = line:gsub(" ", "")
- local formatting = nil
- for i, v in pairs(formats) do
- if vars:find(v) then
- if vars:find(v .. "(.-),") then
- formatting = {vars:find(v .. "(.-),")}
- else
- formatting = {vars:find(v .. "(.-)>")}
- end
- local changes = vars:sub(formatting[1] + #v, formatting[2] - 1)
- if i == 1 then --background
- addLuaFrontLine("term.clear()")
- addLuaFrontLine("term.setBackgroundColor(colors." .. changes .. ")")
- bakcol = changes
- elseif i == 2 then --text
- txtcol = changes
- elseif i == 3 then --align
- defaultAlignment = changes
- end
- end
- end
- end
- function pFormat(line) --used to format <p>'s and <h1>'s
- local vars = line:gsub("(.-)<h1,", "")
- vars = vars:gsub(">(.-)</h1>", "")
- vars = vars:gsub("(.-)<p,", "")
- vars = vars:gsub(">(.-)</p>", "")
- vars = vars:gsub(">", "")
- vars = vars .. ">"
- vars = vars:gsub(" ", "")
- local formatting = nil
- alignment = defaultAlignment
- for i, v in pairs(formats) do
- if vars:find(v) then
- if vars:find(v .. "(.-),") then
- formatting = {vars:find(v .. "(.-),")}
- else
- formatting = {vars:find(v .. "(.-)>")}
- end
- local changes = vars:sub(formatting[1] + #v, formatting[2] - 1)
- if i == 1 then --background
- addLuaLine("term.setBackgroundColor(colors." .. changes .. ")")
- elseif i == 2 then --text
- addLuaLine("term.setTextColor(colors." .. changes .. ")")
- elseif i == 3 then --align
- alignment = changes
- end
- else
- if v == "background=" then
- addLuaLine("term.setBackgroundColor(colors." .. bakcol .. ")")
- elseif v == "text=" then
- addLuaLine("term.setTextColor(colors." .. txtcol .. ")")
- end
- end
- end
- end
- function pDefault() --used to default the text back to the background if format not specified
- addLuaLine("term.setBackgroundColor(colors." .. bakcol .. ")")
- addLuaLine("term.setTextColor(colors." .. txtcol .. ")")
- alignment = defaultAlignment
- end
- function pWrite()
- end
- --[[how the system works is that it checks each line for each available constructor, and then changes the list of available constructors for the next line based on which one it got on this line. An example of this is that is it detects a <head> tag, then the available constructors will be either <title> or </head>. Anything else would be out of place, so it only checks for those 2. Same goes for <body>, which then tells the code to look for either <p>, <h1>, or </body> on the next line.]]--
- --also, I know it's not EXACTLY like html, but where's the fun in that?
- local tArgs = {...}
- for line in io.lines(tArgs[1]) do
- lNum = lNum + 1
- line = line:gsub("<!--(.-)-->", "") --completely deletes comments from code so they don't mess with it
- local test = line:gsub(" ", "") --removes all spaces to test for whitespace lines
- --if test == "" then --Note to Self: figure out how to do this correctly
- --next --Note to Austin: halp
- --end
- if test ~= "" then
- if nextline ~= false then
- for i, v in pairs(nextline) do --checks each available constructor
- if line:find(constructors[v]) then
- con = v
- break
- elseif i == #nextline then --and line ~= "" and not line:find("<!--(.-)-->") then
- error("ERROR: Tag error on line " .. tostring(lNum)) --if it isn't a constructor, an empty line, or a comment, then it errors
- end
- end
- if con == 1 then -- <!DOCTYPE html>
- print(1)
- nextline = {2}
- elseif con == 2 then -- <html>
- print(2)
- nextline = {3, 4, 6}
- elseif con == 3 then -- </html>
- print(3)
- --program is over
- break
- elseif con == 4 then -- <head>
- print(4)
- nextline = {5, 12}
- elseif con == 5 then -- </head>
- print(5)
- nextline = {3, 6}
- elseif con == 6 then -- <body>
- bodyFormat(line)
- print(6)
- nextline = {7, 8, 10}
- elseif con == 7 then -- </body>
- print(7)
- nextline = {3}
- elseif con == 8 then -- <p>
- currentY = currentY + 1
- if line:find("<p,") then --if there is a comma after p then you must be trying to format
- pFormat(line)
- else
- pDefault()
- end
- print(8)
- if line:find("</p>") then --detects if it is a one line paragraph or a multi-line
- local start, finish = line:find(">(.-)</p>") --if it's one line, it figures out the text
- local text = line:sub(start + 1, finish - 4)
- if alignment == "left" then
- currentX = 1
- elseif alignment == "right" then
- currentX = w - #text + 1
- elseif alignment == "center" then
- currentX = w/2 - #text/2 + 1
- else
- error("ERROR: Invalid alignment on line " .. tostring(currentY))
- end
- addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
- addLuaLine("term.write(\"" .. tostring(text) .. "\")")
- nextline = {7, 8, 10}
- else
- nextline = false --if it's multi-line, it skips the constructor check on the next line and goes to the bottom of the code
- mode = "p"
- end
- elseif con == 9 then -- </p>
- print(9)
- nextline = {7, 8, 10}
- elseif con == 10 then -- <h1>
- if line:find("<h1,") then --due to the nature of CC, <h1> is literally exactly the same as <p>
- pFormat(line) --this could potentially be changed with a wrapped monitor, but I haven't worked on that yet
- else
- pDefault()
- end
- print(10)
- currentY = currentY + 1
- if line:find("</h1>") then
- local start, finish = line:find(">(.-)</h1>")
- local text = line:sub(start + 1, finish - 5)
- if alignment == "left" then
- currentX = 1
- elseif alignment == "right" then
- currentX = w - #text + 1
- elseif alignment == "center" then
- currentX = w/2 - #text/2 + 1
- else
- error("ERROR: Invalid alignment on line " .. tostring(currentY))
- end
- addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
- addLuaLine("term.write(\"" .. tostring(text) .. "\")")
- nextline = {7, 8, 10}
- else
- nextline = false
- mode = "h1"
- end
- elseif con == 11 then -- </h1>
- print(11)
- nextline = {7, 8, 10}
- elseif con == 12 then -- <title>
- print(12)
- if line:find("</title>") then --does the same single/multi-line check
- local start, finish = line:find(">(.-)</title>")
- local text = line:sub(start + 1, finish - 8)
- title = text --sets the title for a single line tag
- nextline = {5, 12}
- else
- nextline = false --goes to the bottom for multi-line
- mode = "title"
- end
- elseif con == 13 then -- </title>
- print(13)
- nextline = {5, 12}
- end
- else --here's the so-called "bottom" of the code
- if mode == "p" or mode == "h1" then
- local text = nil --bam. multi-line <p>'s or <h1>'s
- for i = 1, #line do
- if line:sub(i, i) ~= " " then
- text = i
- break
- end
- end
- if text then
- text = line:sub(text, #line)
- end
- if alignment == "left" then
- currentX = 1
- elseif alignment == "right" then
- currentX = w - #text + 1
- elseif alignment == "center" then
- currentX = w/2 - #text/2 + 1
- else
- error("ERROR: Invalid alignment on line " .. tostring(currentY))
- end
- addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
- addLuaLine("term.write(\"" .. tostring(text) .. "\")")
- if mode == "p" then
- nextline = {9}
- elseif mode == "h1" then
- nextline = {11}
- end
- elseif mode == "title" then
- local text = nil --aaaaand multi-line titles
- for i = 1, #line do
- if line:sub(i, i) ~= " " then
- text = i
- break
- end
- end
- if text then
- text = line:sub(text, #line)
- end
- title = text
- nextline = {13}
- end
- end
- end
- end
- if debug == true then --for debugging, so you can see what the code thinks it's doing
- oTemp = fs.open("debug", "w") --set the debug boolean to true on line 2 to use this
- oTemp.write(lua) --it will save the lua loadstring to a file called "debug"
- oTemp.close()
- sleep(200) --makes it so you can see the console for 200 seconds (which seems like plenty of time)
- end
- term.setBackgroundColor(colors.black) -- this is where it actually sets up the web page
- term.clear()
- run = loadstring(lua) --this entire time, the code has been converting the html into computercraft lua via a string
- run() --now it uses a loadstring to run the code
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(1, 1)
- term.clearLine()
- term.setCursorPos(1, 1) --title bar
- term.setTextColor(colors.gray)
- term.write(title)
- term.setBackgroundColor(colors.red)
- term.setTextColor(colors.white)
- term.setCursorPos(w - 2, 1)
- term.write(" X ") --to exit the page, good old X button
- while true do
- local events = {os.pullEvent()}
- if events[1] == "mouse_click" and events[2] == 1 and events[3] >= w - 2 and events[3] <= w and events[4] == 1 then
- term.setBackgroundColor(colors.orange)
- term.setCursorPos(w - 2, 1) --checks to see if you've clicked the X button
- term.write(" X ")
- sleep(.2)
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1, 1)
- break --if so, it cleans up and exits the page
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement