@@ -15,6 +15,7 @@ const kinds = Element.elements;
1515const Attribute = @import ("Attribute.zig" );
1616
1717const log = std .log .scoped (.@"html/ast" );
18+ const fmtlog = std .log .scoped (.@"html/ast/fmt" );
1819
1920has_syntax_errors : bool ,
2021language : Language ,
@@ -949,34 +950,40 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
949950 var current = ast .nodes [1 ];
950951 var direction : enum { enter , exit } = .enter ;
951952 var last_rbracket : u32 = 0 ;
953+ var last_was_text = false ;
952954 var pre : u32 = 0 ;
953955 while (true ) {
954956 const zone_outer = tracy .trace (@src ());
955957 defer zone_outer .end ();
956- log .debug ("looping, ind: {}, dir: {s}" , .{
958+ fmtlog .debug ("looping, ind: {}, dir: {s}" , .{
957959 indentation ,
958960 @tagName (direction ),
959961 });
962+
963+ const crt = current ;
964+ defer last_was_text = crt .kind == .text ;
960965 switch (direction ) {
961966 .enter = > {
962967 const zone = tracy .trace (@src ());
963968 defer zone .end ();
964- log .debug ("rendering enter ({}): {s} {any }" , .{
969+ fmtlog .debug ("rendering enter ({}): {t} lwt: { }" , .{
965970 indentation ,
966- "" ,
967- // current.open.slice(src),
968- current ,
971+ current .kind ,
972+ last_was_text ,
969973 });
970974
971975 const maybe_ws = src [last_rbracket .. current .open .start ];
972- log .debug ("maybe_ws = '{s}'" , .{maybe_ws });
976+ fmtlog .debug ("maybe_ws = '{s}'" , .{maybe_ws });
973977 if (pre > 0 ) {
974978 try w .writeAll (maybe_ws );
975979 } else {
976- const vertical = maybe_ws .len > 0 ;
980+ const vertical = if (last_was_text or current .kind == .text )
981+ std .mem .indexOfScalar (u8 , maybe_ws , '\n ' ) != null
982+ else
983+ maybe_ws .len > 0 ;
977984
978985 if (vertical ) {
979- log .debug ("adding a newline" , .{});
986+ fmtlog .debug ("adding a newline" , .{});
980987 const lines = std .mem .count (u8 , maybe_ws , "\n " );
981988 if (last_rbracket > 0 ) {
982989 if (lines >= 2 ) {
@@ -989,6 +996,8 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
989996 for (0.. indentation ) | _ | {
990997 try w .writeAll ("\t " );
991998 }
999+ } else if ((last_was_text or current .kind == .text ) and maybe_ws .len > 0 ) {
1000+ try w .writeAll (" " );
9921001 }
9931002 }
9941003
@@ -1016,7 +1025,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
10161025 return ;
10171026 }
10181027
1019- log .debug ("rendering exit ({}): {s} {any}" , .{
1028+ fmtlog .debug ("rendering exit ({}): {s} {any}" , .{
10201029 indentation ,
10211030 current .open .slice (src ),
10221031 current ,
@@ -1069,7 +1078,29 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
10691078 const txt = current .open .slice (src );
10701079 const parent_kind = ast .nodes [current .parent_idx ].kind ;
10711080 switch (parent_kind ) {
1072- else = > try w .writeAll (txt ),
1081+ else = > {
1082+ var it = std .mem .splitScalar (u8 , txt , '\n ' );
1083+ var first = true ;
1084+ var empty_line = false ;
1085+ while (it .next ()) | raw_line | {
1086+ const line = std .mem .trim (
1087+ u8 ,
1088+ raw_line ,
1089+ & std .ascii .whitespace ,
1090+ );
1091+ if (line .len == 0 ) {
1092+ if (empty_line ) continue ;
1093+ empty_line = true ;
1094+ if (! first ) for (0.. indentation ) | _ | try w .print ("\t " , .{});
1095+ try w .print ("\n " , .{});
1096+ continue ;
1097+ } else empty_line = false ;
1098+ if (! first ) for (0.. indentation ) | _ | try w .print ("\t " , .{});
1099+ try w .print ("{s}" , .{line });
1100+ if (it .peek () != null ) try w .print ("\n " , .{});
1101+ first = false ;
1102+ }
1103+ },
10731104 .style , .script = > {
10741105 var css_indent = indentation ;
10751106 var it = std .mem .splitScalar (u8 , txt , '\n ' );
@@ -1110,7 +1141,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
11101141 last_rbracket = current .open .end ;
11111142
11121143 if (current .next_idx != 0 ) {
1113- log .debug ("text next: {}" , .{current .next_idx });
1144+ fmtlog .debug ("text next: {}" , .{current .next_idx });
11141145 current = ast .nodes [current .next_idx ];
11151146 } else {
11161147 current = ast .nodes [current .parent_idx ];
@@ -1141,7 +1172,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
11411172 const maybe_name , const maybe_extra = blk : {
11421173 var tt : Tokenizer = .{ .language = ast .language };
11431174 const tag = current .open .slice (src );
1144- log .debug ("doctype tag: {s} {any}" , .{ tag , current });
1175+ fmtlog .debug ("doctype tag: {s} {any}" , .{ tag , current });
11451176 const dt = tt .next (tag ).? .doctype ;
11461177 const maybe_name : ? []const u8 = if (dt .name ) | name |
11471178 name .slice (tag )
@@ -1203,7 +1234,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
12031234 break :blk true ;
12041235 };
12051236
1206- log .debug ("element <{s}> vertical = {}" , .{ name , vertical });
1237+ fmtlog .debug ("element <{s}> vertical = {}" , .{ name , vertical });
12071238
12081239 // if (std.mem.eql(u8, name, "path")) @breakpoint();
12091240
@@ -1294,7 +1325,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
12941325 .return_attrs = true ,
12951326 };
12961327 const tag = current .open .slice (src );
1297- log .debug ("retokenize {s}\n " , .{tag });
1328+ fmtlog .debug ("retokenize {s}\n " , .{tag });
12981329 break :blk tt .getName (tag ).? .slice (tag );
12991330 };
13001331
0 commit comments