jekyll-include_snippet 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00f11048d745f88a45a271aeb681ee50e15ccfc6
4
+ data.tar.gz: 9045192b86846e6e30a556cf3b854acd6a8bf060
5
+ SHA512:
6
+ metadata.gz: 55f1afa12468169590277215842aa22e9ac7c7119bee84371e45ee318c7df82b3d03b72923a704b98b57f22d493c571b5c0b4f463837a6a3528d47a5442f6ac1
7
+ data.tar.gz: 47022efa04a82c1e5ef482acb5ca6d54b5da59e339ba94e8ce57ad42d6a63238bed4eb6aaff230481decff2824540b4f0cc7a276cf9be8037eec672b33136046
@@ -0,0 +1 @@
1
+ /Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Tom Dalling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,52 @@
1
+ # Jekyll::IncludeSnippet
2
+
3
+ Include snippets of text from external files into your markdown
4
+
5
+ ## Installation
6
+
7
+ Add it to your `Gemfile`:
8
+
9
+ source 'https://rubygems.org'
10
+
11
+ gem 'jekyll-include_snippet', github: 'tomdalling/jekyll-include_snippet'
12
+
13
+ ## Usage
14
+
15
+ Put the special "begin-snippet" and "end-snippet" comments into your source file:
16
+
17
+ # blah.rb
18
+
19
+ class Blah
20
+ # begin-snippet: my_method_snippet
21
+ def blah
22
+ puts 'blah blah blah'
23
+ end
24
+ # end-snippet
25
+ end
26
+
27
+ Use it from your markdown:
28
+
29
+ Blah blah here is some code:
30
+
31
+ ```ruby
32
+ {% include_snippet my_method_snippet from path/to/blah.rb %}
33
+ ```
34
+
35
+ Alternatively, you can set a default source path in the YAML frontmatter:
36
+
37
+ ---
38
+ title: "My Blerg Post"
39
+ date: "2018-01-01"
40
+ snippet_source: "path/to/blah.rb"
41
+ ---
42
+
43
+ ```ruby
44
+ {% include_snippet my_method_snippet %}
45
+ ```
46
+
47
+ And the text from `blah.rb` will be included into your post (and processed as markdown).
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jekyll/include_snippet/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-include_snippet"
8
+ spec.version = Jekyll::IncludeSnippet::VERSION
9
+ spec.authors = ["Tom Dalling"]
10
+ spec.email = ["tom@tomdalling.com"]
11
+
12
+ spec.summary = %q{Include snippets of text from external files into your markdown}
13
+ spec.homepage = "https://github.com/tomdalling/jekyll-include_snippet"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'liquid', '>= 3.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rspec", "~> 3.7"
27
+ spec.add_development_dependency "rspec-its", "~> 1.2"
28
+ spec.add_development_dependency "byebug"
29
+ spec.add_development_dependency "gem-release"
30
+ end
@@ -0,0 +1,11 @@
1
+ require 'liquid'
2
+
3
+ module Jekyll
4
+ module IncludeSnippet
5
+ end
6
+ end
7
+
8
+ %w(version extractor liquid_tag).each do |file|
9
+ require "jekyll/include_snippet/#{file}"
10
+ end
11
+
@@ -0,0 +1,61 @@
1
+ module Jekyll
2
+ module IncludeSnippet
3
+ class Extractor
4
+ BEGIN_REGEX = %r{
5
+ (\s*)\#\s* # Line must start with a hash (surrounding whitespace optional)
6
+ begin-snippet: # Magic string for beginning a snippet
7
+ (.+) # The remainder of the line is the snippet name
8
+ }x
9
+
10
+ END_REGEX = %r{
11
+ \s*\#\s* # Line must start with a hash (surrounding whitespace optional)
12
+ end-snippet # Magic string for ending a snippet
13
+ }x
14
+
15
+ def call(source)
16
+ everything = Snippet.new(name: 'everything', indent: 0)
17
+ all_snippets = []
18
+ active_snippets = []
19
+
20
+ source.each_line do |line|
21
+ case line
22
+ when BEGIN_REGEX
23
+ active_snippets << Snippet.new(name: $2.strip, indent: $1.length)
24
+ when END_REGEX
25
+ all_snippets << active_snippets.pop
26
+ else
27
+ (active_snippets + [everything]).each do |snippet|
28
+ snippet.lines << line
29
+ end
30
+ end
31
+ end
32
+
33
+ (all_snippets + [everything])
34
+ .map { |s| [s.name, s.dedented_text] }
35
+ .to_h
36
+ end
37
+
38
+ private
39
+
40
+ def hash_from_snippets(snippets)
41
+ end
42
+
43
+ class Snippet
44
+ attr_reader :name, :indent, :lines
45
+
46
+ def initialize(name:, indent:)
47
+ @name = name
48
+ @indent = indent
49
+ @lines = []
50
+ end
51
+
52
+ def dedented_text
53
+ lines
54
+ .map { |l| l[indent..-1] }
55
+ .join
56
+ .rstrip
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ module Jekyll
2
+ module IncludeSnippet
3
+ class LiquidTag < Liquid::Tag
4
+ def initialize(tag_name, arg_str, tokens)
5
+ super
6
+ @snippet_name, @source_path = arg_str.split('from').map(&:strip)
7
+ end
8
+
9
+ def render(context)
10
+ source_path = source_path_for(context)
11
+ source = File.read(source_path)
12
+ extractor = Extractor.new
13
+ snippets = extractor.(source)
14
+ snippets.fetch(@snippet_name) do
15
+ fail "Snippet not found: #{@snippet_name.inspect}\n in file: #{@source_path}"
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def source_path_for(context)
22
+ page = context['page']
23
+ frontmatter_default_source = page && page['snippet_source']
24
+ result = frontmatter_default_source || @source_path
25
+
26
+ if result.nil?
27
+ fail "No source path provided for snippet: #{@snippet_name}"
28
+ end
29
+
30
+ result
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Liquid::Template.register_tag('include_snippet', Jekyll::IncludeSnippet::LiquidTag)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module IncludeSnippet
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-include_snippet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tom Dalling
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: liquid
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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
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.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-its
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: gem-release
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - tom@tomdalling.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - jekyll-include-snippet.gemspec
110
+ - lib/jekyll/include_snippet.rb
111
+ - lib/jekyll/include_snippet/extractor.rb
112
+ - lib/jekyll/include_snippet/liquid_tag.rb
113
+ - lib/jekyll/include_snippet/version.rb
114
+ homepage: https://github.com/tomdalling/jekyll-include_snippet
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.6.11
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Include snippets of text from external files into your markdown
138
+ test_files: []