Skip to content

Commit 95a2fce

Browse files
committed
refacto(code-highlighter): Refactor plugin code and css style
1 parent dfa217a commit 95a2fce

16 files changed

+456
-439
lines changed

_plugins/highlight-linedivs.rb

Lines changed: 85 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,27 @@ module Formatters
66
class HTMLLegacy < Formatter
77
tag 'html_legacy'
88

9-
# @option opts [String] :css_class ('highlight')
10-
# @option opts [true/false] :line_numbers (false)
11-
# @option opts [true/false] :linewise (false)
12-
# @option opts [RougeLines::CSSTheme] :inline_theme (nil)
13-
# @option opts [true/false] :wrap (true)
14-
#
15-
# Initialize with options.
16-
#
17-
# If `:inline_theme` is given, then instead of rendering the
18-
# tokens as <span> tags with CSS classes, the styles according to
19-
# the given theme will be inlined in "style" attributes. This is
20-
# useful for formats in which stylesheets are not available.
21-
#
22-
# Content will be wrapped in a tag (`div` if tableized, `pre` if
23-
# not) with the given `:css_class` unless `:wrap` is set to `false`.
24-
def initialize(opts={})
25-
@formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme])
26-
: HTML.new
27-
28-
@formatter = HTMLLinewise.new(@formatter, opts) if opts[:linewise]
29-
30-
@formatter = HTMLTable.new(@formatter, opts) if opts[:line_numbers]
31-
32-
if opts.fetch(:wrap, true)
33-
@formatter = HTMLPygments.new(@formatter, opts.fetch(:css_class, 'codehilite'))
34-
end
9+
DEFAULT_OPTS = {
10+
css_class: 'highlight',
11+
line_numbers: false,
12+
linewise: false,
13+
inline_theme: nil,
14+
wrap: true
15+
}
16+
17+
def initialize(opts = {})
18+
opts = DEFAULT_OPTS.merge(opts)
19+
@formatter = select_formatter(opts)
20+
end
21+
22+
private
23+
24+
def select_formatter(opts)
25+
formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme]) : HTML.new
26+
formatter = HTMLLinewise.new(formatter, opts) if opts[:linewise]
27+
formatter = HTMLTable.new(formatter, opts) if opts[:line_numbers]
28+
formatter = HTMLPygments.new(formatter, opts.fetch(:css_class, 'codehilite')) if opts.fetch(:wrap, true)
29+
formatter
3530
end
3631
end
3732
end
@@ -42,98 +37,53 @@ module Tags
4237
class HighlightBlock < Liquid::Block
4338
include Liquid::StandardFilters
4439

40+
OPTIONS_REGEX = %r!(?:\w="[^"]*"|\w=\w|\w)+!
41+
SYNTAX = %r!^([a-zA-Z0-9_+-]+)\s*(\S.*)?$!
42+
4543
def initialize(tag_name, markup, tokens)
4644
super
4745
if markup.strip =~ SYNTAX
4846
@lang = Regexp.last_match(1).downcase
49-
@highlight_options = parse_options(Regexp.last_match(2))
47+
@highlight_options = parse_options(Regexp.last_match(2) || "")
5048
else
51-
raise SyntaxError, <<-MSG
52-
Syntax Error in tag 'highlight' while parsing the following markup:
53-
54-
#{markup}
55-
56-
Valid syntax: highlight <lang> [linenos] [linedivs]
57-
MSG
49+
raise SyntaxError, "Syntax Error in tag 'highlight' while parsing: #{markup.strip}. Valid syntax: highlight <lang> [linenos] [linedivs]"
5850
end
5951
end
6052

6153
def render(context)
62-
prefix = " " \
63-
"
64-
<div class=\"code-container\">
65-
<div class=\"code-window\">
66-
<div class=\"window-header\">
67-
<div class=\"window-controls\">
68-
<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\">
69-
<g fill=\"none\" fillRule=\"evenodd\" transform=\"translate(1 1)\">
70-
<circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#EC6A5E\" stroke=\"#EC6A5E\" strokeWidth=\".5\" />
71-
<circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#F4BF4F\" stroke=\"#F4BF4F\" strokeWidth=\".5\" />
72-
<circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#61C554\" stroke=\"#61C554\" strokeWidth=\".5\" />
73-
</g>
74-
</svg>
75-
</div>
76-
<div class=\"window-copy\">
77-
<svg xmlns=\"http://www.w3.org/2000/svg\" class=\"window-copy-icon\" height=\"24\" width=\"24\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"1.5\">
78-
<path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"></path>
79-
<path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"></path>
80-
</svg>
81-
</div>
82-
</div>
83-
"
84-
suffix = " " \
85-
"
86-
<div class=\"code-label\">
87-
#{@lang.capitalize}
88-
</div>
89-
</div>
90-
</div>
91-
"
92-
code = super.to_s.gsub(%r!\A(\n|\r)+|(\n|\r)+\z!, "")
93-
54+
code = super.to_s.strip
9455
is_safe = !!context.registers[:site].safe
9556

