comparison piecrust/sources/base.py @ 170:c3831a762bc2

sources: Make the `SimplePageSource` more extensible, fix bugs in `prose` source. The `SimplePageSource` now calls a `_populateMetadata` function that subclasses can override to add/edit their custom metadata everwhere it would be returned to the system.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 03 Jan 2015 20:49:00 -0800
parents 8d16ca75075f
children d5991525801d
comparison
equal deleted inserted replaced
169:f3aa511eef99 170:c3831a762bc2
315 315
316 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path): 316 for dirpath, dirnames, filenames in os.walk(self.fs_endpoint_path):
317 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path) 317 rel_dirpath = os.path.relpath(dirpath, self.fs_endpoint_path)
318 dirnames[:] = list(filter(self._filterPageDirname, dirnames)) 318 dirnames[:] = list(filter(self._filterPageDirname, dirnames))
319 for f in filter(self._filterPageFilename, filenames): 319 for f in filter(self._filterPageFilename, filenames):
320 slug, ext = os.path.splitext(os.path.join(rel_dirpath, f))
321 slug = slug.replace('\\', '/')
322 if ext.lstrip('.') not in self.supported_extensions:
323 slug += ext
324 if slug.startswith('./') or slug.startswith('.\\'):
325 slug = slug[2:]
326 if slug == '_index':
327 slug = ''
328 metadata = {'path': slug}
329 fac_path = f 320 fac_path = f
330 if rel_dirpath != '.': 321 if rel_dirpath != '.':
331 fac_path = os.path.join(rel_dirpath, f) 322 fac_path = os.path.join(rel_dirpath, f)
323 slug = self._makeSlug(fac_path)
324 metadata = {'path': slug}
332 fac_path = fac_path.replace('\\', '/') 325 fac_path = fac_path.replace('\\', '/')
326 self._populateMetadata(fac_path, metadata)
333 yield PageFactory(self, fac_path, metadata) 327 yield PageFactory(self, fac_path, metadata)
334 328
335 def resolveRef(self, ref_path): 329 def resolveRef(self, ref_path):
336 return os.path.normpath( 330 return os.path.normpath(
337 os.path.join(self.fs_endpoint_path, ref_path)) 331 os.path.join(self.fs_endpoint_path, ref_path))
338 332
339 def findPagePath(self, metadata, mode): 333 def findPagePath(self, metadata, mode):
340 uri_path = metadata['path'] 334 uri_path = metadata.setdefault('path', '')
341 if uri_path == '': 335 if not uri_path:
342 uri_path = '_index' 336 uri_path = '_index'
343 path = os.path.normpath(os.path.join(self.fs_endpoint_path, uri_path)) 337 path = os.path.normpath(os.path.join(self.fs_endpoint_path, uri_path))
344 _, ext = os.path.splitext(path) 338 _, ext = os.path.splitext(path)
345 339
346 if mode == MODE_CREATING: 340 if mode == MODE_CREATING:
347 if ext == '': 341 if ext == '':
348 path = '%s.%s' % (path, self.default_auto_format) 342 path = '%s.%s' % (path, self.default_auto_format)
349 rel_path = os.path.relpath(path, self.fs_endpoint_path) 343 rel_path = os.path.relpath(path, self.fs_endpoint_path)
350 rel_path = rel_path.replace('\\', '/') 344 rel_path = rel_path.replace('\\', '/')
345 self._populateMetadata(rel_path, metadata)
351 return rel_path, metadata 346 return rel_path, metadata
352 347
353 if ext == '': 348 if ext == '':
354 paths_to_check = ['%s.%s' % (path, e) 349 paths_to_check = [
350 '%s.%s' % (path, e)
355 for e in self.supported_extensions] 351 for e in self.supported_extensions]
356 else: 352 else:
357 paths_to_check = [path] 353 paths_to_check = [path]
358 for path in paths_to_check: 354 for path in paths_to_check:
359 if os.path.isfile(path): 355 if os.path.isfile(path):
360 rel_path = os.path.relpath(path, self.fs_endpoint_path) 356 rel_path = os.path.relpath(path, self.fs_endpoint_path)
361 rel_path = rel_path.replace('\\', '/') 357 rel_path = rel_path.replace('\\', '/')
358 self._populateMetadata(rel_path, metadata)
362 return rel_path, metadata 359 return rel_path, metadata
363 360
364 return None, None 361 return None, None
362
363 def _makeSlug(self, rel_path):
364 slug, ext = os.path.splitext(rel_path)
365 slug = slug.replace('\\', '/')
366 if ext.lstrip('.') not in self.supported_extensions:
367 slug += ext
368 if slug.startswith('./') or slug.startswith('.\\'):
369 slug = slug[2:]
370 if slug == '_index':
371 slug = ''
372 return slug
365 373
366 def _filterPageDirname(self, d): 374 def _filterPageDirname(self, d):
367 return not d.endswith('-assets') 375 return not d.endswith('-assets')
368 376
369 def _filterPageFilename(self, f): 377 def _filterPageFilename(self, f):
370 name, ext = os.path.splitext(f)
371 return (f[0] != '.' and # .DS_store and other crap 378 return (f[0] != '.' and # .DS_store and other crap
372 f[-1] != '~' and # Vim temp files and what-not 379 f[-1] != '~' and # Vim temp files and what-not
373 f not in ['Thumbs.db']) # Windows bullshit 380 f not in ['Thumbs.db']) # Windows bullshit
374 381
382 def _populateMetadata(self, rel_path, metadata):
383 pass
375 384
376 class DefaultPageSource(SimplePageSource, IPreparingSource, 385 class DefaultPageSource(SimplePageSource, IPreparingSource,
377 SimplePaginationSourceMixin): 386 SimplePaginationSourceMixin):
378 SOURCE_NAME = 'default' 387 SOURCE_NAME = 'default'
379 388