Mercurial > piecrust2
comparison piecrust/baking/baker.py @ 25:65ae19c4e8a3
Copy page assets to bake output, use correct slashes when serving assets.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 19 Aug 2014 11:07:42 -0700 |
parents | 923699e816d0 |
children | 43091c9837bf |
comparison
equal
deleted
inserted
replaced
24:644869022b6e | 25:65ae19c4e8a3 |
---|---|
1 import time | 1 import time |
2 import os.path | 2 import os.path |
3 import codecs | 3 import codecs |
4 import urllib.request, urllib.error, urllib.parse | 4 import shutil |
5 import hashlib | 5 import hashlib |
6 import logging | 6 import logging |
7 import threading | 7 import threading |
8 import urllib.request, urllib.error, urllib.parse | |
8 from queue import Queue, Empty | 9 from queue import Queue, Empty |
9 from piecrust.baking.records import TransitionalBakeRecord, BakeRecordPageEntry | 10 from piecrust.baking.records import TransitionalBakeRecord, BakeRecordPageEntry |
10 from piecrust.chefutil import format_timed | 11 from piecrust.chefutil import format_timed |
11 from piecrust.data.filters import (PaginationFilter, HasFilterClause, | 12 from piecrust.data.filters import (PaginationFilter, HasFilterClause, |
12 IsFilterClause, AndBooleanClause) | 13 IsFilterClause, AndBooleanClause) |
19 logger = logging.getLogger(__name__) | 20 logger = logging.getLogger(__name__) |
20 | 21 |
21 | 22 |
22 class PageBaker(object): | 23 class PageBaker(object): |
23 def __init__(self, app, out_dir, force=False, record=None, | 24 def __init__(self, app, out_dir, force=False, record=None, |
24 copy_assets=False): | 25 copy_assets=True): |
25 self.app = app | 26 self.app = app |
26 self.out_dir = out_dir | 27 self.out_dir = out_dir |
27 self.force = force | 28 self.force = force |
28 self.record = record | 29 self.record = record |
29 self.force = force | 30 self.force = force |
30 self.copy_assets = copy_assets | 31 self.copy_assets = copy_assets |
32 self.site_root = app.config.get('site/root') | |
31 self.pretty_urls = app.config.get('site/pretty_urls') | 33 self.pretty_urls = app.config.get('site/pretty_urls') |
32 self.pagination_suffix = app.config.get('site/pagination_suffix') | 34 self.pagination_suffix = app.config.get('site/pagination_suffix') |
33 | 35 |
34 def getOutputUri(self, uri, num): | 36 def getOutputUri(self, uri, num): |
35 suffix = self.pagination_suffix.replace('%num%', str(num)) | 37 suffix = self.pagination_suffix.replace('%num%', str(num)) |
156 pagination_filter, custom_data) | 158 pagination_filter, custom_data) |
157 except Exception as ex: | 159 except Exception as ex: |
158 raise Exception("Error baking page '%s' for URI '%s'." % | 160 raise Exception("Error baking page '%s' for URI '%s'." % |
159 (page.ref_spec, uri)) from ex | 161 (page.ref_spec, uri)) from ex |
160 | 162 |
163 # Copy page assets. | |
164 if (cur_sub == 1 and self.copy_assets and | |
165 ctx.used_assets is not None): | |
166 if self.pretty_urls: | |
167 out_assets_dir = os.path.dirname(out_path) | |
168 else: | |
169 out_assets_dir, out_name = os.path.split(out_path) | |
170 if sub_uri != self.site_root: | |
171 out_name_noext, _ = os.path.splitext(out_name) | |
172 out_assets_dir += out_name_noext | |
173 | |
174 logger.debug("Copying page assets to: %s" % out_assets_dir) | |
175 if not os.path.isdir(out_assets_dir): | |
176 os.makedirs(out_assets_dir, 0o755) | |
177 for ap in ctx.used_assets._getAssetPaths(): | |
178 dest_ap = os.path.join(out_assets_dir, os.path.basename(ap)) | |
179 logger.debug(" %s -> %s" % (ap, dest_ap)) | |
180 shutil.copy(ap, dest_ap) | |
181 | |
182 # Record what we did and figure out if we have more work. | |
161 cur_record_entry.out_uris.append(sub_uri) | 183 cur_record_entry.out_uris.append(sub_uri) |
162 cur_record_entry.out_paths.append(out_path) | 184 cur_record_entry.out_paths.append(out_path) |
163 cur_record_entry.used_source_names |= ctx.used_source_names | 185 cur_record_entry.used_source_names |= ctx.used_source_names |
164 cur_record_entry.used_taxonomy_terms |= ctx.used_taxonomy_terms | 186 cur_record_entry.used_taxonomy_terms |= ctx.used_taxonomy_terms |
165 | 187 |