comparison piecrust/appconfig.py @ 675:3df808b133f8

internal: Improve how theme configuration is validated and merged. * Add default theme config up-front so it benefits from the usual validation. * Add an explicit `use_default_theme_content` setting. * Add/fix unit tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 06 Mar 2016 23:49:41 -0800
parents 81d9c3a3a0b5
children 894d286b348f
comparison
equal deleted inserted replaced
674:f987b29d6fab 675:3df808b133f8
168 values['site']['theme_site'] = True 168 values['site']['theme_site'] = True
169 169
170 # Figure out if we need to generate the configuration for the 170 # Figure out if we need to generate the configuration for the
171 # default content model. 171 # default content model.
172 sitec = values.setdefault('site', {}) 172 sitec = values.setdefault('site', {})
173 if ( 173 gen_default_model = bool(sitec.get('use_default_content'))
174 ('sources' not in sitec and 174 if gen_default_model:
175 'routes' not in sitec and
176 'taxonomies' not in sitec) or
177 sitec.get('use_default_content')):
178 logger.debug("Generating default content model...") 175 logger.debug("Generating default content model...")
179 values = self._generateDefaultContentModel(values) 176 values = self._generateDefaultContentModel(values)
180 177
181 # Add a section for our cached information. 178 # Add a section for our cached information.
182 cachec = collections.OrderedDict() 179 cachec = collections.OrderedDict()
267 264
268 265
269 default_content_model_base = collections.OrderedDict({ 266 default_content_model_base = collections.OrderedDict({
270 'site': collections.OrderedDict({ 267 'site': collections.OrderedDict({
271 'posts_fs': DEFAULT_POSTS_FS, 268 'posts_fs': DEFAULT_POSTS_FS,
272 'date_format': DEFAULT_DATE_FORMAT,
273 'default_page_layout': 'default', 269 'default_page_layout': 'default',
274 'default_post_layout': 'post', 270 'default_post_layout': 'post',
275 'post_url': '%year%/%month%/%day%/%slug%', 271 'post_url': '%year%/%month%/%day%/%slug%',
276 'tag_url': 'tag/%tag%', 272 'tag_url': 'tag/%tag%',
277 'category_url': '%category%', 273 'category_url': '%category%',
322 url_prefix = '' 318 url_prefix = ''
323 tax_page_prefix = '' 319 tax_page_prefix = ''
324 fs_endpoint = 'posts' 320 fs_endpoint = 'posts'
325 data_endpoint = 'blog' 321 data_endpoint = 'blog'
326 item_name = 'post' 322 item_name = 'post'
323
324 if theme_site:
325 # If this is a theme site, show posts from a `sample` directory
326 # so it's clearer that those won't show up when the theme is
327 # actually applied to a normal site.
328 fs_endpoint = 'sample/posts'
327 else: 329 else:
328 url_prefix = blog_name + '/' 330 url_prefix = blog_name + '/'
329 tax_page_prefix = blog_name + '/' 331 tax_page_prefix = blog_name + '/'
330 fs_endpoint = 'posts/%s' % blog_name 332 fs_endpoint = 'posts/%s' % blog_name
331 data_endpoint = blog_name 333 data_endpoint = blog_name
349 url_prefix + values['site']['category_url']).lstrip('/') 351 url_prefix + values['site']['category_url']).lstrip('/')
350 352
351 tags_taxonomy = 'pages:%s_tag.%%ext%%' % tax_page_prefix 353 tags_taxonomy = 'pages:%s_tag.%%ext%%' % tax_page_prefix
352 category_taxonomy = 'pages:%s_category.%%ext%%' % tax_page_prefix 354 category_taxonomy = 'pages:%s_category.%%ext%%' % tax_page_prefix
353 if not theme_site: 355 if not theme_site:
354 tags_taxonomy += ';theme_pages:_tag.%ext%' 356 theme_tag_page = values['site'].get('theme_tag_page')
355 category_taxonomy += ';theme_pages:_category.%ext%' 357 if theme_tag_page:
358 tags_taxonomy += ';' + theme_tag_page
359 theme_category_page = values['site'].get('theme_category_page')
360 if theme_category_page:
361 category_taxonomy += ';' + theme_category_page
356 362
357 return collections.OrderedDict({ 363 return collections.OrderedDict({
358 'site': collections.OrderedDict({ 364 'site': collections.OrderedDict({
359 'sources': collections.OrderedDict({ 365 'sources': collections.OrderedDict({
360 blog_name: collections.OrderedDict({ 366 blog_name: collections.OrderedDict({
474 raise ConfigurationError("There are no sources defined.") 480 raise ConfigurationError("There are no sources defined.")
475 if not isinstance(v, dict): 481 if not isinstance(v, dict):
476 raise ConfigurationError("The 'site/sources' setting must be a " 482 raise ConfigurationError("The 'site/sources' setting must be a "
477 "dictionary.") 483 "dictionary.")
478 484
479 theme_site = values['site']['theme_site']
480 if not theme_site:
481 # Add the theme page source if no sources were defined in the theme
482 # configuration itself.
483 has_any_theme_source = False
484 for sn, sc in v.items():
485 if sc.get('realm') == REALM_THEME:
486 has_any_theme_source = True
487 break
488 if not has_any_theme_source:
489 v['theme_pages'] = {
490 'theme_source': True,
491 'fs_endpoint': 'pages',
492 'ignore_missing_dir': True,
493 'data_endpoint': 'site/pages',
494 'item_name': 'page',
495 'realm': REALM_THEME}
496 values['site']['routes'].append({
497 'url': '/%path:slug%',
498 'source': 'theme_pages',
499 'func': 'pcurl(slug)'})
500
501 # Sources have the `default` scanner by default, duh. Also, a bunch 485 # Sources have the `default` scanner by default, duh. Also, a bunch
502 # of other default values for other configuration stuff. 486 # of other default values for other configuration stuff.
503 reserved_endpoints = set(['piecrust', 'site', 'page', 'route', 487 reserved_endpoints = set(['piecrust', 'site', 'page', 'route',
504 'assets', 'pagination', 'siblings', 488 'assets', 'pagination', 'siblings',
505 'family']) 489 'family'])