1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::copy_button::CopyButton;
#[cfg(feature = "theme")]
use crate::theme::use_theme;
use dioxus::prelude::*;
/// Categoria sintattica del token evidenziato.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SyntaxTokenKind {
Keyword,
Function,
Type,
String,
Comment,
Number,
Operator,
Plain,
}
impl SyntaxTokenKind {
/// Ritorna la classe CSS associata al tipo di token.
#[must_use]
pub const fn class_name(&self) -> &'static str {
match self {
Self::Keyword => "sd-token-keyword",
Self::Function => "sd-token-fn",
Self::Type => "sd-token-type",
Self::String => "sd-token-string",
Self::Comment => "sd-token-comment",
Self::Number => "sd-token-number",
Self::Operator => "sd-token-operator",
Self::Plain => "sd-token-plain",
}
}
}
/// Token sintattico analizzato.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyntaxToken {
pub kind: SyntaxTokenKind,
pub text: String,
}
/// Tokenizer sintattico leggero a zero dipendenze per linguaggi comuni.
#[must_use]
pub fn tokenize_line(line: &str, lang: &str) -> Vec<SyntaxToken> {
let lower_lang = lang.to_lowercase();
let mut tokens = Vec::new();
// Rileva commenti riga intera
let trimmed = line.trim_start();
if trimmed.starts_with("//") || trimmed.starts_with('#') {
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::Comment,
text: line.to_string(),
});
return tokens;
}
let is_rust = lower_lang.contains("rust") || lower_lang == "rs";
let is_js = lower_lang.contains("js") || lower_lang.contains("ts");
let is_py = lower_lang.contains("py");
let rust_keywords = [
"pub", "fn", "let", "mut", "const", "static", "struct", "enum", "impl", "trait", "type",
"match", "if", "else", "for", "in", "while", "loop", "return", "use", "mod", "as",
"async", "await", "move", "where", "self", "Self", "rsx", "component",
];
let js_keywords = [
"const", "let", "var", "function", "return", "if", "else", "for", "while", "import",
"export", "from", "default", "class", "async", "await", "new", "this", "typeof",
];
let py_keywords = [
"def", "class", "return", "if", "elif", "else", "for", "while", "import", "from",
"as", "try", "except", "with", "async", "await", "self",
];
// Tokenizer a scansione di caratteri
let chars: Vec<char> = line.chars().collect();
let len = chars.len();
let mut i = 0;
while i < len {
let ch = chars[i];
// Stringhe letterali
if ch == '"' || ch == '\'' || ch == '`' {
let quote = ch;
let mut s = String::new();
s.push(quote);
i += 1;
while i < len {
let c = chars[i];
s.push(c);
if c == quote {
let mut backslashes = 0;
let mut b = i;
while b > 0 && chars[b - 1] == '\\' {
backslashes += 1;
b -= 1;
}
if backslashes % 2 == 0 {
i += 1;
break;
}
}
i += 1;
}
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::String,
text: s,
});
continue;
}
// Commenti inline //
if ch == '/' && i + 1 < len && chars[i + 1] == '/' {
let comment_text: String = chars[i..].iter().collect();
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::Comment,
text: comment_text,
});
break;
}
// Numeri
if ch.is_ascii_digit() && (i == 0 || !chars[i - 1].is_alphanumeric()) {
let mut num = String::new();
while i < len && (chars[i].is_ascii_digit() || chars[i] == '.' || chars[i] == '_') {
num.push(chars[i]);
i += 1;
}
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::Number,
text: num,
});
continue;
}
// Identificatori / Parole chiave
if ch.is_alphabetic() || ch == '_' {
let mut word = String::new();
while i < len && (chars[i].is_alphanumeric() || chars[i] == '_') {
word.push(chars[i]);
i += 1;
}
let is_keyword = (is_rust && rust_keywords.contains(&word.as_str()))
|| (is_js && js_keywords.contains(&word.as_str()))
|| (is_py && py_keywords.contains(&word.as_str()));
let kind = if is_keyword {
SyntaxTokenKind::Keyword
} else if word.chars().next().is_some_and(|c| c.is_uppercase()) {
SyntaxTokenKind::Type
} else if i < len && chars[i] == '(' {
SyntaxTokenKind::Function
} else {
SyntaxTokenKind::Plain
};
tokens.push(SyntaxToken { kind, text: word });
continue;
}
// Operatori o punteggiatura
if "=+-*/<>!&|:;,.(){}[]".contains(ch) {
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::Operator,
text: ch.to_string(),
});
i += 1;
continue;
}
// Spazi o altri caratteri
tokens.push(SyntaxToken {
kind: SyntaxTokenKind::Plain,
text: ch.to_string(),
});
i += 1;
}
tokens
}
/// Blocco di visualizzazione codice sorgente con syntax highlighting multilinguaggio,
/// numerazione delle righe, badge del linguaggio e pulsante di copia negli appunti.
#[component]
pub fn CodeBlock(
/// Codice sorgente da visualizzare ed evidenziare.
code: String,
/// Linguaggio di programmazione per il tokenizer (es. "rust", "rsx", "javascript", "python", "json", "bash").
#[props(default = "rust".to_string(), into)]
language: String,
/// Nome del file opzionale visualizzato nella barra superiore (es. "main.rs").
#[props(default)]
filename: Option<String>,
/// Se mostrare la numerazione delle righe sul margine sinistro. Defaults to true.
#[props(default = true)]
with_line_numbers: bool,
/// Se includere il pulsante rapido di copia negli appunti. Defaults to true.
#[props(default = true)]
with_copy_button: bool,
/// Numeri di riga (1-indexed) da evidenziare visivamente con accento luminoso.
#[props(default)]
highlight_lines: Vec<usize>,
/// Classi CSS aggiuntive.
#[props(default)]
class: String,
) -> Element {
let lines: Vec<&str> = code.lines().collect();
#[cfg(feature = "theme")]
let _theme = use_theme();
let display_title = filename.unwrap_or_else(|| language.to_uppercase());
rsx! {
div { class: "sd-code-block {class}",
// Barra superiore del blocco codice
div { class: "sd-code-block-header",
div { class: "sd-code-block-title-group",
span { class: "sd-code-block-lang-badge", "{display_title}" }
}
if with_copy_button {
div { class: "sd-code-block-actions",
CopyButton {
value: code.clone(),
label: Some("Copia".to_string()),
copied_label: "Copiato!".to_string(),
}
}
}
}
// Area codice preformattata con righe
div { class: "sd-code-block-body",
pre { class: "sd-code-pre",
code { class: "sd-code-content",
for (idx, line_str) in lines.iter().enumerate() {
{
let line_num = idx + 1;
let is_hl = highlight_lines.contains(&line_num);
let line_class = if is_hl { "sd-code-line sd-code-line--highlighted" } else { "sd-code-line" };
let tokens = tokenize_line(line_str, &language);
rsx! {
div { key: "{line_num}", class: "{line_class}",
if with_line_numbers {
span { class: "sd-code-line-number", "{line_num}" }
}
span { class: "sd-code-line-text",
if tokens.is_empty() {
" "
} else {
for (t_idx, token) in tokens.iter().enumerate() {
span {
key: "{t_idx}",
class: "{token.kind.class_name()}",
"{token.text}"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
/// Componente equivalente a [`CodeBlock`].
#[component]
pub fn CodeHighlight(
/// Codice sorgente da visualizzare ed evidenziare.
code: String,
/// Linguaggio di programmazione per il tokenizer. Defaults to "rust".
#[props(default = "rust".to_string(), into)]
language: String,
/// Nome del file opzionale visualizzato nella barra superiore.
#[props(default)]
filename: Option<String>,
/// Se mostrare la numerazione delle righe sul margine sinistro. Defaults to true.
#[props(default = true)]
with_line_numbers: bool,
/// Se includere il pulsante rapido di copia negli appunti. Defaults to true.
#[props(default = true)]
with_copy_button: bool,
/// Numeri di riga (1-indexed) da evidenziare visivamente con accento luminoso.
#[props(default)]
highlight_lines: Vec<usize>,
/// Classi CSS aggiuntive.
#[props(default)]
class: String,
) -> Element {
rsx! {
CodeBlock {
code,
language,
filename,
with_line_numbers,
with_copy_button,
highlight_lines,
class,
}
}
}