Advertisement
MC403

SJNOS-SWeb API

May 16th, 2015
401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1. -- SWEBINT API (Bex5SsgL)
  2.  
  3. --[[
  4. @public
  5. @doctype=stp
  6. <text=Hallo color=blue>
  7. ]]
  8.  
  9. local function tcolor(color) term.setTextColor(color) end
  10. local function bcolor(color) term.setBackgroundColor(color) end
  11. local function clear() term.clear() end
  12. local function cpos(x,y) term.setCursorPos(x,y) end
  13. local function clearL() term.clearLine() end
  14.  
  15. function getColor(color)
  16.     if (color == "white") then
  17.         return colors.white
  18.     elseif (color == "orange") then
  19.         return colors.orange
  20.     elseif (color == "magenta") then
  21.         return colors.magenta
  22.     elseif (color == "lightblue" or color == "lightBlue") then
  23.         return colors.lightBlue
  24.     elseif (color == "yellow") then
  25.         return colors.yellow
  26.     elseif (color == "lime") then
  27.         return colors.lime
  28.     elseif (color == "pink") then
  29.         return colors.pink
  30.     elseif (color == "gray") then
  31.         return colors.gray
  32.     elseif (color == "lightgray" or color == "lightGray") then
  33.         return colors.lightGray
  34.     elseif (color == "cyan") then
  35.         return colors.cyan
  36.     elseif (color == "purple") then
  37.         return colors.purple
  38.     elseif (color == "blue") then
  39.         return colors.blue
  40.     elseif (color == "brown") then
  41.         return colors.brown
  42.     elseif (color == "green") then
  43.         return colors.green
  44.     elseif (color == "red") then
  45.         return colors.red
  46.     elseif (color == "black") then
  47.         return colors.black
  48.     elseif (tonumber(color) ~= nil) then
  49.         local num = tonumber(color)
  50.         if (num < 32738) then
  51.             local rest = num
  52.             local result = true
  53.             while (rest > 1 and result) do
  54.                 rest = rest / 2
  55.                 if (rest % 1 ~= 0) then
  56.                     result = false
  57.                 end
  58.             end
  59.  
  60.             if (result) then
  61.                 return num
  62.             end
  63.         end
  64.     end
  65.  
  66.     return nil
  67. end
  68.  
  69. function getCommand(cmd, STANDARTTEXT, STANDARTBG)
  70.     local text, color, bg, link
  71.  
  72.     local rest = cmd
  73.     local lastPos = 1
  74.  
  75.     local running = true
  76.  
  77.     local buffer = true
  78.  
  79.     while (running or buffer) do
  80.  
  81.         local pos, newLastPos = string.find(rest, " ")
  82.         pos = pos or lastPos
  83.         lastPos = newLastPos or #rest
  84.  
  85.         local element = string.sub(rest, 1, lastPos)..""
  86.         if (string.sub(element, #element, #element) == " ") then
  87.             element = string.sub(element, 1, #element - 1)
  88.         end
  89.  
  90.         lastPos = lastPos + 1
  91.  
  92.         rest = string.sub(rest, lastPos) .. ""
  93.  
  94.         if (string.find(element, "=") == nil) then
  95.             break
  96.         end
  97.  
  98.         local name = string.sub(element, 1, string.find(element, "=")-1)
  99.         local value = string.sub(element, string.find(element, "=")+1)
  100.         if (name == "text") then
  101.             local newText = value..""
  102.  
  103.             while true do
  104.                 local pos = string.find(newText, "#")
  105.                 if (pos ~= nil) then
  106.                     newText = string.sub(newText, 1, pos-1) .. " " .. string.sub(newText, pos+1)
  107.                 else
  108.                     break
  109.                 end
  110.             end
  111.  
  112.             text = newText
  113.         elseif (name == "color") then
  114.             color = getColor(value)
  115.         elseif (name == "bg") then
  116.             bg = getColor(value)
  117.         elseif (name == "link") then
  118.             link = value
  119.         end
  120.  
  121.         if (not running and buffer) then
  122.             buffer = false
  123.         end
  124.  
  125.         running = string.find(rest, " ") ~= nil
  126.     end
  127.  
  128.     return {["text"] = text or "", ["tcolor"] = color or STANDARTTEXT, ["bcolor"] = bg or STANDARTBG, ["link"] = link or ""}
  129. end
  130.  
  131. local function getPathContent(path)
  132.     local place, name, ending, realName = "", path, "", path
  133.  
  134.     local s1 = string.reverse(path)
  135.     local p1 = string.find(s1, "/")
  136.     if (p1 ~= nil) then
  137.         place = string.reverse(string.sub(s1, p1+1))
  138.         name = string.reverse(string.sub(s1, 1, p1-1))
  139.     end
  140.  
  141.     name = name..""
  142.  
  143.     local p2 = string.find(name, "%.")
  144.     if (p2 ~= nil) then
  145.         ending = string.sub(name, p2+1)
  146.         realName = string.sub(name, 1, p2-1)
  147.     end
  148.  
  149.     return place, name, ending, realName
  150. end
  151.  
  152.  
  153. local function intInner(CONTENT, USERNAME, DNS, SURFTYPE, STANDARTTEXT, STANDARTBG)
  154.     local content = CONTENT.."<>"
  155.     local chars = {
  156.         ["opened"] = {},
  157.         ["closed"] = {}
  158.     }
  159.  
  160.     for i=1, #content do
  161.         if (string.sub(content, i, i) == "<") then
  162.             chars.opened[#chars.opened+1] = i
  163.         elseif (string.sub(content, i, i) == ">") then
  164.             chars.closed[#chars.closed+1] = i
  165.         end
  166.     end
  167.  
  168.     local result = {{["text"] = content, ["tcolor"] = STANDARTTEXT, ["bcolor"] = STANDARTBG}}
  169.  
  170.     if (#chars.opened == #chars.closed) then
  171.         local tags = {}
  172.  
  173.         for i=1, #chars.opened do
  174.             tags[#tags+1] = {
  175.                 ["tag"] = string.sub(content, chars.opened[i]+1, chars.closed[i]-1).."",
  176.                 ["pos1"] = chars.opened[i],
  177.                 ["pos2"] = chars.closed[i]
  178.             }
  179.         end
  180.  
  181.         local newContent = {}
  182.         for i=1, #tags do
  183.             local index = 0
  184.             if (tags[i - 1] ~= nil) then
  185.                 index = tags[i - 1].pos2 or 0
  186.             end
  187.  
  188.             newContent[#newContent+1] = {
  189.                 ["text"] = string.sub(content, index + 1, tags[i].pos1 - 1).."",
  190.                 ["tcolor"] = STANDARTTEXT,
  191.                 ["bcolor"] = STANDARTBG
  192.             }
  193.  
  194.             newContent[#newContent+1] = getCommand(tags[i].tag, STANDARTTEXT, STANDARTBG)
  195.         end
  196.  
  197.         if (#tags ~= 0) then
  198.             result = newContent
  199.         end
  200.     end
  201.  
  202.     return result
  203. end
  204.  
  205. function int(CONTENT, DOCTYPE, USERNAME, DNS, SURFTYPE, STANDARTTEXT, STANDARTBG)
  206.     tcolor(STANDARTTEXT)
  207.     bcolor(STANDARTBG)
  208.     clear()
  209.     cpos(1, 1)
  210.  
  211.     local returnObject = {
  212.         ["link"] = {}
  213.     }
  214.  
  215.     if (DOCTYPE == "stp") then
  216.         local result = intInner(CONTENT, USERNAME, DNS, SURFTYPE, STANDARTTEXT, STANDARTBG)
  217.         for i=1,#result do
  218.             local r = result[i]
  219.             tcolor(r.tcolor)
  220.             bcolor(r.bcolor)
  221.             local x, y = term.getCursorPos()
  222.             write(r.text)
  223.  
  224.             if (r.link ~= "") then
  225.                 table.insert(returnObject.link, {
  226.                     ["x"] = x,
  227.                     ["y"] = y,
  228.                     ["width"] = #r.text,
  229.                     ["target"] = r.link
  230.                 })
  231.             end
  232.         end
  233.     elseif (DOCTYPE == "img") then
  234.         local temp = "SJNOS/users/"..USERNAME.."/config/.temp"
  235.         local f = fs.open(temp, "w")
  236.         f.write(CONTENT)
  237.         f.close()
  238.  
  239.         local image = paintutils.loadImage(temp)
  240.  
  241.         local f = fs.open(temp, "w")
  242.         f.close()
  243.  
  244.         paintutils.drawImage(image, 1, 1)
  245.     else
  246.         write(CONTENT)
  247.     end
  248.  
  249.     return true, returnObject
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement