Skip to content

Commit

Permalink
Add comments to source files
Browse files Browse the repository at this point in the history
Closes #119
  • Loading branch information
klane committed Dec 22, 2019
1 parent b914b00 commit 66a100f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
22 changes: 20 additions & 2 deletions jekyllnb/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,55 @@


class JekyllExporter(MarkdownExporter):
"""Exporter to write Markdown with Jekyll metadata"""

resources = {}

def from_filename(self, filename, resources=None, **kw):
def from_filename(self, filename, resources=None, **kwargs):
"""Convert notebook from a file
Args:
filename (str): Full filename of the notebook to convert.
resources (dict): Additional resources used by preprocessors and filters.
`**kwargs`: Ignored
Returns:
NotebookNode: Converted notebook.
dict: Resources dictionary.
"""
self.resources = resources
return super(JekyllExporter, self).from_filename(
filename, resources=resources, **kw
filename, resources=resources, **kwargs
)

@default("template_file")
def _template_file_default(self): # skipcq: PYL-R0201
"""Default template file"""
return "jekyll"

@property
def template_path(self):
"""Path to available template files"""
return super(JekyllExporter, self).template_path + [
os.path.join(os.path.dirname(__file__), "templates")
]

@property
def preprocessors(self):
"""Add JekyllPreprocessor to list of enabled preprocessors"""
return super(JekyllExporter, self).preprocessors + [
"jekyllnb.JekyllPreprocessor"
]

@property
def default_config(self):
"""Default configuration"""
config = Config({"JekyllPreprocessor": {"enabled": True}})
config.merge(super(JekyllExporter, self).default_config)
return config

def default_filters(self):
"""Default filters"""
for pair in super(JekyllExporter, self).default_filters():
yield pair

Expand Down
20 changes: 20 additions & 0 deletions jekyllnb/jekyllnb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@


class JekyllNB(NbConvertApp):
"""Application to convert notebooks (``*.ipynb``) to Jekyll Markdown (``.md``)"""

name = "jupyter-jekyllnb"
description = "Convert Jupyter notebooks to Jekyll-ready Markdown"
version = __version__
Expand All @@ -42,10 +44,12 @@ class JekyllNB(NbConvertApp):

@default("export_format")
def _export_format_default(self): # skipcq: PYL-R0201
"""Default export format"""
return "jekyll"

@observe("export_format")
def _export_format_changed(self, change):
"""Ensure export format is jekyll"""
default_format = self._export_format_default()

if change["new"].lower() != default_format:
Expand All @@ -57,6 +61,7 @@ def _export_format_changed(self, change):

@catch_config_error
def initialize(self, argv=None):
"""Initialize application, notebooks, writer, and postprocessor"""
super(JekyllNB, self).initialize(argv)
self.writer.build_directory = os.path.join(self.site_dir, self.page_dir)

Expand All @@ -66,6 +71,21 @@ def initialize(self, argv=None):
)

def init_single_notebook_resources(self, notebook_filename):
"""Initialize resources
Initializes the resources dictionary for a single notebook.
Args:
notebook_filename (str): Full filename of the notebook to convert.
Returns:
dict: Resources dictionary for a single notebook.
Dictionary must include the following keys:
- config_dir: location of the Jupyter config directory
- unique_key: notebook name
- output_files_dir: directory where output files should be saved
"""
resources = super(JekyllNB, self).init_single_notebook_resources(
notebook_filename
)
Expand Down
14 changes: 14 additions & 0 deletions jekyllnb/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@


class JekyllPreprocessor(Preprocessor):
"""Preprocessor to add Jekyll metadata"""

def preprocess(self, nb, resources): # skipcq: PYL-R0201
"""Preprocess notebook
Add Jekyll metadata to notebook resources.
Args:
nb (NotebookNode): Notebook being converted.
resources (dict): Additional resources used by preprocessors and filters.
Returns:
NotebookNode: Modified notebook.
dict: Modified resources dictionary.
"""
name = resources["metadata"]["name"]
metadata = {"layout": "page", "title": name, "permalink": "/" + name}
metadata.update(nb.metadata.get("jekyll", {}))
Expand Down

0 comments on commit 66a100f

Please sign in to comment.