@@ -371,32 +371,39 @@ StrList render_block(Arena *a, Block *b) {
371
371
if (b -> type == CODE ) {
372
372
StrList_pushv (a , out , strl ("<code id='" ), b -> id , strl ("'><pre>\n" ));
373
373
s32 line = 1 ;
374
+
375
+ s32 in_comment = 0 ;
376
+ str comment_span = strc ("<span style='color: var(--comment);'>" );
374
377
for (Text * t = b -> text ; t ; t = t -> next , line ++ ) {
375
378
str line_id = strf (a , "%.*s-%d" , b -> id .len , b -> id .str , line );
376
379
StrList_pushv (a , out , strl ("<span id='" ), line_id , strl ("'>" ));
377
380
StrList_pushv (a , out , strl ("<a href='#" ), line_id , strl ("' aria-hidden='true'></a>" ));
381
+
382
+ if (in_comment == 2 ) {
383
+ StrList_push (a , out , comment_span );
384
+ }
378
385
386
+ char in_string = 0 ;
379
387
str s = t -> text ;
380
- s32 in_comment = 0 ;
388
+ char pc = 0 ;
381
389
str_iter (s , i , c ) { /* Escape HTML characters in code blocks */
382
390
383
391
if (str_has_prefix (str_skip (s , i ), strl ("//" ))) {
384
392
in_comment = 1 ;
385
- StrList_pushv (a , out , str_first (s , i - 2 ), strl ( "<span style='color: var(--green);'>//" ) );
386
- s = str_skip (s , i + 2 ); i = -1 ;
393
+ StrList_pushv (a , out , str_first (s , i ), comment_span );
394
+ s = str_skip (s , i ); i = 2 ;
387
395
}
396
+
388
397
if (str_has_prefix (str_skip (s , i ), strl ("/*" ))) {
389
398
in_comment = 2 ;
390
- StrList_pushv (a , out , str_first (s , i ), strl ("<span style='color: var(--green);'>/*" ));
391
- s = str_skip (s , i + 2 ); i = -1 ;
392
- }
393
-
394
- if (in_comment == 2 && str_has_prefix (str_skip (s , i ), strl ("*/" ))) {
399
+ StrList_pushv (a , out , str_first (s , i ), comment_span );
400
+ s = str_skip (s , i ); i = 2 ;
401
+ } else if (in_comment == 2 && str_has_prefix (str_skip (s , i ), strl ("*/" ))) {
395
402
in_comment = 0 ;
396
403
StrList_pushv (a , out , str_first (s , i ), strl ("*/</span>" ));
397
404
s = str_skip (s , i + 2 ); i = -1 ;
398
405
}
399
-
406
+
400
407
switch (c ) {
401
408
case '<' : {
402
409
StrList_pushv (a , out , str_first (s , i ), strl ("<" ));
@@ -410,16 +417,33 @@ StrList render_block(Arena *a, Block *b) {
410
417
StrList_pushv (a , out , str_first (s , i ), strl ("&" ));
411
418
s = str_skip (s , i + 1 ); i = -1 ;
412
419
} break ;
413
- case '\n' : {
414
- if (in_comment == 1 ) {
415
- in_comment = 0 ;
416
- StrList_pushv (a , out , str_first (s , i ), strl ("</span>" ));
417
- s = str_skip (s , i + 1 ); i = -1 ;
420
+ case '\'' :
421
+ case '"' : {
422
+ if (in_comment == 0 ) {
423
+ if (in_string == 0 ) {
424
+ in_string = c ;
425
+ StrList_pushv (a , out , str_first (s , i ), strl ("<span style='color: var(--red);'>" ));
426
+ s = str_skip (s , i ); i = 0 ;
427
+ } else if (in_string == c ) {
428
+ in_string = 0 ;
429
+ StrList_pushv (a , out , str_first (s , i + 1 ), strl ("</span>" ));
430
+ s = str_skip (s , i + 1 ); i = 0 ;
431
+ }
418
432
}
433
+ } break ;
419
434
}
435
+
436
+ pc = c ;
437
+ }
438
+
439
+ StrList_push (a , out , s );
440
+ if (in_comment > 0 ) {
441
+ if (in_comment == 1 ) {
442
+ in_comment = 0 ;
420
443
}
444
+ StrList_push (a , out , strl ("</span>" ));
421
445
}
422
- StrList_pushv (a , out , s , strl ("</span>" ));
446
+ StrList_push (a , out , strl ("</span>" ));
423
447
}
424
448
StrList_pushv (a , out , strl ("</pre></code>\n" ));
425
449
}
0 commit comments