permalinks.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 itertools
  7. import logging
  8. import os
  9. import os.path
  10. from pelican import signals
  11. from pelican.generators import Generator
  12. from pelican.utils import clean_output_dir
  13. from pelican.utils import mkdir_p
  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 = (
  50. self.settings['PERMALINK_ID_METADATA_KEY'])
  51. def generate_output(self, writer=None):
  52. '''
  53. Generate redirect files
  54. '''
  55. logger.info(
  56. 'Generating permalink files in %r', self.permalink_output_path)
  57. clean_output_dir(self.permalink_output_path, [])
  58. mkdir_p(self.permalink_output_path)
  59. for content in itertools.chain(
  60. self.context['articles'], self.context['pages']):
  61. for permalink_id in content.get_permalink_ids_iter():
  62. permalink_path = os.path.join(
  63. self.permalink_output_path, permalink_id) + '.html'
  64. redirect_string = REDIRECT_STRING.format(
  65. url=article_url(content),
  66. title=content.title)
  67. open(permalink_path, 'w').write(redirect_string)
  68. def get_permalink_ids_iter(self):
  69. '''
  70. Method to get permalink ids from content. To be bound to the class last
  71. thing.
  72. '''
  73. permalink_id_key = self.settings['PERMALINK_ID_METADATA_KEY']
  74. permalink_ids = self.metadata.get(permalink_id_key, '')
  75. for permalink_id in permalink_ids.split(','):
  76. if permalink_id:
  77. yield permalink_id.strip()
  78. def get_permalink_ids(self):
  79. '''
  80. Method to get permalink ids from content. To be bound to the class last
  81. thing.
  82. '''
  83. return list(self.get_permalink_ids_iter())
  84. def get_permalink_path(self):
  85. """Get just path component of permalink."""
  86. try:
  87. first_permalink_id = next(self.get_permalink_ids_iter())
  88. except StopIteration:
  89. return None
  90. return '/{settings[PERMALINK_PATH]}/{first_permalink}'.format(
  91. settings=self.settings, first_permalink=first_permalink_id)
  92. def get_permalink_url(self):
  93. '''
  94. Get a permalink URL
  95. '''
  96. return "/".join((self.settings['SITEURL'], self.get_permalink_path()))
  97. PERMALINK_METHODS = (
  98. get_permalink_ids_iter,
  99. get_permalink_ids,
  100. get_permalink_url,
  101. get_permalink_path,
  102. )
  103. def add_permalink_methods(content_inst):
  104. '''
  105. Add permalink methods to object
  106. '''
  107. for permalink_method in PERMALINK_METHODS:
  108. setattr(
  109. content_inst,
  110. permalink_method.__name__,
  111. permalink_method.__get__(content_inst, content_inst.__class__))
  112. def add_permalink_option_defaults(pelicon_inst):
  113. '''
  114. Add perlican defaults
  115. '''
  116. pelicon_inst.settings.setdefault('PERMALINK_PATH', 'permalinks')
  117. pelicon_inst.settings.setdefault(
  118. 'PERMALINK_ID_METADATA_KEY', 'permalink_id')
  119. def get_generators(_pelican_object):
  120. return PermalinkGenerator
  121. def register():
  122. signals.get_generators.connect(get_generators)
  123. signals.content_object_init.connect(add_permalink_methods)
  124. signals.initialized.connect(add_permalink_option_defaults)