extract_toc.py 900 B

123456789101112131415161718192021222324252627282930313233
  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
  10. def extract_toc(content):
  11. soup = BeautifulSoup(content._content)
  12. filename = content.source_path
  13. extension = path.splitext(filename)[1][1:]
  14. toc = ''
  15. # if it is a Markdown file
  16. if extension in readers.MarkdownReader.file_extensions:
  17. toc = soup.find('div', class_='toc')
  18. # else if it is a reST file
  19. elif extension in readers.RstReader.file_extensions:
  20. toc = soup.find('div', class_='contents topic')
  21. if toc:
  22. toc.extract()
  23. content._content = soup.decode()
  24. content.toc = toc.decode()
  25. def register():
  26. signals.content_object_init.connect(extract_toc)