[3] | 1 | """ |
---|
| 2 | Asset Tag Helpers |
---|
| 3 | |
---|
| 4 | Provides functionality for linking an HTML page together with other assets, such as |
---|
| 5 | javascripts, stylesheets, and feeds. |
---|
| 6 | """ |
---|
| 7 | # Last synced with Rails copy at Revision 4103 on Aug 19th, 2006. |
---|
| 8 | |
---|
| 9 | import os |
---|
| 10 | import urlparse |
---|
| 11 | from tags import * |
---|
| 12 | from routes import request_config |
---|
| 13 | |
---|
| 14 | # The absolute path of the WebHelpers javascripts directory |
---|
| 15 | javascript_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
---|
| 16 | 'javascripts') |
---|
| 17 | |
---|
| 18 | # WebHelpers' built-in javascripts. Note: scriptaculous automatically includes all of its |
---|
| 19 | # supporting .js files |
---|
| 20 | javascript_builtins = ('prototype.js', 'scriptaculous.js') |
---|
| 21 | |
---|
| 22 | def auto_discovery_link_tag(source, type='rss', **kwargs): |
---|
| 23 | """ |
---|
| 24 | Returns a link tag allowing browsers and news readers (that support it) to auto-detect |
---|
| 25 | an RSS or ATOM feed for current page. |
---|
| 26 | |
---|
| 27 | ``source`` |
---|
| 28 | The URL of the feed. The URL is ultimately prepended with the environment's |
---|
| 29 | ``SCRIPT_NAME`` (the root path of the web application), unless the URL is |
---|
| 30 | fully-fledged (e.g. http://example.com). |
---|
| 31 | |
---|
| 32 | ``type`` |
---|
| 33 | The type of feed. Specifying 'rss' or 'atom' automatically translates to a type of |
---|
| 34 | 'application/rss+xml' or 'application/atom+xml', respectively. Otherwise the type |
---|
| 35 | is used as specified. Defaults to 'rss'. |
---|
| 36 | |
---|
| 37 | Examples:: |
---|
| 38 | |
---|
| 39 | >>> auto_discovery_link_tag('http://feed.com/feed.xml') |
---|
| 40 | '<link href="http://feed.com/feed.xml" rel="alternate" title="RSS" type="application/rss+xml" />' |
---|
| 41 | |
---|
| 42 | >>> auto_discovery_link_tag('http://feed.com/feed.xml', type='atom') |
---|
| 43 | '<link href="http://feed.com/feed.xml" rel="alternate" title="ATOM" type="application/atom+xml" />' |
---|
| 44 | |
---|
| 45 | >>> auto_discovery_link_tag('app.rss', type='atom', title='atom feed') |
---|
| 46 | '<link href="app.rss" rel="alternate" title="atom feed" type="application/atom+xml" />' |
---|
| 47 | |
---|
| 48 | >>> auto_discovery_link_tag('/app.html', type='text/html') |
---|
| 49 | '<link href="/app.html" rel="alternate" title="" type="text/html" />' |
---|
| 50 | """ |
---|
| 51 | title = '' |
---|
| 52 | if type.lower() in ('rss', 'atom'): |
---|
| 53 | title = type.upper() |
---|
| 54 | type='application/%s+xml' % type.lower() |
---|
| 55 | |
---|
| 56 | tag_args = dict(rel='alternate', type=type, title=title, |
---|
| 57 | href=compute_public_path(source)) |
---|
| 58 | kwargs.pop('href', None) |
---|
| 59 | kwargs.pop('type', None) |
---|
| 60 | tag_args.update(kwargs) |
---|
| 61 | return tag('link', **tag_args) |
---|
| 62 | |
---|
| 63 | def image_tag(source, alt=None, size=None, **options): |
---|
| 64 | """ |
---|
| 65 | Returns an image tag for the specified ``source``. |
---|
| 66 | |
---|
| 67 | ``source`` |
---|
| 68 | The source URL of the image. The URL is prepended with '/images/', unless its full |
---|
| 69 | path is specified. The URL is ultimately prepended with the environment's |
---|
| 70 | ``SCRIPT_NAME`` (the root path of the web application), unless the URL is |
---|
| 71 | fully-fledged (e.g. http://example.com). A source with no filename extension will |
---|
| 72 | be automatically appended with the '.png' extension. |
---|
| 73 | |
---|
| 74 | ``alt`` |
---|
| 75 | The img's alt tag. Defaults to the source's filename, title cased. |
---|
| 76 | |
---|
| 77 | ``size`` |
---|
| 78 | The img's size, specified in the format "XxY". "30x45" becomes |
---|
| 79 | width="30", height="45". "x20" becomes height="20". |
---|
| 80 | |
---|
| 81 | Examples:: |
---|
| 82 | |
---|
| 83 | >>> image_tag('xml') |
---|
| 84 | '<img alt="Xml" src="/images/xml.png" />' |
---|
| 85 | |
---|
| 86 | >>> image_tag('rss', 'rss syndication') |
---|
| 87 | '<img alt="rss syndication" src="/images/rss.png" />' |
---|
| 88 | """ |
---|
| 89 | options['src'] = compute_public_path(source, 'images', 'png') |
---|
| 90 | |
---|
| 91 | if not alt: |
---|
| 92 | alt = os.path.splitext(os.path.basename(source))[0].title() |
---|
| 93 | options['alt'] = alt |
---|
| 94 | |
---|
| 95 | if size: |
---|
| 96 | width, height = size.split('x') |
---|
| 97 | if width: |
---|
| 98 | options['width'] = width |
---|
| 99 | if height: |
---|
| 100 | options['height'] = height |
---|
| 101 | |
---|
| 102 | return tag('img', **options) |
---|
| 103 | |
---|
| 104 | def javascript_include_tag(*sources, **options): |
---|
| 105 | """ |
---|
| 106 | Returns script include tags for the specified javascript ``sources``. |
---|
| 107 | |
---|
| 108 | Each source's URL path is prepended with '/javascripts/' unless their full path is |
---|
| 109 | specified. Each source's URL path is ultimately prepended with the environment's |
---|
| 110 | ``SCRIPT_NAME`` (the root path of the web application), unless the URL path is a |
---|
| 111 | full-fledged URL (e.g. http://example.com). Sources with no filename extension will be |
---|
| 112 | appended with the '.js' extension. |
---|
| 113 | |
---|
| 114 | Optionally includes (prepended) WebHelpers' built-in javascripts when passed the |
---|
| 115 | ``builtins=True`` keyword argument. |
---|
| 116 | |
---|
| 117 | Examples:: |
---|
| 118 | |
---|
| 119 | >>> print javascript_include_tag(builtins=True) |
---|
| 120 | <script src="/javascripts/prototype.js" type="text/javascript"></script> |
---|
| 121 | <script src="/javascripts/scriptaculous.js" type="text/javascript"></script> |
---|
| 122 | |
---|
| 123 | >>> print javascript_include_tag('prototype', '/other-javascripts/util.js') |
---|
| 124 | <script src="/javascripts/prototype.js" type="text/javascript"></script> |
---|
| 125 | <script src="/other-javascripts/util.js" type="text/javascript"></script> |
---|
| 126 | |
---|
| 127 | >>> print javascript_include_tag('app', '/test/test.1.js', builtins=True) |
---|
| 128 | <script src="/javascripts/prototype.js" type="text/javascript"></script> |
---|
| 129 | <script src="/javascripts/scriptaculous.js" type="text/javascript"></script> |
---|
| 130 | <script src="/javascripts/app.js" type="text/javascript"></script> |
---|
| 131 | <script src="/test/test.1.js" type="text/javascript"></script> |
---|
| 132 | """ |
---|
| 133 | if options.get('builtins'): |
---|
| 134 | sources = javascript_builtins + sources |
---|
| 135 | |
---|
| 136 | tags = [content_tag('script', None, |
---|
| 137 | **dict(type='text/javascript', |
---|
| 138 | src=compute_public_path(source, 'javascripts', 'js'))) \ |
---|
| 139 | for source in sources] |
---|
| 140 | return '\n'.join(tags) |
---|
| 141 | |
---|
| 142 | def stylesheet_link_tag(*sources, **options): |
---|
| 143 | """ |
---|
| 144 | Returns CSS link tags for the specified stylesheet ``sources``. |
---|
| 145 | |
---|
| 146 | Each source's URL path is prepended with '/stylesheets/' unless their full path is |
---|
| 147 | specified. Each source's URL path is ultimately prepended with the environment's |
---|
| 148 | ``SCRIPT_NAME`` (the root path of the web application), unless the URL path is a |
---|
| 149 | full-fledged URL (e.g. http://example.com). Sources with no filename extension will be |
---|
| 150 | appended with the '.css' extension. |
---|
| 151 | |
---|
| 152 | Examples:: |
---|
| 153 | |
---|
| 154 | >>> stylesheet_link_tag('style') |
---|
| 155 | '<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />' |
---|
| 156 | |
---|
| 157 | >>> stylesheet_link_tag('/dir/file', media='all') |
---|
| 158 | '<link href="/dir/file.css" media="all" rel="Stylesheet" type="text/css" />' |
---|
| 159 | """ |
---|
| 160 | tag_options = dict(rel='Stylesheet', type='text/css', media='screen') |
---|
| 161 | tag_options.update(options) |
---|
| 162 | tag_options.pop('href', None) |
---|
| 163 | |
---|
| 164 | tags = [tag('link', **dict(href=compute_public_path(source, 'stylesheets', 'css'), |
---|
| 165 | **tag_options)) for source in sources] |
---|
| 166 | return '\n'.join(tags) |
---|
| 167 | |
---|
| 168 | def compute_public_path(source, root_path=None, ext=None): |
---|
| 169 | """ |
---|
| 170 | Format the specified source for publishing, via the public directory, if applicable. |
---|
| 171 | """ |
---|
| 172 | if ext and not os.path.splitext(os.path.basename(source))[1]: |
---|
| 173 | source = '%s.%s' % (source, ext) |
---|
| 174 | |
---|
| 175 | # Avoid munging fully-fledged URLs, including 'mailto:' |
---|
| 176 | parsed = urlparse.urlparse(source) |
---|
| 177 | if not (parsed[0] and (parsed[1] or parsed[2])): |
---|
| 178 | # Prefix apps deployed under any SCRIPT_NAME path |
---|
| 179 | if not root_path or source.startswith('/'): |
---|
| 180 | source = '%s%s' % (get_script_name(), source) |
---|
| 181 | else: |
---|
| 182 | source = '%s/%s/%s' % (get_script_name(), root_path, source) |
---|
| 183 | return source |
---|
| 184 | |
---|
| 185 | def get_script_name(): |
---|
| 186 | """ |
---|
| 187 | Determine the current web application's ``SCRIPT_NAME``. |
---|
| 188 | """ |
---|
| 189 | script_name = '' |
---|
| 190 | config = request_config() |
---|
| 191 | if hasattr(config, 'environ'): |
---|
| 192 | script_name = config.environ.get('SCRIPT_NAME', '') |
---|
| 193 | return script_name |
---|
| 194 | |
---|
| 195 | __all__ = ['javascript_path', 'javascript_builtins', 'auto_discovery_link_tag', |
---|
| 196 | 'image_tag', 'javascript_include_tag', 'stylesheet_link_tag'] |
---|