Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local color = {
- ["WHITE"] = colors.white,
- ["ORANGE"] = colors.orange,
- ["MAGENTA"] = colors.magenta,
- ["LIGHTBLUE"] = colors.lightBlue,
- ["YELLOW"] = colors.yellow,
- ["LIME"] = colors.lime,
- ["PINK"] = colors.pink,
- ["GRAY"] = colors.gray, -- gray
- ["GREY"] = colors.gray, -- grey
- ["LIGHTGRAY"] = colors.lightGray, --gray
- ["LIGHTGREY"] = colors.lightGray, --grey
- ["CYAN"] = colors.cyan,
- ["PURPLE"] = colors.purple,
- ["BLUE"] = colors.blue,
- ["BROWN"] = colors.brown,
- ["GREEN"] = colors.green,
- ["RED"] = colors.red,
- ["BLACK"] = colors.black
- }
- local tags = {
- ["closing"] = { -- anything with a closing tag
- ["C"] = function(arg)
- --go through colors here
- if color[arg] then
- term.setTextColor(color[arg])
- end
- for k,v in pairs(content) do
- if type(v) == "string" then
- term.write(v)
- elseif type(v) == "table" then -- it is another tag
- tags[k:match("%d*(%w+)")](content["args"]) -- all of the args are stored in the args index for all tags
- end
- end -- tags close right after this
- table.remove(buffer.color,#buffer.color) -- removing the currently used color from the buffer
- end,
- ["HL"] = function(arg)
- --same colors
- end,
- ["P"] = function(args,content)
- for k,v in pairs(content) do
- if type(v) == "string" then
- wrap(v)
- elseif type(v) == "table" then
- tags[k:match("%d*(%w+)")](content["args"])
- end
- end
- end,
- ["CELL"] = function(args,content)
- --makes a [P] but wrap X chars from where the tag started
- --int between 1 and 50 (50 being the width of the window)
- for k,v in pairs(content) do
- if type(v) == "string" then
- wrap(v,args)
- elseif type(v) == "table" then
- tags[k:match("%d*(%w+)")](content["args"])
- end
- end
- end,
- ["CENTER"] = function(args,content)
- for k,v in pairs(content) do
- if type(v) == "string" then
- local x,y = term.getSize()
- local cx, cy = term.getCursorPos()
- term.setCursorPos(x-(#v/2),cy)
- end
- end
- end,
- ["A"] = function(args,content)
- --link. first word is the URL
- for k,v in pairs(content) do
- --loop through all the contents of the open and close tag
- end
- end,
- ["NFP"] = function(args,content)
- --draw a paintutils image
- end,
- ["NFT"] = function(args,content)
- --draw a nitropaint image
- end,
- },
- ["BG"] = function(arg)
- --same as [CLEAR] but actually put in a background color
- end,
- ["BR"] = function()
- --linebreak
- local x,y = term.getCursorPos()
- term.setCursorPos(1,y+1)
- end,
- ["CR"] = function()
- --carraige return
- end,
- ["LF"] = function()
- --move cursor down one (linefeed)
- end,
- ["UP"] = function()
- --move cursor up one
- end,
- ["SKIP"] = function(arg)
- --move right X places
- end,
- ["BS"] = function(arg)
- --move left X places
- end,
- ["TOP"] = function()
- --move cursor to top left corner of page
- end,
- ["CLEAR"] = function()
- --disregard everything before this tag
- end,
- ["END"] = function()
- --disregard everything after this tag
- end,
- ["NSFW"] = function()
- --throw an error if the NSFW filter is on
- end,
- ["ERROR"] = function()
- --this is an error page, make the title red
- end,
- ["HR"] = function()
- --draw a horizontal bar
- end,
- ["REFRESH"] = function(arg)
- --refresh the page in X seconds
- end,
- ["COLORLINKS"] = function(arg)
- --true or false - if true (default) [A] will draw in cyan text
- end,
- --afterwards, in order by importance - lists, scripts, SKCH/NFA images, big letters, forms, tables
- }
- setmetatable(tags,{
- __call = function(self,tag,arg)
- arg = ":"..arg or ""
- if self[tag] ~= nil or self.closing[tag] ~= nil then
- return true
- else
- return "["..tag..arg.."]"
- end
- end})
- local function parser(siteContents)
- siteContents = siteContents:sub(1,siteContents:find("%[END%]"))
- siteContents = siteContents:gsub("%[ID%]",os.getComputerID())
- siteContents = siteContents:gsub("%[PROBLEM%]","make this a function later")
- siteContents = siteContents:gsub("%[VERSION%]","kristscape version string")
- local siteData = {}
- local curPath = ""
- local function getfield (f)
- local v = siteData -- start with the table of globals
- if f ~= "" then
- for w in string.gmatch(f, "[%w_]+") do
- w = tonumber(w) or w
- v = v[w]
- end
- end
- if type(v) == "nil" then
- print(f)
- print(textutils.serialize(siteData))
- end
- return v
- end
- local function setfield (f,v)
- local t = siteData -- start with the table of globals
- for w, d in string.gmatch(f, "([%w_]+)(.?)") do
- w = tonumber(w) or w
- if d == "." then -- not last field?
- t[w] = t[w] or {} -- create table if absent
- t = t[w] -- get the table
- else
- if type(t[#t]) == "string" and type(v) == "string" then
- t[#t] = t[#t] .. v
- else
- t[w] = v -- do the assignment
- end
- end
- end
- end
- local function processBlock(str)
- local content = {}
- repeat
- local bx,ex = str:find("%[[^ %s]+%]")
- if bx==nil then
- table.insert(content,str)
- str = ""
- else
- local tag = str:sub(bx,ex)
- local argLoc = tag:find(":") or #tag
- local t = tag:sub(2,argLoc-1)
- local a = tag:sub(argLoc+1, #tag-1)
- if type(tags.closing[t]) ~= "nil" then
- table.insert(content,{tag = t,arg = a,contents = {}})
- str = str:sub(ex+1,#str)
- elseif type(tags[t]) ~= "nil" then
- table.insert(content,{tag = t, arg = a})
- str = str:sub(ex+1, #str)
- elseif type(tags.closing[t:sub(2,#t)]) ~= "nil" then
- table.insert(content,{tag = t})
- str = str:sub(ex+1, #str)
- else
- if type(content[#content]) =="string" then
- content[#content] = content[#content]..str:sub(1,ex)
- str = str:sub(ex+1,#str)
- else
- table.insert(content,str:sub(1,ex))
- str = str:sub(ex+1,#str)
- end
- end
- end
- until #str == 0
- return content
- end
- local function tokenise(...)
- local sLine = table.concat({...}," ")
- local tWords = {}
- local bQuoted = false
- for match in string.gmatch(sLine .. "\"", "(.-)\"") do
- if bquoted == false then
- table.insert( tWords, match )
- else
- for m in string.gmatch( match, "[^ \t]+%s*" ) do
- table.insert( tWords, m )
- end
- end
- end
- return tWords
- end
- for _,block in pairs(tokenise(siteContents)) do
- local contents = processBlock(block)
- for i = 1, #contents do
- local curFile = getfield(curPath)
- if type(contents[i]) == "string" then -- its a word not a tag
- if type(curFile[#curFile]) == "string" then
- setfield( curPath.."."..#curFile , contents[i] )
- elseif type(curFile[#curFile]) == "table" then
- setfield( curPath.."."..(#curFile+1) , contents[i] )
- elseif #curFile == 0 then
- setfield( curPath.."."..1 , contents[i] )
- end
- elseif type(contents[i]) == "table" then -- its a tag
- if contents[i].tag:sub(1,1)=="/" then
- local str = curPath:sub(1,-10)
- curPath = str:sub(1,-(#str:gsub("%w+%.",""))+2)
- elseif type(tags[contents[i].tag]) ~= "nil" then
- setfield( curPath.."."..(#curFile+1) , contents[i])
- elseif type(tags.closing[contents[i].tag]) ~= "nil" then
- curPath = curPath.."."..#curFile+1
- setfield( curPath , contents[i] )
- curPath = curPath..".".."contents"
- end
- else
- error("Invalid data type during parsing: " .. type(contents[i]))
- end
- end
- end
- return siteData
- end
- local function assemble(tbl)
- for i = 1, #tbl do
- if type(tbl[i]) == "string" then
- term.write(tbl[i])
- elseif type(tbl[i]) == "table" then
- tags[tbl[i].tag](tbl[i].arg,tbl[i].contents)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement