def read_metadata(self, post, lang=None): """Read the metadata from a post, and return a metadata dict.""" if lang is None: lang = LocaleBorg().current_lang source_path = post.translated_source_path(lang) with io.open(source_path, 'r', encoding='utf-8') as inf: data = inf.read() _, _, _, document = rst2html(data, logger=self.logger, source_path=source_path, transforms=self.site.rst_transforms, no_title_transform=False) meta = {} if 'title' in document: meta['title'] = document['title'] for docinfo in document.traverse(docutils.nodes.docinfo): for element in docinfo.children: if element.tagname == 'field': # custom fields (e.g. summary) name_elem, body_elem = element.children name = name_elem.astext() value = body_elem.astext() elif element.tagname == 'authors': # author list name = element.tagname value = [element.astext() for element in element.children] else: # standard fields (e.g. address) name = element.tagname value = element.astext() name = name.lower() meta[name] = value # Put 'authors' meta field contents in 'author', too if 'authors' in meta and 'author' not in meta: meta['author'] = '; '.join(meta['authors']) # Map metadata from other platforms to names Nikola expects (Issue #2817) map_metadata(meta, 'rest_docinfo', self.site.config) return meta
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None): """Read the metadata from a post's meta tags, and return a metadata dict.""" if lang is None: lang = LocaleBorg().current_lang source_path = post.translated_source_path(lang) with io.open(source_path, 'r', encoding='utf-8-sig') as inf: data = inf.read() metadata = {} try: doc = lxml.html.document_fromstring(data) except lxml.etree.ParserError as e: # Issue #374 -> #2851 if str(e) == "Document is empty": return {} # let other errors raise raise title_tag = doc.find('*//title') if title_tag is not None and title_tag.text: metadata['title'] = title_tag.text meta_tags = doc.findall('*//meta') for tag in meta_tags: k = tag.get('name', '').lower() if not k: continue elif k == 'keywords': k = 'tags' content = tag.get('content') if content: metadata[k] = content map_metadata(metadata, 'html_metadata', self.site.config) return metadata
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None): """Read the metadata from a post's meta tags, and return a metadata dict.""" if lang is None: lang = LocaleBorg().current_lang source_path = post.translated_source_path(lang) with io.open(source_path, 'r', encoding='utf-8') as inf: data = inf.read() metadata = {} try: doc = lxml.html.document_fromstring(data) except lxml.etree.ParserError as e: # Issue #374 -> #2851 if str(e) == "Document is empty": return {} # let other errors raise raise title_tag = doc.find('*//title') if title_tag is not None and title_tag.text: metadata['title'] = title_tag.text meta_tags = doc.findall('*//meta') for tag in meta_tags: k = tag.get('name', '').lower() if not k: continue elif k == 'keywords': k = 'tags' content = tag.get('content') if content: metadata[k] = content map_metadata(metadata, 'html_metadata', self.site.config) return metadata
def read_metadata(self, post, lang=None): """Read the metadata from a post, and return a metadata dict.""" if lang is None: lang = LocaleBorg().current_lang source_path = post.translated_source_path(lang) # Silence reST errors, some of which are due to a different # environment. Real issues will be reported while compiling. null_logger = logbook.Logger('NULL') null_logger.handlers = [logbook.NullHandler()] with io.open(source_path, 'r', encoding='utf-8') as inf: data = inf.read() _, _, _, document = rst2html(data, logger=null_logger, source_path=source_path, transforms=self.site.rst_transforms) meta = {} if 'title' in document: meta['title'] = document['title'] for docinfo in document.traverse(docutils.nodes.docinfo): for element in docinfo.children: if element.tagname == 'field': # custom fields (e.g. summary) name_elem, body_elem = element.children name = name_elem.astext() value = body_elem.astext() elif element.tagname == 'authors': # author list name = element.tagname value = [element.astext() for element in element.children] else: # standard fields (e.g. address) name = element.tagname value = element.astext() name = name.lower() meta[name] = value # Put 'authors' meta field contents in 'author', too if 'authors' in meta and 'author' not in meta: meta['author'] = '; '.join(meta['authors']) # Map metadata from other platforms to names Nikola expects (Issue #2817) map_metadata(meta, 'rest_docinfo', self.site.config) return meta
def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None): """Read the metadata from a post, and return a metadata dict.""" if not self.support_metadata: return {} if Markdown is None: req_missing(['markdown'], 'build this site (compile Markdown)') if lang is None: lang = LocaleBorg().current_lang source = post.translated_source_path(lang) with io.open(source, 'r', encoding='utf-8') as inf: # Note: markdown meta returns lowercase keys data = inf.read() # If the metadata starts with "---" it's actually YAML and # we should not let markdown parse it, because it will do # bad things like setting empty tags to "''" if data.startswith('---\n'): return {} _, meta = self.converter.convert(data) # Map metadata from other platforms to names Nikola expects (Issue #2817) map_metadata(meta, 'markdown_metadata', self.site.config) return meta
def read_metadata(self, post, lang=None): """Read the metadata from a post, and return a metadata dict.""" if not self.supports_metadata: return {} if Markdown is None: req_missing(['markdown'], 'build this site (compile Markdown)') if lang is None: lang = LocaleBorg().current_lang source = post.translated_source_path(lang) with io.open(source, 'r', encoding='utf-8') as inf: # Note: markdown meta returns lowercase keys data = inf.read() # If the metadata starts with "---" it's actually YAML and # we should not let markdown parse it, because it will do # bad things like setting empty tags to "''" if data.startswith('---\n'): return {} _, meta = self.converter.convert(data) # Map metadata from other platforms to names Nikola expects (Issue #2817) map_metadata(meta, 'markdown_metadata', self.site.config) return meta
def read_metadata(self, post, lang=None): """Read the metadata from a post, and return a metadata dict.""" if lang is None: lang = LocaleBorg().current_lang source_path = post.translated_source_path(lang) # Silence reST errors, some of which are due to a different # environment. Real issues will be reported while compiling. null_logger = logbook.Logger('NULL') null_logger.handlers = [logbook.NullHandler()] with io.open(source_path, 'r', encoding='utf-8') as inf: data = inf.read() _, _, _, document = rst2html(data, logger=null_logger, source_path=source_path, transforms=self.site.rst_transforms) meta = {} if 'title' in document: meta['title'] = document['title'] for docinfo in document.traverse(docutils.nodes.docinfo): for element in docinfo.children: if element.tagname == 'field': # custom fields (e.g. summary) name_elem, body_elem = element.children name = name_elem.astext() value = body_elem.astext() elif element.tagname == 'authors': # author list name = element.tagname value = [element.astext() for element in element.children] else: # standard fields (e.g. address) name = element.tagname value = element.astext() name = name.lower() meta[name] = value # Put 'authors' meta field contents in 'author', too if 'authors' in meta and 'author' not in meta: meta['author'] = '; '.join(meta['authors']) # Map metadata from other platforms to names Nikola expects (Issue #2817) map_metadata(meta, 'rest_docinfo', self.site.config) return meta