Index: cms/converters.py |
=================================================================== |
--- a/cms/converters.py |
+++ b/cms/converters.py |
@@ -378,16 +378,17 @@ |
'linkify': self.linkify, |
'toclist': self.toclist, |
} |
globals = { |
'get_string': self.get_string, |
'has_string': self.has_string, |
'get_page_content': self.get_page_content, |
+ 'get_pages_metadata': self.get_pages_metadata, |
} |
for dirname, dictionary in [('filters', filters), |
('globals', globals)]: |
for filename in self._params['source'].list_files(dirname): |
root, ext = os.path.splitext(filename) |
if ext.lower() != '.py': |
continue |
@@ -466,16 +467,60 @@ |
locale, url = self._params['source'].resolve_link(page, locale) |
return jinja2.Markup('<a{}>'.format(''.join( |
' {}="{}"'.format(name, jinja2.escape(value)) for name, value in [ |
('href', url), |
('hreflang', locale) |
] + attrs.items() |
))) |
+ def get_pages_metadata(self, filters=None): |
+ if not isinstance(filters, dict) and filters: |
+ raise TypeError('Filters are not a dictionary') |
+ |
+ return_data = [] |
+ for page_name, _format in self._params['source'].list_pages(): |
+ data, filename = self._params['source'].read_page(page_name, |
+ _format) |
+ page_data = self.parse_page_metadata(data, page_name) |
+ |
+ if self.filter_metadata(filters, page_data) is True: |
+ return_data.append(page_data) |
+ |
+ return return_data |
+ |
+ def parse_page_metadata(self, data, page): |
Jon Sonesen
2017/06/23 10:09:26
This code is essentially duplicating the logic in
Vasily Kuznetsov
2017/06/23 14:15:24
As discussed, this approach sounds good.
Now look
Jon Sonesen
2017/06/26 07:22:43
Yeah I totally agree here, and actually we talked
|
+ page_metadata = {'page': page} |
+ 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 = value.strip() |
+ if value.startswith('[') and value.endswith(']'): |
+ value = [element.strip() for element in value[1:-1].split(',')] |
+ page_metadata[name.strip()] = value |
+ return page_metadata |
+ |
+ def filter_metadata(self, filters, metadata): |
+ if filters is None: |
+ return True |
+ for filter_name, filter_value in filters.items(): |
+ if filter_name not in metadata: |
+ return False |
+ if isinstance(metadata[filter_name], list): |
+ if isinstance(filter_value, basestring): |
+ filter_value = [filter_value] |
+ for option in filter_value: |
+ if str(option) not in metadata[filter_name]: |
+ return False |
+ elif filter_value != metadata[filter_name]: |
+ return False |
+ return True |
+ |
def toclist(self, content): |
toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' |
flat = [] |
for match in re.finditer(toc_re, content, re.S): |
flat.append({ |
'level': int(match.group(1)), |
'anchor': jinja2.Markup(match.group(2)).unescape(), |
'title': jinja2.Markup(match.group(3)).unescape(), |