123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- ''' Better Tables: Restore sanity to rst->html tables
- This pelican plugin removes the excess attributes and elements in the HTML
- tables generated from RST. Trimming this fat allows them to pass HTML5
- validation. Hopefully rst2html5 will be merged into pelican at some point, but
- until then, this hacky approach is needed.
- This approach has the advantage of restoring sanity to tables, and allows their
- column with to flow normally. All styling is default and must be styled by CSS
- rather than in HTML attributes.
- I make no claim that /all/ HTML table crimes generated are corrected, merely
- the ones which I have stumbled across.
- Usage:
- Enable the plugin in your pelicanconf.py
- PLUGINS = [
- # ...
- 'better_tables',
- # ...
- ]
- And that's it. Life's simple like that sometimes.
- '''
- from pelican import signals, contents
- from bs4 import BeautifulSoup
- def better_tables(content):
- if isinstance(content, contents.Static):
- return
- soup = BeautifulSoup(content._content, 'html.parser')
- for table in soup.findAll('table'):
-
- del(table['border'])
-
- for tag in table.findAll('colgroup'):
- tag.extract()
-
- for tag in table.findAll(['tbody', 'thead']):
- del(tag['valign'])
- soup.renderContents()
- content._content = soup.decode()
- def register():
- signals.content_object_init.connect(better_tables)
|