jekyll-include_snippet 0.1.3 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 305febb9feb26637e9e49f2df6dfa0aef8b8adb0
4
- data.tar.gz: 53c2dd21fc0c293242d3f7d217a9f2671e41fa9f
2
+ SHA256:
3
+ metadata.gz: fc96c6c7044170c2e858747e776b28f800b03c60f03d2e729853904cf784963d
4
+ data.tar.gz: 6203cdb73dfb12262c8886f2b516aa88446037b2a53a41c6aa618e618356775b
5
5
  SHA512:
6
- metadata.gz: b662ca4f9c5cee068093faab4298730aaa57fd048fb48e42f826c561cb2734fcb1191f0cfd10784d25fbc40fc5fc45aa09cc31edf2f9f37fba15d35a1a4e2e1f
7
- data.tar.gz: 83a44f87ccb7c0fa174ee82e771ba1891111e012ae5e4ba11a4c1706696e107ea9992b734909f6e0dd04d573baa53a155860a8f7ccf780011a9db5ea8b1acf94
6
+ metadata.gz: eb15ef38f0bb497d0420d2d81722de82a1426b3cd4d19a1a7ff64c86402b461a7146862348afe8402451c3553034cb697484b9ac9a0de9a2a3f582e50c4900e6
7
+ data.tar.gz: 6a7aa922d917cb5c03d45ef92cdde145fd0709d640435327df6da5ce3870eccc99fb9c7653b63129607cb3392294cbbe54fc63ffaa2b894363cee20c84e16f5e
data/README.md CHANGED
@@ -51,6 +51,30 @@ Optionally, you can set a default source path in the YAML frontmatter:
51
51
  {% include_snippet my_method_snippet %}
52
52
  ```
53
53
 
54
+ ## Languages Other Than Ruby
55
+
56
+ If you're using another language, you will probably need to change the "comment prefix".
57
+
58
+ ---
59
+ title: "My Blerg Post"
60
+ date: "2018-01-01"
61
+ snippet_comment_prefix: "//"
62
+ ---
63
+
64
+ ```js
65
+ {% include_snippet whatever from whatever.js %}
66
+ ```
67
+
68
+ ```js
69
+ // whatever.js
70
+
71
+ // begin-snippet: whatever
72
+ function whatever() {
73
+ console.log("Hello there");
74
+ }
75
+ // end-snippet
76
+ ```
77
+
54
78
  ## License
55
79
 
56
80
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -1,16 +1,11 @@
1
1
  module Jekyll
2
2
  module IncludeSnippet
3
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
4
+ attr_reader :comment_prefix
9
5
 
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
6
+ def initialize(comment_prefix:)
7
+ @comment_prefix = comment_prefix
8
+ end
14
9
 
15
10
  def call(source)
16
11
  everything = Snippet.new(name: 'everything', indent: 0)
@@ -19,9 +14,9 @@ module Jekyll
19
14
 
20
15
  source.each_line.each_with_index do |line, lineno|
21
16
  case line
22
- when BEGIN_REGEX
17
+ when begin_regex
23
18
  active_snippets << Snippet.new(name: $2.strip, indent: $1.length)
24
- when END_REGEX
19
+ when end_regex
25
20
  raise missing_begin_snippet(lineno) if active_snippets.empty?
26
21
  all_snippets << active_snippets.pop
27
22
  else
@@ -38,6 +33,25 @@ module Jekyll
38
33
 
39
34
  private
40
35
 
36
+ def begin_regex
37
+ %r{
38
+ (\s*) # optional whitespace (indenting)
39
+ #{Regexp.quote(comment_prefix)} # the comment prefix
40
+ \s* # optional whitespace
41
+ begin-snippet: # magic string for beginning a snippet
42
+ (.+) # the remainder of the line is the snippet name
43
+ }x
44
+ end
45
+
46
+ def end_regex
47
+ %r{
48
+ \s* # optional whitespace (indenting)
49
+ #{Regexp.quote(comment_prefix)} # the comment prefix
50
+ \s* # optional whitespace
51
+ end-snippet # Magic string for ending a snippet
52
+ }x
53
+ end
54
+
41
55
  def missing_begin_snippet(lineno)
42
56
  <<~END_ERROR
43
57
  There was an `end-snippet` on line #{lineno}, but there doesn't
@@ -1,6 +1,8 @@
1
1
  module Jekyll
2
2
  module IncludeSnippet
3
3
  class LiquidTag < Liquid::Tag
4
+ DEFAULT_COMMENT_PREFIX = '#'
5
+
4
6
  def initialize(tag_name, arg_str, tokens)
5
7
  super
6
8
  @snippet_name, @source_path = arg_str.split(/\sfrom\s/).map(&:strip)
@@ -9,7 +11,7 @@ module Jekyll
9
11
  def render(context)
10
12
  source_path = source_path_for(context)
11
13
  source = File.read(source_path)
12
- extractor = Extractor.new
14
+ extractor = Extractor.new(comment_prefix: comment_prefix_for(context))
13
15
  snippets = extractor.(source)
14
16
  snippets.fetch(@snippet_name) do
15
17
  fail "Snippet not found: #{@snippet_name.inspect}\n in file: #{@source_path}"
@@ -19,9 +21,7 @@ module Jekyll
19
21
  private
20
22
 
21
23
  def source_path_for(context)
22
- page = context['page']
23
- frontmatter_default_source = page && page['snippet_source']
24
- result = frontmatter_default_source || @source_path
24
+ result = get_option(:snippet_source, context) || @source_path
25
25
 
26
26
  if result.nil?
27
27
  fail "No source path provided for snippet: #{@snippet_name}"
@@ -29,6 +29,15 @@ module Jekyll
29
29
 
30
30
  result
31
31
  end
32
+
33
+ def comment_prefix_for(context)
34
+ get_option(:snippet_comment_prefix, context) || DEFAULT_COMMENT_PREFIX
35
+ end
36
+
37
+ def get_option(option_name, context)
38
+ page = context['page']
39
+ page && page[option_name.to_s]
40
+ end
32
41
  end
33
42
  end
34
43
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module IncludeSnippet
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-include_snippet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Dalling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid
@@ -106,7 +106,7 @@ files:
106
106
  - Gemfile
107
107
  - LICENSE.txt
108
108
  - README.md
109
- - jekyll-include-snippet.gemspec
109
+ - jekyll-include_snippet.gemspec
110
110
  - lib/jekyll/include_snippet.rb
111
111
  - lib/jekyll/include_snippet/extractor.rb
112
112
  - lib/jekyll/include_snippet/liquid_tag.rb
@@ -130,8 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  requirements: []
133
- rubyforge_project:
134
- rubygems_version: 2.6.11
133
+ rubygems_version: 3.0.3
135
134
  signing_key:
136
135
  specification_version: 4
137
136
  summary: Include snippets of text from external files into your markdown