annotate piecrust/generation/taxonomy.py @ 840:7f3043f9f26f

internal: Don't check for a page repository, there's always one.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 15 Feb 2017 22:17:13 -0800
parents 58ebf50235a5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import time
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 import unidecode
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from piecrust.chefutil import format_timed, format_timed_scope
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 from piecrust.configuration import ConfigurationError
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 from piecrust.data.filters import (
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 PaginationFilter, SettingFilterClause,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 page_value_accessor)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 from piecrust.generation.base import PageGenerator, InvalidRecordExtraKey
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
11 from piecrust.routing import RouteParameter
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 logger = logging.getLogger(__name__)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 SLUGIFY_ENCODE = 1
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 SLUGIFY_TRANSLITERATE = 2
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 SLUGIFY_LOWERCASE = 4
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 SLUGIFY_DOT_TO_DASH = 8
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 SLUGIFY_SPACE_TO_DASH = 16
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 re_first_dot_to_dash = re.compile(r'^\.+')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 re_dot_to_dash = re.compile(r'\.+')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 re_space_to_dash = re.compile(r'\s+')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 class Taxonomy(object):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 def __init__(self, name, config):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 self.name = name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 self.config = config
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 self.term_name = config.get('term', name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 self.is_multiple = bool(config.get('multiple', False))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 self.separator = config.get('separator', '/')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 self.page_ref = config.get('page')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 @property
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 def setting_name(self):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 if self.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 return self.name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 return self.term_name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 class TaxonomyPageGenerator(PageGenerator):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
46 """ A page generator that handles taxonomies, _i.e._ lists of keywords
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
47 that pages are labelled with, and for which we need to generate
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
48 listing pages.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
49 """
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 GENERATOR_NAME = 'taxonomy'
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 def __init__(self, app, name, config):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53 super(TaxonomyPageGenerator, self).__init__(app, name, config)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 tax_name = config.get('taxonomy')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 if tax_name is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 raise ConfigurationError(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58 "Generator '%s' requires a taxonomy name." % name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 tax_config = app.config.get('site/taxonomies/' + tax_name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 if tax_config is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 raise ConfigurationError(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 "Error initializing generator '%s', no such taxonomy: %s",
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 (name, tax_name))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 self.taxonomy = Taxonomy(tax_name, tax_config)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 sm = config.get('slugify_mode')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 if not sm:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 sm = app.config.get('site/slugify_mode', 'encode')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 self.slugify_mode = _parse_slugify_mode(sm)
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
70 self.slugifier = _Slugifier(self.taxonomy, self.slugify_mode)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
71
792
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
72 def getSupportedRouteParameters(self):
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
73 name = self.taxonomy.term_name
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
74 param_type = (RouteParameter.TYPE_PATH if self.taxonomy.is_multiple
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
75 else RouteParameter.TYPE_STRING)
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
76 return [RouteParameter(name, param_type,
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
77 variadic=self.taxonomy.is_multiple)]
58ebf50235a5 routing: Simplify how routes are defined.
Ludovic Chabant <ludovic@chabant.com>
parents: 789
diff changeset
78
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
79 def slugify(self, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
80 return self.slugifier.slugify(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
81
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
82 def slugifyMultiple(self, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
83 return self.slugifier.slugifyMultiple(terms)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
85 def prepareRenderContext(self, ctx):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
86 # Set the pagination source as the source we're generating for.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
87 ctx.pagination_source = self.source
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
89 # Get the taxonomy terms from the route metadata... this can come from
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
90 # the browser's URL (while serving) or from the baking (see `bake`
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
91 # method below). In both cases, we expect to have the *slugified*
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
92 # version of the term, because we're going to set a filter that also
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
93 # slugifies the terms found on each page.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
94 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
95 # This is because:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
96 # * while serving, we get everything from the request URL, so we only
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
97 # have the slugified version.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
98 # * if 2 slightly different terms "collide" into the same slugified
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
99 # term, we'll get a merge of the 2 on the listing page, which is
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
100 # what the user expects.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
101 #
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 tax_terms, is_combination = self._getTaxonomyTerms(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 ctx.page.route_metadata)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 self._setTaxonomyFilter(ctx, tax_terms, is_combination)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
106 # Add some custom data for rendering.
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
107 ctx.custom_data.update({
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 self.taxonomy.term_name: tax_terms,
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
109 'is_multiple_%s' % self.taxonomy.term_name: is_combination})
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
110 # Add some "plural" version of the term... so for instance, if this
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
111 # is the "tags" taxonomy, "tag" will have one term most of the time,
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
112 # except when it's a combination. Here, we add "tags" as something that
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
113 # is always a tuple, even when it's not a combination.
720
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
114 if (self.taxonomy.is_multiple and
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
115 self.taxonomy.name != self.taxonomy.term_name):
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
116 mult_val = tax_terms
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
117 if not is_combination:
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
118 mult_val = (mult_val,)
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
119 ctx.custom_data[self.taxonomy.name] = mult_val
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121 def _getTaxonomyTerms(self, route_metadata):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
122 # Get the individual slugified terms from the route metadata.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 all_values = route_metadata.get(self.taxonomy.term_name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 if all_values is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 raise Exception("'%s' values couldn't be found in route metadata" %
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 self.taxonomy.term_name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
127
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
128 # If it's a "multiple" taxonomy, we need to potentially split the
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
129 # route value into the individual terms (_e.g._ when listing all pages
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
130 # that have 2 given tags, we need to get each of those 2 tags).
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 if self.taxonomy.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 sep = self.taxonomy.separator
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 if sep in all_values:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 return tuple(all_values.split(sep)), True
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
135 # Not a "multiple" taxonomy, so there's only the one value.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 return all_values, False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 def _setTaxonomyFilter(self, ctx, term_value, is_combination):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
139 # Set up the filter that will check the pages' terms.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 flt = PaginationFilter(value_accessor=page_value_accessor)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 flt.addClause(HasTaxonomyTermsFilterClause(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142 self.taxonomy, self.slugify_mode, term_value, is_combination))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 ctx.pagination_filter = flt
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
145 def onRouteFunctionUsed(self, route, route_metadata):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
146 # Get the values, and slugify them appropriately.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147 values = route_metadata[self.taxonomy.term_name]
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 if self.taxonomy.is_multiple:
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
149 # TODO: here we assume the route has been properly configured.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
150 slugified_values = self.slugifyMultiple((str(v) for v in values))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
151 route_val = self.taxonomy.separator.join(slugified_values)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 else:
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
153 slugified_values = self.slugify(str(values))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
154 route_val = slugified_values
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156 # We need to register this use of a taxonomy term.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 eis = self.app.env.exec_info_stack
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 cpi = eis.current_page_info.render_ctx.current_pass_info
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 if cpi:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 utt = cpi.getCustomInfo('used_taxonomy_terms', [], True)
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
161 utt.append(slugified_values)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
163 # Put the slugified values in the route metadata so they're used to
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
164 # generate the URL.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
165 route_metadata[self.taxonomy.term_name] = route_val
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 def bake(self, ctx):
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
168 if not self.page_ref.exists:
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
169 logger.debug(
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
170 "No page found at '%s', skipping taxonomy '%s'." %
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
171 (self.page_ref, self.taxonomy.name))
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
172 return
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
173
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
174 logger.debug("Baking %s pages...", self.taxonomy.name)
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
175 analyzer = _TaxonomyTermsAnalyzer(self.source_name, self.taxonomy,
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
176 self.slugify_mode)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 with format_timed_scope(logger, 'gathered taxonomy terms',
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 level=logging.DEBUG, colored=False):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
179 analyzer.analyze(ctx)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181 start_time = time.perf_counter()
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
182 page_count = self._bakeTaxonomyTerms(ctx, analyzer)
736
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
183 if page_count > 0:
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
184 logger.info(format_timed(
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
185 start_time,
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
186 "baked %d %s pages for %s." % (
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
187 page_count, self.taxonomy.term_name, self.source_name)))
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
189 def _bakeTaxonomyTerms(self, ctx, analyzer):
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 # Start baking those terms.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 logger.debug(
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
192 "Baking '%s' for source '%s': %d terms" %
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
193 (self.taxonomy.name, self.source_name,
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
194 len(analyzer.dirty_slugified_terms)))
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 route = self.app.getGeneratorRoute(self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 if route is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 raise Exception("No routes have been defined for generator: %s" %
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 logger.debug("Using taxonomy page: %s" % self.page_ref)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 fac = self.page_ref.getFactory()
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 job_count = 0
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
205 for slugified_term in analyzer.dirty_slugified_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
206 extra_route_metadata = {
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
207 self.taxonomy.term_name: slugified_term}
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
209 # Use the slugified term as the record's extra key seed.
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 logger.debug(
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
211 "Queuing: %s [%s=%s]" %
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
212 (fac.ref_spec, self.taxonomy.name, slugified_term))
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 ctx.queueBakeJob(fac, route, extra_route_metadata, slugified_term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214 job_count += 1
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
215 ctx.runJobQueue()
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
217 # Now we create bake entries for all the terms that were *not* dirty.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 # This is because otherwise, on the next incremental bake, we wouldn't
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 # find any entry for those things, and figure that we need to delete
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 # their outputs.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 for prev_entry, cur_entry in ctx.getAllPageRecords():
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 # Only consider taxonomy-related entries that don't have any
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 # current version (i.e. they weren't baked just now).
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
224 if prev_entry and not cur_entry:
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 try:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 t = ctx.getSeedFromRecordExtraKey(prev_entry.extra_key)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 except InvalidRecordExtraKey:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 continue
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
230 if analyzer.isKnownSlugifiedTerm(t):
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 logger.debug("Creating unbaked entry for %s term: %s" %
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 (self.name, t))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 ctx.collapseRecord(prev_entry)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235 logger.debug("Term %s in %s isn't used anymore." %
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236 (self.name, t))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 return job_count
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
240
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
241 class HasTaxonomyTermsFilterClause(SettingFilterClause):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 def __init__(self, taxonomy, slugify_mode, value, is_combination):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243 super(HasTaxonomyTermsFilterClause, self).__init__(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
244 taxonomy.setting_name, value)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
245 self._taxonomy = taxonomy
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
246 self._is_combination = is_combination
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247 self._slugifier = _Slugifier(taxonomy, slugify_mode)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
249 def pageMatches(self, fil, page):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250 if self._taxonomy.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
251 # Multiple taxonomy, i.e. it supports multiple terms, like tags.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
252 page_values = fil.value_accessor(page, self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
253 if page_values is None or not isinstance(page_values, list):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
254 return False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
255
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
256 page_set = set(map(self._slugifier.slugify, page_values))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257 if self._is_combination:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258 # Multiple taxonomy, and multiple terms to match. Check that
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 # the ones to match are all in the page's terms.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
260 value_set = set(self.value)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
261 return value_set.issubset(page_set)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
263 # Multiple taxonomy, one term to match.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
264 return self.value in page_set
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
265 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
266 # Single taxonomy. Just compare the values.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
267 page_value = fil.value_accessor(page, self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
268 if page_value is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
269 return False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
270 page_value = self._slugifier.slugify(page_value)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
271 return page_value == self.value
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
272
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
273
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
274 class _TaxonomyTermsAnalyzer(object):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
275 def __init__(self, source_name, taxonomy, slugify_mode):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
276 self.source_name = source_name
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
277 self.taxonomy = taxonomy
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
278 self.slugifier = _Slugifier(taxonomy, slugify_mode)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
279 self._all_terms = {}
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
280 self._single_dirty_slugified_terms = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
281 self._all_dirty_slugified_terms = None
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
282
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
283 @property
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
284 def dirty_slugified_terms(self):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
285 """ Returns the slugified terms that have been 'dirtied' during
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
286 this bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
287 """
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
288 return self._all_dirty_slugified_terms
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
289
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
290 def isKnownSlugifiedTerm(self, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
291 """ Returns whether the given slugified term has been seen during
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
292 this bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
293 """
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
294 return term in self._all_terms
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
295
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
296 def analyze(self, ctx):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
297 # Build the list of terms for our taxonomy, and figure out which ones
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
298 # are 'dirty' for the current bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
299 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
300 # Remember all terms used.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
301 for _, cur_entry in ctx.getAllPageRecords():
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
302 if cur_entry and not cur_entry.was_overriden:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
303 cur_terms = cur_entry.config.get(self.taxonomy.setting_name)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
304 if cur_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
305 if not self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
306 self._addTerm(cur_entry.path, cur_terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
307 else:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
308 self._addTerms(cur_entry.path, cur_terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
309
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
310 # Re-bake all taxonomy terms that include new or changed pages, by
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
311 # marking them as 'dirty'.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
312 for prev_entry, cur_entry in ctx.getBakedPageRecords():
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
313 if cur_entry.source_name != self.source_name:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
314 continue
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
315
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
316 entries = [cur_entry]
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
317 if prev_entry:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
318 entries.append(prev_entry)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
319
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
320 for e in entries:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
321 entry_terms = e.config.get(self.taxonomy.setting_name)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
322 if entry_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
323 if not self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
324 self._single_dirty_slugified_terms.add(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
325 self.slugifier.slugify(entry_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
326 else:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
327 self._single_dirty_slugified_terms.update(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
328 (self.slugifier.slugify(t)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
329 for t in entry_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
330
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
331 self._all_dirty_slugified_terms = list(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
332 self._single_dirty_slugified_terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
333 logger.debug("Gathered %d dirty taxonomy terms",
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
334 len(self._all_dirty_slugified_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
335
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
336 # Re-bake the combination pages for terms that are 'dirty'.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
337 # We make all terms into tuple, even those that are not actual
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
338 # combinations, so that we have less things to test further down the
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
339 # line.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
340 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
341 # Add the combinations to that list. We get those combinations from
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
342 # wherever combinations were used, so they're coming from the
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
343 # `onRouteFunctionUsed` method.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
344 if self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
345 known_combinations = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
346 for _, cur_entry in ctx.getAllPageRecords():
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
347 if cur_entry:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
348 used_terms = _get_all_entry_taxonomy_terms(cur_entry)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
349 for terms in used_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
350 if len(terms) > 1:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
351 known_combinations.add(terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
352
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
353 dcc = 0
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
354 for terms in known_combinations:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
355 if not self._single_dirty_slugified_terms.isdisjoint(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
356 set(terms)):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
357 self._all_dirty_slugified_terms.append(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
358 self.taxonomy.separator.join(terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
359 dcc += 1
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
360 logger.debug("Gathered %d term combinations, with %d dirty." %
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
361 (len(known_combinations), dcc))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
362
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
363 def _addTerms(self, entry_path, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
364 for t in terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
365 self._addTerm(entry_path, t)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
366
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
367 def _addTerm(self, entry_path, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
368 st = self.slugifier.slugify(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
369 orig_terms = self._all_terms.setdefault(st, [])
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
370 if orig_terms and orig_terms[0] != term:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
371 logger.warning(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
372 "Term '%s' in '%s' is slugified to '%s' which conflicts with "
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
373 "previously existing '%s'. The two will be merged." %
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
374 (term, entry_path, st, orig_terms[0]))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
375 orig_terms.append(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
376
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
377
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
378 def _get_all_entry_taxonomy_terms(entry):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
379 res = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
380 for o in entry.subs:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
381 for pinfo in o.render_info:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
382 if pinfo:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
383 terms = pinfo.getCustomInfo('used_taxonomy_terms')
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
384 if terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
385 res |= set(terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
386 return res
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
387
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
388
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
389 class _Slugifier(object):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
390 def __init__(self, taxonomy, mode):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
391 self.taxonomy = taxonomy
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
392 self.mode = mode
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
393
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
394 def slugifyMultiple(self, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
395 return tuple(map(self.slugify, terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
396
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
397 def slugify(self, term):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
398 if self.mode & SLUGIFY_TRANSLITERATE:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
399 term = unidecode.unidecode(term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
400 if self.mode & SLUGIFY_LOWERCASE:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
401 term = term.lower()
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
402 if self.mode & SLUGIFY_DOT_TO_DASH:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
403 term = re_first_dot_to_dash.sub('', term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
404 term = re_dot_to_dash.sub('-', term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
405 if self.mode & SLUGIFY_SPACE_TO_DASH:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
406 term = re_space_to_dash.sub('-', term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
407 return term
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
408
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
409
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
410 def _parse_slugify_mode(value):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
411 mapping = {
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
412 'encode': SLUGIFY_ENCODE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
413 'transliterate': SLUGIFY_TRANSLITERATE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
414 'lowercase': SLUGIFY_LOWERCASE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
415 'dot_to_dash': SLUGIFY_DOT_TO_DASH,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
416 'space_to_dash': SLUGIFY_SPACE_TO_DASH}
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
417 mode = 0
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
418 for v in value.split(','):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
419 f = mapping.get(v.strip())
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
420 if f is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
421 if v == 'iconv':
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
422 raise Exception("'iconv' is not supported as a slugify mode "
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
423 "in PieCrust2. Use 'transliterate'.")
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
424 raise Exception("Unknown slugify flag: %s" % v)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
425 mode |= f
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
426 return mode
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
427