interlinks.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. if 'img' in content:
  31. for img in text.find_all('img', src=re.compile("(.+?)>")):
  32. url = img.get('src')
  33. m = re.search(r"(.+?)>", url).groups()
  34. name = m[0]
  35. if name in interlinks:
  36. hi = url.replace(name+">",interlinks[name])
  37. img['src'] = hi
  38. instance._content = text.decode()
  39. def register():
  40. signals.generator_init.connect(getSettings)
  41. signals.content_object_init.connect(content_object_init)