Guest User

Untitled

a guest
Apr 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. """
  2. A plugin for handling slug URLs. A slug is a nice, short URL for reaching a
  3. particular post.
  4.  
  5. If a URL ending in /slug/hello-world is requested this plugin will search the
  6. datadir for a post that has the metadata line:
  7.  
  8. #slug hello-world
  9.  
  10. If such a post is found the page for that post will be shown in the browser. So
  11. by adding a #slug metadata key to your post you can create a custom, short
  12. permalink for that post.
  13.  
  14. Unfortunately this plugin is not very efficient. When a slug URL is requested
  15. the plugin searches the datadir constructing a FileEntry for every entry file
  16. that it finds until it finds an entry with a matching slug or has searched the
  17. entire datadir. A cached dictionary mapping slugs to entry filenames might be
  18. more efficient, but it would need to be updated every time an entry is added,
  19. deleted, moved or modified.
  20.  
  21. Warning: if two posts have the same slug this plugin will just return
  22. whichever post it finds first.
  23.  
  24. No configuration is necessary to use this plugin, just put it in your plugins
  25. directory and it to your load_plugins list in config.py if necessary.
  26.  
  27. Permission is hereby granted, free of charge, to any person
  28. obtaining a copy of this software and associated documentation
  29. files (the "Software"), to deal in the Software without restriction,
  30. including without limitation the rights to use, copy, modify,
  31. merge, publish, distribute, sublicense, and/or sell copies of the
  32. Software, and to permit persons to whom the Software is furnished
  33. to do so, subject to the following conditions:
  34.  
  35. The above copyright notice and this permission notice shall be
  36. included in all copies or substantial portions of the Software.
  37.  
  38. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  39. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  40. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  41. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  42. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  43. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  44. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  45. SOFTWARE.
  46.  
  47. Copyright 2009 Sean Hammond
  48.  
  49. """
  50. __author__ = "Sean Hammond, seanhammond -at- lavabit -dot- com
  51. __version__ = '1'
  52. __url__ = ''
  53. __description = 'A plugin for handling slug URLs. A slug is a nice, short URL for reaching a particular post.'
  54.  
  55. import os
  56. from Pyblosxom import entries
  57.  
  58. def cb_filelist(args):
  59. """If the requested URL ends with /slug/something then search for an entry
  60. file in the datadir that has a metadata value '#slug something', if an
  61. entry with the requested slug is found then provide Pyblosxom with an entry
  62. list containing only that entry.
  63.  
  64. """
  65. request = args['request'] # The request object.
  66. config = request.getConfiguration() # The configuration dict.
  67. datadir = config['datadir'] # The datadir.
  68. data = request.getData() # The data dict.
  69. extensions = data['extensions'] # List of filename extensions for entries.
  70.  
  71. # pi_bl seems to be the end part of the requested URL, the part following
  72. # base_url/. What does pi_bl stand for anyway?
  73. pi_bl = data['pi_bl']
  74.  
  75. if not pi_bl.startswith('slug/'):
  76. # The requested URL is not a slug/ URL, nothing for this plugin to do,
  77. # allow another plugin or Pyblosxom to create the filelist instead.
  78. return None
  79.  
  80. slug = pi_bl[5:]
  81.  
  82. # Recursively find all files in the datadir that have a filename extension
  83. # that Pyblosxom is currently handling for entries, and construct a
  84. # FileEntry for each.
  85. # FIXME: this assumes FileEntry is the appropriate class for every type of
  86. # entry. Is that correct?
  87. for root, dirs, files in os.walk(datadir):
  88. for f in files:
  89. if not os.path.isfile(os.path.join(root,f)):
  90. continue
  91. elif os.path.splitext(f)[1][1:] not in extensions:
  92. continue
  93. else:
  94. entry = entries.fileentry.FileEntry(request,os.path.join(datadir,os.path.join(root,f)),'','')
  95. if entry.has_key('slug'):
  96. if entry['slug'] == slug:
  97. # bl_type = file tells pyblosxom that it's a file
  98. # page (showing a single file) not an index page.
  99. data['bl_type'] = 'file'
  100. return [entry]
  101.  
  102. # If we get here then no entry with the requested slug was found. Return
  103. # None, which will allow another plugin of Pyblosxom itself to handle
  104. # generating the file list instead.
  105. return None
Add Comment
Please, Sign In to add comment