annotate piecrust/uriutil.py @ 1145:e94737572542

serve: Fix an issue where false positive matches were rendered as the requested page. Now we try to render the page, but also try to detect for the most common "empty" pages.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 22:08:51 -0700
parents e1b33cbf9874
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import re
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
2 import os.path
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 import logging
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 logger = logging.getLogger(__name__)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 def multi_replace(text, replacements):
5
474c9882decf Upgrade to Python 3.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
10 reps = dict((re.escape(k), v) for k, v in replacements.items())
474c9882decf Upgrade to Python 3.
Ludovic Chabant <ludovic@chabant.com>
parents: 3
diff changeset
11 pattern = re.compile("|".join(list(reps.keys())))
3
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 return pattern.sub(lambda m: reps[re.escape(m.group(0))], text)
f485ba500df3 Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
12
30a42341cfa8 Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
14
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
15 def split_uri(app, uri):
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
16 root = app.config.get('site/root')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
17 uri_root = uri[:len(root)]
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
18 if uri_root != root:
942
e1b33cbf9874 internal: Give better exception messages when splitting URIs.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
19 raise Exception("URI '%s' is not a full URI, expected root '%s'." %
e1b33cbf9874 internal: Give better exception messages when splitting URIs.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
20 (uri, root))
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
21 uri = uri[len(root):]
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
22 return uri_root, uri
12
30a42341cfa8 Define page slugs properly, avoid recursions with debug data.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
23
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
24
298
b7ab1b503510 data: Fix incorrect next/previous page URLs in pagination data.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
25 def split_sub_uri(app, uri):
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
26 root = app.config.get('site/root')
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
27 if not uri.startswith(root):
942
e1b33cbf9874 internal: Give better exception messages when splitting URIs.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
28 raise Exception("URI '%s' is not a full URI, expected root '%s'." %
e1b33cbf9874 internal: Give better exception messages when splitting URIs.
Ludovic Chabant <ludovic@chabant.com>
parents: 852
diff changeset
29 (uri, root))
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
30
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
31 pretty_urls = app.config.get('site/pretty_urls')
484
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
32 trailing_slash = app.config.get('site/trailing_slash')
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
33 if not pretty_urls:
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
34 uri, ext = os.path.splitext(uri)
495
4284c673bb91 internal: Fix some edge-cases for splitting sub-URIs.
Ludovic Chabant <ludovic@chabant.com>
parents: 484
diff changeset
35 else:
484
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
36 uri = uri.rstrip('/')
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
37
298
b7ab1b503510 data: Fix incorrect next/previous page URLs in pagination data.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
38 page_num = 1
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
39 pgn_suffix_re = app.config.get('__cache/pagination_suffix_re')
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
40 m = re.search(pgn_suffix_re, uri)
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
41 if m:
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
42 uri = uri[:m.start()]
298
b7ab1b503510 data: Fix incorrect next/previous page URLs in pagination data.
Ludovic Chabant <ludovic@chabant.com>
parents: 33
diff changeset
43 page_num = int(m.group('num'))
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
44
484
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
45 if len(uri) < len(root):
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
46 # The only reasons the URI could have gotten shorter are:
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
47 # - if the regexp "ate" the trailing slash of the root.
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
48 # - if we stripped the trailing slash on a root URL.
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
49 uri += '/'
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
50
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
51 if len(uri) > len(root):
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
52 # Now if we don't have a root URI, make it conform to the rules
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
53 # (re-add the extension, or re-add the trailing slash).
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
54 if not pretty_urls:
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
55 uri += ext
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
56 elif trailing_slash:
d4321317beae internal: Correctly split sub URIs. Add unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 329
diff changeset
57 uri += '/'
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
58
329
422052d2e978 internal: Try handling URLs in a consistent way.
Ludovic Chabant <ludovic@chabant.com>
parents: 298
diff changeset
59 return uri, page_num
33
62c7a97c8340 Get the un-paginated URL of a page early and pass that around.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
60
852
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 710
diff changeset
61
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 710
diff changeset
62 def uri_to_title(slug):
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 710
diff changeset
63 slug = re.sub(r'[\-_]', ' ', slug)
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 710
diff changeset
64 return slug.title()
4850f8c21b6e core: Start of the big refactor for PieCrust 3.0.
Ludovic Chabant <ludovic@chabant.com>
parents: 710
diff changeset
65