fortune_html_parser.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8
  2. from HTMLParser import HTMLParser
  3. class FortuneHTMLParser(HTMLParser):
  4. body = []
  5. valid = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr><tr><td>11</td><td>&lt;script&gt;alert(&quot;This should not be displayed in a browser alert box.&quot;);&lt;/script&gt;</td></tr><tr><td>4</td><td>A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1</td></tr><tr><td>5</td><td>A computer program does what you tell it to do, not what you want it to do.</td></tr><tr><td>2</td><td>A computer scientist is someone who fixes things that aren&apos;t broken.</td></tr><tr><td>8</td><td>A list is only as strong as its weakest link. — Donald Knuth</td></tr><tr><td>0</td><td>Additional fortune added at request time.</td></tr><tr><td>3</td><td>After enough decimal places, nobody gives a damn.</td></tr><tr><td>7</td><td>Any program that runs right is obsolete.</td></tr><tr><td>10</td><td>Computers make very fast, very accurate mistakes.</td></tr><tr><td>6</td><td>Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen</td></tr><tr><td>9</td><td>Feature: A bug with seniority.</td></tr><tr><td>1</td><td>fortune: No such file or directory</td></tr><tr><td>12</td><td>フレームワークのベンチマーク</td></tr></table></body></html>'
  6. # Is called when a doctype or other such tag is read in.
  7. # For our purposes, we assume this is only going to be
  8. # "DOCTYPE html", so we will surround it with "<!" and ">".
  9. def handle_decl(self, decl):
  10. self.body.append("<!{d}>".format(d=decl))
  11. # This is called when an HTML character is parsed (i.e.
  12. # &quot;). There are a number of issues to be resolved
  13. # here. For instance, some tests choose to leave the
  14. # "+" character as-is, which should be fine as far as
  15. # character escaping goes, but others choose to use the
  16. # character reference of "&#43;", which is also fine.
  17. # Therefore, this method looks for all possible character
  18. # references and normalizes them so that we can
  19. # validate the input against a single valid spec string.
  20. # Another example problem: "&quot;" is valid, but so is
  21. # "&#34;"
  22. def handle_charref(self, name):
  23. # "&#34;" is a valid escaping, but we are normalizing
  24. # it so that our final parse can just be checked for
  25. # equality.
  26. if name == "34" or name == "x22":
  27. # Append our normalized entity reference to our body.
  28. self.body.append("&quot;")
  29. # "&#39;" is a valid escaping of "-", but it is not
  30. # required, so we normalize for equality checking.
  31. if name == "39" or name == "x27":
  32. self.body.append("&apos;")
  33. # Again, "&#43;" is a valid escaping of the "+", but
  34. # it is not required, so we need to normalize for out
  35. # final parse and equality check.
  36. if name == "43" or name == "x2B":
  37. self.body.append("+")
  38. # Again, "&#62;" is a valid escaping of ">", but we
  39. # need to normalize to "&gt;" for equality checking.
  40. if name == "62" or name == "x3E":
  41. self.body.append("&gt;")
  42. # Again, "&#60;" is a valid escaping of "<", but we
  43. # need to normalize to "&lt;" for equality checking.
  44. if name == "60" or name == "x3C":
  45. self.body.append("&lt;")
  46. # Not sure why some are escaping '/'
  47. if name == "47" or name == "x2F":
  48. self.body.append("/")
  49. def handle_entityref(self, name):
  50. # Again, "&mdash;" is a valid escaping of "—", but we
  51. # need to normalize to "—" for equality checking.
  52. if name == "mdash":
  53. self.body.append("—")
  54. else:
  55. self.body.append("&{n};".format(n=name))
  56. # This is called every time a tag is opened. We append
  57. # each one wrapped in "<" and ">".
  58. def handle_starttag(self, tag, attrs):
  59. self.body.append("<{t}>".format(t=tag))
  60. # This is called whenever data is presented inside of a
  61. # start and end tag. Generally, this will only ever be
  62. # the contents inside of "<td>" and "</td>", but there
  63. # are also the "<title>" and "</title>" tags.
  64. # Note: The data is stripped of leading and trailing
  65. # white-space.
  66. def handle_data (self, data):
  67. self.body.append("{d}".format(d=data.strip()))
  68. # This is called every time a tag is closed. We append
  69. # each one wrapped in "</" and ">".
  70. def handle_endtag(self, tag):
  71. self.body.append("</{t}>".format(t=tag))
  72. # Returns whether the HTML input parsed by this parser
  73. # is valid against our known "fortune" spec.
  74. # The parsed data in 'body' is joined on empty strings
  75. # and checked for equality against our spec.
  76. def isValidFortune(self):
  77. return self.valid == ''.join(self.body)