96-
output =
97-
case context.registers[:site].highlighter
98-
when "pygments"
99-
render_pygments(code, is_safe)
100-
when "rouge"
101-
render_rouge(code)
102-
else
103-
render_codehighlighter(code)
104-
end
57+
output = case context.registers[:site].highlighter
58+
when "pygments"
59+
render_pygments(code, is_safe)
60+
when "rouge"
61+
render_rouge(code)
62+
else
63+
render_codehighlighter(code)
64+
end
10565

10666
rendered_output = add_code_tag(output)
107-
prefix + rendered_output + suffix
67+
"#{prefix_html}#{rendered_output}#{suffix_html}"
10868
end
10969

11070
def sanitized_opts(opts, is_safe)
11171
if is_safe
112-
Hash[[
113-
[:startinline, opts.fetch(:startinline, nil)],
114-
[:hl_lines, opts.fetch(:hl_lines, nil)],
115-
[:linenos, opts.fetch(:linenos, nil)],
116-
[:linedivs, opts.fetch(:linedivs, nil)],
117-
[:encoding, opts.fetch(:encoding, "utf-8")],
118-
[:cssclass, opts.fetch(:cssclass, nil)],
119-
].reject { |f| f.last.nil? }]
72+
opts.slice(:startinline, :hl_lines, :linenos, :linedivs, :encoding, :cssclass).compact
12073
else
12174
opts
12275
end
12376
end
12477

125-
private
126-
127-
OPTIONS_REGEX = %r!(?:\w="[^"]*"|\w=\w|\w)+!
78+
private
12879

12980
def parse_options(input)
13081
options = {}
13182
return options if input.empty?
13283

133-
# Split along 3 possible forms -- key="<quoted list>", key=value, or key
13484
input.scan(OPTIONS_REGEX) do |opt|
135-
key, value = opt.split("=")
136-
# If a quoted list, convert to array
85+
key, value = opt.split("=", 2)
86+
# Si la valeur est une chaîne entre guillemets
13787
if value && value.include?('"')
13888
value.delete!('"')
13989
value = value.split
@@ -147,16 +97,54 @@ def parse_options(input)
14797

14898
def render_rouge(code)
14999
formatter = ::Rouge::Formatters::HTMLLegacy.new(
150-
:line_numbers => @highlight_options[:linenos],
151-
:linewise => @highlight_options[:linedivs],
152-
:wrap => false,
153-
:css_class => "highlight",
154-
:gutter_class => "gutter",
155-
:code_class => "code"
100+
line_numbers: @highlight_options[:linenos],
101+
linewise: @highlight_options[:linedivs],
102+
wrap: false,
103+
css_class: "highlight",
104+
gutter_class: "gutter",
105+
code_class: "code"
156106
)
157-
lexer = ::Rouge::Lexer.find_fancy(@lang, code) || Rouge::Lexers::PlainText
107+
lexer = ::Rouge::Lexer.find_fancy(@lang, code)
108+
unless lexer
109+
lexer = Rouge::Lexers::PlainText
110+
puts "Warning: Language '#{@lang}' not found. Defaulting to plain text."
111+
end
158112
formatter.format(lexer.lex(code))
159113
end
114+
115+
def prefix_html
116+
<<~HTML
117+
<div class="code-container">
118+
<div class="code-window">
119+
<div class="window-header">
120+
<div class="window-controls">
121+
<svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14">
122+
<g fill="none" fill-rule="evenodd" transform="translate(1 1)">
123+
<circle cx="6" cy="6" r="6" fill="#EC6A5E" stroke="#EC6A5E" stroke-width=".5" />
124+
<circle cx="26" cy="6" r="6" fill="#F4BF4F" stroke="#F4BF4F" stroke-width=".5" />
125+
<circle cx="46" cy="6" r="6" fill="#61C554" stroke="#61C554" stroke-width=".5" />
126+
</g>
127+
</svg>
128+
</div>
129+
<div class="window-copy">
130+
<svg xmlns="http://www.w3.org/2000/svg" class="window-copy-icon" height="24" width="24" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.5">
131+
<path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path>
132+
<path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path>
133+
</svg>
134+
</div>
135+
</div>
136+
HTML
137+
end
138+
139+
def suffix_html
140+
<<~HTML
141+
<div class="code-label">
142+
#{@lang.capitalize}
143+
</div>
144+
</div>
145+
</div>
146+
HTML
147+
end
160148
end
161149
end
162150
end

_posts/2021-03-16-installer-java-sdkman.markdown

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,28 @@ correctement sur votre système. Voici les instructions détaillées pour instal
6363

6464
### Installation sur macOS et Linux:
6565

66-
1. Ouvrez votre terminal.
66+
- **&Eacute;tape 1** : Ouvrez votre terminal.
6767

68-
2. Exécutez la commande suivante pour télécharger le script d'installation de SDKMAN :
69-
{% highlight shell %}curl -s "https://get.sdkman.io" | bash{% endhighlight %}
68+
- **&Eacute;tape 2** : Exécutez la commande suivante pour télécharger le script d'installation de SDKMAN :
69+
{% highlight shell %}curl -s "https://get.sdkman.io" | bash{% endhighlight %}
7070

