From b0b24795b24ee6809397fbbadf42f31f310a219f Mon Sep 17 00:00:00 2001 From: Agustin Henze Date: Wed, 8 Jul 2015 07:35:02 -0300 Subject: Imported Upstream version 7.6.0 --- nikola/plugins/compile/__init__.py | 2 +- nikola/plugins/compile/html.plugin | 2 +- nikola/plugins/compile/html.py | 8 +- nikola/plugins/compile/ipynb.plugin | 8 +- nikola/plugins/compile/ipynb.py | 150 +++++++++++++++++++++++++ nikola/plugins/compile/ipynb/README.txt | 44 -------- nikola/plugins/compile/ipynb/__init__.py | 97 ---------------- nikola/plugins/compile/markdown.plugin | 2 +- nikola/plugins/compile/markdown/__init__.py | 12 +- nikola/plugins/compile/markdown/mdx_gist.py | 48 ++++---- nikola/plugins/compile/markdown/mdx_nikola.py | 4 +- nikola/plugins/compile/markdown/mdx_podcast.py | 2 +- nikola/plugins/compile/pandoc.plugin | 2 +- nikola/plugins/compile/pandoc.py | 11 +- nikola/plugins/compile/php.plugin | 2 +- nikola/plugins/compile/php.py | 3 +- nikola/plugins/compile/rest.plugin | 2 +- nikola/plugins/compile/rest/__init__.py | 114 +++++++++++-------- nikola/plugins/compile/rest/chart.py | 2 +- nikola/plugins/compile/rest/doc.py | 2 +- nikola/plugins/compile/rest/gist.py | 20 +--- nikola/plugins/compile/rest/listing.py | 112 ++++++++++++++---- nikola/plugins/compile/rest/media.py | 2 +- nikola/plugins/compile/rest/post_list.py | 20 +++- nikola/plugins/compile/rest/slides.py | 2 +- nikola/plugins/compile/rest/thumbnail.plugin | 9 ++ nikola/plugins/compile/rest/thumbnail.py | 69 ++++++++++++ nikola/plugins/compile/rest/vimeo.py | 12 +- nikola/plugins/compile/rest/youtube.py | 2 +- 29 files changed, 467 insertions(+), 298 deletions(-) create mode 100644 nikola/plugins/compile/ipynb.py delete mode 100644 nikola/plugins/compile/ipynb/README.txt delete mode 100644 nikola/plugins/compile/ipynb/__init__.py create mode 100644 nikola/plugins/compile/rest/thumbnail.plugin create mode 100644 nikola/plugins/compile/rest/thumbnail.py (limited to 'nikola/plugins/compile') diff --git a/nikola/plugins/compile/__init__.py b/nikola/plugins/compile/__init__.py index 6ad8bac..a1d17a6 100644 --- a/nikola/plugins/compile/__init__.py +++ b/nikola/plugins/compile/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright © 2012-2014 Roberto Alsina and others. +# Copyright © 2012-2015 Roberto Alsina and others. # Permission is hereby granted, free of charge, to any # person obtaining a copy of this software and associated diff --git a/nikola/plugins/compile/html.plugin b/nikola/plugins/compile/html.plugin index 21dd338..66623b2 100644 --- a/nikola/plugins/compile/html.plugin +++ b/nikola/plugins/compile/html.plugin @@ -4,7 +4,7 @@ Module = html [Documentation] Author = Roberto Alsina -Version = 0.1 +Version = 1.0 Website = http://getnikola.com Description = Compile HTML into HTML (just copy) diff --git a/nikola/plugins/compile/html.py b/nikola/plugins/compile/html.py index 24bf385..ab0c2f6 100644 --- a/nikola/plugins/compile/html.py +++ b/nikola/plugins/compile/html.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright © 2012-2014 Roberto Alsina and others. +# Copyright © 2012-2015 Roberto Alsina and others. # Permission is hereby granted, free of charge, to any # person obtaining a copy of this software and associated @@ -29,18 +29,16 @@ from __future__ import unicode_literals import os -import re import io from nikola.plugin_categories import PageCompiler from nikola.utils import makedirs, write_metadata -_META_SEPARATOR = '(' + os.linesep * 2 + '|' + ('\n' * 2) + '|' + ("\r\n" * 2) + ')' - class CompileHtml(PageCompiler): """Compile HTML into HTML.""" name = "html" + friendly_name = "HTML" def compile_html(self, source, dest, is_two_file=True): makedirs(os.path.dirname(dest)) @@ -48,7 +46,7 @@ class CompileHtml(PageCompiler): with io.open(source, "r", encoding="utf8") as in_file: data = in_file.read() if not is_two_file: - data = re.split(_META_SEPARATOR, data, maxsplit=1)[-1] + _, data = self.split_metadata(data) out_file.write(data) return True diff --git a/nikola/plugins/compile/ipynb.plugin b/nikola/plugins/compile/ipynb.plugin index e258d8a..efe6702 100644 --- a/nikola/plugins/compile/ipynb.plugin +++ b/nikola/plugins/compile/ipynb.plugin @@ -3,8 +3,8 @@ Name = ipynb Module = ipynb [Documentation] -Author = Damian Avila -Version = 1.0 -Website = http://www.oquanta.info -Description = Compile IPython notebooks into HTML +Author = Damian Avila, Chris Warrick and others +Version = 2.0.0 +Website = http://www.damian.oquanta.info/ +Description = Compile IPython notebooks into Nikola posts diff --git a/nikola/plugins/compile/ipynb.py b/nikola/plugins/compile/ipynb.py new file mode 100644 index 0000000..82b76c8 --- /dev/null +++ b/nikola/plugins/compile/ipynb.py @@ -0,0 +1,150 @@ +# -*- coding: utf-8 -*- + +# Copyright © 2013-2015 Damián Avila, Chris Warrick and others. + +# Permission is hereby granted, free of charge, to any +# person obtaining a copy of this software and associated +# documentation files (the "Software"), to deal in the +# Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the +# Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice +# shall be included in all copies or substantial portions of +# the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +"""Implementation of compile_html based on nbconvert.""" + +from __future__ import unicode_literals, print_function +import io +import os +import sys + +try: + import IPython + from IPython.nbconvert.exporters import HTMLExporter + if IPython.version_info[0] >= 3: # API changed with 3.0.0 + from IPython import nbformat + current_nbformat = nbformat.current_nbformat + from IPython.kernel import kernelspec + else: + import IPython.nbformat.current as nbformat + current_nbformat = 'json' + kernelspec = None + + from IPython.config import Config + flag = True +except ImportError: + flag = None + +from nikola.plugin_categories import PageCompiler +from nikola.utils import makedirs, req_missing, get_logger + + +class CompileIPynb(PageCompiler): + """Compile IPynb into HTML.""" + + name = "ipynb" + friendly_name = "Jupyter/IPython Notebook" + demote_headers = True + default_kernel = 'python2' if sys.version_info[0] == 2 else 'python3' + + def set_site(self, site): + self.logger = get_logger('compile_ipynb', site.loghandlers) + super(CompileIPynb, self).set_site(site) + + def compile_html(self, source, dest, is_two_file=True): + if flag is None: + req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)') + makedirs(os.path.dirname(dest)) + HTMLExporter.default_template = 'basic' + c = Config(self.site.config['IPYNB_CONFIG']) + exportHtml = HTMLExporter(config=c) + with io.open(dest, "w+", encoding="utf8") as out_file: + with io.open(source, "r", encoding="utf8") as in_file: + nb_json = nbformat.read(in_file, current_nbformat) + (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. + """ + if flag is None: + req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)') + source = post.source_path + with io.open(source, "r", encoding="utf8") as in_file: + nb_json = nbformat.read(in_file, current_nbformat) + # Metadata might not exist in two-file posts or in hand-crafted + # .ipynb files. + return nb_json.get('metadata', {}).get('nikola', {}) + + def create_post(self, path, **kw): + if flag is None: + req_missing(['ipython[notebook]>=2.0.0'], 'build this site (compile ipynb)') + content = kw.pop('content', None) + onefile = kw.pop('onefile', False) + kernel = kw.pop('ipython_kernel', None) + # is_page is not needed to create the file + kw.pop('is_page', False) + + metadata = {} + metadata.update(self.default_metadata) + metadata.update(kw) + + makedirs(os.path.dirname(path)) + + if content.startswith("{"): + # imported .ipynb file, guaranteed to start with "{" because it’s JSON. + nb = nbformat.reads(content, current_nbformat) + else: + if IPython.version_info[0] >= 3: + nb = nbformat.v4.new_notebook() + nb["cells"] = [nbformat.v4.new_markdown_cell(content)] + else: + nb = nbformat.new_notebook() + nb["worksheets"] = [nbformat.new_worksheet(cells=[nbformat.new_text_cell('markdown', [content])])] + + if kernelspec is not None: + if kernel is None: + kernel = self.default_kernel + self.logger.notice('No kernel specified, assuming "{0}".'.format(kernel)) + + IPYNB_KERNELS = {} + ksm = kernelspec.KernelSpecManager() + for k in ksm.find_kernel_specs(): + IPYNB_KERNELS[k] = ksm.get_kernel_spec(k).to_dict() + IPYNB_KERNELS[k]['name'] = k + del IPYNB_KERNELS[k]['argv'] + + if kernel not in IPYNB_KERNELS: + self.logger.error('Unknown kernel "{0}". Maybe you mispelled it?'.format(kernel)) + self.logger.info("Available kernels: {0}".format(", ".join(sorted(IPYNB_KERNELS)))) + raise Exception('Unknown kernel "{0}"'.format(kernel)) + + nb["metadata"]["kernelspec"] = IPYNB_KERNELS[kernel] + else: + # Older IPython versions don’t need kernelspecs. + pass + + if onefile: + nb["metadata"]["nikola"] = metadata + + with io.open(path, "w+", encoding="utf8") as fd: + if IPython.version_info[0] >= 3: + nbformat.write(nb, fd, 4) + else: + nbformat.write(nb, fd, 'ipynb') diff --git a/nikola/plugins/compile/ipynb/README.txt b/nikola/plugins/compile/ipynb/README.txt deleted file mode 100644 index 0a7d6db..0000000 --- a/nikola/plugins/compile/ipynb/README.txt +++ /dev/null @@ -1,44 +0,0 @@ -To make this work... - -1- You can install the "jinja-site-ipython" theme using this command: - -$ nikola install_theme -n jinja-site-ipython - -(or xkcd-site-ipython, if you want xkcd styling) - -More info here about themes: -http://getnikola.com/handbook.html#getting-more-themes - -OR - -You can to download the "jinja-site-ipython" theme from here: -https://github.com/damianavila/jinja-site-ipython-theme-for-Nikola -and copy the "site-ipython" folder inside the "themes" folder of your site. - - -2- Then, just add: - -post_pages = ( - ("posts/*.ipynb", "posts", "post.tmpl", True), - ("stories/*.ipynb", "stories", "story.tmpl", False), -) - -and - -THEME = 'jinja-site-ipython' (or 'xkcd-site-ipython', if you want xkcd styling) - -to your conf.py. -Finally... to use it: - -$nikola new_page -f ipynb - -**NOTE**: Just IGNORE the "-1" and "-2" options in nikola new_page command, by default this compiler -create one metadata file and the corresponding naive IPython notebook. - -$nikola build - -And deploy the output folder... to see it locally: $nikola serve -If you have any doubts, just ask: @damianavila - -Cheers. -Damián diff --git a/nikola/plugins/compile/ipynb/__init__.py b/nikola/plugins/compile/ipynb/__init__.py deleted file mode 100644 index 7dde279..0000000 --- a/nikola/plugins/compile/ipynb/__init__.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright © 2013-2014 Damián Avila and others. - -# Permission is hereby granted, free of charge, to any -# person obtaining a copy of this software and associated -# documentation files (the "Software"), to deal in the -# Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, -# distribute, sublicense, and/or sell copies of the -# Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice -# shall be included in all copies or substantial portions of -# the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY -# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE -# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR -# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR -# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR -# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -"""Implementation of compile_html based on nbconvert.""" - -from __future__ import unicode_literals, print_function -import io -import os - -try: - from IPython.nbconvert.exporters import HTMLExporter - from IPython.nbformat import current as nbformat - from IPython.config import Config - flag = True -except ImportError: - flag = None - -from nikola.plugin_categories import PageCompiler -from nikola.utils import makedirs, req_missing - - -class CompileIPynb(PageCompiler): - """Compile IPynb into HTML.""" - - name = "ipynb" - supports_onefile = False - demote_headers = True - - def compile_html(self, source, dest, is_two_file=True): - if flag is None: - req_missing(['ipython>=1.1.0'], 'build this site (compile ipynb)') - makedirs(os.path.dirname(dest)) - HTMLExporter.default_template = 'basic' - c = Config(self.site.config['IPYNB_CONFIG']) - exportHtml = HTMLExporter(config=c) - with io.open(dest, "w+", encoding="utf8") as out_file: - with io.open(source, "r", encoding="utf8") as in_file: - nb = in_file.read() - nb_json = nbformat.reads_json(nb) - (body, resources) = exportHtml.from_notebook_node(nb_json) - out_file.write(body) - - def create_post(self, path, **kw): - # content and onefile are ignored by ipynb. - kw.pop('content', None) - onefile = kw.pop('onefile', False) - kw.pop('is_page', False) - - makedirs(os.path.dirname(path)) - if onefile: - raise Exception('The one-file format is not supported by this compiler.') - with io.open(path, "w+", encoding="utf8") as fd: - fd.write("""{ - "metadata": { - "name": "" - }, - "nbformat": 3, - "nbformat_minor": 0, - "worksheets": [ - { - "cells": [ - { - "cell_type": "code", - "collapsed": false, - "input": [], - "language": "python", - "metadata": {}, - "outputs": [] - } - ], - "metadata": {} - } - ] -}""") diff --git a/nikola/plugins/compile/markdown.plugin b/nikola/plugins/compile/markdown.plugin index 157579a..a44b798 100644 --- a/nikola/plugins/compile/markdown.plugin +++ b/nikola/plugins/compile/markdown.plugin @@ -4,7 +4,7 @@ Module = markdown [Documentation] Author = Roberto Alsina -Version = 0.1 +Version = 1.0 Website = http://getnikola.com Description = Compile Markdown into HTML diff --git a/nikola/plugins/compile/markdown/__init__.py b/nikola/plugins/compile/markdown/__init__.py index 47c7c9b..fbe049d 100644 --- a/nikola/plugins/compile/markdown/__init__.py +++ b/nikola/plugins/compile/markdown/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright © 2012-2014 Roberto Alsina and others. +# Copyright © 2012-2015 Roberto Alsina and others. # Permission is hereby granted, free of charge, to any # person obtaining a copy of this software and associated @@ -30,7 +30,6 @@ from __future__ import unicode_literals import io import os -import re try: from markdown import markdown @@ -45,24 +44,27 @@ from nikola.utils import makedirs, req_missing, write_metadata class CompileMarkdown(PageCompiler): - """Compile markdown into HTML.""" + """Compile Markdown into HTML.""" name = "markdown" + friendly_name = "Markdown" demote_headers = True extensions = [] site = None def set_site(self, site): + self.config_dependencies = [] for plugin_info in site.plugin_manager.getPluginsOfCategory("MarkdownExtension"): if plugin_info.name in site.config['DISABLED_PLUGINS']: site.plugin_manager.removePluginFromCategory(plugin_info, "MarkdownExtension") continue - + self.config_dependencies.append(plugin_info.name) site.plugin_manager.activatePluginByName(plugin_info.name) plugin_info.plugin_object.set_site(site) self.extensions.append(plugin_info.plugin_object) plugin_info.plugin_object.short_help = plugin_info.description + self.config_dependencies.append(str(sorted(site.config.get("MARKDOWN_EXTENSIONS")))) return super(CompileMarkdown, self).set_site(site) def compile_html(self, source, dest, is_two_file=True): @@ -74,7 +76,7 @@ class CompileMarkdown(PageCompiler): with io.open(source, "r", encoding="utf8") as in_file: data = in_file.read() if not is_two_file: - data = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)[-1] + _, data = self.split_metadata(data) output = markdown(data, self.extensions) out_file.write(output) diff --git a/nikola/plugins/compile/markdown/mdx_gist.py b/nikola/plugins/compile/markdown/mdx_gist.py index 4209bdd..70e7394 100644 --- a/nikola/plugins/compile/markdown/mdx_gist.py +++ b/nikola/plugins/compile/markdown/mdx_gist.py @@ -203,14 +203,11 @@ except ImportError: Extension = Pattern = object from nikola.plugin_categories import MarkdownExtension -from nikola.utils import get_logger, req_missing, STDERR_HANDLER +from nikola.utils import get_logger, STDERR_HANDLER -LOGGER = get_logger('compile_markdown.mdx_gist', STDERR_HANDLER) +import requests -try: - import requests -except ImportError: - requests = None # NOQA +LOGGER = get_logger('compile_markdown.mdx_gist', STDERR_HANDLER) GIST_JS_URL = "https://gist.github.com/{0}.js" GIST_FILE_JS_URL = "https://gist.github.com/{0}.js?file={1}" @@ -261,32 +258,27 @@ class GistPattern(Pattern): gist_elem.set('class', 'gist') script_elem = etree.SubElement(gist_elem, 'script') - if requests: - noscript_elem = etree.SubElement(gist_elem, 'noscript') - - try: - if gist_file: - script_elem.set('src', GIST_FILE_JS_URL.format( - gist_id, gist_file)) - raw_gist = (self.get_raw_gist_with_filename( - gist_id, gist_file)) + noscript_elem = etree.SubElement(gist_elem, 'noscript') - else: - script_elem.set('src', GIST_JS_URL.format( - gist_id)) - raw_gist = (self.get_raw_gist(gist_id)) + try: + if gist_file: + script_elem.set('src', GIST_FILE_JS_URL.format( + gist_id, gist_file)) + raw_gist = (self.get_raw_gist_with_filename( + gist_id, gist_file)) - # Insert source as
 within