A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 lines
2.4 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. Contains classes and utilities related to hyde urls.
  4. """
  5. import re
  6. from hyde.fs import File, Folder
  7. from hyde.model import Expando
  8. from hyde.plugin import Plugin
  9. from hyde.site import Site, Node, Resource
  10. from functools import wraps
  11. class UrlCleanerPlugin(Plugin):
  12. """
  13. Url Cleaner plugin for hyde. Adds to hyde the ability to generate clean
  14. urls.
  15. Configuration example
  16. ---------------------
  17. #yaml
  18. urlcleaner:
  19. index_file_names:
  20. # Identifies the files that represents a directory listing.
  21. # These file names are automatically stripped away when
  22. # the content_url function is called.
  23. - index.html
  24. strip_extensions:
  25. # The following extensions are automatically removed when
  26. # generating the urls using content_url function.
  27. - html
  28. # This option will append a slash to the end of directory paths
  29. append_slash: true
  30. """
  31. def __init__(self, site):
  32. super(UrlCleanerPlugin, self).__init__(site)
  33. def begin_site(self):
  34. """
  35. Replace the content_url method in the site object with a custom method
  36. that cleans urls based on the given configuration.
  37. """
  38. config = self.site.config
  39. if not hasattr(config, 'urlcleaner'):
  40. return
  41. if (hasattr(Site, '___url_cleaner_patched___')):
  42. return
  43. settings = config.urlcleaner
  44. print settings.to_dict()
  45. def clean_url(urlgetter):
  46. @wraps(urlgetter)
  47. def wrapper(site, path):
  48. url = urlgetter(site, path)
  49. index_file_names = getattr(settings,
  50. 'index_file_names',
  51. ['index.html'])
  52. rep = File(url)
  53. if rep.name in index_file_names:
  54. url = rep.parent.path.rstrip('/')
  55. if hasattr(settings, 'append_slash') and \
  56. settings.append_slash:
  57. url += '/'
  58. elif hasattr(settings, 'strip_extensions'):
  59. if rep.kind in settings.strip_extensions:
  60. url = rep.parent.child(rep.name_without_extension)
  61. return url or '/'
  62. return wrapper
  63. Site.___url_cleaner_patched___ = True
  64. Site.content_url = clean_url(Site.content_url)