jekyll_pre 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc048288e5f189d4519766b6db9a8807d6f41b4193be5fcd7ea7d39ed1af4a7b
4
- data.tar.gz: 356f768bba6d1bbdb4abe52394a75e6aa387257b4fd1c58d7f9775324382ac6f
3
+ metadata.gz: 4cc10df37a945ecc8abddce501dd71a176084b9fafdde6e3aa6a48e18c7e4551
4
+ data.tar.gz: 73fe925e47914475d889e814cae15f8c8331ed7abf2498aaf0a207fd2e0bf7d9
5
5
  SHA512:
6
- metadata.gz: 5e0833a721b44a3f9078d9ae76a70152d516469bb6062424366c3a7dc1171fe78a9407d6195e5ba5ae728224f78a278cff2f8fb145d94c5bc9346bf75bc44435
7
- data.tar.gz: ff46960e6cfd7fd339e9d835e2bcaf0934d29dc7bb14bcf860208ced8bfab349f6bd680ac9b5cab7b426d4af5f0ae1dc10758181e24327e645cbeb0a323f8d63
6
+ metadata.gz: 76a1487e85b498708d27a2258fce364703fe2613253eff69e80475b22303f81f519a8bcedb14671fd62179d53521c80ca9c47d859e838d2bb43060ef58adc236
7
+ data.tar.gz: 59edd4611b9d6553096d76ce57d518a4e6dd670d2e7f236cfa6ae000c34b269584ecf318705db9bd68d32d9da42590ff71628a7a122107dca8d8e2057e048900
data/.rubocop.yml CHANGED
@@ -20,7 +20,8 @@ Gemspec/RequireMFA:
20
20
  Enabled: false
21
21
 
22
22
  Layout/HashAlignment:
23
- Enabled: false
23
+ EnforcedColonStyle: table
24
+ EnforcedHashRocketStyle: table
24
25
 
25
26
  Layout/LineLength:
26
27
  Max: 150
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 1.4.0
2
+ * Added `dedent` option and config setting.
3
+ * No longer dies when exec is passed an empty string, or a string with just whitespace.
4
+
5
+ ## 1.3.2
6
+ * No longer strips leading spaces from exec result lines.
7
+
8
+ ## 1.3.1
9
+ * Generates outer `div` with `jekyll_pre` class, like this:
10
+ ```html
11
+ <div class="jekyll_pre">
12
+ ... HTML generated by previous versions of `pre` tag here
13
+ </div>
14
+ ```
15
+ * Added `wrapper_class` and `wrapper_style` name/value options.
16
+
1
17
  ## 1.3.0
2
18
  * Updated to `jekyll_plugin_support` v0.6.0 for attribution support.
3
19
 
data/README.md CHANGED
@@ -15,6 +15,7 @@ This Jekyll plugin provides 3 new Liquid tags that work together:
15
15
  - `clear` &ndash; Line break after floating HTML elements
16
16
  - `copyButton` &ndash; Generate a copy button
17
17
  - `dark` &ndash; Dark mode
18
+ - `dedent` &ndash; Remove leading spaces common to all lines, like Ruby's <<~ squiggly heredoc (default is false)
18
19
  - `label='This is a label'` &ndash; Apply text above `pre` tag.
19
20
  The `label` parameter value can also be specified in free text.
20
21
  For example, the following produce the same results:
@@ -47,14 +48,16 @@ This Jekyll plugin provides 3 new Liquid tags that work together:
47
48
 
48
49
  - `cd="relative/or/absolute/directory"` - Change to specified directory before executing shell command.
49
50
  Environment variables in the directory path will be expanded.
50
- - `no_escape` – Do not HTML escape the result of running the shell command.
51
- - `no_strip` – Do not remove leading and trailing whitespace from the result.
52
51
  - `die_if_nonzero` – Set `false` to treat non-zero return codes as non-fatal.
53
52
  Instead of terminating Jekyll with an error message,
54
53
  the message will be displayed as an error by the Jekyll logger,
55
54
  and a red message will appear in place of the result on the web page.
56
55
  - `die_if_error` – Set `false` to treat exceptions generated by this plugin as non-fatal.
57
56
  Instead of terminating Jekyll with an error message, the message will be displayed as an error by the Jekyll logger.
57
+ - `no_escape` – Do not HTML escape the result of running the shell command.
58
+ - `no_strip` – Do not remove leading and trailing whitespace from the result.
59
+ - `wrapper_class` class applied to outer `div`.
60
+ - `wrapper_style` style applied to outer `div`.
58
61
 
