interlinks.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. """
  3. Interlinks
  4. =========================
  5. This plugin allows you to include "interwiki" or shortcuts links into the blog,
  6. as keyword>rest_of_url
  7. """
  8. import re
  9. from pelican import signals
  10. from bs4 import BeautifulSoup
  11. from bs4 import SoupStrainer
  12. interlinks = {}
  13. def getSettings(generator):
  14. global interlinks
  15. interlinks = {'this': generator.settings['SITEURL']+"/"}
  16. if 'INTERLINKS' in generator.settings:
  17. for key, value in generator.settings['INTERLINKS'].items():
  18. interlinks[key] = value
  19. def parse_links(instance):
  20. if instance._content is not None:
  21. content = instance._content
  22. if '<a' in content:
  23. text = BeautifulSoup(
  24. content, "html.parser", parse_only=SoupStrainer("a"))
  25. for link in text.find_all("a", href=re.compile("(.+?)>")):
  26. old_tag = link.decode()
  27. url = link.get('href')
  28. m = re.search(r"(.+?)>", url).groups()
  29. name = m[0]
  30. if name in interlinks:
  31. hi = url.replace(name + ">", interlinks[name])
  32. link['href'] = hi
  33. content = content.replace(old_tag, link.decode())
  34. if '<img' in content:
  35. text = BeautifulSoup(
  36. content, "html.parser", parse_only=SoupStrainer("img"))
  37. for img in text.find_all('img', src=re.compile("(.+?)>")):
  38. old_tag = img.decode()
  39. url = img.get('src')
  40. m = re.search(r"(.+?)>", url).groups()
  41. name = m[0]
  42. if name in interlinks:
  43. hi = url.replace(name+">", interlinks[name])
  44. img['src'] = hi
  45. content = content.replace(
  46. old_tag.replace("&gt;", ">").replace("/>", ">"),
  47. img.decode()
  48. )
  49. instance._content = content
  50. def register():
  51. signals.generator_init.connect(getSettings)
  52. signals.content_object_init.connect(parse_links)