Mercurial > piecrust2
view piecrust/uriutil.py @ 196:154b8df04829
processing: Add Compass and Sass processors.
The Sass processor is similar to the Less processor, i.e. it tries to be
part of the structured pipeline processing by using the mapfile produced by
the Sass compiler in order to provide a list of dependencies.
The Compass processor is completely acting outside of the pipeline, so the
server won't know what's up to date and what's not. It's expected that the
user will run `compass watch` to keep things up to date. However, it will
require to pass the server's cache directory to put things in, so we'll need
to add some easy way to get that path for the user.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 11 Jan 2015 23:08:49 -0800 |
parents | 62c7a97c8340 |
children | b7ab1b503510 |
line wrap: on
line source
import re import os.path import string import logging import functools logger = logging.getLogger(__name__) class UriError(Exception): def __init__(self, uri): super(UriError, self).__init__("Invalid URI: %s" % uri) @functools.total_ordering class UriInfo(object): def __init__(self, uri, source, args, taxonomy=None, page_num=1): self.uri = uri self.source = source self.args = args self.taxonomy = taxonomy self.page_num = page_num def __eq__(self, other): return ((self.uri, self.source, self.args, self.taxonomy, self.page_num) == (other.uri, other.source, other.args, other.taxonomy, other.page_num)) def __lt__(self, other): return ((self.uri, self.source, self.args, self.taxonomy, self.page_num) < (other.uri, other.source, other.args, other.taxonomy, other.page_num)) pagenum_pattern = re.compile(r'/(\d+)/?$') def parse_uri(routes, uri): if uri.find('..') >= 0: raise UriError(uri) page_num = 1 match = pagenum_pattern.search(uri) if match is not None: uri = uri[:match.start()] page_num = int(match.group(1)) uri = '/' + uri.strip('/') for rn, rc in routes.items(): pattern = route_to_pattern(rn) m = re.match(pattern, uri) if m is not None: args = m.groupdict() return UriInfo(uri, rc['source'], args, rc.get('taxonomy'), page_num) return None r2p_pattern = re.compile(r'%(\w+)%') def route_to_pattern(route): return r2p_pattern.sub(r'(?P<\1>[\w\-]+)', route) def multi_replace(text, replacements): reps = dict((re.escape(k), v) for k, v in replacements.items()) pattern = re.compile("|".join(list(reps.keys()))) return pattern.sub(lambda m: reps[re.escape(m.group(0))], text) def get_slug(app, uri): site_root = app.config.get('site/root') uri = uri[len(site_root):] return uri.lstrip('/') def get_first_sub_uri(app, uri): pretty_urls = app.config.get('site/pretty_urls') if not pretty_urls: uri, ext = os.path.splitext(uri) pgn_suffix_re = app.config.get('__cache/pagination_suffix_re') m = re.search(pgn_suffix_re, uri) if m: uri = uri[:m.start()] if not pretty_urls: uri += ext return uri