59
62
 
60
63
  ## CSS
@@ -62,8 +65,38 @@ See [`demo/assets/css/style.css`](demo/assets/css/style.css) for the CSS declara
62
65
  between `/* Start of pre tag css */` and `/* End of pre tag css */`.
63
66
 
64
67
 
68
+ ## Configuration
69
+ Default options can be set for the `pre` tag by entries in `_config.yml`.
70
+ The following demonstrates setting a default value for every possible option:
71
+
72
+ ```yml
73
+ pre:
74
+ class: bg_yellow
75
+ clear: true
76
+ dark: true
77
+ dedent: true
78
+ highlight: 'Error:.*'
79
+ label: Shell
80
+ copyButton: true
81
+ number: true
82
+ style: 'font-face: courier'
83
+ wrapper_class: rounded shadow
84
+ wrapper_style: 'padding: 2em; border: thin green dashed;'
85
+ ```
86
+
87
+ The default values used on [`mslinn.com`](https://www.mslinn.com) are:
88
+
89
+ ```yml
90
+ pre:
91
+ dedent: true
92
+ label: Shell
93
+ copyButton: true
94
+ ```
95
+
96
+
65
97
  ## Additional Information
66
- More information is available on [my website](https://www.mslinn.com/blog/2020/10/03/jekyll-plugins.html#jekyll_pre).
98
+ More information is available on
99
+ [Mike Slinn&rsquo;s website](https://www.mslinn.com/jekyll_plugins/jekyll_pre.html).
67
100
 
68
101
 
69
102
  ## Installation
@@ -78,11 +111,24 @@ end
78
111
 
79
112
  And then execute:
80
113
 
81
- $ bundle install
114
+ $ bundle
82
115
 
83
116
 
84
117
  ## Usage
85
- The following examples are rendered on [my website](https://www.mslinn.com/blog/2020/10/03/jekyll-plugins.html#jekyll_pre).
118
+ The following examples are rendered on
119
+ [Mike Slinn&rsquo;s website](https://www.mslinn.com/jekyll_plugins/jekyll_pre.html).
120
+
121
+ ### Example 0
122
+ <pre data-lt-active="false" class="maxOneScreenHigh copyContainer" id="id110c50d624b4">{% pre dedent %}
123
+ This line was indented 4 spaces
124
+ This line was indented 6 spaces
125
+ This line was indented 4 spaces
126
+ {% endpre %}</pre>
127
+ Which renders as:
128
+
129
+ <pre data-lt-active="false" class="maxOneScreenHigh copyContainer" id="id377433c30186">This line was indented 4 spaces
130
+ This line was indented 6 spaces
131
+ This line was indented 4 spaces</pre>
86
132
 
87
133
  ### Example 1
88
134
  This example does not generate a copy button and does not demonstrate `noselect`.
data/jekyll_pre.gemspec CHANGED
@@ -1,3 +1,4 @@
1
+ require 'jekyll'
1
2
  require_relative 'lib/jekyll_pre/version'
2
3
 
3
4
  Gem::Specification.new do |spec|
@@ -33,4 +34,5 @@ Gem::Specification.new do |spec|
33
34
 
34
35
  spec.add_dependency 'jekyll', '>= 3.5.0'
35
36
  spec.add_dependency 'jekyll_plugin_support', '~> 0.6.0'
37
+ spec.add_dependency 'rack'
36
38
  end
data/lib/exec_tag.rb CHANGED
@@ -1,7 +1,16 @@
1
1
  require 'jekyll_plugin_support'
2
+ require 'rack/utils'
2
3
  require_relative 'jekyll_pre/version'
3
4
 
4
- module ExecTag
5
+ module JekyllPreModule
6
+ def self.compress(response, no_strip)
7
+ result = response.chomp
8
+ result = result.strip unless no_strip
9
+ result = result.gsub('\n\n', '<br>\n')
10
+ result = Rack::Utils.escape_html(result) unless @no_escape
11
+ result
12
+ end
13
+
5
14
  class ExecTag < JekyllSupport::JekyllTag
6
15
  include JekyllPreVersion
7
16
 
@@ -13,11 +22,18 @@ module ExecTag
13
22
  parse_args
14
23
  @original_command = @helper.remaining_markup_original
15
24
  command = JekyllPluginHelper.expand_env @original_command
16
- raise PreError, "Command is empty on on line #{@line_number} (after front matter) of #{@page['path']}", [] if command.strip.empty?
25
+ if command.strip.empty?
26
+ msg = "Command is empty on on line #{@line_number} (after front matter) of #{@page['path']}"
27
+ unless @die_if_error
28
+ @logger.warn { msg }
29
+ return ''
30
+ end
31
+ raise PreError, msg, []
32
+ end
17
33
 
18
34
  response = run_command(command)
19
35
  response = if @child_status.success?
20
- compress(response)
36
+ ExecTagModule.compress(response, @no_strip)
21
37
  else
22
38
  handle_error(command)
23
39
  end
@@ -29,20 +45,13 @@ module ExecTag
29
45
  rescue PreError => e
30
46
  raise PreError, e.message, []
31
47
  rescue StandardError => e
32
- msg = self.class.remove_html_tags(e.message) + " from executing '#{@original_command}' on line #{@line_number} (after front matter) of #{@page['path']}"
48
+ msg = self.class.remove_html_tags(e.message) +
49
+ " from executing '#{@original_command}' on line #{@line_number} (after front matter) of #{@page['path']}"
33
50
  raise PreError, msg.red, [] if @die_if_error
34
51
  end
35
52
 
36
53
  private
37
54
 
38
- def compress(response)
39
- result = response.chomp
40
- result = result.strip unless @no_strip
41
- result = result.gsub('\n\n', '<br>\n')
42
- result = Rack::Utils.escape_html(result) unless @no_escape
43
- result
44
- end
45
-
46
55
  def die(msg)
47
56
  msg_no_html = self.class.remove_html_tags(msg)
48
57
  @logger.error("#{@page['path']} - #{msg_no_html}")
@@ -87,7 +96,8 @@ module ExecTag
87
96
  @child_status = $CHILD_STATUS
88
97
  result
89
98
  rescue StandardError => e
90
- msg = self.class.remove_html_tags(e.message) + " from executing '#{@original_command}' on line #{@line_number} (after front matter) of #{@page['path']}"
99
+ msg = self.class.remove_html_tags(e.message) +
100
+ " from executing '#{@original_command}' on line #{@line_number} (after front matter) of #{@page['path']}"
91
101
  raise PreError, msg.red, [] if @die_if_error
92
102
  ensure
93
103
  @child_status = $CHILD_STATUS
@@ -1,3 +1,3 @@
1
1
  module JekyllPreVersion
2
- VERSION = '1.3.0'.freeze
2
+ VERSION = '1.4.0'.freeze
3
3
  end
data/lib/jekyll_pre.rb CHANGED
@@ -8,10 +8,14 @@ require_relative './pre_tag_block'
8
8
 
9
9
  PreError = Class.new(Liquid::Error)
10
10
 
11
- module JekyllPreModule
12
- include NoSelectTag
13
- include PreTagBlock
14
- include ExecTag
11
+ class String
12
+ # Works like <<~ from Ruby 2.3.0
13
+ def dedent
14
+ # Find the margin whitespace on the first line
15
+ margin = self[/\A\s*/]
16
+ # Remove margin-sized whitespace from each line
17
+ gsub(/^\s{#{margin.size}}/, '')
18
+ end
15
19
  end
16
20
 
17
21
  PluginMetaLogger.instance.info { "Loaded #{JekyllPluginPreName::PLUGIN_NAME} v#{JekyllPreVersion::VERSION} plugin." }
data/lib/noselect_tag.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'jekyll_plugin_support'
2
2
  require_relative 'jekyll_pre/version'
3
3
 
4
- module NoSelectTag
4
+ module JekyllPreModule
5
5
  # """\\{% noselect %} or \\{% noselect this all gets copied.
6
6
  # Also, space before the closing percent is signficant %}"""
7
7
  class NoSelectTag < JekyllSupport::JekyllTagNoArgParsing
data/lib/pre_tag_block.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'jekyll_plugin_support'
2
2
  require_relative 'jekyll_pre/version'
3
3
 
4
- module PreTagBlock
4
+ module JekyllPreModule
5
5
  class PreTagBlock < JekyllSupport::JekyllBlock
6
6
  include JekyllPreVersion
7
7
 
@@ -31,23 +31,48 @@ module PreTagBlock
31
31
  result
32
32
  end
33
33
 
34
+ # remove leading blank lines and trailing whitespace
35
+ def self.remove_surrounding(text)
36
+ text.gsub(/\A(\s?\n)*/, '').rstrip
37
+ end
38
+
39
+ def option(name)
40
+ value = @helper.parameter_specified? name
41
+ return value unless value.nil?
42
+
43
+ @pre_config[name] if @pre_config
44
+ end
45
+
34
46
  def render_impl(text)
35
- text.strip!
47
+ text = PreTagBlock.remove_surrounding text
48
+
36
49
  @helper.gem_file __FILE__ # Enables plugin attribution
37
50
 
38
- @clear = @helper.parameter_specified? 'clear'
39
- @class = @helper.parameter_specified? 'class'
40
- @highlight = @helper.parameter_specified? 'highlight'
41
- @make_copy_button = @helper.parameter_specified? 'copyButton'
42
- @number_lines = @helper.parameter_specified? 'number'
43
- @dark = ' dark' if @helper.parameter_specified? 'dark'
44
- @style = @helper.parameter_specified? 'style'
45
- @label = @helper.parameter_specified? 'label'
51
+ @pre_config = @config['pre']
52
+
53
+ @class = option 'class'
54
+ @clear = option 'clear'
55
+ @dark = ' dark' if option 'dark'
56
+ @dedent = option 'dedent'
57
+ @highlight = option 'highlight'
58
+ @label = option 'label'
59
+ @make_copy_button = option 'copyButton'
60
+ @number_lines = option 'number'
61
+ @style = option 'style'
62
+ @wrapper_class = option 'wrapper_class'
63
+ @wrapper_style = option 'wrapper_style'
64
+
65
+ @class = @class ? " #{@class}" : ''
66
+ @style = @style ? " style='#{@style}'" : ''
67
+ @wrapper_class = @wrapper_class ? " #{@wrapper_class}" : ''
68
+ @wrapper_style = @wrapper_style ? " style='#{@wrapper_style}'" : ''
46
69
 
47
70
  # If a label was specified, use it, otherwise concatenate any dangling parameters and use that as the label
48
71
  @label ||= @helper.argv.join(' ')
49
72
 
50
73
  @logger.debug { "@make_copy_button = '#{@make_copy_button}'; @label = '#{@label}'" }
74
+
75
+ text = text.dedent if @dedent
51
76
  make_pre(text)
52
77
  end
53
78
 
@@ -62,8 +87,6 @@ module PreTagBlock
62
87
  label_clear = ' clear'
63
88
  end
64
89
  end
65
- @class = @class ? " #{@class}" : ''
66
- @style = @style ? " style='#{@style}'" : ''
67
90
  dark_label = ' darkLabel' if @dark
68
91
  @label = if @label.to_s.empty?
69
92
  ''
@@ -81,9 +104,11 @@ module PreTagBlock
81
104
  pre_content = "#{copy_button}#{content}"
82
105
  attribution = @helper.attribute if @helper.attribution
83
106
  <<~END_OUTPUT
107
+ <div class="jekyll_pre#{@wrapper_class}" #{@wrapper_style}>
84
108
  #{@label}
85
109
  <pre data-lt-active='false' class='#{classes}'#{@style} id='#{pre_id}'>#{pre_content}</pre>
86
110
  #{attribution}
111
+ </div>
87
112
  END_OUTPUT
88
113
  end
89
114
 
data/spec/exec_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ require_relative '../lib/jekyll_pre'
2
+
3
+ module ExecTagSpec
4
+ RSpec.describe(JekyllPreModule) do
5
+ it 'strips properly' do
6
+ content = <<~END_CONTENT
7
+ Line 1
8
+ Line 2
9
+ Line 3
10
+ Line 4
11
+ Line 5
12
+ Line 6
13
+ Line 7
14
+ Line 8
15
+ Line 9
16
+ Line 10
17
+ END_CONTENT
18
+ actual = described_class.compress content, false
19
+ expected = <<~END_CONTENT
20
+ Line 1
21
+ Line 2
22
+ Line 3
23
+ Line 4
24
+ Line 5
25
+ Line 6
26
+ Line 7
27
+ Line 8
28
+ Line 9
29
+ Line 10
30
+ END_CONTENT
31
+ expect(expected.strip).to eq(actual)
32
+ end
33
+ end
34
+ end
data/spec/pre_spec.rb CHANGED
@@ -4,7 +4,71 @@ require 'key_value_parser'
4
4
  require 'shellwords'
5
5
  require_relative '../lib/jekyll_pre'
6
6
 
7
- RSpec.describe(PreTagBlock) do
7
+ RSpec.describe(JekyllPreModule::PreTagBlock) do
8
+ it 'dedents content lines' do
9
+ content = <<-END_CONTENT
10
+ Line 1
11
+ Line 2
12
+ Line 3
13
+ Line 4
14
+ Line 5
15
+ Line 6
16
+ Line 7
17
+ Line 8
18
+ Line 9
19
+ Line 10
20
+ END_CONTENT
21
+ expected = <<~END_CONTENT
22
+ Line 1
23
+ Line 2
24
+ Line 3
25
+ Line 4
26
+ Line 5
27
+ Line 6
28
+ Line 7
29
+ Line 8
30
+ Line 9
31
+ Line 10
32
+ END_CONTENT
33
+ actual = content.dedent
34
+ expect(actual).to eq(expected)
35
+ end
36
+
37
+ it 'removes surrounding whitespace' do
38
+ text = <<-END_CONTENT
39
+
40
+
41
+ Line 1
42
+ Line 2
43
+ Line 3
44
+ Line 4
45
+ Line 5
46
+ Line 6
47
+ Line 7
48
+ Line 8
49
+ Line 9
50
+ Line 10
51
+
52
+
53
+ END_CONTENT
54
+ actual = described_class.remove_surrounding(text)
55
+
56
+ expected = <<-END_CONTENT
57
+ Line 1
58
+ Line 2
59
+ Line 3
60
+ Line 4
61
+ Line 5
62
+ Line 6
63
+ Line 7
64
+ Line 8
65
+ Line 9
66
+ Line 10
67
+ END_CONTENT
68
+
69
+ expect(actual).to eq(expected.rstrip)
70
+ end
71
+
8
72
  it 'parses arguments' do
9
73
  argv = Shellwords.split 'number copyButton shell'
10
74
  options = KeyValueParser.new.parse(argv)
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'jekyll'
2
2
  require 'fileutils'
3
3
  require 'key_value_parser'
4
+ require 'jekyll_plugin_support'
5
+ require 'rack/utils'
4
6
  require 'shellwords'
5
7
 
6
8
  require_relative '../lib/jekyll_pre'
@@ -1,5 +1,13 @@
1
- example_id | status | run_time |
2
- ----------------------- | ------ | --------------- |
3
- ./spec/pre_spec.rb[1:1] | passed | 0.00075 seconds |
4
- ./spec/pre_spec.rb[1:2] | passed | 0.00044 seconds |
5
- ./spec/pre_spec.rb[1:3] | passed | 0.00018 seconds |
1
+ example_id | status | run_time |
2
+ --------------------------------------------------------------- | ------ | --------------- |
3
+ ./spec/exec_spec.rb[1:1] | passed | 0.00377 seconds |
4
+ ./spec/pre_spec.rb[1:1] | passed | 0.00014 seconds |
5
+ ./spec/pre_spec.rb[1:2] | passed | 0.00351 seconds |
6
+ ./spec/pre_spec.rb[1:3] | passed | 0.00016 seconds |
7
+ ./spec/pre_spec.rb[1:4] | passed | 0.00011 seconds |
8
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/exec_spec.rb[1:1] | passed | 0.00457 seconds |
9
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/pre_spec.rb[1:1] | passed | 0.00326 seconds |
10
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/pre_spec.rb[1:2] | passed | 0.00067 seconds |
11
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/pre_spec.rb[1:3] | passed | 0.00362 seconds |
12
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/pre_spec.rb[1:4] | passed | 0.00033 seconds |
13
+ /mnt/_/work/jekyll/my_plugins/jekyll_pre/spec/pre_spec.rb[1:5] | passed | 0.00083 seconds |
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll_pre
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Slinn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-06 00:00:00.000000000 Z
11
+ date: 2023-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: 'Jekyll tags pre and noselect, for HTML <pre/> tag, prompts and unselectable
42
56
  text. Can number lines.
43
57
 
@@ -59,6 +73,7 @@ files:
59
73
  - lib/jekyll_pre/version.rb
60
74
  - lib/noselect_tag.rb
61
75
  - lib/pre_tag_block.rb
76
+ - spec/exec_spec.rb
62
77
  - spec/pre_spec.rb
63
78
  - spec/spec_helper.rb
64
79
  - spec/status_persistence.txt
@@ -95,6 +110,7 @@ specification_version: 4
95
110
  summary: Jekyll tags pre and noselect, for HTML <pre/> tag, prompts and unselectable
96
111
  text. Can number lines.
97
112
  test_files:
113
+ - spec/exec_spec.rb
98
114
  - spec/pre_spec.rb
99
115
  - spec/spec_helper.rb
100
116
  - spec/status_persistence.txt