permalinks.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # -*- coding: utf-8 -*-
  2. """
  3. This plugin enables a kind of permalink which can be used to refer to a piece
  4. of content which is resistant to the file being moved or renamed.
  5. """
  6. import logging
  7. import itertools
  8. import os
  9. import os.path
  10. from pelican import signals
  11. from pelican.generators import Generator
  12. from pelican.utils import mkdir_p
  13. from pelican.utils import clean_output_dir
  14. logger = logging.getLogger(__name__)
  15. def article_url(content):
  16. '''
  17. Get the URL for an item of content
  18. '''
  19. return '{content.settings[SITEURL]}/{content.url}'.format(
  20. content=content).encode('utf-8')
  21. REDIRECT_STRING = '''
  22. <!DOCTYPE HTML>
  23. <html lang="en-US">
  24. <head>
  25. <meta charset="UTF-8">
  26. <meta http-equiv="refresh" content="0;url={url}">
  27. <script type="text/javascript">
  28. window.location.href = "{url}"
  29. </script>
  30. <title>Page Redirection to {title}</title>
  31. </head>
  32. <body>
  33. If you are not redirected automatically, follow the
  34. <a href='{url}'>link to {title}</a>
  35. </body>
  36. </html>
  37. '''
  38. class PermalinkGenerator(Generator):
  39. '''
  40. Generate a redirect page for every item of content with a
  41. permalink_id metadata
  42. '''
  43. def generate_context(self):
  44. '''
  45. Setup context
  46. '''
  47. self.permalink_output_path = os.path.join(
  48. self.output_path, self.settings['PERMALINK_PATH'])
  49. self.permalink_id_metadata_key = self.settings['PERMALINK_ID_METADATA_KEY']
  50. def generate_output(self, writer=None):
  51. '''
  52. Generate redirect files
  53. '''
  54. logger.info(
  55. 'Generating permalink files in %r', self.permalink_output_path)
  56. clean_output_dir(self.permalink_output_path, [])
  57. mkdir_p(self.permalink_output_path)
  58. for content in itertools.chain(
  59. self.context['articles'], self.context['pages']):
  60. for permalink_id in content.get_permalink_ids_iter():
  61. permalink_path = os.path.join(
  62. self.permalink_output_path, permalink_id) + '.html'
  63. redirect_string = REDIRECT_STRING.format(
  64. url=article_url(content),
  65. title=content.title)
  66. open(permalink_path, 'w').write(redirect_string)
  67. def get_permalink_ids_iter(self):
  68. '''
  69. Method to get permalink ids from content. To be bound to the class last thing
  70. '''
  71. permalink_id_key = self.settings['PERMALINK_ID_METADATA_KEY']
  72. permalink_ids_raw = self.metadata.get(permalink_id_key, '')
  73. for permalink_id in permalink_ids_raw.split(','):
  74. if permalink_id:
  75. yield permalink_id.strip()
  76. def get_permalink_ids(self):
  77. '''
  78. Method to get permalink ids from content. To be bound to the class last thing
  79. '''
  80. return list(self.get_permalink_ids_iter())
  81. def get_permalink_path(self):
  82. """Get just path component of permalink."""
  83. try:
  84. first_permalink_id = self.get_permalink_ids_iter().next()
  85. except StopIteration:
  86. return None
  87. return '/{settings[PERMALINK_PATH]}/{first_permalink}'.format(
  88. settings=self.settings, first_permalink=first_permalink_id)
  89. def get_permalink_url(self):
  90. '''
  91. Get a permalink URL
  92. '''
  93. return "/".join((self.settings['SITEURL'], self.get_permalink_path()))
  94. PERMALINK_METHODS = (
  95. get_permalink_ids_iter,
  96. get_permalink_ids,
  97. get_permalink_url,
  98. get_permalink_path,
  99. )
  100. def add_permalink_methods(content_inst):
  101. '''
  102. Add permalink methods to object
  103. '''
  104. for permalink_method in PERMALINK_METHODS:
  105. setattr(
  106. content_inst,
  107. permalink_method.__name__,
  108. permalink_method.__get__(content_inst, content_inst.__class__))
  109. def add_permalink_option_defaults(pelicon_inst):
  110. '''
  111. Add perlican defaults
  112. '''
  113. pelicon_inst.settings.setdefault('PERMALINK_PATH', 'permalinks')
  114. pelicon_inst.settings.setdefault('PERMALINK_ID_METADATA_KEY', 'permalink_id')
  115. def get_generators(_pelican_object):
  116. return PermalinkGenerator
  117. def register():
  118. signals.get_generators.connect(get_generators)
  119. signals.content_object_init.connect(add_permalink_methods)
  120. signals.initialized.connect(add_permalink_option_defaults)