better_tables.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (c) 2015 Alex Waite
  2. # Permission is hereby granted, free of charge, to any person obtaining a copy
  3. # of this software and associated documentation files (the "Software"), to deal
  4. # in the Software without restriction, including without limitation the rights
  5. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. # copies of the Software, and to permit persons to whom the Software is
  7. # furnished to do so, subject to the following conditions:
  8. # The above copyright notice and this permission notice shall be included in
  9. # all copies or substantial portions of the Software.
  10. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
  12. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. # SOFTWARE.
  17. ''' Better Tables: Restore sanity to rst->html tables
  18. This pelican plugin removes the excess attributes and elements in the HTML
  19. tables generated from RST. Trimming this fat allows them to pass HTML5
  20. validation. Hopefully rst2html5 will be merged into pelican at some point, but
  21. until then, this hacky approach is needed.
  22. This approach has the advantage of restoring sanity to tables, and allows their
  23. column with to flow normally. All styling is default and must be styled by CSS
  24. rather than in HTML attributes.
  25. I make no claim that /all/ HTML table crimes generated are corrected, merely
  26. the ones which I have stumbled across.
  27. Usage:
  28. Enable the plugin in your pelicanconf.py
  29. PLUGINS = [
  30. # ...
  31. 'better_tables',
  32. # ...
  33. ]
  34. And that's it. Life's simple like that sometimes.
  35. '''
  36. from pelican import signals, contents
  37. from bs4 import BeautifulSoup
  38. def better_tables(content):
  39. if isinstance(content, contents.Static):
  40. return
  41. soup = BeautifulSoup(content._content, 'html.parser')
  42. for table in soup.findAll('table'):
  43. # table's "border" is so 1996
  44. del(table['border'])
  45. # col widths. not only /infuriating/ it's also not in HTML5
  46. for tag in table.findAll('colgroup'):
  47. tag.extract()
  48. # tbody and thead's valign
  49. for tag in table.findAll(['tbody', 'thead']):
  50. del(tag['valign'])
  51. soup.renderContents()
  52. content._content = soup.decode()
  53. def register():
  54. signals.content_object_init.connect(better_tables)