71-
3. Attendez que le téléchargement et l'installation du script soient terminés.
71+
- **&Eacute;tape 3** : Attendez que le téléchargement et l'installation du script soient terminés.
7272

73-
4. Après l'installation :
73+
- **&Eacute;tape 4** : Après l'installation :
7474
- Lancez un nouveau terminal
7575
- Ou exécutez la commande suivante pour charger SDKMAN dans votre session en cours :
76-
{% highlight shell %}source "$HOME/.sdkman/bin/sdkman-init.sh"{% endhighlight %}
76+
{% highlight shell %}
77+
source "$HOME/.sdkman/bin/sdkman-init.sh"
78+
{% endhighlight %}
7779

78-
5. Pour vérifier si SDKMAN a été installé avec succès, tapez la commande suivante :
79-
{% highlight shell %}sdk version{% endhighlight %}
80+
- **&Eacute;tape 5** : Pour vérifier si SDKMAN a été installé avec succès, tapez la commande suivante :
81+
{% highlight shell %}
82+
sdk version
83+
{% endhighlight %}
8084

81-
6. Vous devriez voir :
85+
Vous devriez voir :
8286

83-
{% highlight shell %}
87+
{% highlight output %}
8488
SDKMAN!
8589
script: 5.18.2
8690
native: 0.4.2
@@ -120,7 +124,9 @@ Il faut donc déjà choisir le SDK (candidate) à installer.
120124
#### Candidate
121125

122126
Pour voir la liste des SDK/candidate, lancez la commande suivante :
123-
{% highlight zsh %}% sdk list {% endhighlight %}
127+
{% highlight shell %}
128+
sdk list
129+
{% endhighlight %}
124130

125131
> info "Note"
126132
> Tapez `q` pour sortir de la liste
@@ -142,7 +148,9 @@ Vous voyez qu'il est possible d'installer pas mal de choses. Pour n'en citer que
142148
Bon, le candidat qui nous intéresse, c'est `Java`. Voyons à présent les versions que nous propose SDKMAN.
143149

144150
Pour cela, interrogeons SDKMAN :
145-
{% highlight zsh %}% sdk list java{% endhighlight %}
151+
{% highlight shell %}
152+
sdk list java
153+
{% endhighlight %}
146154

147155
Voici la liste que j'obtiens :
148156

@@ -296,7 +304,9 @@ de code Java natif, je choisis `GraalVM CE` version `20.0.2` et je sélectionne
296304

297305
Maintenant, c'est à votre tour. Pour l'installer, j'exécute la commande suivante :
298306

299-
{% highlight zsh %} % sdk install java 20.0.2-graalce{% endhighlight %}
307+
{% highlight shell %}
308+
sdk install java 20.0.2-graalce
309+
{% endhighlight %}
300310

301311
Cela lancera le processus d'installation, et la sortie sera :
302312

@@ -323,9 +333,12 @@ nous avons installé cet outil, pour pouvoir installer plusieurs JDKs.
323333

324334
Pour ma part, j'en installe deux autres :
325335

326-
{% highlight zsh %}
327-
% sdk install java 21.fx-librca
328-
% sdk install java 23.r20-nik
336+
{% highlight shell %}
337+
sdk install java 21.fx-librca
338+
{% endhighlight %}
339+
340+
{% highlight shell %}
341+
sdk install java 23.r20-nik
329342
{% endhighlight %}
330343

331344
<hr class="hr-text" data-content="Choix du JDK">
@@ -339,14 +352,14 @@ Voyons à présent comment sélectionner une version spécifique de Java.
339352

340353
Voyons ce que nous dit la commande `sdk` :
341354

342-
{% highlight zsh %}
355+
{% highlight output %}
343356
% sdk current java
344357
Using java version 20.0.2-graalce
345358
{% endhighlight %}
346359

347360
Et voyons ce que nous dit `java` :
348361

349-
{% highlight zsh %}
362+
{% highlight output %}
350363
% java --version
351364
openjdk 20.0.2 2023-07-18
352365
OpenJDK Runtime Environment GraalVM CE 20.0.2+9.1 (build 20.0.2+9-jvmci-23.0-b15)
@@ -356,21 +369,21 @@ OpenJDK 64-Bit Server VM GraalVM CE 20.0.2+9.1 (build 20.0.2+9-jvmci-23.0-b15, m
356369

357370
### Affichons les versions installées
358371

359-
{% highlight output %}
360-
% sdk list java
372+
{% highlight shell %}
373+
sdk list java
361374
{% endhighlight %}
362375

363376
### Changeons de version
364377

365-
{% highlight zsh %}
378+
{% highlight output %}
366379
% sdk use java 21.fx-librca
367380

368381
Using java version 21.fx-librca in this shell.
369382
{% endhighlight %}
370383

371384
Puis vérifions avec Java
372385

373-
{% highlight zsh %}
386+
{% highlight output %}
374387
% java --version
375388

376389
openjdk 21 2023-09-19 LTS

0 commit comments

Comments
 (0)