Skip to content

Commit

Permalink
Merge pull request #2622 from getnikola/markdown-to-string
Browse files Browse the repository at this point in the history
Allowing to compile MarkDown posts from string to string.
  • Loading branch information
Kwpolska committed Jan 8, 2017
2 parents c2da98c + 9a1d901 commit 80abb4a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions nikola/plugins/compile/markdown/__init__.py
Expand Up @@ -63,19 +63,26 @@ def set_site(self, site):

self.config_dependencies.append(str(sorted(site.config.get("MARKDOWN_EXTENSIONS"))))

def compile_string(self, content, source_path=None, is_two_file=True):
"""Compile Markdown into HTML strings."""
if markdown is None:
req_missing(['markdown'], 'build this site (compile Markdown)')
self.extensions += self.site.config.get("MARKDOWN_EXTENSIONS")
if not is_two_file:
_, content = self.split_metadata(content)
output = markdown(content, self.extensions, output_format="html5")
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source_path, with_dependencies=True, extra_context={'post': post})
return output, shortcode_deps

def compile(self, source, dest, is_two_file=True, post=None, lang=None):
"""Compile the source file into HTML and save as dest."""
if markdown is None:
req_missing(['markdown'], 'build this site (compile Markdown)')
makedirs(os.path.dirname(dest))
self.extensions += self.site.config.get("MARKDOWN_EXTENSIONS")
with io.open(dest, "w+", encoding="utf8") as out_file:
with io.open(source, "r", encoding="utf8") as in_file:
data = in_file.read()
if not is_two_file:
_, data = self.split_metadata(data)
output = markdown(data, self.extensions, output_format="html5")
output, shortcode_deps = self.site.apply_shortcodes(output, filename=source, with_dependencies=True, extra_context=dict(post=post))
output, shortcode_deps = self.compile_string(data, source, is_two_file)
out_file.write(output)
if post is None:
if shortcode_deps:
Expand Down

0 comments on commit 80abb4a

Please sign in to comment.