xuma202

Untitled

Aug 4th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. --[[
  2. HTML Parser in Lua
  3. Inspired by this C# code:
  4. http://www.codeproject.com/useritems/HTML_to_Plain_Text.asp
  5.  
  6. It does exactly the same as described there.
  7. Only converts offline files.
  8.  
  9. Distributed under a modified BSD license, see at the end of the file
  10. ]]
  11.  
  12. -- What is the desired newline?
  13. nl = "\n"
  14.  
  15. filename = "page"
  16.  
  17. function HTML_ToText (file)
  18. -- Declare variables, load the file. Make tags lowercase.
  19. local text
  20. local f=io.open (file)
  21. if f then
  22. text = f:read ("*a")
  23. f:close()
  24. end
  25. text = string.gsub (text,"(%b<>)",
  26. function (tag)
  27. return tag:lower()
  28. end)
  29. --[[
  30. First we kill the developer formatting (tabs, CR, LF)
  31. and produce a long string with no newlines and tabs.
  32. We also kill repeated spaces as browsers ignore them anyway.
  33. ]]
  34. local devkill=
  35. {
  36. ["("..string.char(10)..")"] = " ",
  37. ["("..string.char(13)..")"] = " ",
  38. ["("..string.char(15)..")"] = "",
  39. ["(%s%s+)"]=" ",
  40. }
  41. for pat, res in pairs (devkill) do
  42. text = string.gsub (text, pat, res)
  43. end
  44. -- Then we remove the header. We do this by stripping it first.
  45. text = string.gsub (text, "(<%s*head[^>]*>)", "<head>")
  46. text = string.gsub (text, "(<%s*%/%s*head%s*>)", "</head>")
  47. text = string.gsub (text, "(<head>,*<%/head>)", "")
  48. -- Kill all scripts. First we nuke their attribs.
  49. text = string.gsub (text, "(<%s*script[^>]*>)", "<script>")
  50. text = string.gsub (text, "(<%s*%/%s*script%s*>)", "</script>")
  51. text = string.gsub (text, "(<script>,*<%/script>)", "")
  52. -- Ok, same for styles.
  53. text = string.gsub (text, "(<%s*style[^>]*>)", "<style>")
  54. text = string.gsub (text, "(<%s*%/%s*style%s*>)", "</style>")
  55. text = string.gsub (text, "(<style>.*<%/style>)", "")
  56.  
  57. -- Replace <td> with tabulators.
  58. text = string.gsub (text, "(<%s*td[^>]*>)","\t")
  59.  
  60. -- Replace <br> with linebreaks.
  61. text = string.gsub (text, "(<%s*br%s*%/%s*>)",nl)
  62.  
  63. -- Replace <li> with an asterisk surrounded by 2 spaces.
  64. -- Replace </li> with a newline.
  65. text = string.gsub (text, "(<%s*li%s*%s*>)"," * ")
  66. text = string.gsub (text, "(<%s*/%s*li%s*%s*>)",nl)
  67.  
  68. -- <p>, <div>, <tr>, <ul> will be replaced to a double newline.
  69. text = string.gsub (text, "(<%s*div[^>]*>)", nl..nl)
  70. text = string.gsub (text, "(<%s*p[^>]*>)", nl..nl)
  71. text = string.gsub (text, "(<%s*tr[^>]*>)", nl..nl)
  72. text = string.gsub (text, "(<%s*%/*%s*ul[^>]*>)", nl..nl)
  73. --
  74.  
  75. -- Nuke all other tags now.
  76. text = string.gsub (text, "(%b<>)","")
  77.  
  78. -- Replace entities to their correspondant stuff where applicable.
  79. -- C# is owned badly here by using a table. :-P
  80. -- A metatable secures entities, so you can add them natively as keys.
  81. -- Enclosing brackets also get added automatically (capture!)
  82. local entities = {}
  83. setmetatable (entities,
  84. {
  85. __newindex = function (tbl, key, value)
  86. key = string.gsub (key, "(%#)" , "%%#")
  87. key = string.gsub (key, "(%&)" , "%%&")
  88. key = string.gsub (key, "(%;)" , "%%;")
  89. key = string.gsub (key, "(.+)" , "("..key..")")
  90. rawset (tbl, key, value)
  91. end
  92. })
  93. entities =
  94. {
  95. ["&nbsp;"] = " ",
  96. ["&bull;"] = " * ",
  97. ["&lsaquo;"] = "<",
  98. ["&rsaquo;"] = ">",
  99. ["&trade;"] = "(tm)",
  100. ["&frasl;"] = "/",
  101. ["&lt;"] = "<",
  102. ["&gt;"] = ">",
  103. ["&copy;"] = "(c)",
  104. ["&reg;"] = "(r)",
  105. -- Then kill all others.
  106. -- You can customize this table if you would like to,
  107. -- I just got bored of copypasting. :-)
  108. -- http://hotwired.lycos.com/webmonkey/reference/special_characters/
  109. ["%&.+%;"] = "",
  110. }
  111. for entity, repl in pairs (entities) do
  112. text = string.gsub (text, entity, repl)
  113. end
  114.  
  115. return text
  116.  
  117. end
  118.  
  119. HTML_ToText (filename)
  120.  
  121. --[[
  122. Copyright (c) 2007, bastya_elvtars
  123.  
  124. All rights reserved.
  125.  
  126. Redistribution and use in source and binary forms, with or without modification,
  127. are permitted provided that the following conditions are met:
  128.  
  129. * Redistributions of source code must retain the above copyright notice, this
  130. list of conditions and the following disclaimer.
  131. * Redistributions in binary form must reproduce the above copyright notice,
  132. this list of conditions and the following disclaimer in the documentation
  133. and/or other materials provided with the distribution.
  134. * Neither the name of the author nor the names of contributors may be used
  135. to endorse or promote products derived from this code without specific
  136. prior written permission.
  137.  
  138. THIS CODE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  139. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  140. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  141. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  142. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  143. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  144. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  145. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  146. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  147. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  148. CODE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  149. ]]
Advertisement
Add Comment
Please, Sign In to add comment