extract_toc.py 984 B

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. Extract Table of Content
  3. ========================
  4. This plugin allows you to extract table of contents (ToC) from article.content
  5. and place it in its own article.toc variable.
  6. """
  7. from os import path
  8. from bs4 import BeautifulSoup
  9. from pelican import signals, readers, contents
  10. def extract_toc(content):
  11. if isinstance(content, contents.Static):
  12. return
  13. soup = BeautifulSoup(content._content,'html.parser')
  14. filename = content.source_path
  15. extension = path.splitext(filename)[1][1:]
  16. toc = ''
  17. # if it is a Markdown file
  18. if extension in readers.MarkdownReader.file_extensions:
  19. toc = soup.find('div', class_='toc')
  20. # else if it is a reST file
  21. elif extension in readers.RstReader.file_extensions:
  22. toc = soup.find('div', class_='contents topic')
  23. if toc:
  24. toc.extract()
  25. content._content = soup.decode()
  26. content.toc = toc.decode()
  27. def register():
  28. signals.content_object_init.connect(extract_toc)