Advertisement
Guest User

Untitled

a guest
May 27th, 2015
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.33 KB | None | 0 0
  1. local VERSION = "COAL"
  2.  
  3. local color = {
  4. ["WHITE"] = colors.white,
  5. ["ORANGE"] = colors.orange,
  6. ["MAGENTA"] = colors.magenta,
  7. ["LIGHTBLUE"] = colors.lightBlue,
  8. ["YELLOW"] = colors.yellow,
  9. ["LIME"] = colors.lime,
  10. ["PINK"] = colors.pink,
  11. ["GRAY"] = colors.gray, -- gray
  12. ["GREY"] = colors.gray, -- grey
  13. ["LIGHTGRAY"] = colors.lightGray, --gray
  14. ["LIGHTGREY"] = colors.lightGray, --grey
  15. ["CYAN"] = colors.cyan,
  16. ["PURPLE"] = colors.purple,
  17. ["BLUE"] = colors.blue,
  18. ["BROWN"] = colors.brown,
  19. ["GREEN"] = colors.green,
  20. ["RED"] = colors.red,
  21. ["BLACK"] = colors.black
  22. }
  23.  
  24. local function wrap(word,width) -- width might not be provided
  25. local lines = {""}
  26. width = arg or term.getSize()~= 51 and term.getSize() or 50
  27. xmin = term.getCursorPos()
  28. xmax = xmin+width
  29. for wordspace in word:gmatch("%w+%s+") do
  30. if #(lines[#lines]..word)>width then
  31. lines[#lines+1] = word
  32. else
  33. lines[#lines] = lines[#lines]..word
  34. end
  35. end
  36. for k,v in pairs(lines) do
  37. print(v)
  38. local _,y = term.getCursorPos()
  39. term.setCursorPos(xmin,y)
  40. end
  41. end
  42.  
  43. local tags = {
  44. ["closing"] = { -- anything with a closing tag
  45. ["C"] = function(arg,content)
  46. --go through colors here
  47. if color[arg] then
  48. term.setTextColor(color[arg])
  49. end
  50. for i=1, #content do
  51. if type(content[i]) == "string" then
  52. term.write(content[i])
  53. elseif type(content[i]) == "table" then -- it is another tag
  54. if tags[content[i].tag] ~= nil then
  55. tags[content[i].tag](content[i].arg)
  56. elseif tags.closing[content[i]] ~= nil then
  57. tags.closing[content[i].tag](content[i].arg, content[i].content)
  58. end
  59. end
  60. end
  61. table.remove(buffer.color,#buffer.color) -- removing the currently used color from the buffer
  62. end,
  63. ["HL"] = function(arg,content) -- Maybe Word wrapping should go here? see [P] k
  64. local ccolor = term.getBackgroundColor()
  65. term.setBackgroundColor(colors.arg)
  66. term.write(content)
  67. term.setBackgroundColor(ccolor)
  68. end,
  69. ["TITLE"] = function(arg,content)
  70. --append the content to the page title
  71. --local pTitle = --todo
  72. end,
  73. ["TAB"] = function(arg,content)
  74. --display this in the tab text, but not the title bar
  75. end,
  76. ["NOTAB"] = function(arg,content)
  77. --display this in the title bar, but not the tab text
  78. end,
  79. ["P"] = function(args,content)
  80. for i=1, #content do
  81. if type(content[i]) == "string" then
  82. wrap(content[i],args)
  83. elseif type(content[i]) == "table" then -- it is another tag
  84. if tags[content[i].tag] ~= nil then
  85. tags[content[i].tag](content[i].arg)
  86. elseif tags.closing[content[i]] ~= nil then
  87. tags.closing[content[i].tag](content[i].arg, content[i].content)
  88. end
  89. end
  90. end
  91. end,
  92. ["CELL"] = function(args,content)
  93. --makes a [P] but wrap X chars from where the tag started
  94. --int between 1 and 50 (50 being the width of the window)
  95. for i=1, #content do
  96. if type(content[i]) == "string" then
  97. wrap(content[i],args)
  98. elseif type(content[i]) == "table" then -- it is another tag
  99. if tags[content[i].tag] ~= nil then
  100. tags[content[i].tag](content[i].arg)
  101. elseif tags.closing[content[i]] ~= nil then
  102. tags.closing[content[i].tag](content[i].arg, content[i].content)
  103. end
  104. end
  105. end
  106. end,
  107. ["CENTER"] = function(args,content)
  108. for i=1, #content do
  109. if type(content[i]) == "string" then
  110. local x,y = term.getSize()
  111. local cx, cy = term.getCursorPos()
  112. term.setCursorPos(x/2-(#v/2),cy)
  113. print(content[i])
  114. elseif type(content[i]) == "table" then -- it is another tag
  115. if tags[content[i].tag] ~= nil then
  116. tags[content[i].tag](content[i].arg)
  117. elseif tags.closing[content[i]] ~= nil then
  118. tags.closing[content[i].tag](content[i].arg, content[i].content)
  119. end
  120. end
  121. end
  122. end,
  123. ["A"] = function(args,content)
  124. --link. argument is URL
  125. -- need to register a button by getting starting 2 positions, and ending 2
  126. for i = 1, #content do
  127. if type(content[i]) == "string" then
  128. term.write(content[i],args)
  129. elseif type(content[i]) == "table" then -- it is another tag
  130. if tags[content[i].tag] ~= nil then
  131. tags[content[i].tag](content[i].arg)
  132. elseif tags.closing[content[i]] ~= nil then
  133. tags.closing[content[i].tag](content[i].arg, content[i].content)
  134. end
  135. end
  136. end
  137. end,
  138. ["D"] = function(args,content)
  139. --downloads file at URL to computer, otherwise same as [A]
  140. end,
  141. ["NFP"] = function(args,content)
  142. --don't use the paintutils api either - we need to measure it's length
  143. --draw a paintutils image
  144. end,
  145. ["NFT"] = function(args,content)
  146. --draw a nitropaint image
  147. end,
  148. ["SKCH"] = function(arg,content)
  149. --draw an image in oeed's format
  150. end,
  151. ["SCRIPT"] = function(arg,content)
  152. -- sandboxed lua, but our own functions that we parse
  153. end,
  154. ["LIST"] = function(arg,content)
  155. -- going to have to put special stuff in parser for this
  156. -- alternatively we could just make one for list items
  157. -- this could handle things nested in it differently, which would work
  158. end
  159. ["H"] = function(arg,content)
  160. -- write in block letters (we can load NFP images for this)
  161. end
  162. },
  163. ["BG"] = function(arg)
  164.  
  165. --clear the container with the color
  166. end,
  167. ["KSML"] = function(arg)
  168. --specify the version of KSML. this should probably be handled beforehand
  169.  
  170. end,
  171. ["BR"] = function()
  172. --linebreak
  173. local x,y = term.getCursorPos()
  174. term.setCursorPos(1,y+1)
  175. end,
  176. ["CR"] = function()
  177. --carraige return - move cursor x to the first spot in the container
  178. local x,y = term.getCursorPos()
  179. term.setCursorPos(1,y)
  180. end,
  181. ["LF"] = function()
  182. --move cursor down one (linefeed)
  183. local x,y = term.getCursorPos()
  184. term.setCursorPos(x,y+1)
  185. end,
  186. ["UP"] = function()
  187. --move cursor up one
  188. local x,y = term.getCursorPos()
  189. term.setCursorPos(x,y-1)
  190. end,
  191. ["SKIP"] = function(arg)
  192. --I don't know what the standard for poorly written code is, but this is probably it... thanks medders was that sarcasm lol?
  193. --move right X places
  194. local pos = {term.getCursorPos()}
  195. arg = tonumber(arg) or 0
  196. term.setCursorPos(pos[1]+arg,pos[2])
  197. end,
  198. ["BS"] = function(arg)
  199. --move left X places
  200. local pos = {term.getCursorPos()}
  201. arg = tonumber(arg) or 0
  202. term.setCursorPos(pos[1]+arg,pos[2])
  203. end,
  204. ["TOP"] = function()
  205. --move cursor to top left corner of page or cell
  206. term.setCursorPos(1,1)
  207. end,
  208. ["CLEAR"] = function()
  209. --disregard everything before the last instance of this tag
  210. term.clear()
  211. end,
  212. ["END"] = function()
  213. --disregard everything after the first instance of this tag
  214.  
  215. end,
  216. ["NSFW"] = function()
  217. --throw an error if the NSFW filter is on
  218. end,
  219. ["ERROR"] = function()
  220. --this is an error page, make the title red
  221. --in the future, make this work only on scape:// pages
  222. end,
  223. ["HR"] = function()
  224. --draw a horizontal bar
  225. --a bunch of gray dashes that are as wide as the container
  226. end,
  227. ["REFRESH"] = function(arg)
  228. --refresh the page in X seconds. no faster than 5? seconds
  229. end,
  230. ["REDIRECT"] = function(arg)
  231. --same as [A] but go directly to the specified page
  232. end,
  233. ["COLORLINKS"] = function(arg)
  234. --true or false - if true (default) [A] will draw in cyan text
  235. --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
  236. end,
  237. --afterwards, in order by importance - lists, scripts, SKCH/NFA images, big letters, forms, tables
  238. }
  239.  
  240. setmetatable(tags,{
  241. __call = function(self,tag,arg)
  242. arg = ":"..arg or ""
  243. if self[tag] ~= nil or self.closing[tag] ~= nil then
  244. return true
  245. else
  246. return "["..tag..arg.."]"
  247. end
  248. end})
  249.  
  250. local function parser(siteContents)
  251. siteContents = siteContents:sub(1,siteContents:find("%[END%]"))
  252. siteContents = siteContents:gsub("%[ID%]",os.getComputerID())
  253. local siteData = {}
  254. local curPath = ""
  255.  
  256. local function getfield (f)
  257. local v = siteData -- start with the table of globals
  258. if f ~= "" then
  259. for w in string.gmatch(f, "[%w_]+") do
  260. w = tonumber(w) or w
  261. v = v[w]
  262. end
  263. end
  264. if type(v) == "nil" then
  265. print(f)
  266. print(textutils.serialize(siteData))
  267. end
  268. return v
  269. end
  270.  
  271. local function setfield (f,v)
  272. local t = siteData -- start with the table of globals
  273. for w, d in string.gmatch(f, "([%w_]+)(.?)") do
  274. w = tonumber(w) or w
  275. if d == "." then -- not last field?
  276. t[w] = t[w] or {} -- create table if absent
  277. t = t[w] -- get the table
  278. else
  279. if type(t[#t]) == "string" and type(v) == "string" then
  280. t[#t] = t[#t] .. v
  281. else
  282. t[w] = v -- do the assignment
  283. end
  284. end
  285. end
  286. end
  287.  
  288. local function processBlock(str)
  289. local content = {}
  290. repeat
  291. local bx,ex = str:find("%[[^ %s]+%]")
  292. if bx==nil then
  293. table.insert(content,str)
  294. str = ""
  295. else
  296. local tag = str:sub(bx,ex)
  297. local argLoc = tag:find(":") or #tag
  298. local t = tag:sub(2,argLoc-1)
  299. local a = tag:sub(argLoc+1, #tag-1)
  300. if type(tags.closing[t]) ~= "nil" then
  301. table.insert(content,{tag = t,arg = a,contents = {}})
  302. str = str:sub(ex+1,#str)
  303. elseif type(tags[t]) ~= "nil" then
  304. table.insert(content,{tag = t, arg = a})
  305. str = str:sub(ex+1, #str)
  306. elseif type(tags.closing[t:sub(2,#t)]) ~= "nil" then
  307. table.insert(content,{tag = t})
  308. str = str:sub(ex+1, #str)
  309. else
  310. if type(content[#content]) =="string" then
  311. content[#content] = content[#content]..str:sub(1,ex)
  312. str = str:sub(ex+1,#str)
  313. else
  314. table.insert(content,str:sub(1,ex))
  315. str = str:sub(ex+1,#str)
  316. end
  317. end
  318. end
  319. until #str == 0
  320. return content
  321. end
  322.  
  323. local function tokenise(...)
  324. local sLine = table.concat({...}," ")
  325. local tWords = {}
  326. local bQuoted = false
  327. for match in string.gmatch(sLine .. "\"", "(.-)\"") do
  328. if bquoted == false then
  329. table.insert( tWords, match )
  330. else
  331. for m in string.gmatch( match, "[^ \t]+%s*" ) do
  332. table.insert( tWords, m )
  333. end
  334. end
  335. end
  336. return tWords
  337. end
  338.  
  339. for _,block in pairs(tokenise(siteContents)) do
  340. local contents = processBlock(block)
  341. for i = 1, #contents do
  342. local curFile = getfield(curPath)
  343.  
  344. if type(contents[i]) == "string" then -- its a word not a tag
  345. if type(curFile[#curFile]) == "string" then
  346. setfield( curPath.."."..#curFile , contents[i] )
  347. elseif type(curFile[#curFile]) == "table" then
  348. setfield( curPath.."."..(#curFile+1) , contents[i] )
  349. elseif #curFile == 0 then
  350. setfield( curPath.."."..1 , contents[i] )
  351. end
  352.  
  353. elseif type(contents[i]) == "table" then -- its a tag
  354. if contents[i].tag:sub(1,1)=="/" then
  355. local str = curPath:sub(1,-10)
  356. curPath = str:sub(1,-(#str:gsub("%w+%.",""))+2)
  357. elseif type(tags[contents[i].tag]) ~= "nil" then
  358. setfield( curPath.."."..(#curFile+1) , contents[i])
  359. elseif type(tags.closing[contents[i].tag]) ~= "nil" then
  360. curPath = curPath.."."..#curFile+1
  361. setfield( curPath , contents[i] )
  362. curPath = curPath..".".."contents"
  363. end
  364. else
  365. error("Invalid data type during parsing: " .. type(contents[i]))
  366. end
  367. end
  368. end
  369. return siteData
  370. end
  371.  
  372. local function assemble(tbl)
  373. for i = 1, #tbl do
  374. if type(tbl[i]) == "string" then
  375. term.write(tbl[i])
  376. elseif type(tbl[i]) == "table" then
  377. tags[tbl[i].tag](tbl[i].arg,tbl[i].contents)
  378. end
  379. end
  380. end
  381.  
  382. local function readurl(url)
  383. local protocol = string.sub(url,1,string.find(url,"://")-1)
  384. local name = ""
  385. if protocol == "http" then
  386. name = string.sub(url,8,string.find(url,".kst")-1)
  387. else
  388. name = string.sub(url,string.find(url,"://")+3,string.find(url,"/"-1))
  389. end
  390. url = url:sub(url:find("://"))
  391. local path = url:sub(url:find("/")+1)
  392.  
  393.  
  394. return addressbar, httplink-- look in game
  395. end
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418. --[[ New parser example
  419.  
  420. { --start of the site Contents
  421. { -- opening of a new tag
  422. tag = "C",
  423. arg = "arg",
  424. contents = { -- what is between the opening and closing tag
  425. "Hello my name is text",
  426. { -- opening of a tag
  427. tag = "BG",
  428. arg = "RED",
  429. contents = { -- only closing tags will have this
  430. "More Txt"
  431. }
  432. },
  433. "Some more text"
  434. } --closing of a tag
  435. } --closing of the tag
  436. }
  437.  
  438. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement