Advertisement
Guest User

Untitled

a guest
May 26th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. local color = {
  2. ["WHITE"] = colors.white,
  3. ["ORANGE"] = colors.orange,
  4. ["MAGENTA"] = colors.magenta,
  5. ["LIGHTBLUE"] = colors.lightBlue,
  6. ["YELLOW"] = colors.yellow,
  7. ["LIME"] = colors.lime,
  8. ["PINK"] = colors.pink,
  9. ["GRAY"] = colors.gray, -- gray
  10. ["GREY"] = colors.gray, -- grey
  11. ["LIGHTGRAY"] = colors.lightGray, --gray
  12. ["LIGHTGREY"] = colors.lightGray, --grey
  13. ["CYAN"] = colors.cyan,
  14. ["PURPLE"] = colors.purple,
  15. ["BLUE"] = colors.blue,
  16. ["BROWN"] = colors.brown,
  17. ["GREEN"] = colors.green,
  18. ["RED"] = colors.red,
  19. ["BLACK"] = colors.black
  20. }
  21.  
  22. local tags = {
  23. ["closing"] = { -- anything with a closing tag
  24. ["C"] = function(arg)
  25. --go through colors here
  26. if color[arg] then
  27. term.setTextColor(color[arg])
  28. end
  29. for k,v in pairs(content) do
  30. if type(v) == "string" then
  31. term.write(v)
  32. elseif type(v) == "table" then -- it is another tag
  33. tags[k:match("%d*(%w+)")](content["args"]) -- all of the args are stored in the args index for all tags
  34. end
  35. end -- tags close right after this
  36. table.remove(buffer.color,#buffer.color) -- removing the currently used color from the buffer
  37. end,
  38. ["HL"] = function(arg)
  39. --same colors
  40. end,
  41. ["P"] = function(args,content)
  42. for k,v in pairs(content) do
  43. if type(v) == "string" then
  44. wrap(v)
  45. elseif type(v) == "table" then
  46. tags[k:match("%d*(%w+)")](content["args"])
  47. end
  48. end
  49. end,
  50. ["CELL"] = function(args,content)
  51. --makes a [P] but wrap X chars from where the tag started
  52. --int between 1 and 50 (50 being the width of the window)
  53. for k,v in pairs(content) do
  54. if type(v) == "string" then
  55. wrap(v,args)
  56. elseif type(v) == "table" then
  57. tags[k:match("%d*(%w+)")](content["args"])
  58. end
  59. end
  60. end,
  61. ["CENTER"] = function(args,content)
  62. for k,v in pairs(content) do
  63. if type(v) == "string" then
  64. local x,y = term.getSize()
  65. local cx, cy = term.getCursorPos()
  66. term.setCursorPos(x-(#v/2),cy)
  67. end
  68. end
  69. end,
  70. ["A"] = function(args,content)
  71. --link. first word is the URL
  72. for k,v in pairs(content) do
  73. --loop through all the contents of the open and close tag
  74. end
  75. end,
  76. ["NFP"] = function(args,content)
  77. --draw a paintutils image
  78. end,
  79. ["NFT"] = function(args,content)
  80. --draw a nitropaint image
  81. end,
  82. },
  83. ["BG"] = function(arg)
  84.  
  85. --same as [CLEAR] but actually put in a background color
  86. end,
  87. ["BR"] = function()
  88. --linebreak
  89. local x,y = term.getCursorPos()
  90. term.setCursorPos(1,y+1)
  91. end,
  92. ["CR"] = function()
  93. --carraige return
  94. end,
  95. ["LF"] = function()
  96. --move cursor down one (linefeed)
  97. end,
  98. ["UP"] = function()
  99. --move cursor up one
  100. end,
  101. ["SKIP"] = function(arg)
  102. --move right X places
  103. end,
  104. ["BS"] = function(arg)
  105. --move left X places
  106. end,
  107. ["TOP"] = function()
  108. --move cursor to top left corner of page
  109. end,
  110. ["CLEAR"] = function()
  111. --disregard everything before this tag
  112. end,
  113. ["END"] = function()
  114. --disregard everything after this tag
  115. end,
  116. ["NSFW"] = function()
  117. --throw an error if the NSFW filter is on
  118. end,
  119. ["ERROR"] = function()
  120. --this is an error page, make the title red
  121. end,
  122. ["HR"] = function()
  123. --draw a horizontal bar
  124. end,
  125. ["REFRESH"] = function(arg)
  126. --refresh the page in X seconds
  127. end,
  128. ["COLORLINKS"] = function(arg)
  129. --true or false - if true (default) [A] will draw in cyan text
  130. end,
  131. --afterwards, in order by importance - lists, scripts, SKCH/NFA images, big letters, forms, tables
  132. }
  133.  
  134. setmetatable(tags,{
  135. __call = function(self,tag,arg)
  136. arg = ":"..arg or ""
  137. if self[tag] ~= nil or self.closing[tag] ~= nil then
  138. return true
  139. else
  140. return "["..tag..arg.."]"
  141. end
  142. end})
  143.  
  144. local function parser(siteContents)
  145. siteContents = siteContents:sub(1,siteContents:find("%[END%]"))
  146. siteContents = siteContents:gsub("%[ID%]",os.getComputerID())
  147. siteContents = siteContents:gsub("%[PROBLEM%]","make this a function later")
  148. siteContents = siteContents:gsub("%[VERSION%]","kristscape version string")
  149. local siteData = {}
  150. local curPath = ""
  151.  
  152. local function getfield (f)
  153. local v = siteData -- start with the table of globals
  154. if f ~= "" then
  155. for w in string.gmatch(f, "[%w_]+") do
  156. w = tonumber(w) or w
  157. v = v[w]
  158. end
  159. end
  160. if type(v) == "nil" then
  161. print(f)
  162. print(textutils.serialize(siteData))
  163. end
  164. return v
  165. end
  166.  
  167. local function setfield (f,v)
  168. local t = siteData -- start with the table of globals
  169. for w, d in string.gmatch(f, "([%w_]+)(.?)") do
  170. w = tonumber(w) or w
  171. if d == "." then -- not last field?
  172. t[w] = t[w] or {} -- create table if absent
  173. t = t[w] -- get the table
  174. else
  175. if type(t[#t]) == "string" and type(v) == "string" then
  176. t[#t] = t[#t] .. v
  177. else
  178. t[w] = v -- do the assignment
  179. end
  180. end
  181. end
  182. end
  183.  
  184. local function processBlock(str)
  185. local content = {}
  186. repeat
  187. local bx,ex = str:find("%[[^ %s]+%]")
  188. if bx==nil then
  189. table.insert(content,str)
  190. str = ""
  191. else
  192. local tag = str:sub(bx,ex)
  193. local argLoc = tag:find(":") or #tag
  194. local t = tag:sub(2,argLoc-1)
  195. local a = tag:sub(argLoc+1, #tag-1)
  196. if type(tags.closing[t]) ~= "nil" then
  197. table.insert(content,{tag = t,arg = a,contents = {}})
  198. str = str:sub(ex+1,#str)
  199. elseif type(tags[t]) ~= "nil" then
  200. table.insert(content,{tag = t, arg = a})
  201. str = str:sub(ex+1, #str)
  202. elseif type(tags.closing[t:sub(2,#t)]) ~= "nil" then
  203. table.insert(content,{tag = t})
  204. str = str:sub(ex+1, #str)
  205. else
  206. if type(content[#content]) =="string" then
  207. content[#content] = content[#content]..str:sub(1,ex)
  208. str = str:sub(ex+1,#str)
  209. else
  210. table.insert(content,str:sub(1,ex))
  211. str = str:sub(ex+1,#str)
  212. end
  213. end
  214. end
  215. until #str == 0
  216. return content
  217. end
  218.  
  219. local function tokenise(...)
  220. local sLine = table.concat({...}," ")
  221. local tWords = {}
  222. local bQuoted = false
  223. for match in string.gmatch(sLine .. "\"", "(.-)\"") do
  224. if bquoted == false then
  225. table.insert( tWords, match )
  226. else
  227. for m in string.gmatch( match, "[^ \t]+%s*" ) do
  228. table.insert( tWords, m )
  229. end
  230. end
  231. end
  232. return tWords
  233. end
  234.  
  235. for _,block in pairs(tokenise(siteContents)) do
  236. local contents = processBlock(block)
  237. for i = 1, #contents do
  238. local curFile = getfield(curPath)
  239.  
  240. if type(contents[i]) == "string" then -- its a word not a tag
  241. if type(curFile[#curFile]) == "string" then
  242. setfield( curPath.."."..#curFile , contents[i] )
  243. elseif type(curFile[#curFile]) == "table" then
  244. setfield( curPath.."."..(#curFile+1) , contents[i] )
  245. elseif #curFile == 0 then
  246. setfield( curPath.."."..1 , contents[i] )
  247. end
  248.  
  249. elseif type(contents[i]) == "table" then -- its a tag
  250. if contents[i].tag:sub(1,1)=="/" then
  251. local str = curPath:sub(1,-10)
  252. curPath = str:sub(1,-(#str:gsub("%w+%.",""))+2)
  253. elseif type(tags[contents[i].tag]) ~= "nil" then
  254. setfield( curPath.."."..(#curFile+1) , contents[i])
  255. elseif type(tags.closing[contents[i].tag]) ~= "nil" then
  256. curPath = curPath.."."..#curFile+1
  257. setfield( curPath , contents[i] )
  258. curPath = curPath..".".."contents"
  259. end
  260. else
  261. error("Invalid data type during parsing: " .. type(contents[i]))
  262. end
  263. end
  264. end
  265. return siteData
  266. end
  267.  
  268. local function assemble(tbl)
  269. for i = 1, #tbl do
  270. if type(tbl[i]) == "string" then
  271. term.write(tbl[i])
  272. elseif type(tbl[i]) == "table" then
  273. tags[tbl[i].tag](tbl[i].arg,tbl[i].contents)
  274. end
  275. end
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement