interlinks.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- coding: utf-8 -*-
  2. """
  3. Interlinks
  4. =========================
  5. This plugin allows you to include "interwiki" or shortcuts links into the blog, as keyword>rest_of_url
  6. """
  7. from bs4 import BeautifulSoup
  8. from pelican import signals
  9. import re
  10. interlinks = {}
  11. def getSettings (generator):
  12. global interlinks
  13. interlinks = {'this': generator.settings['SITEURL']+"/"}
  14. if 'INTERLINKS' in generator.settings:
  15. for key, value in generator.settings['INTERLINKS'].items():
  16. interlinks[key] = value
  17. def content_object_init(instance):
  18. if instance._content is not None:
  19. content = instance._content
  20. # use Python's built-in parser so no duplicated html & body tags appear, or use tag.unwrap()
  21. text = BeautifulSoup(content, "html.parser")
  22. if 'a' in content:
  23. for link in text.find_all(href=re.compile("(.+?)>")):
  24. url = link.get('href')
  25. m = re.search(r"(.+?)>", url).groups()
  26. name = m[0]
  27. if name in interlinks:
  28. hi = url.replace(name+">",interlinks[name])
  29. link['href'] = hi
  30. instance._content = text.decode()
  31. def register():
  32. signals.generator_init.connect(getSettings)
  33. signals.content_object_init.connect(content_object_init)