annotate piecrust/generation/taxonomy.py @ 789:b8e760b3413e

bake: Fix how slugified taxonomy terms are handled. This fixes a problem where multiple terms all slugifying to the same thing would lead to a fatal bake error.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 05 Sep 2016 21:03:00 -0700
parents 661f7ba15762
children 58ebf50235a5
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
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
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 logger = logging.getLogger(__name__)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14
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 SLUGIFY_ENCODE = 1
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 SLUGIFY_TRANSLITERATE = 2
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 SLUGIFY_LOWERCASE = 4
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 SLUGIFY_DOT_TO_DASH = 8
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 SLUGIFY_SPACE_TO_DASH = 16
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21
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 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
24 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
25 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
26
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 class Taxonomy(object):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 def __init__(self, name, config):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 self.name = name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 self.config = config
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 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
33 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
34 self.separator = config.get('separator', '/')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 self.page_ref = config.get('page')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 @property
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def setting_name(self):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 if self.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 return self.name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 return self.term_name
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42
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 class TaxonomyPageGenerator(PageGenerator):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
45 """ 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
46 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
47 listing pages.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
48 """
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 GENERATOR_NAME = 'taxonomy'
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 def __init__(self, app, name, config):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 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
53
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 tax_name = config.get('taxonomy')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 if tax_name is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 raise ConfigurationError(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 "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
58 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
59 if tax_config is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60 raise ConfigurationError(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 "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
62 (name, tax_name))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63 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
64
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 sm = config.get('slugify_mode')
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 if not sm:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 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
68 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
69 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
70
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
71 def slugify(self, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
72 return self.slugifier.slugify(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
73
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
74 def slugifyMultiple(self, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
75 return self.slugifier.slugifyMultiple(terms)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
77 def prepareRenderContext(self, ctx):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
78 # 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
79 ctx.pagination_source = self.source
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
81 # 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
82 # 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
83 # 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
84 # 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
85 # 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
86 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
87 # This is because:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
88 # * 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
89 # have the slugified version.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
90 # * 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
91 # 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
92 # what the user expects.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
93 #
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 tax_terms, is_combination = self._getTaxonomyTerms(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 ctx.page.route_metadata)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 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
97
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
98 # 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
99 ctx.custom_data.update({
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 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
101 '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
102 # 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
103 # 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
104 # 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
105 # 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
106 if (self.taxonomy.is_multiple and
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
107 self.taxonomy.name != self.taxonomy.term_name):
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
108 mult_val = tax_terms
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
109 if not is_combination:
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
110 mult_val = (mult_val,)
3e188d88a9ac bake: Fix some bugs with taxonomy combinations.
Ludovic Chabant <ludovic@chabant.com>
parents: 711
diff changeset
111 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
112
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 def _getTaxonomyTerms(self, route_metadata):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
114 # 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
115 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
116 if all_values is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 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
118 self.taxonomy.term_name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
120 # 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
121 # 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
122 # 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
123 if self.taxonomy.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 sep = self.taxonomy.separator
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 if sep in all_values:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 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
127 # 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
128 return all_values, False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 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
131 # 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
132 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
133 flt.addClause(HasTaxonomyTermsFilterClause(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 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
135 ctx.pagination_filter = flt
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 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
138 # 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
139 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
140 if self.taxonomy.is_multiple:
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
141 # 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
142 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
143 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
144 else:
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
145 slugified_values = self.slugify(str(values))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
146 route_val = slugified_values
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
147
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
148 # 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
149 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
150 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
151 if cpi:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
152 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
153 utt.append(slugified_values)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
155 # 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
156 # generate the URL.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
157 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
158
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159 def bake(self, ctx):
723
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
160 if not self.page_ref.exists:
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
161 logger.debug(
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
162 "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
163 (self.page_ref, self.taxonomy.name))
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
164 return
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
165
606f6d57b5df routing: Cleanup URL routing and improve page matching.
Ludovic Chabant <ludovic@chabant.com>
parents: 720
diff changeset
166 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
167 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
168 self.slugify_mode)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 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
170 level=logging.DEBUG, colored=False):
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
171 analyzer.analyze(ctx)
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 start_time = time.perf_counter()
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
174 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
175 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
176 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
177 start_time,
13ec290bfc13 bake: Fix some crashes with new blog archive/taxonomy for incremental bakes.
Ludovic Chabant <ludovic@chabant.com>
parents: 723
diff changeset
178 "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
179 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
180
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
181 def _bakeTaxonomyTerms(self, ctx, analyzer):
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 # Start baking those terms.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 logger.debug(
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
184 "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
185 (self.taxonomy.name, self.source_name,
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
186 len(analyzer.dirty_slugified_terms)))
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
188 route = self.app.getGeneratorRoute(self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 if route is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 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
191 self.name)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 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
194 fac = self.page_ref.getFactory()
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 job_count = 0
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
197 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
198 extra_route_metadata = {
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
199 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
200
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
201 # 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
202 logger.debug(
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
203 "Queuing: %s [%s=%s]" %
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
204 (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
205 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
206 job_count += 1
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 ctx.runJobQueue()
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 # 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
210 # 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
211 # 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
212 # their outputs.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 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
214 # 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
215 # 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
216 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
217 try:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
218 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
219 except InvalidRecordExtraKey:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220 continue
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
222 if analyzer.isKnownSlugifiedTerm(t):
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 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
224 (self.name, t))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 ctx.collapseRecord(prev_entry)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 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
228 (self.name, t))
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230 return job_count
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 class HasTaxonomyTermsFilterClause(SettingFilterClause):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 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
235 super(HasTaxonomyTermsFilterClause, self).__init__(
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
236 taxonomy.setting_name, value)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
237 self._taxonomy = taxonomy
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
238 self._is_combination = is_combination
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
239 self._slugifier = _Slugifier(taxonomy, slugify_mode)
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 def pageMatches(self, fil, page):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
242 if self._taxonomy.is_multiple:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
243 # 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
244 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
245 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
246 return False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
247
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
248 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
249 if self._is_combination:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
250 # 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
251 # 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
252 value_set = set(self.value)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
253 return value_set.issubset(page_set)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
254 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
255 # Multiple taxonomy, one term to match.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
256 return self.value in page_set
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257 else:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
258 # Single taxonomy. Just compare the values.
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 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
260 if page_value is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
261 return False
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
262 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
263 return page_value == self.value
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
264
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
265
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
266 class _TaxonomyTermsAnalyzer(object):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
267 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
268 self.source_name = source_name
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
269 self.taxonomy = taxonomy
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
270 self.slugifier = _Slugifier(taxonomy, slugify_mode)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
271 self._all_terms = {}
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
272 self._single_dirty_slugified_terms = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
273 self._all_dirty_slugified_terms = None
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
274
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
275 @property
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
276 def dirty_slugified_terms(self):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
277 """ 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
278 this bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
279 """
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
280 return self._all_dirty_slugified_terms
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
281
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
282 def isKnownSlugifiedTerm(self, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
283 """ 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
284 this bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
285 """
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
286 return term in self._all_terms
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 def analyze(self, ctx):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
289 # 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
290 # are 'dirty' for the current bake.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
291 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
292 # Remember all terms used.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
293 for _, cur_entry in ctx.getAllPageRecords():
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
294 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
295 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
296 if cur_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
297 if not self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
298 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
299 else:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
300 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
301
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
302 # 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
303 # marking them as 'dirty'.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
304 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
305 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
306 continue
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
307
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
308 entries = [cur_entry]
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
309 if prev_entry:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
310 entries.append(prev_entry)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
311
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
312 for e in entries:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
313 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
314 if entry_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
315 if not self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
316 self._single_dirty_slugified_terms.add(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
317 self.slugifier.slugify(entry_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
318 else:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
319 self._single_dirty_slugified_terms.update(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
320 (self.slugifier.slugify(t)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
321 for t in entry_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
322
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
323 self._all_dirty_slugified_terms = list(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
324 self._single_dirty_slugified_terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
325 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
326 len(self._all_dirty_slugified_terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
327
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
328 # 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
329 # 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
330 # 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
331 # line.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
332 #
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
333 # 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
334 # 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
335 # `onRouteFunctionUsed` method.
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
336 if self.taxonomy.is_multiple:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
337 known_combinations = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
338 for _, cur_entry in ctx.getAllPageRecords():
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
339 if cur_entry:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
340 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
341 for terms in used_terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
342 if len(terms) > 1:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
343 known_combinations.add(terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
344
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
345 dcc = 0
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
346 for terms in known_combinations:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
347 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
348 set(terms)):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
349 self._all_dirty_slugified_terms.append(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
350 self.taxonomy.separator.join(terms))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
351 dcc += 1
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
352 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
353 (len(known_combinations), dcc))
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
354
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
355 def _addTerms(self, entry_path, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
356 for t in terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
357 self._addTerm(entry_path, t)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
358
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
359 def _addTerm(self, entry_path, term):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
360 st = self.slugifier.slugify(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
361 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
362 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
363 logger.warning(
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
364 "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
365 "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
366 (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
367 orig_terms.append(term)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
368
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
369
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
370 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
371 res = set()
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
372 for o in entry.subs:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
373 for pinfo in o.render_info:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
374 if pinfo:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
375 terms = pinfo.getCustomInfo('used_taxonomy_terms')
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
376 if terms:
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
377 res |= set(terms)
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
378 return res
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
379
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
380
711
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
381 class _Slugifier(object):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
382 def __init__(self, taxonomy, mode):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
383 self.taxonomy = taxonomy
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
384 self.mode = mode
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
385
789
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
386 def slugifyMultiple(self, terms):
b8e760b3413e bake: Fix how slugified taxonomy terms are handled.
Ludovic Chabant <ludovic@chabant.com>
parents: 785
diff changeset
387 return tuple(map(self.slugify, terms))
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 def slugify(self, term):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
390 if self.mode & SLUGIFY_TRANSLITERATE:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
391 term = unidecode.unidecode(term)
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
392 if self.mode & SLUGIFY_LOWERCASE:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
393 term = term.lower()
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
394 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
395 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
396 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
397 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
398 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
399 return term
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
400
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
401
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
402 def _parse_slugify_mode(value):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
403 mapping = {
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
404 'encode': SLUGIFY_ENCODE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
405 'transliterate': SLUGIFY_TRANSLITERATE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
406 'lowercase': SLUGIFY_LOWERCASE,
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
407 '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
408 '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
409 mode = 0
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
410 for v in value.split(','):
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
411 f = mapping.get(v.strip())
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
412 if f is None:
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
413 if v == 'iconv':
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
414 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
415 "in PieCrust2. Use 'transliterate'.")
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
416 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
417 mode |= f
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
418 return mode
ab5c6a8ae90a bake: Replace hard-coded taxonomy support with "generator" system.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
419