Skip to content

Commit

Permalink
Add ipynb posts support to tags plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
punchagan committed May 30, 2016
1 parent 172af8d commit 228a0b0
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions v6/tags/tags.py
Expand Up @@ -27,11 +27,13 @@
from __future__ import unicode_literals, print_function
import codecs
from collections import defaultdict
import io
import math
from os.path import relpath
import re
from textwrap import dedent

from nikola.plugins.compile.ipynb import current_nbformat, flag as ipy_flag, ipy_modern, nbformat
from nikola.plugin_categories import Command
from nikola.utils import bytes_str, LOGGER, sys_decode, unicode_str

Expand Down Expand Up @@ -69,7 +71,7 @@ def add_tags(site, tags, filepaths, dry_run=False):
)

elif new_tags != old_tags:
_replace_tags_line(post, new_tags)
_replace_tags(site, post, new_tags)

return all_new_tags

Expand Down Expand Up @@ -134,7 +136,7 @@ def merge_tags(site, tags, filepaths, dry_run=False):
)

elif new_tags != old_tags:
_replace_tags_line(post, new_tags)
_replace_tags(site, post, new_tags)

return all_new_tags

Expand Down Expand Up @@ -175,7 +177,7 @@ def remove_tags(site, tags, filepaths, dry_run=False):
)

elif new_tags != old_tags:
_replace_tags_line(post, new_tags)
_replace_tags(site, post, new_tags)

return all_new_tags

Expand Down Expand Up @@ -236,7 +238,7 @@ def sort_tags(site, filepaths, dry_run=False):
)

elif new_tags != old_tags:
_replace_tags_line(post, new_tags)
_replace_tags(site, post, new_tags)

return all_new_tags

Expand Down Expand Up @@ -657,6 +659,31 @@ def _remove_tags(tags, removals):
return tags


def _replace_tags(site, post, tags):
"""Chooses the appropriate replace method based on post type."""
compiler = site.get_compiler(post.source_path)
if compiler.name == 'ipynb':
_replace_ipynb_tags(post, tags)

else:
_replace_tags_line(post, tags)


def _replace_ipynb_tags(post, tags):
"""Replaces tags in the notebook metadata with the given tags."""
if ipy_flag is None:
req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)')

nb = nbformat.read(post.source_path, current_nbformat)
metadata = nb['metadata']['nikola']
metadata['tags'] = ','.join(tags)

with io.open(post.source_path, "w+", encoding="utf8") as fd:
if ipy_modern:
nbformat.write(nb, fd, 4)
else:
nbformat.write(nb, fd, 'ipynb')

def _replace_tags_line(post, tags):
""" Replaces the line that lists the tags, with given tags. """

Expand Down

0 comments on commit 228a0b0

Please sign in to comment.