Advertisement
justync7

kristscape

Dec 21st, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 19.02 KB | None | 0 0
  1. -- BEGIN kst/parsers/ksml/coal
  2. local VERSION = "COAL"
  3.  
  4. local color = {
  5.     ["WHITE"] = colors.white,
  6.     ["ORANGE"] = colors.orange,
  7.     ["MAGENTA"] = colors.magenta,
  8.     ["LIGHTBLUE"] = colors.lightBlue,
  9.     ["YELLOW"] = colors.yellow,
  10.     ["LIME"] = colors.lime,
  11.     ["PINK"] = colors.pink,
  12.     ["GRAY"] = colors.gray, -- gray
  13.     ["GREY"] = colors.gray, -- grey
  14.     ["LIGHTGRAY"] = colors.lightGray, --gray
  15.     ["LIGHTGREY"] = colors.lightGray, --grey
  16.     ["CYAN"] = colors.cyan,
  17.     ["PURPLE"] = colors.purple,
  18.     ["BLUE"] = colors.blue,
  19.     ["BROWN"] = colors.brown,
  20.     ["GREEN"] = colors.green,
  21.     ["RED"] = colors.red,
  22.     ["BLACK"] = colors.black
  23. }
  24.  
  25. local function wrap(word,width) -- width might not be provided
  26.     local lines = {""}
  27.     width = arg or term.getSize()~= 51 and term.getSize() or 50
  28.     xmin = term.getCursorPos()
  29.     xmax = xmin+width
  30.     for wordspace in word:gmatch("%w+%s+") do
  31.         if #(lines[#lines]..wordspace)>width then
  32.             lines[#lines+1] = wordspace
  33.         else
  34.             lines[#lines] = lines[#lines]..wordspace
  35.         end
  36.     end
  37.     for k,v in pairs(lines) do
  38.         print(v)
  39.         local _,y = term.getCursorPos()
  40.         term.setCursorPos(xmin,y)
  41.     end
  42. end
  43.  
  44. if not oldSetTColor then
  45.     oldSetTColor = term.setTextColor
  46. end
  47. if not oldSetBColor then
  48.     oldSetBColor = term.setBackgroundColor
  49. end
  50.  
  51. local tColor = {}
  52. local bColor = {}
  53.  
  54. _G.term.setTextColor = function(color)
  55.     if colors[color] or type(color) == "number" then
  56.         table.insert(tColor,color)
  57.     end
  58.     return oldSetTColor(color)
  59. end
  60.  
  61. _G.term.getTextColor = function()
  62.     return tColor[#tColor] or colors.black
  63. end
  64.  
  65. _G.term.setBackgroundColor = function(color)
  66.     if colors[color] or type(color) == "number" then
  67.         table.insert(bColor,color)
  68.     end
  69.     return oldSetBColor
  70. end
  71.  
  72. _G.term.getBackgroundColor = function()
  73.     return bColor[#bColor] or colors.black
  74. end
  75.  
  76. tags = {
  77.     ["closing"] = { -- anything with a closing tag
  78.         ["C"] = function(arg,content)
  79.             --go through colors here
  80.             local oldColor = term.getTextColor()
  81.             if color[arg] then
  82.                 term.setTextColor(color[arg])
  83.             end
  84.             for i=1, #content do
  85.                 if type(content[i]) == "string" then
  86.                     term.write(content[i])
  87.                 elseif type(content[i]) == "table" then -- it is another tag
  88.                     if tags[content[i].tag] ~= nil then
  89.                         tags[content[i].tag](content[i].arg)
  90.                     elseif tags.closing[content[i].tag] ~= nil then
  91.                         tags.closing[content[i].tag](content[i].arg, content[i].contents)
  92.                     end
  93.                 end
  94.             end
  95.             term.setTextColor(oldColor)
  96.             --table.remove(buffer.color,#buffer.color) -- removing the currently used color from the buffer
  97.         end,
  98.         ["HL"] = function(arg,content) -- Maybe Word wrapping should go here? see [P] k
  99.             local ccolor = term.getBackgroundColor()
  100.             term.setBackgroundColor(colors.arg)
  101.             term.write(content)
  102.             term.setBackgroundColor(ccolor)
  103.         end,
  104.         ["TITLE"] = function(arg,content)
  105.             --append the content to the page title
  106.             --local pTitle = --todo --this should be declared in the gui
  107.         end,
  108.         ["TAB"] = function(arg,content)
  109.             --display this in the tab text, but not the title bar
  110.         end,
  111.         ["NOTAB"] = function(arg,content)
  112.             --display this in the title bar, but not the tab text
  113.         end,
  114.         ["P"] = function(args,content)
  115.             for i=1, #content do
  116.                 if type(content[i]) == "string" then
  117.                     wrap(content[i],args)
  118.                 elseif type(content[i]) == "table" then -- it is another tag
  119.                     if tags[content[i].tag] ~= nil then
  120.                         tags[content[i].tag](content[i].arg)
  121.                     elseif tags.closing[content[i]] ~= nil then
  122.                         tags.closing[content[i].tag](content[i].arg, content[i].contents)
  123.                     end
  124.                 end
  125.             end
  126.         end,
  127.         ["CELL"] = function(args,content)
  128.             --makes a [P] but wrap X chars from where the tag started
  129.             --int between 1 and 50 (50 being the width of the window)
  130.             local xArg,yArg = args:match("(%d+),(%d+)")
  131.             if tonumber(x) == nil or tonumber(y) == nil then
  132.                 return
  133.             end
  134.             local x,y = term.getCursorPos()
  135.             local cellWindow = window.create(term.current(),x,y,xArg,yArg)
  136.             local oldCur = term.current()
  137.             term.redirect(cellWindow)
  138.             for i=1, #content do
  139.                 if type(content[i]) == "string" then
  140.                     wrap(content[i],xArg)
  141.                 elseif type(content[i]) == "table" then -- it is another tag
  142.                     if tags[content[i].tag] ~= nil then
  143.                         tags[content[i].tag](content[i].arg)
  144.                     elseif tags.closing[content[i]] ~= nil then
  145.                         tags.closing[content[i].tag](content[i].arg, content[i].contents)
  146.                     end
  147.                 end
  148.             end
  149.             term.redirect(term.current)
  150.         end,
  151.         ["CENTER"] = function(args,content)
  152.             local pos = {term.getCursorPos()}
  153.             if pos[1] ~= 1 then
  154.                 term.setCursorPos(1,pos[2]+1)
  155.             end
  156.             for i=1, #content do
  157.                 if type(content[i]) == "string" then
  158.                     local x,y = term.getSize()
  159.                     local cy = pos[2]
  160.                     term.setCursorPos(x/2-(#content[#content]/2),cy)
  161.                     print(content[i])
  162.                 elseif type(content[i]) == "table" then -- it is another tag
  163.                     if tags[content[i].tag] ~= nil then
  164.                         tags[content[i].tag](content[i].arg)
  165.                     elseif tags.closing[content[i]] ~= nil then
  166.                         tags.closing[content[i].tag](content[i].arg, content[i].contents)
  167.                     end
  168.                 end
  169.             end
  170.         end,
  171.         ["A"] = function(args,content)
  172.             --link. argument is .kst URL
  173.             -- need to register a button by getting starting 2 positions, and ending 2
  174.             for i = 1, #content do
  175.                 if type(content[i]) == "string" then
  176.                     term.write(content[i],args)
  177.                 elseif type(content[i]) == "table" then -- it is another tag
  178.                     if tags[content[i].tag] ~= nil then
  179.                         tags[content[i].tag](content[i].arg)
  180.                     elseif tags.closing[content[i]] ~= nil then
  181.                         tags.closing[content[i].tag](content[i].arg, content[i].contents)
  182.                     end
  183.                 end
  184.             end
  185.         end,
  186.         ["D"] = function(args,content)
  187.             --downloads file at URL to computer, otherwise same as [A]
  188.         end,
  189.         ["NFP"] = function(args,content)
  190.             --don't use the paintutils api either - we need to measure its length
  191.             --draw a paintutils image
  192.         end,
  193.         ["NFT"] = function(args,content)
  194.             --draw a nitropaint image
  195.         end,
  196.         ["SKCH"] = function(arg,content)
  197.             --draw an image in oeed's format
  198.             --by no means important for release
  199.         end,
  200.         ["SCRIPT"] = function(arg,content)
  201.            -- sandboxed lua, but our own functions that we parse
  202.            --by no means important for release
  203.         end,
  204.         ["LIST"] = function(arg,content)
  205.            -- going to have to put special stuff in parser for this
  206.            -- alternatively we could just make one for list items
  207.            -- this could handle things nested in it differently, which would work
  208.            --by no means important for release
  209.         end,
  210.         ["H"] = function(arg,content)
  211.            -- write in block letters (we can load NFP images for this)
  212.            --by no means important for release
  213.         end,
  214.         ["SET"] = function(arg,content)
  215.            --sets cookie "arg" equal to "content"
  216.            --by no means important for release
  217.         end
  218.     },
  219.     ["EAT"] = function(arg)
  220.         --deletes cookie "arg"
  221.     end,
  222.     ["FEAST"] = function()
  223.         --deletes all cookies set by the site
  224.     end,
  225.     ["BG"] = function(arg)
  226.         term.setBackgroundColor(color[arg])
  227.     end,
  228.     ["BR"] = function()
  229.         --linebreak
  230.         local x,y = term.getCursorPos()
  231.         term.setCursorPos(1,y+1)
  232.     end,
  233.     ["LN"] = function()
  234.         --linebreak, but only if the cursor is not at the beginning
  235.         local x,y = term.getCursorPos()
  236.         if x ~= 1 then
  237.             term.setCursorPos(1,y+1)
  238.         end
  239.     end,
  240.     ["CR"] = function()
  241.         --carraige return - move cursor x to the first spot in the container
  242.         local x,y = term.getCursorPos()
  243.         term.setCursorPos(1,y)
  244.     end,
  245.     ["LF"] = function()
  246.         --move cursor down one (linefeed)
  247.         local x,y = term.getCursorPos()
  248.         term.setCursorPos(x,y+1)
  249.     end,
  250.     ["UP"] = function()
  251.         --move cursor up one
  252.         local x,y = term.getCursorPos()
  253.         term.setCursorPos(x,y-1)
  254.     end,
  255.     ["SKIP"] = function(args)
  256.         --move right X places
  257.         local pos = {term.getCursorPos()}
  258.         arg = tonumber(args) or 0
  259.         term.setCursorPos(pos[1]+arg,pos[2])
  260.     end,
  261.     ["BS"] = function(args)
  262.         --move left X places
  263.         local pos = {term.getCursorPos()}
  264.         arg = tonumber(args) or 0
  265.         term.setCursorPos(pos[1]+arg,pos[2])
  266.     end,
  267.     ["TOP"] = function(args)
  268.         --move cursor to top left corner of page or cell
  269.         term.setCursorPos(1,1)
  270.     end,
  271.     ["CLEAR"] = function(args)
  272.         --disregard everything before the last instance of this tag - actually don't, because containers
  273.         term.clear()
  274.     end,
  275.     ["END"] = function(args)
  276.         --disregard everything after the first instance of this tag
  277.         --this function technically should never be reached
  278.         return "doneloading"
  279.     end,
  280.     ["NSFW"] = function(args)
  281.         --throw an error if the NSFW filter is on
  282.         --this variable should be defined in the gui, not here
  283.         local nsfw = true
  284.     end,
  285.     ["HR"] = function(args)
  286.         --draw a horizontal bar
  287.         --a bunch of gray dashes that are as wide as the container
  288.         local pos = {term.getCursorPos()}
  289.         if pos[1] ~= 1 then
  290.             term.setCursorPos(1,pos[2]+1)
  291.         end
  292.         local ccolor = {term.getTextColor()}
  293.         term.setTextColor(args)
  294.         for i = 1,(pos[1]-1)-pos[1] do
  295.             term.write("-")
  296.         end
  297.         term.setTextColor(ccolor)
  298.     end,
  299.     ["REFRESH"] = function(arg)
  300.         --refresh the page in X seconds. no faster than 2? seconds
  301.     end,
  302.     ["REDIRECT"] = function(arg)
  303.         --same as [A] but go directly to the specified page
  304.     end,
  305.     --afterwards, in order by importance - lists, scripts, SKCH/NFA images, big letters, forms, tables
  306. }
  307. setmetatable(tags,{
  308.     __call = function(self,tag,arg)
  309.         arg = ":"..arg or ""
  310.         if self[tag] ~= nil or self.closing[tag]  ~= nil then
  311.             return true
  312.         else
  313.             return "["..tag..arg.."]"
  314.         end
  315.     end})
  316. local function prerequisition(siteContents)
  317.     -- [END]
  318.     -- This might leave a [ on the page. I can't remember if we fixed that
  319.     siteContents = siteContents:sub(1,siteContents:find("%[END%]")-1)
  320.     -- [ID]
  321.     siteContents = siteContents:gsub("%[ID%]",os.getComputerID())
  322.     -- [HOSTNAME] - get the server's name from the DNS
  323.     -- [CHECK:arg] - replace with contents of cookie "arg"
  324.     -- [CLOSE] - just don't parse, close the tab
  325. end
  326. local function parser(siteContents)
  327.     siteContents = prerequisition(siteContents)
  328.     local siteData = {}
  329.     local curPath = ""
  330.        
  331.     local function getfield (f)
  332.         local v = siteData    -- start with the table of globals
  333.         if f ~= "" then
  334.             for w in string.gmatch(f, "[%w_]+") do
  335.                 w = tonumber(w) or w
  336.                 v = v[w]
  337.             end
  338.         end
  339.         if type(v) == "nil" then
  340.           print(f)
  341.           print(textutils.serialize(siteData))
  342.         end
  343.         return v
  344.     end
  345.  
  346.     local function setfield (f,v)
  347.         local t = siteData           -- start with the table of globals
  348.             for w, d in string.gmatch(f, "([%w_]+)(.?)") do
  349.               w = tonumber(w) or w
  350.                 if d == "." then      -- not last field?
  351.                     t[w] = t[w] or {}   -- create table if absent
  352.                     t = t[w]            -- get the table
  353.                 else        
  354.                 if type(t[#t]) == "string" and type(v) == "string" then
  355.                     t[#t] = t[#t] .. v
  356.                 else
  357.                     t[w] = v            -- do the assignment
  358.                 end
  359.             end
  360.         end
  361.     end
  362.    
  363.     local function processBlock(str)
  364.         local content = {}
  365.         repeat
  366.             local bx,ex = str:find("%[[^ %s%]]+%]")
  367.             if bx==nil then
  368.                 table.insert(content,str)
  369.                 str = ""
  370.             else
  371.                 local tag = str:sub(bx,ex)
  372.                 local argLoc = tag:find(":") or #tag
  373.                 local t = tag:sub(2,argLoc-1)
  374.                 local a = tag:sub(argLoc+1, #tag-1)
  375.                 if type(tags.closing[t]) ~= "nil" then
  376.                     table.insert(content,str:sub(1,bx-1))
  377.                     table.insert(content,{tag = t,arg = a,contents = {}})
  378.                     str = str:sub(ex+1,#str)
  379.                 elseif type(tags[t]) ~= "nil" then
  380.                     table.insert(content,str:sub(1,bx-1))
  381.                     table.insert(content,{tag = t, arg = a})
  382.                     str = str:sub(ex+1, #str)
  383.                 elseif type(tags.closing[t:sub(2,#t)]) ~= "nil" then
  384.                     table.insert(content,str:sub(1,bx-1))
  385.                     table.insert(content,{tag = t})
  386.                     str = str:sub(ex+1, #str)
  387.                 else
  388.                     if type(content[#content]) =="string" then
  389.                         content[#content] = content[#content]..str:sub(1,ex)
  390.                         str = str:sub(ex+1,#str)
  391.                     else
  392.                         table.insert(content,str:sub(1,ex))
  393.                         str = str:sub(ex+1,#str)
  394.                     end
  395.                 end
  396.             end
  397.         until #str == 0
  398.         return content
  399.     end    
  400.    
  401.     local function tokenise(...)
  402.         local sLine = table.concat({...}," ")
  403.         local tWords = {}
  404.         local bQuoted = false
  405.         for match in string.gmatch(sLine .. "\"", "(.-)\"") do
  406.             if bquoted == false then
  407.                 table.insert( tWords, match )
  408.             else
  409.                 for m in string.gmatch( match, "[^ \t]+%s*" ) do
  410.                     table.insert( tWords, m )
  411.                 end
  412.             end
  413.         end
  414.         return tWords
  415.     end
  416.    
  417.     for _,block in pairs(tokenise(siteContents)) do
  418.         local contents = processBlock(block)
  419.         for i = 1, #contents do
  420.             local curFile = getfield(curPath)
  421.             if type(contents[i]) == "string" then -- its a word not a tag
  422.                 if type(curFile[#curFile]) == "string" then
  423.                     setfield( curPath.."."..#curFile , contents[i] )
  424.                 elseif type(curFile[#curFile]) == "table" then
  425.                     setfield( curPath.."."..(#curFile+1) , contents[i] )
  426.                 elseif #curFile == 0 then
  427.                     setfield( curPath.."."..1 , contents[i] )
  428.                 end
  429.                
  430.             elseif type(contents[i]) == "table" then -- its a tag
  431.                 if contents[i].tag:sub(1,1)=="/" then
  432.                     local str = curPath:sub(1,-10)
  433.                     curPath = str:sub(1,-(#str:gsub("%w+%.",""))+2)
  434.                 elseif type(tags[contents[i].tag]) ~= "nil" then
  435.                     setfield( curPath.."."..(#curFile+1) , contents[i])
  436.                 elseif type(tags.closing[contents[i].tag]) ~= "nil" then
  437.                     curPath = curPath.."."..#curFile+1
  438.                     setfield( curPath , contents[i] )
  439.                     curPath = curPath..".".."contents"
  440.                 end
  441.             else
  442.                 error("Invalid data type during parsing: " .. type(contents[i]))
  443.             end
  444.         end
  445.     end
  446.     return siteData
  447. end
  448.  
  449. local function assemble(tbl)
  450.     for i = 1, #tbl do
  451.         if type(tbl[i]) == "string" then
  452.             term.write(tbl[i])
  453.         elseif type(tbl[i]) == "table" then
  454.             if tags[tbl[i].tag] then
  455.                 tags[tbl[i].tag](tbl[i].arg)
  456.             elseif tags.closing[tbl[i].tag] then
  457.                 tags.closing[tbl[i].tag](tbl[i].arg,tbl[i].contents)
  458.             end
  459.         end
  460.     end
  461. end
  462. -- END kst/parsers/ksml/coal
  463. -- BEGIN kristscape
  464.  
  465. local headers = {}
  466. local function readurl(url)
  467.     local addressbar = url
  468.     if addressbar:find("://") == nil then
  469.         addressbar = "http://" .. addressbar
  470.     end
  471.     addressbar = addressbar:gsub(".com",".kst")
  472.     addressbar = addressbar:gsub(".org",".kst")
  473.     addressbar = addressbar:gsub(".net",".kst")
  474.    
  475.     local protocol = string.sub(addressbar,1,string.find(addressbar,"://")-1)
  476.     local name = ""
  477.     if protocol == "http" then
  478.         name = string.sub(url,8,string.find(url,".kst")-1)
  479.         name = http.get("http://krist.ceriat.net/?a="..name,nil,headers).readAll()
  480.     elseif protocol == "rdnt"
  481.        
  482.     elseif protocol == "knet"
  483.        
  484.     elseif protocol == "file"
  485.        
  486.     elseif protocol == "scape"
  487.         name = string.sub(url,string.find(url,"://")+3,string.find(url,"/"-1))
  488.     end
  489.     url = url:sub(url:find("://"))
  490.     local destination = url:sub(url:find("/")+1)
  491.    
  492.    
  493.     return addressbar, protocol, destination
  494. end
  495. --END kristscape
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516. --[[To Do list
  517. 1: Finish Tags
  518. 2: Refresh
  519. 3: Redirect
  520. 4: Cells
  521. 5: Bugfixes
  522. 6: Donuts
  523. ]]--
  524. --[[Tags no longer in use
  525.     ["COLORLINKS"] = function(arg)
  526.         --true or false - if true (default) [A] will draw in cyan text
  527.     end,
  528.     Reason no longer in use:actually don't do this one. we can just put a [C] tag in the link's text, and have [A] add cyan to the color table
  529. ]]--
  530. --[[ New parser example
  531.  
  532. {    --start of the site Contents
  533.     { -- opening of a new tag
  534.         tag = "C",
  535.         arg = "arg",
  536.         contents = { -- what is between the opening and closing tag
  537.              "Hello my name is text",
  538.              { -- opening of a tag
  539.                  tag = "BG",
  540.                  arg = "RED",
  541.                  contents = { -- only closing tags will have this
  542.                       "More Txt"  
  543.                  }    
  544.              },
  545.              "Some more text"  
  546.         }    --closing of a tag
  547.     }   --closing of the tag
  548. }
  549.    
  550. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement