comparison piecrust/data/assetor.py @ 32:43091c9837bf

Fix problems with asset URLs.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 19 Aug 2014 14:30:19 -0700
parents 65ae19c4e8a3
children 62c7a97c8340
comparison
equal deleted inserted replaced
31:8c15fc45d712 32:43091c9837bf
1 import re
1 import os 2 import os
2 import os.path 3 import os.path
3 import logging 4 import logging
4 from piecrust.uriutil import multi_replace 5 from piecrust.uriutil import multi_replace
5 6
9 10
10 class UnsupportedAssetsError(Exception): 11 class UnsupportedAssetsError(Exception):
11 pass 12 pass
12 13
13 14
14 def build_base_url(app, uri, assets_path): 15 def build_base_url(app, uri, rel_assets_path):
15 base_url_format = app.env.base_asset_url_format 16 base_url_format = app.env.base_asset_url_format
16 site_root = app.config.get('site/root') 17 rel_assets_path = rel_assets_path.replace('\\', '/')
17 rel_path = os.path.relpath(assets_path, app.root_dir).replace('\\', '/') 18
19 # Remove any extension or pagination suffix from the URL, since
20 # we'll be copying assets into the 1st sub-page's folder.
18 pretty = app.config.get('site/pretty_urls') 21 pretty = app.config.get('site/pretty_urls')
19 if not pretty: 22 if not pretty:
20 uri, _ = os.path.splitext(uri) 23 uri, _ = os.path.splitext(uri)
21 uri = uri.lstrip('/') 24 pgn_suffix_re = app.config.get('__cache/pagination_suffix_re')
25 m = re.search(pgn_suffix_re, uri)
26 if m:
27 uri = uri[:m.start():]
28
22 base_url = multi_replace( 29 base_url = multi_replace(
23 base_url_format, 30 base_url_format,
24 { 31 {
25 '%site_root%': site_root, 32 '%path%': rel_assets_path,
26 '%path%': rel_path,
27 '%uri%': uri}) 33 '%uri%': uri})
28 return base_url.rstrip('/') + '/' 34 return base_url.rstrip('/') + '/'
29 35
30 36
31 class Assetor(object): 37 class Assetor(object):
72 name, ext = os.path.splitext(self._page.path) 78 name, ext = os.path.splitext(self._page.path)
73 assets_dir = name + Assetor.ASSET_DIR_SUFFIX 79 assets_dir = name + Assetor.ASSET_DIR_SUFFIX
74 if not os.path.isdir(assets_dir): 80 if not os.path.isdir(assets_dir):
75 return 81 return
76 82
77 base_url = build_base_url(self._page.app, self._uri, assets_dir) 83 rel_assets_dir = os.path.relpath(assets_dir, self._page.app.root_dir)
84 base_url = build_base_url(self._page.app, self._uri, rel_assets_dir)
78 for fn in os.listdir(assets_dir): 85 for fn in os.listdir(assets_dir):
79 full_fn = os.path.join(assets_dir, fn) 86 full_fn = os.path.join(assets_dir, fn)
80 if not os.path.isfile(full_fn): 87 if not os.path.isfile(full_fn):
81 raise Exception("Skipping: %s" % full_fn) 88 raise Exception("Skipping: %s" % full_fn)
82 continue 89 continue