rust_playground_highlight 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c5c72a7fc01087051546bc74ff730daafe70be3
4
+ data.tar.gz: 5e65de416638025c5f52d8cb96bdb861c9da9e4a
5
+ SHA512:
6
+ metadata.gz: b5308f18cafa27081619bece3a652d4618b2bf54ad552cb2728077294b8093a3e6d6df9a225f1c21763230e99bc52b6c30612e8cd7f201a11992d0c4a4b6556f
7
+ data.tar.gz: cccd163653d4bece3779aa97a2adc385a96b5503e3517c2166c650be4fa80e16b04fb4201f77aba55d2929186c2bb8526bd9f9e4b1e72ae2514c44b5ad1f9414
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Rust Playground Highlight Blocks
2
+
3
+ Automatically add links to the Rust Playground for code samples in your Jekyll site, so that they look like this:
4
+
5
+ <img src="http://i.imgur.com/gkFQsHJ.png" />
6
+
7
+ ## Usage
8
+
9
+ You can link two different kinds of Rust Playground pages -- dynamically generated & gist generated
10
+
11
+ ### Dynamically Generated
12
+
13
+ The content of your block appears in the Rust Playground exactly as it appears. This takes advantage of Rust Playground's support for displaying code via URL query strings.
14
+
15
+ The advantage of this approach is that you only have to type your Rust code once. The code in your highlight block appears exactly as is in the Playground.
16
+
17
+ ### Use
18
+ If you wrap a block of Rust code in rp_highlight tags, like this
19
+
20
+ ```
21
+ {% rp_highlight rust %}
22
+ fn main() {
23
+ println!("{}", "Rust Is Memory Safe!"};
24
+ }
25
+ {% endrp_highlight %}
26
+ ```
27
+
28
+ This will be rendered like a normal HighlightBlock, but with the addition of a "Run" button that links to a Rust Playground page with your code.
29
+
30
+ The code above would link to a playground page [like this](https://play.rust-lang.org/?code=fn+main%28%29+%7B%0A++println!%28"%7B%7D","Rust%20is%20Memory%20Safe!"%29%3B%0A%7D&version=stable)
31
+
32
+ # Gist Generated
33
+
34
+ The Rust Playground can also display code that is in a Gist. You can use the Gist ID to create a Rust Playground link for your code block:
35
+
36
+ ```
37
+ {% rp_highlight rust gist_id=cc6e026b0654a4216074 %}
38
+ println!("{}", "Rust Is Memory Safe!"};
39
+ }
40
+ {% endrp_highlight %}
41
+ ```
42
+
43
+ With Gist Generated blocks only the code in the Gist is displayed in the Playground. The code in your block is ignored. This is nice if you want to only display a small section of code in your block, but you want the full version of the code to be in the Playground.
44
+
45
+ In the example above, I've left the `main` function out of the sample code, but it is present in the [Playground](https://play.rust-lang.org/?gist=cc6e026b0654a4216074&version=stable)
46
+
47
+ ## Requirements
48
+
49
+ I am using this with Jekyll 2.5.3. It *should* work with Jekyll 3
50
+
51
+ ## Limitations
52
+
53
+ - Links always point to the stable Rust channel. Support for Beta or Nightly would be nice.
54
+
data/VERSION.md ADDED
@@ -0,0 +1,10 @@
1
+ # Version History
2
+
3
+ # 0.1.1
4
+
5
+ - Deploying as a gem
6
+ - Marking the plugin as `safe`
7
+
8
+ # 0.1
9
+
10
+ Initial Release
@@ -0,0 +1,100 @@
1
+ module Jekyll
2
+ module Tags
3
+ class RustPlaygroundHighlight < HighlightBlock
4
+ safe true
5
+ attr_accessor :raw, :gist_id
6
+
7
+ def add_code_tag(code)
8
+ super.sub(/\n*<\/pre>/,"</pre><a href='#{playground_url}' target='_new' class='playground btn btn-xs btn-primary active' role='button'>Run</a>").strip
9
+ end
10
+
11
+ def render(context)
12
+ #calls render on Liquid::Block, which returns the content of the block
13
+ self.raw = self.method(:render).super_method.super_method.call(context).to_s.strip
14
+ self.gist_id = options_hash[:playground_id]
15
+ super
16
+ end
17
+
18
+ # The @options ivar was renamed to @highlight_options in commit:
19
+ # https://github.com/jekyll/jekyll/commit/a7730914df272d003de6d3b63669e8e8417e0c61
20
+ def options_hash
21
+ instance_variable_get(:@highlight_options) || instance_variable_get(:@options)
22
+ end
23
+
24
+ def playground_url
25
+ PlaygroundLink.build(self).url
26
+ end
27
+ end
28
+
29
+ class PlaygroundLink < SimpleDelegator
30
+ def self.build(rust_highlight_block)
31
+ link_type = [GistGenerated, DynamicallyGenerated, InvalidType].detect { |type| type.block_allowed?(rust_highlight_block) }
32
+
33
+ new(
34
+ link_type.new(
35
+ rust_highlight_block.send(link_type.id_method)
36
+ )
37
+ )
38
+ end
39
+
40
+ def url
41
+ "https://play.rust-lang.org/?#{query_string}"
42
+ end
43
+ end
44
+
45
+ class LinkType
46
+ def self.block_allowed?(block)
47
+ block.public_send(self.required_value)
48
+ rescue
49
+ false
50
+ end
51
+
52
+ def self.required_value
53
+ :object_id
54
+ end
55
+
56
+ def initialize(x)
57
+ self.query_value = x
58
+ end
59
+
60
+ def query_string
61
+ ""
62
+ end
63
+
64
+ private
65
+
66
+ attr_accessor :query_value
67
+ end
68
+
69
+ class GistGenerated < LinkType
70
+ def self.required_value
71
+ :gist_id
72
+ end
73
+
74
+ def query_string
75
+ "gist=#{query_value}"
76
+ end
77
+ end
78
+
79
+ class DynamicallyGenerated < LinkType
80
+ def self.required_value
81
+ :raw
82
+ end
83
+
84
+ def query_string
85
+ "code=#{CGI.escape(query_value)}"
86
+ end
87
+ end
88
+
89
+ class InvalidType < LinkType
90
+ def self.block_allowed?(_)
91
+ true
92
+ end
93
+ end
94
+
95
+ Liquid::Template.register_tag(
96
+ 'rp_highlight',
97
+ Jekyll::Tags::RustPlaygroundHighlight
98
+ )
99
+ end
100
+ end
data/license.txt ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2015 Ian Whitney
3
+
4
+ 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:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ 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.
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rust_playground_highlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - IanWhitney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-20 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: '2.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ description: Automatically add links to the Rust Playground for code samples in your
34
+ Jekyll site.
35
+ email: ian@ianwhitney.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - README.md
41
+ - VERSION.md
42
+ - lib/rust_playground_highlight.rb
43
+ - license.txt
44
+ homepage: https://github.com/IanWhitney/rust_playground_highlight
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.4.5.1
65
+ signing_key:
66
+ specification_version: 4
67
+ summary: Adds a 'Run' button to your Rust code samples. You can choose to have the
68
+ code in Rust Playground be dynamically generated or generated from a Gist. See the
69
+ Readme for more information.
70
+ test_files: []