/apps/wiki/plugins/base_uri.py.disable

https://github.com/myles/comfy · Python · 85 lines · 62 code · 4 blank · 19 comment · 17 complexity · 647fe963d69dd7ca84ec1b96ba9e6022 MD5 · raw file

  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. BaseURI.py
  5. A Yaki plugin to reformat links and page references
  6. Created by Rui Carmo on 2006-09-12.
  7. Published under the MIT license.
  8. """
  9. import yaki.Engine, yaki.Locale
  10. import urlparse, re
  11. from BeautifulSoup import *
  12. from yaki.Utils import *
  13. class BaseURIWikiPlugin(yaki.Engine.WikiPlugin):
  14. def __init__(self, registry, webapp):
  15. registry.register('markup',self, 'a','baseuri')
  16. c = webapp.getContext()
  17. self.i18n = yaki.Locale.i18n[c.locale]
  18. self.schemas = self.i18n['uri_schemas']
  19. self.base = c.base
  20. self.store = c.store
  21. self.indexer = c.indexer
  22. def run(self, serial, tag, tagname, pagename, soup, request, response):
  23. try:
  24. uri = tag['href']
  25. except KeyError:
  26. return True
  27. # Try to handle the uri as a schema/path pair
  28. (schema,netloc,path,parameters,query,fragment) = urlparse.urlparse(uri)
  29. known = False
  30. if schema == '':
  31. uri = self.indexer.resolveAlias(path)
  32. if uri != path:
  33. path = tag['href'] = uri
  34. if uri in self.indexer.allpages:
  35. known = True
  36. if(schema == ''):
  37. if(known): # this is a known Wiki link, so there is no need to run it through more plugins
  38. if request is False:
  39. # check for a direct outbound link
  40. if path in self.indexer.defaultlinks:
  41. uri = self.indexer.defaultlinks[path]
  42. (schema,netloc,path,parameters,query,fragment) = urlparse.urlparse(uri)
  43. tag['href'] = uri
  44. tag['title'] = self.schemas[schema]['title'] % {'uri':uri}
  45. tag['class'] = self.schemas[schema]['class']
  46. return False
  47. tag['href'] = self.base + tag['href']
  48. tag['class'] = "wiki"
  49. try: # to use indexed metadata to annotate links
  50. last = self.indexer.pageinfo[path]['last-modified']
  51. tag['title'] = self.i18n['link_update_format'] % (path,timeSince(self.i18n,last))
  52. except:
  53. tag['title'] = self.i18n['link_defined_notindexed_format'] % path
  54. elif((schema == netloc == path == parameters == query == '') and (fragment != '')):
  55. # this is an anchor, leave it alone
  56. tag['href'] = self.base + pagename + "#" + fragment
  57. tag['class'] = "anchor"
  58. tag['title'] = self.i18n['link_anchor_format'] % fragment
  59. else: # this is an unknown wiki link
  60. if request is False:
  61. # remove unknown wiki links
  62. tag.replaceWith(tag.contents[0])
  63. else:
  64. # format for online viewing
  65. tag['href'] = self.base + tag['href']
  66. tag['class'] = "wikiunknown"
  67. tag['title'] = self.i18n['link_undefined_format'] % path
  68. elif(schema in self.schemas.keys()): # this is an external link, so reformat it
  69. tag['title'] = self.schemas[schema]['title'] % {'uri':uri}
  70. tag['class'] = self.schemas[schema]['class']
  71. #tag['target'] = '_blank'
  72. else: # assume this is an interwiki link (i.e., it seems to have a custom schema)
  73. tag['title'] = self.i18n['link_interwiki_format'] % uri
  74. tag['class'] = "interwiki"
  75. #tag['target'] = '_blank'
  76. # Signal that this tag needs further processing
  77. return True
  78. # We're done
  79. return False