Left4Cake2

Html Reader

Apr 11th, 2013
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.14 KB | None | 0 0
  1. --[[
  2. Intializing
  3. ]]--
  4.  
  5. local tArgs = { ... }
  6. veiw = "page"
  7. shell.run("clear")
  8. file = io.open( tArgs[1] )
  9. split = function(s, pattern, maxsplit)
  10.   local pattern = pattern or ' '
  11.   local maxsplit = maxsplit or -1
  12.   local s = s
  13.   local t = {}
  14.   local patsz = #pattern
  15.   while maxsplit ~= 0 do
  16.         local curpos = 1
  17.         local found = string.find(s, pattern)
  18.         if found ~= nil then
  19.           table.insert(t, string.sub(s, curpos, found - 1))
  20.           curpos = found + patsz
  21.           s = string.sub(s, curpos)
  22.         else
  23.           table.insert(t, string.sub(s, curpos))
  24.           break
  25.         end
  26.         maxsplit = maxsplit - 1
  27.         if maxsplit == 0 then
  28.           table.insert(t, string.sub(s, curpos - patsz - 1))
  29.         end
  30.   end
  31.   return t
  32. end
  33.  
  34.  
  35. --[[
  36. Reading Html
  37. ]]--
  38.  
  39. -- where to find links
  40. herf = {"herf='(.-)'", "Herf='(.-)'", "HERF='(.-)'",
  41. "herf=\"(.-)\"", "Href=\"(.-)\"", "HREF=\"(.-)\"",
  42. "herf=(.-) ", "Href=(.-) ", "HREF=(.-) ",
  43. "herf=(.-)>", "Href=(.-)>", "HREF=(.-)>",
  44. "href='(.-)'", "Href='(.-)'", "HREF='(.-)'",
  45. "href=\"(.-)\"", "Herf=\"(.-)\"", "HERF=\"(.-)\"",
  46. "href=(.-)>", "Herf=(.-)>", "HERF=(.-)>",
  47. "href=(.-) ", "Herf=(.-) ", "HERF=(.-) ",
  48. "src='(.-)'", "Src='(.-)'", "SRC='(.-)'",
  49. "src=\"(.-)\"", "Src=\"(.-)\"", "SRC=\"(.-)\"",
  50. "src=(.-)>", "Src=(.-)>", "SRC=(.-)>",
  51. "src=(.-) ", "Src=(.-) ", "SRC=(.-) ",
  52. }
  53.  
  54. -- Tags to remove
  55. remove = {"<!DOCTYPE(.-)>", "<!doctype(.-)>",
  56. "<!--(.-)-->", "<!--(.-)-->",
  57. "<html(.-)>", "<HTML(.-)>", "</html>", "</HTML>",
  58. "<head>(.-)</head>", "<HEAD>(.-)</HEAD>",
  59. "<body>", "<body(.-)>", "</body>", "<BODY>", "<BODY(.-)>", "</BODY>",
  60. "<HEAD>(.-)</HEAD>",
  61. "<form(.-)>", "</form>", "<FORM(.-)>", "</FORM>",
  62. "<input(.-)>", "</input>", "<INPUT(.-)>", "</INPUT>",
  63. "<script>(.-)</script>","<SCRIPT>(.-)</SCRIPT>",
  64. "<script(.-)>", "</script>", "<SCRIPT(.-)>", "</SCRIPT>",
  65. "<span(.-)>", "</span>", "<SPAN(.-)>", "</SPAN>",
  66. "<div(.-)</div>", "<div(.-)>", "</div>",
  67. "<Iframe(.-)</Iframe>", "<iframe(.-)</iframe>",
  68. "<a(.-)>", "</a>", "<A(.-)>", "</A>",
  69. "<TH>", "<TH(.-)>", "</TH>", "<th>", "<th(.-)>", "</th>",
  70. "<img(.-)>", "</img>", "<IMG(.-)>", "</IMG>",
  71. "<TD>", "<TD(.-)>", "</TD>", "<td>", "<td(.-)>", "</td>",
  72. "<ol>", "<ol(.-)>", "</ol>", "<OL>", "<OL(.-)>", "</OL>",
  73. "<sub>", "</sub>", "<SUB>", "</SUB>",
  74. "</ul>", "</UL>",
  75. "</NAV>", "</Nav>", "</nav>",
  76. "<FOOTER>", "<FOOTER(.-)>", "</FOOTER>",
  77. "<Footer>", "<Footer(.-)>", "</Footer>",
  78. "<footer>", "<footer(.-)>", "</footer>",
  79. }
  80. -- Bullets
  81. Bullets = { "<li>", "<LI>", "<li(.-)>", "<LI(.-)>" }
  82.  
  83. -- Tags to add spaces too
  84. newline = {"<br(.-)>", "<BR(.-)>", "<br />", "<BR />", "<br/>", "<BR/>", "</br>", "</BR>",
  85. "<h1>", "<h1(.-)>", "</h1>", "<H1>", "<H1(.-)>", "</H1>",
  86. "<h2>", "<h2(.-)>", "</h2>", "<H2>", "<H2(.-)>", "</H2>",
  87. "<h3>", "<h3(.-)>",  "</h3>", "<H3>", "<H3(.-)>", "</H3>",
  88. "<h4>", "<h4(.-)>",  "</h4>", "<H4>", "<H4(.-)>", "</H4>",
  89. "<h5>", "<h5(.-)>",  "</h5>", "<H5>", "<H5(.-)>", "</H5>",
  90. "<HEADER>", "<HEADER(.-)>", "</HEADER>",
  91. "<Header>", "<Header(.-)>", "</Header>",
  92. "<header>", "<header(.-)>", "</header>",
  93. "<ARTICLE>", "<ARTICLE(.-)>", "</ARTICLE>",
  94. "<Article>", "<Article(.-)>", "</Article>",
  95. "<article>", "<article(.-)>", "</article>",
  96. "<p(.-)>", "</p>", "<P(.-)>", "</P>",
  97. "<TR(.-)>", "</TR>", "<tr(.-)>", "</tr>",
  98. "<TABLE(.-)>", "<Table(.-)>", "<table(.-)>", "</TABLE>", "</Table>", "</table>",
  99. "<ul>", "<UL>", "</li>", "</LI>"
  100. }
  101.  
  102. -- Predefing Variables
  103. htmlpage = ""
  104. links = {"None"}
  105. viewpage = {}
  106. center={}
  107. hr={}
  108. centerenabled = false
  109.  
  110. -- Reading filedata
  111. while true do
  112. filedata = file:read()
  113. if filedata then
  114.   htmlpage = htmlpage .. filedata
  115. else break end
  116. end
  117. file:close()
  118.  
  119. -- Get all outgoing links
  120. i = 1
  121. ia = 1
  122.  
  123. j = 1
  124. while herf[j] do
  125.     while string.find(htmlpage, herf[j]) do
  126.         links[ia] = string.match(htmlpage, herf[j])
  127.         htmlpage = string.gsub(htmlpage, herf[j], "", 1)
  128.         ia = ia + 1
  129.     end
  130. j = j + 1
  131. end
  132.  
  133.  
  134. -- Remove useless tags
  135. i = 1
  136. while remove[i] do
  137.     while string.find(htmlpage, remove[i]) do
  138.         htmlpage = string.gsub(htmlpage, remove[i], "")
  139.     end
  140. i = i + 1
  141. end
  142.  
  143. -- HTML Entities - Spaces
  144. while string.find(htmlpage, "&#160;") do
  145.     htmlpage = string.gsub(htmlpage, "&#160;", " ")
  146. end
  147. while string.find(htmlpage, "&nbsp;") do
  148.     htmlpage = string.gsub(htmlpage, "&nbsp;", " ")
  149. end
  150.  
  151. -- HTML Entities - inverted exclamation mark
  152. while string.find(htmlpage, "&#161;") do
  153.     htmlpage = string.gsub(htmlpage, "&#161;", "¡")
  154. end
  155. while string.find(htmlpage, "&iexcl;") do
  156.     htmlpage = string.gsub(htmlpage, "&iexcl;", " ")
  157. end
  158.  
  159. -- HTML Entities - right pointing guillemet
  160. while string.find(htmlpage, "&raquo;") do
  161.     htmlpage = string.gsub(htmlpage, "&raquo;", ">>")
  162. end
  163.  
  164. while string.find(htmlpage, "&#169;") do
  165.     htmlpage = string.gsub(htmlpage, "&#169;", "(c)")
  166. end
  167. while string.find(htmlpage, "&copy;") do
  168.     htmlpage = string.gsub(htmlpage, "&copy;", "(c)")
  169. end
  170.  
  171. -- Reduce Spaces
  172. while string.find(htmlpage, "  ") do
  173.     htmlpage = string.gsub(htmlpage, "  ", " ")
  174. end
  175. while string.find(htmlpage, "   ") do
  176.     htmlpage = string.gsub(htmlpage, "  ", " ")
  177. end
  178.  
  179.  
  180. -- Add Bullet points
  181. i = 1
  182. while Bullets[i] do
  183.     while string.find(htmlpage, Bullets[i]) do
  184.         htmlpage = string.gsub(htmlpage, Bullets[i], " * ")
  185.     end
  186. i = i + 1
  187. end
  188.  
  189. -- adding new line where needed
  190. i = 1
  191. while newline[i] do
  192.     htmlpage = string.gsub(htmlpage, newline[i], " ?!@ ")
  193.     i = i + 1
  194. end
  195.  
  196. viewpage = split(htmlpage, "?!@")
  197. i = 1
  198. while viewpage[i] do
  199.  
  200. -- Style Tags
  201.  
  202.  
  203. --center
  204. donotdeisablecenter = true
  205. if string.find(string.lower(viewpage[i]), "<center>") then
  206.   centerenabled = true
  207.   donotdeisablecenter = false
  208.   viewpage[i] = string.gsub(viewpage[i], "<center>", "")
  209.   viewpage[i] = string.gsub(viewpage[i], "<Center>", "")
  210.   viewpage[i] = string.gsub(viewpage[i], "<CENTER>", "")
  211. end
  212. if string.find(string.lower(viewpage[i]), "</center>") then
  213.   if donotdeisablecenter then centerenabled = false end
  214.   viewpage[i] = string.gsub(viewpage[i], "</center>", "")
  215.   viewpage[i] = string.gsub(viewpage[i], "</Center>", "")
  216.   viewpage[i] = string.gsub(viewpage[i], "</CENTER>", "")
  217. end
  218.  
  219. if centerenabled then
  220.   center[i] = 1
  221. else
  222.   center[i] = false
  223. end
  224.  
  225. --Horzonle Line
  226. if string.find(string.lower(viewpage[i]), "<hr>") then
  227.   viewpage[i] = string.gsub(viewpage[i], "<hr>", "")
  228.   viewpage[i] = string.gsub(viewpage[i], "<Hr>", "")
  229.   viewpage[i] = string.gsub(viewpage[i], "<HR>", "")
  230.   hr[i] = true
  231. else
  232.   hr[i] = false
  233. end
  234.  
  235.  
  236. i = i + 1
  237. end
  238.  
  239.  
  240. --[[
  241. Displaying Result
  242. ]]--
  243.  
  244. i = 0
  245. while true do
  246.  
  247. -- Displaying Page
  248.  
  249.  
  250. if veiw == "page" then
  251.     shell.run("clear")
  252.     y = 1
  253.     j = i + y
  254.     w,h = term.getSize()
  255.     while viewpage[j] and y <= h do
  256.      
  257.       --set x to center
  258.       if center[j] == 1 then
  259.                        x = math.ceil((w / 2) - (viewpage[j]:len() / 2))
  260.                        if x < 1 then
  261.                             x = 1
  262.                        end
  263.       else
  264.       -- not center
  265.             x = 1
  266.       end
  267.       term.setCursorPos(x,y)
  268.       term.clearLine()
  269.       -- Draw Horizontle line
  270.       if hr[j] then
  271.             hrX = 1
  272.             while hrX <= w do
  273.                 term.setCursorPos(hrX,y)
  274.                 write("-")
  275.                 hrX = hrX + 1
  276.             end
  277.             term.setCursorPos(x,y + 1)
  278.             term.clearLine()
  279.       end
  280.       -- Line wrap handaling
  281.       stringSize = string.len(viewpage[j])
  282.       while stringSize > w do
  283.         stringSize = stringSize - w
  284.         y = y + 1
  285.       end
  286.       --print text
  287.       write(viewpage[j])
  288.        y = y + 1
  289.        j = i + y
  290.       end
  291.     local evt, arg1, arg2 = os.pullEvent()
  292.     if arg1 == 17 or arg1 == 200 then
  293.       if viewpage[i + 21] then
  294.        i = i + 1
  295.       end
  296.     elseif arg1 == 31 or arg1 == 208 then
  297.       if viewpage[i - 1] then
  298.        i = i - 1
  299.       end
  300.     elseif arg1 == 14 or arg1 == 207 then
  301.         shell.run("clear")
  302.         break
  303.     elseif arg1 == 28 and links[1] ~= "None" then
  304.         veiw = "links"
  305.     end
  306.  
  307. -- Displaying Links
  308. elseif veiw == "links" then
  309.     shell.run("clear")
  310.     print("Use the arrows and enter to chose.")
  311.     j=0
  312.     max=-1
  313.     while true do
  314.         j=j+1
  315.         max=max+1
  316.         if links[j]==njl then break end
  317.     end
  318.     pointer = 1
  319.     term.setCursorPos(1,10)
  320.     term.clearLine(none)
  321.     print("->"..pointer..":"..links[pointer])
  322.     while true do
  323.  
  324.     list=1
  325.     while list < 10 do
  326.         term.setCursorPos(1,10-list)
  327.         term.clearLine(none)
  328.         if links[pointer - (list)] then
  329.         write(""..pointer - (list)..":"..links[pointer - (list)])
  330.     end
  331.     list=list+1
  332.     end
  333.     term.setCursorPos(1,10)
  334.     selectedItemString = "->"..pointer..":"..links[pointer] .. "<-"
  335.     if string.len(selectedItemString) > w then
  336.         diffrenct = string.len(selectedItemString) - w
  337.         selectedItemString = string.sub(selectedItemString, diffrenct)
  338.        
  339.     end
  340.     term.clearLine(none)
  341.     print(selectedItemString)
  342.     list=1
  343.     while list< 10 do
  344.         term.setCursorPos(1,10+list)
  345.         term.clearLine(none)
  346.         if links[pointer + (list)] then
  347.             write(""..pointer + (list)..":".. links[pointer + (list)])
  348.         end
  349.         list=list+1
  350.     end
  351.     local evt,arg1,arg2 = os.pullEvent()
  352.     if arg1 == 14 or arg1 == 207 then veiw = "page" break end
  353.     if arg1 == 200 and pointer > 1 then
  354.         pointer = pointer - 1
  355.     elseif arg1 == 208 and pointer < max then
  356.         pointer = pointer + 1
  357.     end
  358.     if arg1 == 28 then veiw = "none" break end
  359.  
  360.     end
  361.  
  362.  
  363. else
  364.     shell.run("clear")
  365.     shell.run("Firebox", links[pointer])
  366.     break
  367. end
  368.  
  369.  
  370. end
Advertisement
Add Comment
Please, Sign In to add comment