jekyll-enhanced-codeblocks 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 47bbe21f32e87e7b986157289abda2e52b13b35a169d7eb399a5adf97f546cda
4
+ data.tar.gz: 824afbce3d4610eda28db0b1752824c05eeb26b32f899dcbd0399cf4a6235c54
5
+ SHA512:
6
+ metadata.gz: 11c0307a3f8e47555f098c31dfcb35510658e6617159500a3f849fe195d2491dfe5f1682013d303867f73c211e88d92195fe769fa969dfca671ca1b795b8d291
7
+ data.tar.gz: 90c51e38151b5fd55f70920a5c437e8fde737085aa3484d80d9706a90c60b79ce86ce8093ecfcfdbc7f2067ce3c7a4b1dc2041b0a2ef5d46b4a880ccb879bdd9
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll::EnhancedCodeBlocks
4
+ class EnhancedCodeBlockGenerator < Jekyll::Generator
5
+ safe true
6
+
7
+ def generate(site)
8
+ (site.pages + site.posts.docs + site.collections.flat_map { |_, collection| collection.docs }).each do |doc|
9
+ next unless markdown_file?(doc)
10
+ doc.content = transform_code_blocks(doc.content)
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def markdown_file?(doc)
17
+ doc.extname == '.md' || doc.extname == '.markdown'
18
+ end
19
+
20
+ def transform_code_blocks(content)
21
+ content.gsub(
22
+ /^```(\w+)(.*?)\s*\n(.*?)^```/m
23
+ ) do
24
+ lang = Regexp.last_match(1)
25
+ raw_opts = Regexp.last_match(2).strip
26
+ code = Regexp.last_match(3)
27
+
28
+ highlight_args = parse_options(raw_opts)
29
+ "{% highlight #{lang}#{' ' + highlight_args.join(' ') unless highlight_args.empty?} %}\n#{code}{% endhighlight %}"
30
+ end
31
+ end
32
+
33
+ def parse_options(opts_str)
34
+ args = []
35
+
36
+ linenos_match = opts_str.match(/:linenos(?:=(\d+))?/)
37
+ if linenos_match
38
+ args << "linenos"
39
+ relative_line = linenos_match[1] ? linenos_match[1].to_i : 1
40
+ args << "start_line=#{relative_line}" if relative_line != 1
41
+ relative_line -= 1
42
+ end
43
+
44
+ highlight_match = opts_str.match(/:highlight(?:=\{([^}]+)\})?(?:=(\d+))?/)
45
+ if highlight_match
46
+ expanded_lines = []
47
+
48
+ if highlight_match[1] # handle bracketed list, i.e., :highlight={123, 342-345}
49
+ highlight_lines = highlight_match[1].split(',').map(&:strip)
50
+
51
+ highlight_lines.each do |line|
52
+ if line.include?('-') # handle range like "342-345"
53
+ start_line, end_line = line.split('-').map(&:to_i)
54
+
55
+ start_line -= relative_line if relative_line != nil
56
+ end_line -= relative_line if relative_line != nil
57
+
58
+ if start_line > end_line
59
+ raise ArgumentError, "Invalid range: #{line}. The start line must be less than or equal to the end line."
60
+ end
61
+
62
+ expanded_lines.concat((start_line..end_line).to_a)
63
+ else # handle individual line like "123"
64
+ line_number = line.to_i
65
+ line_number -= relative_line if relative_line != nil
66
+ expanded_lines << line_number
67
+ end
68
+ end
69
+ elsif highlight_match[2] # handle single number, i.e., :highlight=123
70
+ line_number = highlight_match[2].to_i
71
+ line_number -= relative_line if relative_line != nil
72
+ expanded_lines << line_number
73
+ end
74
+
75
+ expanded_lines = expanded_lines.reject { |line| line < 1 }
76
+
77
+ if expanded_lines.any?
78
+ expanded_lines = expanded_lines.uniq.sort
79
+ args << %(mark_lines="#{expanded_lines.join(' ')}")
80
+ end
81
+ end
82
+
83
+ args
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Patch Jekyll to expose start_line tags for highlight liquid blocks
4
+ module Jekyll
5
+ module Tags
6
+ class HighlightBlock < Liquid::Block
7
+ def table_formatter(formatter)
8
+ options = {
9
+ :css_class => "highlight",
10
+ :gutter_class => "gutter",
11
+ :code_class => "code"
12
+ }
13
+ if @highlight_options[:start_line]
14
+ options[:start_line] = @highlight_options[:start_line].to_i
15
+ end
16
+ Rouge::Formatters::HTMLTable.new(formatter, options)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module Jekyll
3
+ module EnhancedCodeBlocks
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll-enhanced-codeblocks/start_line_formatter_patch'
4
+ require 'jekyll-enhanced-codeblocks/generator'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-enhanced-codeblocks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - damned-me
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-05-02 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: '4.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: This Jekyll plugin provides better support for markdown fenced codeblocks,
70
+ allowing to specify options such as highlighting and line numbering in the info
71
+ strings allowed by the GFM syntax
72
+ email:
73
+ - damned-me@protonmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - lib/jekyll-enhanced-codeblocks.rb
79
+ - lib/jekyll-enhanced-codeblocks/generator.rb
80
+ - lib/jekyll-enhanced-codeblocks/start_line_formatter_patch.rb
81
+ - lib/jekyll-enhanced-codeblocks/version.rb
82
+ homepage: https://github.com/damned-me/jekyll-enhanced-codeblocks
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '3.0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.2.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Enhanced code block syntax support for Jekyll
105
+ test_files: []