Skip to content
Permalink
Browse files

Pelican style Markdown metadata (Issue #1923)

  • Loading branch information...
ralsina committed May 28, 2017
1 parent 1ea109b commit 2e4169bdf80905d65997427aa5183063570ee16d
Showing with 26 additions and 4 deletions.
  1. +2 −1 CHANGES.txt
  2. +24 −3 nikola/plugins/compile/markdown/__init__.py
@@ -4,7 +4,8 @@ New in master
Features
--------


* Support for Markdown Meta extension for Pelican
compatibility (Issue #1923)
* Support for YAML and TOML metadata (Issue #2801)

New in v7.8.6
@@ -42,7 +42,7 @@

from nikola import shortcodes as sc
from nikola.plugin_categories import PageCompiler
from nikola.utils import makedirs, req_missing, write_metadata
from nikola.utils import makedirs, req_missing, write_metadata, LocaleBorg


class ThreadLocalMarkdown(threading.local):
@@ -58,8 +58,14 @@ def __init__(self, extensions):
def convert(self, data):
"""Convert data to HTML and reset internal state."""
result = self.markdown.convert(data)
try:
meta = self.markdown.Meta.copy()
for k in meta: # This reads everything as lists
meta[k] = ','.join(meta[k])
except Exception:
meta = {}
self.markdown.reset()
return result
return result, meta


class CompileMarkdown(PageCompiler):
@@ -85,6 +91,7 @@ def set_site(self, site):
extensions.extend(site_extensions)
if Markdown is not None:
self.converter = ThreadLocalMarkdown(extensions)
self.support_metadata = 'markdown.extensions.meta' in extensions

def compile_string(self, data, source_path=None, is_two_file=True, post=None, lang=None):
"""Compile Markdown into HTML strings."""
@@ -93,7 +100,7 @@ def compile_string(self, data, source_path=None, is_two_file=True, post=None, la
if not is_two_file:
_, data = self.split_metadata(data)
new_data, shortcodes = sc.extract_shortcodes(data)
output = self.converter.convert(new_data)
output, _ = self.converter.convert(new_data)
output, shortcode_deps = self.site.apply_shortcodes_uuid(output, shortcodes, filename=source_path, with_dependencies=True, extra_context=dict(post=post))
return output, shortcode_deps

@@ -134,3 +141,17 @@ def create_post(self, path, **kw):
fd.write(write_metadata(metadata))
fd.write('-->\n\n')
fd.write(content)

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
# FIXME: what about translations????
source = post.source_path
with io.open(source, 'r', encoding='utf-8') as inf:
_, meta = self.converter.convert(inf.read())
return meta

0 comments on commit 2e4169b

Please sign in to comment.
You can’t perform that action at this time.