interlinks.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 bs4 import SoupStrainer
  9. from pelican import signals
  10. import re
  11. interlinks = {}
  12. def getSettings (generator):
  13. global interlinks
  14. interlinks = {'this': generator.settings['SITEURL']+"/"}
  15. if 'INTERLINKS' in generator.settings:
  16. for key, value in generator.settings['INTERLINKS'].items():
  17. interlinks[key] = value
  18. def parse_links(instance):
  19. if instance._content is not None:
  20. content = instance._content
  21. if '<a' in content:
  22. text = BeautifulSoup(content, "html.parser", parse_only=SoupStrainer("a"))
  23. for link in text.find_all("a",href=re.compile("(.+?)>")):
  24. old_tag = str(link)
  25. url = link.get('href')
  26. m = re.search(r"(.+?)>", url).groups()
  27. name = m[0]
  28. if name in interlinks:
  29. hi = url.replace(name + ">", interlinks[name])
  30. link['href'] = hi
  31. content = content.replace(old_tag, str(link))
  32. if '<img' in content:
  33. text = BeautifulSoup(content, "html.parser", parse_only=SoupStrainer("img"))
  34. for img in text.find_all('img', src=re.compile("(.+?)>")):
  35. old_tag = str(img)
  36. url = img.get('src')
  37. m = re.search(r"(.+?)>", url).groups()
  38. name = m[0]
  39. if name in interlinks:
  40. hi = url.replace(name+">",interlinks[name])
  41. img['src'] = hi
  42. content = content.replace(old_tag.replace("&gt;", ">").replace("/>",">"), str(img))
  43. instance._content = content
  44. def register():
  45. signals.generator_init.connect(getSettings)
  46. signals.content_object_init.connect(parse_links)