| 1 | # -*- coding: iso-8859-1 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006-2011 Christian Boos <cboos@neuf.fr> |
|---|
| 4 | # All rights reserved. |
|---|
| 5 | # |
|---|
| 6 | # This software is licensed as described in the file COPYING, which |
|---|
| 7 | # you should have received as part of this distribution. The terms |
|---|
| 8 | # are also available at http://trac.edgewall.com/license.html. |
|---|
| 9 | # |
|---|
| 10 | # Author: Christian Boos <cboos@neuf.fr> |
|---|
| 11 | |
|---|
| 12 | from trac.util.html import Markup, html as tag |
|---|
| 13 | |
|---|
| 14 | from trac.core import implements, Component |
|---|
| 15 | from trac.wiki import IWikiSyntaxProvider |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | class UNCPathLink(Component): |
|---|
| 19 | """Transform UNC paths to `file://` links. |
|---|
| 20 | |
|---|
| 21 | The UNC (Universal Naming Convention) specifies a common syntax to describe |
|---|
| 22 | the location of a network resource, such as a shared file, directory, or |
|---|
| 23 | printer. The UNC syntax has the generic form: |
|---|
| 24 | {{{ |
|---|
| 25 | \\\\ComputerName\SharedFolder\Resource |
|---|
| 26 | }}} |
|---|
| 27 | |
|---|
| 28 | Activate this component to automatically transform UNC paths to `file://` |
|---|
| 29 | links. The following wiki markups are supported: |
|---|
| 30 | {{{ |
|---|
| 31 | \\\\ComputerName\SharedFolder\Resource |
|---|
| 32 | [\\\\ComputerName\SharedFolder\Resource Label] |
|---|
| 33 | [unc:\\\\ComputerName\SharedFolder\Resource Label] |
|---|
| 34 | [unc:"\\\\ComputerName\SharedFolder\Resource" Label] |
|---|
| 35 | }}} |
|---|
| 36 | |
|---|
| 37 | Some web browsers need a plugin to enable them to open `file://` links on |
|---|
| 38 | web pages with `http(s)://`-URLs: |
|---|
| 39 | * Chrome: |
|---|
| 40 | [https://chrome.google.com/webstore/detail/jllpkdkcdjndhggodimiphkghogcpida LocalLinks] |
|---|
| 41 | plugin |
|---|
| 42 | * Firefox: [http://locallink.mozdev.org/ LocalLink] plugin |
|---|
| 43 | * IE9: supported by default |
|---|
| 44 | """ |
|---|
| 45 | |
|---|
| 46 | implements(IWikiSyntaxProvider) |
|---|
| 47 | |
|---|
| 48 | # IWikiSyntaxProvider methods |
|---|
| 49 | |
|---|
| 50 | _unc_path_regexp = ( |
|---|
| 51 | r'!?(?:\\\\[^\s\\\\/]+(?:[\\/][^\s\\]*)+' # \\host\share[\path] |
|---|
| 52 | r'|"\\\\[^"\s\\\\/]+(?:[\\/][^"\\]*)+"' # "\\host\share[\path ...]" |
|---|
| 53 | r'|<\\\\[^>\s\\\\/]+(?:[\\/][^>\\]*)+>' # <\\host\share[\path ...]> |
|---|
| 54 | r'|\[(?P<unc_path>\\\\[^\s\\\\/]+(?:[\\/][^\s\\]*)+)' |
|---|
| 55 | r'(?P<unc_label>\s+[^]]+)?\]' # [\\host\share\path label] |
|---|
| 56 | r')') |
|---|
| 57 | |
|---|
| 58 | def get_wiki_syntax(self): |
|---|
| 59 | def filelink(formatter, match, fullmatch): |
|---|
| 60 | label = None |
|---|
| 61 | if match[0] == '[': |
|---|
| 62 | match, label = (fullmatch.group('unc_path'), |
|---|
| 63 | fullmatch.group('unc_label')) |
|---|
| 64 | elif match[0] in '"<': |
|---|
| 65 | match = match[1:-1] |
|---|
| 66 | return tag.a(label or match, |
|---|
| 67 | href='file:///%s' % match.replace('\\', '/')) |
|---|
| 68 | yield (self._unc_path_regexp, filelink) |
|---|
| 69 | |
|---|
| 70 | def get_link_resolvers(self): |
|---|
| 71 | def filelink(formatter, ns, target, label, fullmatch=None): |
|---|
| 72 | return tag.a(label or target, |
|---|
| 73 | href='file:///%s' % target.replace('\\', '/')) |
|---|
| 74 | yield ('unc', filelink) |
|---|