jekyll-jupyter 0.1.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19152f1071153323b530c1e1a65630f0151ebbc2
4
+ data.tar.gz: 9654a530d830e02628f97fe9407e540339e8ea50
5
+ SHA512:
6
+ metadata.gz: 9d890b4ff822fe78a5ace0374698d92b063ac2eda7c3f38233abff22d48cf43529646f637b860b55d68c4d7b0c2f4ff050f4455111c8d229ee8a3a72ec9d3aff
7
+ data.tar.gz: 15850ebc6458481c9fcfd21b091fda7d19360e6214c9b55781389317a34747aaf3f3891e60525c5eb779ea75e77e8250284bd104768dad645cce2c64970fdac1
@@ -0,0 +1,167 @@
1
+ require 'json'
2
+ require 'tmpdir'
3
+ JEKYLL_MIN_VERSION_3 = Gem::Version.new(Jekyll::VERSION) >= Gem::Version.new('3.0.0') unless defined? JEKYLL_MIN_VERSION_3
4
+
5
+ module Jekyll
6
+ module Jupyter
7
+ module Utils
8
+ def self.has_front_matter?(delegate_method, notebook_ext_re, path)
9
+ ::File.extname(path) =~ notebook_ext_re ? true : delegate_method.call(path)
10
+ end
11
+ end
12
+ end
13
+
14
+ module Converters
15
+ class NotebookConverter < Converter
16
+ IMPLICIT_ATTRIBUTES = %W(
17
+ env=site env-site site-gen=jekyll site-gen-jekyll builder=jekyll builder-jekyll jekyll-version=#{Jekyll::VERSION}
18
+ )
19
+ HEADER_BOUNDARY_RE = /(?<=\p{Graph})\n\n/
20
+
21
+ safe true
22
+
23
+ highlighter_prefix "\n"
24
+ highlighter_suffix "\n"
25
+
26
+ def initialize(config)
27
+ @config = config
28
+ config['notebook'] ||= 'nbconvert'
29
+ notebook_ext = (config['notebook_ext'] ||= 'ipynb')
30
+ notebook_ext_re = (config['notebook_ext_re'] = /^\.(?:#{notebook_ext.tr ',', '|'})$/ix)
31
+ config['notebook_page_attribute_prefix'] ||= 'page'
32
+ unless (nbconvert_config = (config['nbconvert'] ||= {})).frozen?
33
+ # NOTE convert keys to symbols
34
+ nbconvert_config.keys.each do |key|
35
+ nbconvert_config[key.to_sym] = nbconvert_config.delete(key)
36
+ end
37
+ nbconvert_config[:safe] ||= 'safe'
38
+ (nbconvert_config[:attributes] ||= []).tap do |attributes|
39
+ attributes.unshift('notitle', 'hardbreaks', 'idprefix', 'idseparator=-', 'linkattrs')
40
+ attributes.concat(IMPLICIT_ATTRIBUTES)
41
+ end
42
+ if JEKYLL_MIN_VERSION_3
43
+ if (del_method = ::Jekyll::Utils.method(:has_yaml_header?))
44
+ unless (new_method = ::Jekyll::Jupyter::Utils.method(:has_front_matter?)).respond_to?(:curry)
45
+ new_method = new_method.to_proc # Ruby < 2.2
46
+ end
47
+ del_method.owner.define_singleton_method(del_method.name, new_method.curry[del_method][notebook_ext_re])
48
+ end
49
+ end
50
+ nbconvert_config.freeze
51
+ end
52
+ end
53
+
54
+ def setup
55
+ return if @setup
56
+ @setup = true
57
+ case @config['notebook']
58
+ when 'nbconvert'
59
+ unless system('jupyter nbconvert --help > /dev/null')
60
+ STDERR.puts 'Cannot find jupyter. Check virtual enviroments are active or run:'
61
+ STDERR.puts ' $ [sudo] pip install jupyter'
62
+ raise ::Jekyll::Errors::FatalException.new('Missing dependency: jupyter')
63
+ end
64
+ else
65
+ STDERR.puts "Invalid AsciiDoc processor: #{@config['notebook']}"
66
+ STDERR.puts ' Valid options are [ nbconvert ]'
67
+ raise FatalException.new("Invalid AsciiDoc processor: #{@config['notebook']}")
68
+ end
69
+ end
70
+
71
+ def matches(ext)
72
+ ext =~ @config['notebook_ext_re']
73
+ end
74
+
75
+ def output_ext(ext)
76
+ '.html'
77
+ end
78
+
79
+ def convert(content)
80
+ return content if content.empty?
81
+ setup
82
+ case @config['notebook']
83
+ when 'nbconvert'
84
+ json = JSON.parse(content)
85
+ json["cells"].shift
86
+ body = json.to_json
87
+ Dir.mktmpdir do |dir|
88
+ File.open(File.join(dir,"convert.ipynb"), 'w') { |f| f.write(body) }
89
+ system("jupyter", "nbconvert", "--to", "html", "--template",
90
+ "basic", "--output-dir", dir, "#{dir}/convert.ipynb",)
91
+ File.read("#{dir}/convert.html")
92
+ end
93
+ else
94
+ warn 'Unknown AsciiDoc converter. Passing through unparsed content.'
95
+ content
96
+ end
97
+ end
98
+
99
+ def load_header(content)
100
+ setup
101
+ # NOTE merely an optimization; if this doesn't match, the header still gets isolated by the processor
102
+ header = content.split(HEADER_BOUNDARY_RE, 2)[0]
103
+ case @config['notebook']
104
+ when 'nbconvert'
105
+ # NOTE return a document even if header is empty because attributes may be inherited from config
106
+ first_cell = JSON.parse(header)["cells"][0]["source"]
107
+ first_cell.join("")
108
+ else
109
+ warn 'Unknown AsciiDoc converter. Cannot load document header.'
110
+ end
111
+ end
112
+ end
113
+ end
114
+
115
+ module Generators
116
+ # Promotes select AsciiDoc attributes to Jekyll front matter
117
+ class AsciiDocPreprocessor < Generator
118
+ module NoLiquid
119
+ def render_with_liquid?
120
+ false
121
+ end
122
+ end
123
+
124
+ def generate(site)
125
+ notebook_converter = JEKYLL_MIN_VERSION_3 ?
126
+ site.find_converter_instance(Jekyll::Converters::NotebookConverter) :
127
+ site.getConverterImpl(Jekyll::Converters::NotebookConverter)
128
+ notebook_converter.setup
129
+ unless (page_attr_prefix = site.config['notebook_page_attribute_prefix']).empty?
130
+ page_attr_prefix = %(#{page_attr_prefix}-)
131
+ end
132
+ page_attr_prefix_l = page_attr_prefix.length
133
+
134
+ site.pages.each do |page|
135
+ if notebook_converter.matches(page.ext)
136
+ next unless (doc = notebook_converter.load_header(page.content))
137
+ page.data.update(SafeYAML.load(doc))
138
+ page.extend NoLiquid unless page.data['liquid']
139
+ end
140
+ end
141
+
142
+ (JEKYLL_MIN_VERSION_3 ? site.posts.docs : site.posts).each do |post|
143
+ if notebook_converter.matches(JEKYLL_MIN_VERSION_3 ? post.data['ext'] : post.ext)
144
+ next unless (doc = notebook_converter.load_header(post.content))
145
+ post.data.update(SafeYAML.load(doc))
146
+ post.extend NoLiquid unless post.data['liquid']
147
+ end
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ module Filters
154
+ # Convert an AsciiDoc string into HTML output.
155
+ #
156
+ # input - The AsciiDoc String to convert.
157
+ #
158
+ # Returns the HTML formatted String.
159
+ def notebookify(input)
160
+ site = @context.registers[:site]
161
+ converter = JEKYLL_MIN_VERSION_3 ?
162
+ site.find_converter_instance(Jekyll::Converters::NotebookConverter) :
163
+ site.getConverterImpl(Jekyll::Converters::NotebookConverter)
164
+ converter.convert(input)
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Jupyter
3
+ VERSION = '0.1.0.pre'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-jupyter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Tom Jin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jekyll
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ description: A Jekyll plugin that converts Jupyter notebooks in your site source to
42
+ HTML pages using nbconvert.
43
+ email:
44
+ - tom@jin.me.uk
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/jekyll-jupyter.rb
50
+ - lib/jekyll-jupyter/version.rb
51
+ homepage: https://github.com/tom-jin/jekyll-jupyter
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.1
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.6.8
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: A Jekyll plugin that converts Jupyter notebooks in your site source to HTML
75
+ pages using nbconvert.
76
+ test_files: []