Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 import re | |
2 from jinja2 import contextfunction | |
3 | |
4 | |
5 @contextfunction | |
6 def get_pages_metadata(context, filters=None): | |
7 if not isinstance(filters, dict) and filters: | |
8 raise TypeError('Filters are not a dictionary') | |
9 | |
10 return_data = [] | |
11 for page_name, _format in context['source'].list_pages(): | |
12 data, filename = context['source'].read_page(page_name, _format) | |
13 page_data = parse_page_metadata(data, page_name) | |
14 | |
15 if filter_metadata(filters, page_data, page_name) is True: | |
16 return_data.append(page_data) | |
17 | |
18 return return_data | |
19 | |
20 | |
21 def parse_page_metadata(data, page): | |
22 page_metadata = {} | |
23 lines = data.splitlines(True) | |
24 for i, line in enumerate(lines): | |
25 if not re.search(r'^\s*[\w\-]+\s*=', line): | |
26 break | |
27 name, value = line.split('=', 1) | |
28 value = tuple(value.strip().split(',')) | |
29 page_metadata[name.strip()] = value | |
30 page_metadata['page'] = page | |
Vasily Kuznetsov
2017/01/12 10:37:47
I don't think we want to execute this for each lin
Jon Sonesen
2017/01/19 07:51:40
Done.
| |
31 return page_metadata | |
32 | |
33 | |
34 def filter_metadata(filters, metadata, page): | |
35 if filters is None: | |
36 return True | |
37 for filter_name, filter_value in filters.items(): | |
38 if filter_name not in metadata[page]: | |
Vasily Kuznetsov
2017/01/12 10:37:47
This change looks like a mistake. `metadata` is al
Jon Sonesen
2017/01/19 07:51:40
Yeah, was a fail. Will fix
| |
39 return False | |
40 for option in filter_value.split(','): | |
41 if option not in metadata[page][filter_name]: | |
Vasily Kuznetsov
2017/01/12 10:37:47
Same here regarding the `[page]` part. Also, what
Jon Sonesen
2017/01/19 07:51:40
You are right
| |
42 return False | |
43 return True | |
OLD | NEW |