@@ -31,13 +31,13 @@ module JekyllPluginPreName
31
31
class PreTagBlock < Liquid ::Block
32
32
@@prefix = "<button class='copyBtn' data-clipboard-target="
33
33
@@suffix = " title='Copy to clipboard'><img src='/assets/images/clippy.svg' " \
34
- "alt='Copy to clipboard' style='width: 13px'></button>"
34
+ "alt='Copy to clipboard' style='width: 13px'></button>"
35
35
36
36
def self . make_copy_button ( pre_id )
37
37
"#{ @@prefix } '##{ pre_id } '#{ @@suffix } "
38
38
end
39
39
40
- def self . make_pre ( make_copy_button , label , content )
40
+ def self . make_pre ( make_copy_button , number_lines , label , content )
41
41
label = if label . to_s . empty?
42
42
""
43
43
elsif label . to_s . downcase . strip == "shell"
@@ -47,47 +47,73 @@ def self.make_pre(make_copy_button, label, content)
47
47
end
48
48
pre_id = "id#{ SecureRandom . hex ( 6 ) } "
49
49
copy_button = make_copy_button ? PreTagBlock . make_copy_button ( pre_id ) : ""
50
+ content = PreTagBlock . number_content ( content ) if number_lines
50
51
"#{ label } <pre data-lt-active='false' class='maxOneScreenHigh copyContainer' id='#{ pre_id } '>#{ copy_button } #{ content . strip } </pre>"
51
52
end
52
53
53
- # @param tag_name [String] is the name of the tag, which we already know.
54
- # @param text [Hash, String, Liquid::Tag::Parser] the arguments from the web page.
55
- # @param tokens [Liquid::ParseContext] tokenized command line
54
+ def self . number_content ( content )
55
+ lines = content . split ( "\n " )
56
+ digits = lines . length . to_s . length
57
+ i = 0
58
+ numbered_content = lines . map do |line |
59
+ i += 1
60
+ number = i . to_s . rjust ( digits , " " )
61
+ "<span class='unselectable'> #{ number } : </span>#{ line } "
62
+ end
63
+ result = numbered_content . join ( "\n " )
64
+ result += "\n " unless result . end_with? ( "\n " )
65
+ result
66
+ end
67
+
68
+ # @param _tag_name [String] is the name of the tag, which we already know.
69
+ # @param argument_string [String] the arguments from the web page.
70
+ # @param _tokens [Liquid::ParseContext] tokenized command line
56
71
# @return [void]
57
- def initialize ( tag_name , text , tokens )
58
- super ( tag_name , text , tokens )
59
- text = "" if text . nil?
60
- text . strip!
61
- @make_copy_button = text . include? "copyButton"
62
- remaining_text = text . sub ( "copyButton" , "" ) . strip
72
+ def initialize ( _tag_name , argument_string , _tokens )
73
+ super
74
+ argument_string = "" if argument_string . nil?
75
+ argument_string . strip!
76
+
63
77
@logger = PluginMetaLogger . instance . new_logger ( self )
64
- @logger . debug { "@make_copy_button = '#{ @make_copy_button } '; text = '#{ text } '; remaining_text = '#{ remaining_text } '" }
78
+
79
+ @make_copy_button = argument_string . include? "copyButton"
80
+ remaining_text = argument_string . sub ( "copyButton" , "" ) . strip
81
+
82
+ @number_lines = remaining_text . include? "number"
83
+ remaining_text = remaining_text . sub ( "number" , "" ) . strip
84
+
65
85
@label = remaining_text
86
+
87
+ @logger . debug { "@make_copy_button = '#{ @make_copy_button } '; argument_string = '#{ argument_string } '; remaining_text = '#{ remaining_text } '" }
66
88
end
67
89
68
90
# Method prescribed by the Jekyll plugin lifecycle.
69
91
# @return [String]
70
92
def render ( context )
71
93
content = super
72
94
@logger . debug { "@make_copy_button = '#{ @make_copy_button } '; @label = '#{ @label } '" }
73
- PreTagBlock . make_pre ( @make_copy_button , @label , content )
95
+ PreTagBlock . make_pre ( @make_copy_button , @number_lines , @ label, content )
74
96
end
75
97
end
76
98
77
99
# """\\{% noselect %} or \\{% noselect this all gets copied.
78
100
# Also, space before the closing percent is signficant %}"""
79
101
class UnselectableTag < Liquid ::Tag
80
- def initialize ( tag_name , text , tokens )
81
- super ( tag_name , text , tokens )
82
- @content = text
102
+ # @param _tag_name [String] is the name of the tag, which we already know.
103
+ # @param argument_string [String] the arguments from the web page.
104
+ # @param _tokens [Liquid::ParseContext] tokenized command line
105
+ # @return [void]
106
+ def initialize ( _tag_name , argument_string , _tokens )
107
+ super
83
108
@logger = PluginMetaLogger . instance . new_logger ( self )
84
- @logger . debug { "UnselectableTag: content1= '#{ @content } '" }
85
- @content = "$ " if @content . nil? || @content . empty?
86
- @logger . debug { "UnselectableTag: content2= '#{ @content } '" }
109
+
110
+ @argument_string = argument_string
111
+ @argument_string = "$ " if @argument_string . nil? || @argument_string . empty?
112
+ @logger . debug { "UnselectableTag: argument_string= '#{ @argument_string } '" }
87
113
end
88
114
89
115
def render ( _ )
90
- "<span class='unselectable'>#{ @content } </span>"
116
+ "<span class='unselectable'>#{ @argument_string } </span>"
91
117
end
92
118
end
93
119
0 commit comments