Advertisement
guitarplayer616

HTMLua for pastebin WIP

Jun 22nd, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.99 KB | None | 0 0
  1. --http://pastebin.com/u/CaptainSpaceCat      this brilliant man came up with the semi-original idea, i've just expanded upon it
  2. local debug = false
  3. local nextline = {1}
  4. local constructors = {"<paste>", "<paste_key(.-)>", "</paste_key>", "<paste_date(.-)>", "</paste_date>", "<paste_title(.-)>", "</paste_title>", "<paste_size(.-)>", "</paste_size>", "<paste_hits(.-)>", "</paste_hits>", "<paste_url(.-)>", "</paste_url>"}
  5. local formats = {"background=", "text=", "align="}
  6. local w, h = term.getSize()
  7. local lua = ""
  8. local currentX = 1
  9. local currentY = 1
  10. local mode = nil
  11. local con = nil
  12. local lNum = 0
  13. local bakcol = "black"
  14. local txtcol = "white"
  15. local title = "Untitled"
  16. local defaultAlignment = "left"
  17. local alignment = defaultAlignment
  18. --an example code to run using this code can be found here http://pastebin.com/fwCkxy6G
  19.  
  20. function addLuaLine(input)                  --adds lines to the lua loadstring
  21.   lua = lua .. "\n" .. input
  22. end
  23. function addLuaFrontLine(input)
  24.   lua = "\n" .. input .. lua
  25. end
  26.  
  27. function bodyFormat(line)                  --used to format page body
  28.   local vars = line:gsub(" ", "")
  29.   local formatting = nil
  30.   for i, v in pairs(formats) do
  31.     if vars:find(v) then
  32.       if vars:find(v .. "(.-),") then
  33.         formatting = {vars:find(v .. "(.-),")}
  34.       else
  35.         formatting = {vars:find(v .. "(.-)>")}
  36.       end
  37.       local changes = vars:sub(formatting[1] + #v, formatting[2] - 1)
  38.       if i == 1 then --background
  39.         addLuaFrontLine("term.clear()")
  40.         addLuaFrontLine("term.setBackgroundColor(colors." .. changes .. ")")
  41.         bakcol = changes
  42.       elseif i == 2 then --text
  43.         txtcol = changes
  44.       elseif i == 3 then --align
  45.         defaultAlignment = changes
  46.       end
  47.     end
  48.   end
  49. end
  50. function pFormat(line)                    --used to format <p>'s and <h1>'s
  51.   local vars = line:gsub("(.-)<h1,", "")
  52.   vars = vars:gsub(">(.-)</h1>", "")
  53.   vars = vars:gsub("(.-)<p,", "")
  54.   vars = vars:gsub(">(.-)</p>", "")
  55.   vars = vars:gsub(">", "")
  56.   vars = vars .. ">"
  57.   vars = vars:gsub(" ", "")
  58.   local formatting = nil
  59.   alignment = defaultAlignment
  60.   for i, v in pairs(formats) do
  61.     if vars:find(v) then
  62.       if vars:find(v .. "(.-),") then
  63.         formatting = {vars:find(v .. "(.-),")}
  64.       else
  65.         formatting = {vars:find(v .. "(.-)>")}
  66.       end
  67.       local changes = vars:sub(formatting[1] + #v, formatting[2] - 1)
  68.       if i == 1 then --background
  69.         addLuaLine("term.setBackgroundColor(colors." .. changes .. ")")
  70.       elseif i == 2 then --text
  71.         addLuaLine("term.setTextColor(colors." .. changes .. ")")
  72.       elseif i == 3 then --align
  73.         alignment = changes
  74.       end
  75.     else
  76.       if v == "background=" then
  77.         addLuaLine("term.setBackgroundColor(colors." .. bakcol .. ")")
  78.       elseif v == "text=" then
  79.         addLuaLine("term.setTextColor(colors." .. txtcol .. ")")
  80.       end
  81.     end
  82.   end
  83. end
  84.  
  85. function pDefault()                     --used to default the text back to the background if format not specified
  86.   addLuaLine("term.setBackgroundColor(colors." .. bakcol .. ")")
  87.   addLuaLine("term.setTextColor(colors." .. txtcol .. ")")
  88.   alignment = defaultAlignment
  89. end
  90.  
  91. function pWrite()
  92. end
  93.  
  94. --[[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.]]--
  95. --also, I know it's not EXACTLY like html, but where's the fun in that?
  96. local tArgs = {...}
  97. for line in io.lines(tArgs[1]) do
  98.   lNum = lNum + 1
  99.   line = line:gsub("<!--(.-)-->", "")    --completely deletes comments from code so they don't mess with it
  100.   local test = line:gsub(" ", "")        --removes all spaces to test for whitespace lines
  101.   --if test == "" then      --Note to Self: figure out how to do this correctly
  102.     --next                  --Note to Austin: halp
  103.   --end
  104.   if test ~= "" then
  105.   if nextline ~= false then
  106.     for i, v in pairs(nextline) do          --checks each available constructor
  107.       if line:find(constructors[v]) then
  108.         con = v
  109.         break
  110.       elseif i == #nextline then --and line ~= "" and not line:find("<!--(.-)-->") then
  111.         error("ERROR: Tag error on line " .. tostring(lNum)) --if it isn't a constructor, an empty line, or a comment, then it errors
  112.       end
  113.     end
  114.     if con == 1 then -- <!DOCTYPE html>
  115.       print(1)
  116.       nextline = {2}
  117.     elseif con == 2 then -- <html>
  118.       print(2)
  119.       nextline = {3, 4, 6}
  120.     elseif con == 3 then -- </html>
  121.       print(3)
  122.       --program is over
  123.       break
  124.     elseif con == 4 then -- <head>
  125.       print(4)
  126.       nextline = {5, 12}
  127.     elseif con == 5 then -- </head>
  128.       print(5)
  129.       nextline = {3, 6}
  130.     elseif con == 6 then -- <body>
  131.       bodyFormat(line)
  132.       print(6)
  133.       nextline = {7, 8, 10}
  134.     elseif con == 7 then -- </body>
  135.       print(7)
  136.       nextline = {3}
  137.     elseif con == 8 then -- <p>
  138.       currentY = currentY + 1
  139.       if line:find("<p,") then    --if there is a comma after p then you must be trying to format
  140.         pFormat(line)
  141.       else
  142.         pDefault()
  143.       end
  144.       print(8)
  145.       if line:find("</p>") then    --detects if it is a one line paragraph or a multi-line
  146.         local start, finish = line:find(">(.-)</p>")     --if it's one line, it figures out the text
  147.         local text = line:sub(start + 1, finish - 4)
  148.         if alignment == "left" then
  149.           currentX = 1
  150.         elseif alignment == "right" then
  151.           currentX = w - #text + 1
  152.         elseif alignment == "center" then
  153.           currentX = w/2 - #text/2 + 1
  154.         else
  155.           error("ERROR: Invalid alignment on line " .. tostring(currentY))
  156.         end
  157.         addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
  158.         addLuaLine("term.write(\"" .. tostring(text) .. "\")")
  159.         nextline = {7, 8, 10}
  160.       else
  161.         nextline = false --if it's multi-line, it skips the constructor check on the next line and goes to the bottom of the code
  162.         mode = "p"
  163.       end
  164.     elseif con == 9 then -- </p>
  165.       print(9)
  166.       nextline = {7, 8, 10}
  167.     elseif con == 10 then -- <h1>
  168.       if line:find("<h1,") then  --due to the nature of CC, <h1> is literally exactly the same as <p>
  169.         pFormat(line)            --this could potentially be changed with a wrapped monitor, but I haven't worked on that yet
  170.       else
  171.         pDefault()
  172.       end
  173.       print(10)
  174.       currentY = currentY + 1
  175.       if line:find("</h1>") then
  176.         local start, finish = line:find(">(.-)</h1>")
  177.         local text = line:sub(start + 1, finish - 5)
  178.         if alignment == "left" then
  179.           currentX = 1
  180.         elseif alignment == "right" then
  181.           currentX = w - #text + 1
  182.         elseif alignment == "center" then
  183.           currentX = w/2 - #text/2 + 1
  184.         else
  185.           error("ERROR: Invalid alignment on line " .. tostring(currentY))
  186.         end
  187.         addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
  188.         addLuaLine("term.write(\"" .. tostring(text) .. "\")")
  189.         nextline = {7, 8, 10}
  190.       else
  191.         nextline = false
  192.         mode = "h1"
  193.       end
  194.     elseif con == 11 then -- </h1>
  195.       print(11)
  196.       nextline = {7, 8, 10}
  197.     elseif con == 12 then -- <title>
  198.       print(12)
  199.       if line:find("</title>") then   --does the same single/multi-line check
  200.         local start, finish = line:find(">(.-)</title>")
  201.         local text = line:sub(start + 1, finish - 8)
  202.         title = text    --sets the title for a single line tag
  203.         nextline = {5, 12}
  204.       else
  205.         nextline = false    --goes to the bottom for multi-line
  206.         mode = "title"
  207.       end
  208.     elseif con == 13 then -- </title>
  209.       print(13)
  210.       nextline = {5, 12}
  211.     end
  212.   else                                            --here's the so-called "bottom" of the code
  213.     if mode == "p" or mode == "h1" then
  214.       local text = nil                    --bam. multi-line <p>'s or <h1>'s
  215.       for i = 1, #line do
  216.         if line:sub(i, i) ~= " " then
  217.           text = i
  218.           break
  219.         end
  220.       end
  221.       if text then
  222.         text = line:sub(text, #line)
  223.       end
  224.       if alignment == "left" then
  225.         currentX = 1
  226.       elseif alignment == "right" then
  227.         currentX = w - #text + 1
  228.       elseif alignment == "center" then
  229.         currentX = w/2 - #text/2 + 1
  230.       else
  231.         error("ERROR: Invalid alignment on line " .. tostring(currentY))
  232.       end
  233.         addLuaLine("term.setCursorPos(" .. tostring(currentX) .. ", " .. tostring(currentY) .. ")")
  234.       addLuaLine("term.write(\"" .. tostring(text) .. "\")")
  235.       if mode == "p" then
  236.         nextline = {9}
  237.       elseif mode == "h1" then
  238.         nextline = {11}
  239.       end
  240.     elseif mode == "title" then
  241.       local text = nil                --aaaaand multi-line titles
  242.       for i = 1, #line do
  243.         if line:sub(i, i) ~= " " then
  244.           text = i
  245.           break
  246.         end
  247.       end
  248.       if text then
  249.         text = line:sub(text, #line)
  250.       end
  251.       title = text
  252.       nextline = {13}
  253.     end
  254.   end
  255.   end
  256. end
  257. if debug == true then              --for debugging, so you can see what the code thinks it's doing
  258.   oTemp = fs.open("debug", "w")    --set the debug boolean to true on line 2 to use this
  259.   oTemp.write(lua)                 --it will save the lua loadstring to a file called "debug"
  260.   oTemp.close()
  261.   sleep(200)    --makes it so you can see the console for 200 seconds (which seems like plenty of time)
  262. end
  263. term.setBackgroundColor(colors.black)   -- this is where it actually sets up the web page
  264. term.clear()
  265. run = loadstring(lua)   --this entire time, the code has been converting the html into computercraft lua via a string
  266. run()                   --now it uses a loadstring to run the code
  267. term.setBackgroundColor(colors.lightGray)
  268. term.setCursorPos(1, 1)
  269. term.clearLine()
  270. term.setCursorPos(1, 1)                   --title bar
  271. term.setTextColor(colors.gray)
  272. term.write(title)
  273. term.setBackgroundColor(colors.red)
  274. term.setTextColor(colors.white)
  275. term.setCursorPos(w - 2, 1)
  276. term.write(" X ")                    --to exit the page, good old X button
  277. while true do
  278.   local events = {os.pullEvent()}
  279.   if events[1] == "mouse_click" and events[2] == 1 and events[3] >= w - 2 and events[3] <= w and events[4] == 1 then
  280.     term.setBackgroundColor(colors.orange)
  281.     term.setCursorPos(w - 2, 1)              --checks to see if you've clicked the X button
  282.     term.write(" X ")
  283.     sleep(.2)
  284.     term.setBackgroundColor(colors.black)
  285.     term.clear()
  286.     term.setCursorPos(1, 1)
  287.     break                           --if so, it cleans up and exits the page
  288.   end
  289. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement