jekyll-scalafiddle 1.0.0

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: 3ed10c91cd36c0bc2e03495e38fe421ad68eaefb
4
+ data.tar.gz: 3e2b879f48fe12c7160ce9526bc61f1eed8e885c
5
+ SHA512:
6
+ metadata.gz: 86dc2f8931afe0ced55e4f88c301335b03dfd053d83330c0f5590abe6dc1496d29a22afb4535ba0f650e0fdac29b7ff3dc08c0add48001c70021e1031114980d
7
+ data.tar.gz: e2426b94663bd0000e817d45aff528aff75ba8c95f9ebe4322cf2073e0625aecebac79fc06a0dbab0ed28b417b8e1a7c306ea0286692e4340593f53b37433830
@@ -0,0 +1,26 @@
1
+ ---
2
+ engines:
3
+ duplication:
4
+ enabled: true
5
+ config:
6
+ languages:
7
+ - ruby
8
+ - javascript
9
+ fixme:
10
+ enabled: true
11
+ rubocop:
12
+ enabled: true
13
+ eslint:
14
+ enabled: true
15
+ ratings:
16
+ paths:
17
+ - "**.inc"
18
+ - "**.js"
19
+ - "**.jsx"
20
+ - "**.module"
21
+ - "**.php"
22
+ - "**.py"
23
+ - "**.rb"
24
+ exclude_paths:
25
+ - script/
26
+ - spec/
@@ -0,0 +1,9 @@
1
+ .idea
2
+ *.gem
3
+ Gemfile.lock
4
+ tmp
5
+ spec/dest
6
+ coverage
7
+ _site
8
+ node_modules
9
+ .DS_store
@@ -0,0 +1,127 @@
1
+ ---
2
+ AllCops:
3
+ TargetRubyVersion: 2.0
4
+ Include:
5
+ - lib/**/*.rb
6
+ - spec/**/*.rb
7
+ Lint/EndAlignment:
8
+ Severity: error
9
+ Lint/UnreachableCode:
10
+ Severity: error
11
+ Lint/UselessAccessModifier:
12
+ Enabled: false
13
+ Metrics/AbcSize:
14
+ Max: 20
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - spec/**/*.rb
18
+ Metrics/ClassLength:
19
+ Max: 300
20
+ Exclude:
21
+ - !ruby/regexp /spec\/.*.rb$/
22
+ Metrics/CyclomaticComplexity:
23
+ Max: 8
24
+ Metrics/LineLength:
25
+ Exclude:
26
+ - Rakefile
27
+ - Gemfile
28
+ - jekyll-maps.gemspec
29
+ Max: 90
30
+ Severity: warning
31
+ Metrics/MethodLength:
32
+ Max: 20
33
+ CountComments: false
34
+ Severity: error
35
+ Metrics/ModuleLength:
36
+ Max: 240
37
+ Metrics/ParameterLists:
38
+ Max: 4
39
+ Metrics/PerceivedComplexity:
40
+ Max: 8
41
+ Style/Alias:
42
+ Enabled: false
43
+ Style/AlignArray:
44
+ Enabled: false
45
+ Style/AlignHash:
46
+ EnforcedHashRocketStyle: table
47
+ Style/AlignParameters:
48
+ EnforcedStyle: with_fixed_indentation
49
+ Enabled: false
50
+ Style/AndOr:
51
+ Severity: error
52
+ Style/Attr:
53
+ Enabled: false
54
+ Style/BracesAroundHashParameters:
55
+ Enabled: false
56
+ Style/ClassAndModuleChildren:
57
+ Enabled: false
58
+ Style/Documentation:
59
+ Enabled: false
60
+ Style/DoubleNegation:
61
+ Enabled: false
62
+ Style/EmptyLinesAroundAccessModifier:
63
+ Enabled: false
64
+ Style/EmptyLinesAroundModuleBody:
65
+ Enabled: false
66
+ Style/ExtraSpacing:
67
+ AllowForAlignment: true
68
+ Style/FileName:
69
+ Enabled: false
70
+ Style/FirstParameterIndentation:
71
+ EnforcedStyle: consistent
72
+ Style/GuardClause:
73
+ Enabled: false
74
+ Style/HashSyntax:
75
+ EnforcedStyle: hash_rockets
76
+ Severity: error
77
+ Style/IfUnlessModifier:
78
+ Enabled: false
79
+ Style/IndentArray:
80
+ EnforcedStyle: consistent
81
+ Style/IndentHash:
82
+ EnforcedStyle: consistent
83
+ Style/IndentationWidth:
84
+ Severity: error
85
+ Style/ModuleFunction:
86
+ Enabled: false
87
+ Style/MultilineMethodCallIndentation:
88
+ EnforcedStyle: indented
89
+ Style/MultilineOperationIndentation:
90
+ EnforcedStyle: indented
91
+ Style/MultilineTernaryOperator:
92
+ Severity: error
93
+ Style/PercentLiteralDelimiters:
94
+ PreferredDelimiters:
95
+ "%q": "{}"
96
+ "%Q": "{}"
97
+ "%r": "!!"
98
+ "%s": "()"
99
+ "%w": "()"
100
+ "%W": "()"
101
+ "%x": "()"
102
+ Style/RedundantReturn:
103
+ Enabled: false
104
+ Style/RedundantSelf:
105
+ Enabled: false
106
+ Style/RegexpLiteral:
107
+ EnforcedStyle: percent_r
108
+ Style/RescueModifier:
109
+ Enabled: false
110
+ Style/SignalException:
111
+ EnforcedStyle: only_raise
112
+ Style/SingleLineMethods:
113
+ Enabled: false
114
+ Style/SpaceAroundOperators:
115
+ Enabled: false
116
+ Style/SpaceInsideBrackets:
117
+ Enabled: false
118
+ Style/StringLiterals:
119
+ EnforcedStyle: double_quotes
120
+ Style/StringLiteralsInInterpolation:
121
+ EnforcedStyle: double_quotes
122
+ Style/UnneededCapitalW:
123
+ Enabled: false
124
+ Style/IndentHeredoc:
125
+ Enabled: false
126
+ Style/SymbolArray:
127
+ Enabled: false
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.1
4
+ - 2.3.0
5
+ - 2.2
6
+ - 2.1
7
+ script: script/cibuild
8
+ after_success:
9
+ - bundle exec codeclimate-test-reporter
10
+ sudo: false
11
+ cache: bundler
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "codeclimate-test-reporter", :group => :test, :require => nil
@@ -0,0 +1 @@
1
+ ## 1.0.0
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,135 @@
1
+ # Jekyll ScalaFiddle
2
+
3
+ Integrate ScalaFiddle easily into your Jekyll documentation using this plugin.
4
+
5
+ ## Installation
6
+
7
+ 1. Add the following to your site's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'jekyll-scalafiddle'
11
+ ```
12
+
13
+ 2. Add the following to your site's `_config.yml`:
14
+
15
+ ```yml
16
+ plugins:
17
+ - jekyll-scalafiddle
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ The ScalaFiddle plugin provides a tag `scalafiddle` which you can use to convert a code block in your documentation into an
23
+ editable and runnable fiddle. Simply surround a code block with the tag as shown below.
24
+
25
+ Before:
26
+ ````
27
+ ```scala
28
+ def sum(a: Int, b: Int) = a + b
29
+
30
+ println(sum(2, 2))
31
+ ```
32
+ ````
33
+
34
+ After:
35
+ ````
36
+ {% scalafiddle %}
37
+ ```scala
38
+ def sum(a: Int, b: Int) = a + b
39
+
40
+ println(sum(2, 2))
41
+ ```
42
+ {% endscalafiddle %}
43
+ ````
44
+
45
+ This will instruct the plugin to generate a special `<div>` around your code which will turn it into an editable fiddle when the
46
+ viewer clicks the `Run` button.
47
+
48
+ ![Code block with a Run button](../gitbook/images/fiddle_run.png)
49
+
50
+ ### Parameters
51
+
52
+ Each fiddle can be further customized with parameters. These parameters are described in more detail in the
53
+ [main integration documentation](https://github.com/scalafiddle/scalafiddle-core/blob/master/integrations/README.md). For example:
54
+
55
+ ````
56
+ {% scalafiddle prefix="import scala.util.Random" theme="dark" %}
57
+ ````
58
+
59
+ ### Templates
60
+
61
+ Each ScalaFiddle consists of a "user visible" part and of a template that is hidden from the user. You can use this template
62
+ code to provide additional or common functionality without cluttering each of your fiddles. A template can contain code that
63
+ comes before the user code, or also code that comes afterwards (separated by a line starting with four slashes `////`).
64
+
65
+ The example template below wraps the user's code into a `{}` block, assigns it to a `val` and prints the result at the end.
66
+
67
+ ```scala
68
+ val result = {
69
+ ////
70
+ }
71
+ println(result)
72
+ ```
73
+
74
+ Templates are stored by default under the `_scalafiddle` directory (which can be changed via configuration). Each template must
75
+ have a `.scala` extension. For the example above, the correct file name would be `_scalafiddle/Result.scala`
76
+
77
+ Use the template in documentation
78
+ ````
79
+ {% scalafiddle template="Result" %}
80
+ ```scala
81
+ def sum(a: Int, b: Int) = a + b
82
+ sum(2, 2)
83
+ ```
84
+ {% endscalafiddle %}
85
+ ````
86
+
87
+ You will see only the user defined code in the fiddle. The result pane, however, shows you that the template code was also
88
+ run.
89
+
90
+ ![Using a template](../gitbook/images/use_template.png)
91
+
92
+ The final code executed will actually be:
93
+
94
+ ```scala
95
+ val result = {
96
+ def sum(a: Int, b: Int) = a + b
97
+ sum(2, 2)
98
+ }
99
+ println(result)
100
+ ```
101
+
102
+ ## Configuration
103
+
104
+ The ScalaFiddle plugin can be configured the same as any other Jekyll plugin via `_config.yaml`. For example:
105
+
106
+ ```yaml
107
+ scalafiddle:
108
+ dependency: io.circe %%% circe-core % 0.8.0,io.circe %%% circe-generic % 0.8.0,io.circe %%% circe-parser % 0.8.0
109
+ scalaFiddleUrl: http://localhost:8880/
110
+ ```
111
+
112
+ In the configuration you can provide default values for fiddle parameters such as `dependency`, `minheight`, `theme` etc.
113
+ Typically you would use the `dependency` configuration in a library documentation where each code example has the same dependency
114
+ to your library.
115
+
116
+ You can also configure the location of templates with `templateDir` and the URL for the ScalaFiddle service (if you want to
117
+ run your own ScalaFiddle server, for example locally) using `scalaFiddleUrl`.
118
+
119
+ ## Usage with sbt-microsites
120
+
121
+ [sbt-microsites](https://47deg.github.io/sbt-microsites/) is a plugin for sbt that helps Scala developers build and publish
122
+ documentation for their project. It's based on Jekyll, so you can use this same plugin. Unlike regular Jekyll, you cannot use
123
+ `gem` to install the plugin, but you must manually copy the `jekyll-scalafiddle.rb` file under a `plugins` directory in your
124
+ microsite project.
125
+
126
+ To configure ScalaFiddle plugin, use the `micrositeConfigYaml` option in your project definition. For example:
127
+
128
+ ```scala
129
+ micrositeConfigYaml := ConfigYml(yamlInline =
130
+ """
131
+ |scalafiddle:
132
+ | url: http://localhost:8880/
133
+ | dependency: io.circe %%% circe-core % 0.8.0,io.circe %%% circe-generic % 0.8.0,io.circe %%% circe-parser % 0.8.0
134
+ """.stripMargin)
135
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "jekyll-scalafiddle/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "jekyll-scalafiddle"
7
+ spec.summary = "Jekyll ScalaFiddle integration"
8
+ spec.description = "Easily embed ScalaFiddle in Jekyll pages"
9
+ spec.version = Jekyll::ScalaFiddle::VERSION
10
+ spec.authors = ["Otto Chrons"]
11
+ spec.email = ["otto@chrons.me"]
12
+
13
+ spec.homepage = "https://github.com/scalafiddle/scalafiddle-core/integrations/jekyll"
14
+ spec.licenses = ["Apache-2.0"]
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|spec|features)/!) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "jekyll", "~> 3.0"
19
+
20
+ spec.add_development_dependency "rake", "~> 11.0"
21
+ spec.add_development_dependency "rspec", "~> 3.5"
22
+ spec.add_development_dependency "rubocop", "0.49.1"
23
+ end
@@ -0,0 +1,191 @@
1
+ module Jekyll
2
+ module ScalaFiddle
3
+ class OptionsParser
4
+ OPTIONS_SYNTAX = %r!([^\s]+)\s*=\s*['"]+([^'"]+)['"]+!
5
+ ALLOWED_FLAGS = %w(
6
+ autorun
7
+ ).freeze
8
+ ALLOWED_ATTRIBUTES = %w(
9
+ template
10
+ prefix
11
+ dependency
12
+ scalaversion
13
+ selector
14
+ minheight
15
+ layout
16
+ theme
17
+ ).freeze
18
+
19
+ class << self
20
+ def parse(raw_options)
21
+ options = {
22
+ :attributes => {},
23
+ :filters => {},
24
+ :flags => {}
25
+ }
26
+ raw_options.scan(OPTIONS_SYNTAX).each do |key, value|
27
+ if ALLOWED_ATTRIBUTES.include?(key)
28
+ options[:attributes][key.to_sym] = value
29
+ else
30
+ options[:filters][key] = value
31
+ end
32
+ end
33
+ ALLOWED_FLAGS.each do |key|
34
+ options[:flags][key.to_sym] = true if raw_options.include?(key)
35
+ end
36
+ options
37
+ end
38
+ end
39
+ end
40
+
41
+ class ScalaFiddleTag < Liquid::Block
42
+
43
+ def initialize(tag, args, _)
44
+ @args = OptionsParser.parse(args)
45
+ super
46
+ end
47
+
48
+ def render(context)
49
+ site = context.registers[:site]
50
+ converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
51
+ content = converter.convert(super(context))
52
+ config = site.config.fetch("scalafiddle", {})
53
+ result = <<HTML
54
+ <div #{render_attributes(config)}>#{content}</div>
55
+ HTML
56
+ result
57
+ end
58
+
59
+ private
60
+ def render_attributes(config)
61
+ attributes = {"data-scalafiddle" => ""}
62
+
63
+ if @args[:attributes][:template]
64
+ attributes["data-template"] = "'#{@args[:attributes][:template]}'"
65
+ end
66
+ # apply default attributes from config
67
+ if config.key?("dependency")
68
+ attributes["data-dependency"] = "'#{config["dependency"]}'"
69
+ end
70
+ if config.key?("scalaversion")
71
+ attributes["data-scalaversion"] = "'#{config["scalaversion"]}'"
72
+ end
73
+ if config.key?("selector")
74
+ attributes["data-selector"] = "'#{config["selector"]}'"
75
+ end
76
+ if config.key?("theme")
77
+ attributes["data-theme"] = "'#{config["theme"]}'"
78
+ end
79
+ # apply tag attributes
80
+ if @args[:attributes][:dependency]
81
+ attributes["data-dependency"] = "'#{@args[:attributes][:dependency]}'"
82
+ end
83
+ if @args[:attributes][:scalaversion]
84
+ attributes["data-scalaversion"] = "'#{@args[:attributes][:scalaversion]}'"
85
+ end
86
+ if @args[:attributes][:selector]
87
+ attributes["data-selector"] = "'#{@args[:attributes][:selector]}'"
88
+ end
89
+ if @args[:attributes][:prefix]
90
+ attributes["data-prefix"] = "'#{@args[:attributes][:prefix]}'"
91
+ end
92
+ if @args[:attributes][:minheight]
93
+ attributes["data-minheight"] = "'#{@args[:attributes][:minheight]}'"
94
+ end
95
+ if @args[:attributes][:layout]
96
+ attributes["data-layout"] = "'#{@args[:attributes][:layout]}'"
97
+ end
98
+ if @args[:attributes][:theme]
99
+ attributes["data-theme"] = "'#{@args[:attributes][:theme]}'"
100
+ end
101
+ if @args[:flags][:autorun]
102
+ attributes["data-autorun"] = ""
103
+ end
104
+ attrs = ""
105
+ attributes.each {|key, value|
106
+ if value.empty?
107
+ attrs << "#{key} "
108
+ else
109
+ attrs << "#{key}=#{value} "
110
+ end
111
+ }
112
+ attrs.rstrip
113
+ end
114
+ end
115
+
116
+ class ScalaFiddleIntegration
117
+ BODY_END_TAG = %r!</body>!
118
+
119
+ class << self
120
+ def append_scalafiddle_code(doc)
121
+ @config = doc.site.config
122
+ if doc.output =~ BODY_END_TAG
123
+ # Insert code before body's end if this document has one.
124
+ location = doc.output.index(BODY_END_TAG)
125
+ doc.output = doc.output.slice(0, location) + api_code(doc) + doc.output.slice(location, doc.output.length - location)
126
+ else
127
+ doc.output.prepend(api_code(doc))
128
+ end
129
+ end
130
+
131
+ private
132
+ def load_template(template, dir)
133
+ file = dir + "/" + template + ".scala"
134
+ content = File.readlines(file)
135
+ if content.index {|l| l.start_with?("////")} == nil
136
+ raise RuntimeException, "Template is missing a //// marker"
137
+ end
138
+ {
139
+ :name => template,
140
+ :pre => content.take_while {|l| !l.start_with?("////")},
141
+ :post => content.drop_while {|l| !l.start_with?("////")}.drop(1)
142
+ }
143
+ end
144
+
145
+ private
146
+ def escape_string(strs)
147
+ strs.join.gsub(/\\/, "\\\\\\\\").gsub(/\n/, "\\n").gsub(/\r/, "").gsub(/\t/, "\\t").gsub(/'/) {|m| "\\'"}
148
+ end
149
+
150
+ private
151
+ def api_code(page)
152
+ result = ""
153
+ if page.output =~ /<div data-scalafiddle=""/
154
+ templates = page.output.scan(/<div data-scalafiddle="" data-template="([^"]+)"/).flatten
155
+ unless templates.empty?
156
+ result += %Q(
157
+ <script>
158
+ window.scalaFiddleTemplates = {
159
+ )
160
+ dir = page.site.source + "/" + @config.fetch("scalafiddle", {}).fetch("templateDir", "_scalafiddle")
161
+ js_code = templates.map {|template| load_template(template, dir)}
162
+ result += js_code.map {|template|
163
+ %Q(
164
+ '#{template[:name]}': {
165
+ pre: '#{escape_string(template[:pre])}',
166
+ post: '#{escape_string(template[:post])}'
167
+ }
168
+ )
169
+ }.join(",\n")
170
+ result += %Q(
171
+ }
172
+ </script>
173
+ )
174
+ end
175
+ result += %Q(
176
+ <script defer src='#{@config.fetch("scalafiddle", {}).fetch("scalaFiddleUrl", "https://embed.scalafiddle.io/")}integration.js'></script>
177
+ )
178
+ end
179
+ result
180
+ end
181
+ end
182
+ end
183
+ end
184
+ end
185
+
186
+ Liquid::Template.register_tag("scalafiddle", Jekyll::ScalaFiddle::ScalaFiddleTag)
187
+
188
+ Jekyll::Hooks.register [:pages, :documents], :post_render do |doc|
189
+ Jekyll::ScalaFiddle::ScalaFiddleIntegration.append_scalafiddle_code(doc)
190
+ end
191
+
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module ScalaFiddle
3
+ VERSION = "1.0.0".freeze
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ bundle install
@@ -0,0 +1,4 @@
1
+ #! /bin/sh
2
+
3
+ bundle exec rspec --color $@
4
+ bundle exec rubocop -S -D
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-scalafiddle
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Otto Chrons
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '11.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '11.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.49.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.49.1
69
+ description: Easily embed ScalaFiddle in Jekyll pages
70
+ email:
71
+ - otto@chrons.me
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".codeclimate.yml"
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - History.md
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - jekyll-scalafiddle.gemspec
86
+ - lib/jekyll-scalafiddle.rb
87
+ - lib/jekyll-scalafiddle/version.rb
88
+ - script/bootstrap
89
+ - script/cibuild
90
+ homepage: https://github.com/scalafiddle/scalafiddle-core/integrations/jekyll
91
+ licenses:
92
+ - Apache-2.0
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.5.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Jekyll ScalaFiddle integration
114
+ test_files: []