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) 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 = {'page': page} | |
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 = value.strip() | |
29 if value.startswith('[') and value.endswith(']'): | |
30 value = [element.strip() for element in value[1:-1].split(',')] | |
31 page_metadata[name.strip()] = value | |
32 return page_metadata | |
33 | |
34 | |
35 def filter_metadata(filters, metadata): | |
36 if filters is None: | |
37 return True | |
38 for filter_name, filter_value in filters.items(): | |
39 if filter_name not in metadata: | |
40 return False | |
41 if isinstance(metadata[filter_name], list): | |
42 for option in filter_value: | |
Vasily Kuznetsov
2017/03/15 18:02:58
How about the situation when the field is a list b
Jon Sonesen
2017/03/17 07:53:33
Odd, I thought I had actually changed this. But pe
| |
43 if str(option) not in metadata[filter_name]: | |
44 return False | |
45 if filter_value != metadata[filter_name]: | |
Vasily Kuznetsov
2017/03/15 18:02:58
Should not this be `elif` instead of `if`. It seem
Jon Sonesen
2017/03/17 07:53:33
Oh yeah, that is a good catch.
| |
46 return False | |
47 return True | |
OLD | NEW |