Skip to content

Commit

Permalink
developing ...
Browse files Browse the repository at this point in the history
  • Loading branch information
liu xue yan committed Aug 5, 2019
1 parent ed1bb46 commit 7291331
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 24 deletions.
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Authors

liu xue yan <[email protected]>
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Changelog
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Contributing
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ verify_ssl = true
autopep8 = "*"

[packages]
mkdocs-nbconvert = {editable = true,path = "."}

[requires]
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ A [MkDocs][] plug-in that provides a source parser for `*.ipynb` [Jupyter][] Not
[MkDocs]: http://www.mkdocs.org/
[Jupyter]: https://jupyter.org/
[nbconvert]: https://nbconvert.readthedocs.io

33 changes: 21 additions & 12 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import io
import os

from setuptools import find_packages, setup

setup(
Expand All @@ -9,29 +12,35 @@
license='MIT',
description='A MkDocs plug-in provides a source parser for *.ipynb files',
url='https://github.com/tanbro/mkdocs-nbconvert',
long_description=open('README.md', encoding='UTF-8').read(),
long_description=(os.linesep*2).join(
io.open(fn, encoding='UTF8').read()
for fn in ('README.md', 'CHANGELOG.md', 'CONTRIBUTING.md', 'AUTHORS.md')
),
long_description_content_type='text/markdown',
keywords='mkdocs',
python_requires='>=3.4',
keywords=['mkdocs', 'plugin', 'jupyter', 'notebook', 'markdown', 'nbconvert'],
python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*',
install_requires=[
'mkdocs<2.0,>=1.0.4',
'nbconvert<6.0,>=5.0',
'nbformat',
],
# entry_points={
# 'mkdocs.plugins': [
# 'mkdocs-nbconvert = mkdocs_nbconvert.plugin:NbConvertPlugin'
# ]
# },
entry_points={
'mkdocs.plugins': [
'nbconvert = mkdocs_nbconvert.plugin:NbConvertPlugin'
]
},
classifiers=[
'Development Status :: 4 - Beta',
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Intended Audience :: Information Technology',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
'Programming Language :: Python :: 3.7',
],
setup_requires=[
'setuptools_scm',
Expand Down
14 changes: 3 additions & 11 deletions src/mkdocs_nbconvert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import os
import sys
from timeit import default_timer as timer
from datetime import datetime, timedelta
from __future__ import absolute_import

from mkdocs import utils as mkdocs_utils
from mkdocs.config import config_options, Config
from mkdocs.plugins import BasePlugin


class NbConvertPlugin(BasePlugin):
pass
from .version import version as __version__
from .plugin import NbConvertPlugin
95 changes: 95 additions & 0 deletions src/mkdocs_nbconvert/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import logging
import os
import subprocess
import sys
from glob import iglob

import nbformat
from mkdocs import utils as mkdocs_utils
from mkdocs.config import Config, config_options
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import File
from nbconvert import MarkdownExporter

PYTHON_VERSION_MAJOR_MINOR = '{}.{}'.format(*sys.version_info)


log = logging.getLogger('mkdocs.plugins.NbConvertPlugin')


class NbConvertPlugin(BasePlugin):

config_scheme = (
(
'input_dir',
config_options.OptionallyRequired(default='notebooks')
), (
'recursive',
config_options.OptionallyRequired(default=False)
), (
'output_dir',
config_options.OptionallyRequired(default='notebooks')
),
)

def __init__(self, *args, **kwargs):
self._exported_files = []
return super().__init__(*args, **kwargs)

def on_files(self, files, config):
# deal with dirs
config_file_dir = os.path.dirname(config['config_file_path'])
input_dir = os.path.normpath(self.config['input_dir'])
output_dir = os.path.join(
config['docs_dir'],
os.path.normpath(self.config['output_dir'])
)
if not os.path.isabs(input_dir):
input_dir = os.path.realpath(
os.path.join(config_file_dir, input_dir))
# glob match
if self.config['recursive'] if PYTHON_VERSION_MAJOR_MINOR >= '3.5' else False:
nb_file_iter = iglob(
os.path.join(input_dir, '**', '*.ipynb'),
recursive=True
)
else:
nb_file_iter = iglob(os.path.join(input_dir, '*.ipynb'))
# Exporter
md_exporter = MarkdownExporter()
# Converting
for nb_path in nb_file_iter:
# Prepare output file/dir
nb_dirname, nb_basename = os.path.split(nb_path)
nb_basename, _ = os.path.splitext(nb_basename)
nb_subdir = os.path.relpath(nb_dirname, input_dir)
md_dir = os.path.realpath(os.path.join(output_dir, nb_subdir))
md_path = os.path.realpath(os.path.join(
md_dir, '{0}.md'.format(nb_basename)))
md_rel_dir = os.path.relpath(md_dir, config['docs_dir'])
md_rel_path = os.path.relpath(md_path, config['docs_dir'])
#
log.debug('nbconvert export notebook %s => %s',
nb_path, md_rel_path)
# run nbconvert
with open(nb_path) as fp:
nb = nbformat.read(fp, nbformat.NO_CONVERT)
body, resources = md_exporter.from_notebook_node(nb)
# save exported
if not os.path.exists(md_dir):
os.makedirs(md_dir)
with open(md_path, 'w') as fp:
fp.write(body)
self._exported_files.append(md_path)
files.append(File(
md_rel_path,
docs_dir,
config['site_dir'],
config['use_directory_urls']
))

return files

def on_post_build(self, config):
for path in self._exported_files:
os.remove(path)

0 comments on commit 7291331

Please sign in to comment.