Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins to provide metadata #1603

Merged
merged 1 commit into from Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.txt
Expand Up @@ -9,6 +9,7 @@ Aurelien Naldi <https://github.com/aurelien-naldi>
Ben Mather <https://github.com/bwhmather>
Boris Kaul <https://github.com/localvoid>
Brandon W. Maister <https://github.com/quodlibetor>
Bussonnier Matthias <https://gtihub.com/carreau>
Carsten Grohmann <https://github.com/CarstenGrohmann>
Casey M. Bessette <https://github.com/caseybessette>
Chris Lee <https://github.com/clee>
Expand Down
1 change: 1 addition & 0 deletions CHANGES.txt
Expand Up @@ -26,6 +26,7 @@ New in v7.3.0
Features
--------

* Added possibility for plugins to define how to read metadata from files
* Added ``-a``, ``--author`` option to set post author field
* Added option INDEXES_PRETTY_PAGE_URL to make URLs for indexes
pages more pretty. (Issue #1548)
Expand Down
4 changes: 4 additions & 0 deletions docs/extending.txt
Expand Up @@ -374,6 +374,10 @@ They must provide:
If the compiler produces something other than HTML files, it should also implement ``extension`` which
returns the preferred extension for the output file.

These plugin can also be used to extract metadata from file. To do so, the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These plugins*

a dict containing the metadata*

plugin may implement ``read_metadata`` that will return a dict containing some
metadata contained in the file.

RestExtension Plugins
---------------------

Expand Down
6 changes: 6 additions & 0 deletions nikola/plugin_categories.py
Expand Up @@ -245,6 +245,12 @@ def extension(self):
"""The preferred extension for the output of this compiler."""
return ".html"

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
"""
return {}


class RestExtension(BasePlugin):
name = "dummy_rest_extension"
Expand Down
13 changes: 13 additions & 0 deletions nikola/plugins/compile/ipynb/__init__.py
Expand Up @@ -69,6 +69,19 @@ def compile_html(self, source, dest, is_two_file=True):
(body, resources) = exportHtml.from_notebook_node(nb_json)
out_file.write(body)

def read_metadata(self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None):
"""read metadata directly from ipynb file.

As ipynb file support arbitrary metadata as json, the metadata used by Nikola
will be assume to be in the 'nikola' subfield.
"""
source = post.source_path
with io.open(source, "r", encoding="utf8") as in_file:
nb_json = nbformat.read(in_file, current_nbformat)
# metadata shoudl always exist, but we never know
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# metadata should always exist, but we never know
# if someone crafted the ipynb by hand.

# if Someone craft an ipynb by hand.
return nb_json.get('metadata', {}).get('nikola', {})

def create_post(self, path, **kw):
content = kw.pop('content', None)
onefile = kw.pop('onefile', False)
Expand Down
11 changes: 6 additions & 5 deletions nikola/post.py
Expand Up @@ -917,8 +917,6 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)
"""
meta = defaultdict(lambda: '')

newstylemeta = True

try:
config = post.config
except AttributeError:
Expand All @@ -927,9 +925,8 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)
_, newstylemeta = get_metadata_from_meta_file(post.metadata_path, config, lang)
meta.update(_)

if meta:
return meta, newstylemeta
post.is_two_file = False
if not meta:
post.is_two_file = False

if file_metadata_regexp is not None:
meta.update(_get_metadata_from_filename_by_regex(post.source_path,
Expand All @@ -938,6 +935,10 @@ def get_meta(post, file_metadata_regexp=None, unslugify_titles=False, lang=None)

meta.update(get_metadata_from_file(post.source_path, config, lang))

if getattr(post, 'compiler', None):
compiler_meta = post.compiler.read_metadata(post, file_metadata_regexp, unslugify_titles, lang)
meta.update(compiler_meta)

if lang is None:
# Only perform these checks for the default language

Expand Down
3 changes: 3 additions & 0 deletions tests/test_rss_feeds.py
Expand Up @@ -34,6 +34,9 @@ class FakeCompiler(object):
compile_html = None
extension = lambda self: '.html'

def read_metadata(*args, **kwargs):
return {}

def register_extra_dependencies(self, post):
pass

Expand Down