Skip to content

Commit 88e986d

Browse files
committed
Add the possibility to have a title on code block
```hint:title Lorem ipsum ``
1 parent 05f05a6 commit 88e986d

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

sites/build.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ echo_step 'Second pass static build'
2424
# Second pass to take the rebuilt search index into account.
2525
jekyll build --source hurl.dev --destination hurl.dev/_site --future
2626

27-
echo_step 'Highlight code snippets'
28-
# Second pass to take the rebuilt search index into account.
29-
# Highlight Hurl snippet.
30-
python3 highlight.py
31-
3227
echo_step 'Replace home samples'
3328
python3 build_home_samples.py
3429

30+
echo_step 'Highlight code snippets'
31+
python3 highlight.py
32+
3533
echo_step 'Add title anchors'
3634
python3 build_anchors.py
3735

sites/build_home_samples.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import re
33
from pathlib import Path
44

5-
import highlight
6-
75

86
class Sample:
97
name: str
@@ -12,15 +10,15 @@ class Sample:
1210

1311
def __init__(self, name: str, src: str):
1412
self.name = name
15-
src_padding = src
13+
src_padded = src
1614
# Count number of line in src, add padding if necessary
17-
nl = src_padding.count("\n")
15+
nl = src_padded.count("\n")
1816
max_line = 14
1917
if nl < max_line:
20-
src_padding += "\n" * (max_line - nl)
21-
self.src = src_padding
18+
src_padded += "\n" * (max_line - nl)
19+
self.src = src_padded
2220
html = '<pre><code class="language-hurl">'
23-
html += highlight.hurl_to_html(src_padding)
21+
html += src_padded
2422
html += "</code></pre>\n"
2523
self.html = html
2624

@@ -375,9 +373,9 @@ def make_home_samples():
375373
select_html = ""
376374
select_html += '<div class="home-picker">\n'
377375
select_html += (
378-
'<label class="home-picker-label" for="samples">Choose example</label>\n'
376+
'<label class="home-picker-label" for="home-samples">Choose example</label>\n'
379377
)
380-
select_html += '<select id="home-samples" names="samples"">\n'
378+
select_html += '<select id="home-samples" name="samples">\n'
381379
for idx, sample in enumerate(samples):
382380
if idx == feature_sample:
383381
select = " selected"
@@ -386,7 +384,7 @@ def make_home_samples():
386384
select_html += (
387385
f' <option value="home-sample-{idx}"{select}>{sample.name}</option>\n'
388386
)
389-
select_html += "/<select>\n"
387+
select_html += "</select>\n"
390388
select_html += "</div>\n"
391389
select_html += '<div class="home-sample">\n'
392390
select_html += samples[feature_sample].html

sites/highlight.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import subprocess
33
from subprocess import CalledProcessError
44
import re
5+
from dataclasses import dataclass
56
from pathlib import Path
67
from typing import List, Callable
78
import platform
@@ -17,6 +18,15 @@ def main():
1718
highlight_code(language="rust", to_html_func=rust_to_html)
1819

1920

21+
@dataclass
22+
class Snippet:
23+
"""Represents a code snippet."""
24+
25+
content: str
26+
title: str | None
27+
language: str
28+
29+
2030
def get_os() -> str:
2131
"""Return linux, macos or windows."""
2232
if platform.system() == "Linux":
@@ -42,18 +52,17 @@ def highlight_code(language: str, to_html_func: Callable[[str], str]) -> None:
4252
print(f" Highlighting {filename}")
4353
dst = src
4454
for snippet in snippets:
45-
unescaped_snippet = unescape_html(snippet)
46-
colored_snippet = to_html_func(unescaped_snippet.strip())
47-
dst = dst.replace(
48-
f'<pre><code class="language-{language}">{snippet}</code></pre>',
49-
f'<pre><code class="language-{language}">{colored_snippet}</code></pre>',
50-
)
51-
52-
# Patch for <https://github.com/Orange-OpenSource/hurl/issues/4117> to delete with 7.0.0
53-
dst = dst.replace(
54-
'<span class="line"><span class="multiline">variable {',
55-
'<span class="line"><span class="multiline">variables {',
56-
)
55+
title = snippet.title
56+
content = snippet.content
57+
unescaped_content = unescape_html(content)
58+
colored_content = to_html_func(unescaped_content)
59+
if title:
60+
old = f'<pre><code class="language-{language}:{title}">{content}</code></pre>'
61+
new = f'<div class="code-block"><div class="code-title">{title}</div>\n<pre><code class="language-{language}">{colored_content}</code></pre></div>'
62+
else:
63+
old = f'<pre><code class="language-{language}">{content}</code></pre>'
64+
new = f'<div class="code-block"><pre><code class="language-{language}">{colored_content}</code></pre></div>'
65+
dst = dst.replace(old, new)
5766
Path(filename).write_text(dst)
5867

5968

@@ -91,6 +100,7 @@ def hurl_to_html(snippet: str) -> str:
91100

92101
# PATCH: https://github.com/Orange-OpenSource/hurl/issues/3242
93102
output = output.replace("cert.pem\\", "cert.pem")
103+
# To be uniform with other snippets, we're removing the <pre><code> tag
94104
return extract(output, '<pre><code class="language-hurl">', "</code></pre>")
95105

96106

@@ -164,24 +174,29 @@ def shell_to_html(snippet: str) -> str:
164174
return output
165175

166176

167-
def extract_snippet(language: str, text: str) -> List[str]:
168-
prefix = f'<pre><code class="language-{language}">'
177+
def extract_snippet(language: str, text: str) -> List[Snippet]:
178+
prefix = re.compile(f'<pre><code class="language\\-{language}(?P<title>:.*)?">')
169179
suffix = "</code></pre>"
170180
index = 0
171181

172-
snippets: List[str] = []
182+
snippets: List[Snippet] = []
173183
while True:
174-
begin = text.find(prefix, index)
175-
if begin == -1:
184+
match = prefix.search(text, index)
185+
if not match:
176186
break
177-
end = text.find(suffix, begin)
187+
prefix_end = match.end(0)
188+
title = match.group("title")
189+
if title:
190+
title = title[1:]
191+
end = text.find(suffix, prefix_end)
178192
index = end
179-
snippet = text[begin + len(prefix) : end]
193+
content = text[prefix_end:end]
194+
snippet = Snippet(content=content, title=title, language=language)
180195
snippets.append(snippet)
181196
return snippets
182197

183198

184-
def extract(text, prefix, suffix):
199+
def extract(text: str, prefix: str, suffix: str) -> str | None:
185200
reg = re.compile(f"""{prefix}(.+?){suffix}""", re.DOTALL)
186201
match = reg.search(text)
187202
if match:

0 commit comments

Comments
 (0)