jcs
/wikipedia
/amendments
/45
browser: Make sure H[1-5] elements have two newlines before them
Keep track of the last two printed characters so we don't end up
        printing more than two newlines
    jcs made amendment 45 over 2 years ago
--- browser.c	Wed Aug 30 15:44:45 2023
+++ browser.c	Wed Aug 30 17:47:55 2023
@@ -836,8 +836,11 @@ browser_print(struct browser *browser, const char *str
 		link->pos = was_len;
 		
 	TESetSelect(SHRT_MAX, SHRT_MAX, browser->te);
-	if ((last_style & STYLE_ITALIC) && !(style & STYLE_ITALIC))
+	if ((last_style & STYLE_ITALIC) && !(style & STYLE_ITALIC)) {
 		TEStylInsert(" ", 1, scrp_rec_h, browser->te);
+		browser->last_printed[0] = browser->last_printed[1];
+		browser->last_printed[1] = ' ';
+	}
 	
 	if (style & (STYLE_H1 | STYLE_H2 | STYLE_H3 | STYLE_H4 | STYLE_H5)) {
 		while (len && (str[0] == ' ' || str[0] == '\r')) {
@@ -847,14 +850,37 @@ browser_print(struct browser *browser, const char *str
 		while (len && (str[len - 1] == ' ' || str[len - 1] == '\r')) {
 			len--;
 		}
+		
+		if (browser->last_printed[1] != '\0') {
+			if (browser->last_printed[1] == '\r') {
+				if (browser->last_printed[0] != '\r')
+					TEStylInsert("\r", 1, scrp_rec_h, browser->te);
+			} else
+				TEStylInsert("\r\r", 2, scrp_rec_h, browser->te);
+		}
+				
+		browser->last_printed[0] = browser->last_printed[1] = '\r';
 	}
 
 	TEStylInsert(str, len, scrp_rec_h, browser->te);
 	
-	if (style & (STYLE_H1 | STYLE_H2))
+	if (len == 1) {
+		browser->last_printed[0] = browser->last_printed[1];
+		browser->last_printed[1] = str[0];
+	} else {
+		browser->last_printed[0] = str[len - 2];
+		browser->last_printed[1] = str[len - 1];
+	}
+	
+	if (style & (STYLE_H1 | STYLE_H2)) {
 		browser_draw_line(browser);
-	else if (style & (STYLE_H3 | STYLE_H4 | STYLE_H5))
+		browser->last_printed[0] = '-';
+		browser->last_printed[1] = '\r';
+	} else if (style & (STYLE_H3 | STYLE_H4 | STYLE_H5)) {
 		TEStylInsert("\r", 1, scrp_rec_h, browser->te);
+		browser->last_printed[0] = browser->last_printed[1];
+		browser->last_printed[1] = '\r';
+	}
 	
 	if (was_len == 0) {
 		SetCtlValue(browser->te_scroller, GetCtlMin(browser->te_scroller));
@@ -890,6 +916,7 @@ browser_clear(struct browser *browser)
 	
 	HLock(browser->te);
 	TESetText("", 0, browser->te);
+	browser->last_printed[0] = browser->last_printed[1] = '\0';
 	InvalRect(&(*(browser->te))->viewRect);
 	HUnlock(browser->te);
 	
--- browser.h	Tue Aug 29 09:55:47 2023
+++ browser.h	Wed Aug 30 17:27:24 2023
@@ -58,6 +58,7 @@ struct browser {
 	size_t links_count;
 	size_t links_size;
 	struct browser_link *links;
+	char last_printed[2];
 };
 
 struct browser *browser_init(void);