Index: tests/test_site/globals/get_pages_metadata.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/tests/test_site/globals/get_pages_metadata.py |
@@ -0,0 +1,43 @@ |
+import re |
+from jinja2 import contextfunction |
+ |
+ |
+@contextfunction |
+def get_pages_metadata(context, filters=None): |
+ if not isinstance(filters, dict) and filters: |
+ raise TypeError('Filters are not a dictionary') |
+ |
+ return_data = [] |
+ for page_name, _format in context['source'].list_pages(): |
+ data, filename = context['source'].read_page(page_name, _format) |
+ page_data = parse_page_metadata(data, page_name) |
+ |
+ if filter_metadata(filters, page_data, page_name) is True: |
+ return_data.append(page_data) |
+ |
+ return return_data |
+ |
+ |
+def parse_page_metadata(data, page): |
+ page_metadata = {} |
+ lines = data.splitlines(True) |
+ for i, line in enumerate(lines): |
+ if not re.search(r'^\s*[\w\-]+\s*=', line): |
+ break |
+ name, value = line.split('=', 1) |
+ value = tuple(value.strip().split(',')) |
+ page_metadata[name.strip()] = value |
+ 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.
|
+ return page_metadata |
+ |
+ |
+def filter_metadata(filters, metadata, page): |
+ if filters is None: |
+ return True |
+ for filter_name, filter_value in filters.items(): |
+ 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
|
+ return False |
+ for option in filter_value.split(','): |
+ 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
|
+ return False |
+ return True |