22import subprocess
33from subprocess import CalledProcessError
44import re
5+ from dataclasses import dataclass
56from pathlib import Path
67from typing import List , Callable
78import 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+
2030def 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