From 118e3d3c39242225baa876319cdbfbb1adadc77b Mon Sep 17 00:00:00 2001
From: Vicent Marti <tanoku@gmail.com>
Date: Mon, 15 Sep 2014 15:28:49 +0200
Subject: Cleanup external APIs

---
 src/blocks.c     |  11 ++--
 src/html/html.c  | 163 ++++++++++++++++++++++++++++---------------------------
 src/inlines.c    |   1 +
 src/main.c       |   8 +--
 src/print.c      | 114 +++++++++++++++++++-------------------
 src/references.c |   1 +
 src/stmd.h       |  26 ++-------
 7 files changed, 159 insertions(+), 165 deletions(-)

(limited to 'src')

diff --git a/src/blocks.c b/src/blocks.c
index 30a8284..2ac7032 100644
--- a/src/blocks.c
+++ b/src/blocks.c
@@ -6,8 +6,9 @@
 
 #include "stmd.h"
 #include "utf8.h"
-#include "html/houdini.h"
 #include "scanners.h"
+#include "inlines.h"
+#include "html/houdini.h"
 
 #define peek_at(i, n) (i)->data[n]
 
@@ -224,7 +225,7 @@ static void finalize(node_block* b, int line_number)
 }
 
 // Add a node_block as child of another.  Return pointer to child.
-extern node_block* add_child(node_block* parent,
+static node_block* add_child(node_block* parent,
 		int block_type, int start_line, int start_column)
 {
 	assert(parent);
@@ -252,7 +253,7 @@ extern node_block* add_child(node_block* parent,
 }
 
 // Free a node_block list and any children.
-extern void free_blocks(node_block* e)
+void stmd_free_nodes(node_block *e)
 {
 	node_block * next;
 	while (e != NULL) {
@@ -264,7 +265,7 @@ extern void free_blocks(node_block* e)
 		} else if (e->tag == BLOCK_DOCUMENT) {
 			reference_map_free(e->as.document.refmap);
 		}
-		free_blocks(e->children);
+		stmd_free_nodes(e->children);
 		free(e);
 		e = next;
 	}
@@ -279,8 +280,6 @@ void process_inlines(node_block* cur, reference_map *refmap)
 		case BLOCK_ATX_HEADER:
 		case BLOCK_SETEXT_HEADER:
 			cur->inline_content = parse_inlines(&cur->string_content, refmap);
-			// MEM
-			// strbuf_free(&cur->string_content);
 			break;
 
 		default:
diff --git a/src/html/html.c b/src/html/html.c
index b48b10b..6f3bc76 100644
--- a/src/html/html.c
+++ b/src/html/html.c
@@ -32,8 +32,89 @@ static inline void cr(strbuf *html)
 		strbuf_putc(html, '\n');
 }
 
+// Convert an inline list to HTML.  Returns 0 on success, and sets result.
+static void inlines_to_html(strbuf *html, node_inl* ils)
+{
+	strbuf scrap = GH_BUF_INIT;
+
+	while(ils != NULL) {
+		switch(ils->tag) {
+			case INL_STRING:
+				escape_html(html, ils->content.literal.data, ils->content.literal.len);
+				break;
+
+			case INL_LINEBREAK:
+				strbuf_puts(html, "<br />\n");
+				break;
+
+			case INL_SOFTBREAK:
+				strbuf_putc(html, '\n');
+				break;
+
+			case INL_CODE:
+				strbuf_puts(html, "<code>");
+				escape_html(html, ils->content.literal.data, ils->content.literal.len);
+				strbuf_puts(html, "</code>");
+				break;
+
+			case INL_RAW_HTML:
+				strbuf_put(html,
+						ils->content.literal.data,
+						ils->content.literal.len);
+				break;
+
+			case INL_LINK:
+				strbuf_puts(html, "<a href=\"");
+				if (ils->content.linkable.url)
+					escape_href(html, ils->content.linkable.url, -1);
+
+				if (ils->content.linkable.title) {
+					strbuf_puts(html, "\" title=\"");
+					escape_html(html, ils->content.linkable.title, -1);
+				}
+
+				strbuf_puts(html, "\">");
+				inlines_to_html(html, ils->content.inlines);
+				strbuf_puts(html, "</a>");
+				break;
+
+			case INL_IMAGE:
+				strbuf_puts(html, "<img src=\"");
+				if (ils->content.linkable.url)
+					escape_href(html, ils->content.linkable.url, -1);
+
+				inlines_to_html(&scrap, ils->content.inlines);
+				strbuf_puts(html, "\" alt=\"");
+				if (scrap.size)
+					escape_html(html, scrap.ptr, scrap.size);
+				strbuf_clear(&scrap);
+
+				if (ils->content.linkable.title) {
+					strbuf_puts(html, "\" title=\"");
+					escape_html(html, ils->content.linkable.title, -1);
+				}
+
+				strbuf_puts(html, "\"/>");
+				break;
+
+			case INL_STRONG:
+				strbuf_puts(html, "<strong>");
+				inlines_to_html(html, ils->content.inlines);
+				strbuf_puts(html, "</strong>");
+				break;
+
+			case INL_EMPH:
+				strbuf_puts(html, "<em>");
+				inlines_to_html(html, ils->content.inlines);
+				strbuf_puts(html, "</em>");
+				break;
+		}
+		ils = ils->next;
+	}
+}
+
 // Convert a node_block list to HTML.  Returns 0 on success, and sets result.
-void blocks_to_html(strbuf *html, node_block *b, bool tight)
+static void blocks_to_html(strbuf *html, node_block *b, bool tight)
 {
 	struct ListData *data;
 
@@ -139,83 +220,7 @@ void blocks_to_html(strbuf *html, node_block *b, bool tight)
 	}
 }
 
-// Convert an inline list to HTML.  Returns 0 on success, and sets result.
-void inlines_to_html(strbuf *html, node_inl* ils)
+void stmd_render_html(strbuf *html, node_block *root)
 {
-	strbuf scrap = GH_BUF_INIT;
-
-	while(ils != NULL) {
-		switch(ils->tag) {
-			case INL_STRING:
-				escape_html(html, ils->content.literal.data, ils->content.literal.len);
-				break;
-
-			case INL_LINEBREAK:
-				strbuf_puts(html, "<br />\n");
-				break;
-
-			case INL_SOFTBREAK:
-				strbuf_putc(html, '\n');
-				break;
-
-			case INL_CODE:
-				strbuf_puts(html, "<code>");
-				escape_html(html, ils->content.literal.data, ils->content.literal.len);
-				strbuf_puts(html, "</code>");
-				break;
-
-			case INL_RAW_HTML:
-				strbuf_put(html,
-						ils->content.literal.data,
-						ils->content.literal.len);
-				break;
-
-			case INL_LINK:
-				strbuf_puts(html, "<a href=\"");
-				if (ils->content.linkable.url)
-					escape_href(html, ils->content.linkable.url, -1);
-
-				if (ils->content.linkable.title) {
-					strbuf_puts(html, "\" title=\"");
-					escape_html(html, ils->content.linkable.title, -1);
-				}
-
-				strbuf_puts(html, "\">");
-				inlines_to_html(html, ils->content.inlines);
-				strbuf_puts(html, "</a>");
-				break;
-
-			case INL_IMAGE:
-				strbuf_puts(html, "<img src=\"");
-				if (ils->content.linkable.url)
-					escape_href(html, ils->content.linkable.url, -1);
-
-				inlines_to_html(&scrap, ils->content.inlines);
-				strbuf_puts(html, "\" alt=\"");
-				if (scrap.size)
-					escape_html(html, scrap.ptr, scrap.size);
-				strbuf_clear(&scrap);
-
-				if (ils->content.linkable.title) {
-					strbuf_puts(html, "\" title=\"");
-					escape_html(html, ils->content.linkable.title, -1);
-				}
-
-				strbuf_puts(html, "\"/>");
-				break;
-
-			case INL_STRONG:
-				strbuf_puts(html, "<strong>");
-				inlines_to_html(html, ils->content.inlines);
-				strbuf_puts(html, "</strong>");
-				break;
-
-			case INL_EMPH:
-				strbuf_puts(html, "<em>");
-				inlines_to_html(html, ils->content.inlines);
-				strbuf_puts(html, "</em>");
-				break;
-		}
-		ils = ils->next;
-	}
+	blocks_to_html(html, root, false);
 }
diff --git a/src/inlines.c b/src/inlines.c
index cd2d124..145825c 100644
--- a/src/inlines.c
+++ b/src/inlines.c
@@ -8,6 +8,7 @@
 #include "html/houdini.h"
 #include "utf8.h"
 #include "scanners.h"
+#include "inlines.h"
 
 typedef struct Subject {
 	chunk input;
diff --git a/src/main.c b/src/main.c
index 90bb16d..76a0e12 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,9 +17,9 @@ static void print_document(node_block *document, bool ast)
 	strbuf html = GH_BUF_INIT;
 
 	if (ast) {
-		print_blocks(document, 0);
+		stmd_debug_print(document);
 	} else {
-		blocks_to_html(&html, document, false);
+		stmd_render_html(&html, document);
 		printf("%s", html.ptr);
 		strbuf_free(&html);
 	}
@@ -54,7 +54,7 @@ int main(int argc, char *argv[])
 	if (numfps == 0) {
 		document = stmd_parse_file(stdin);
 		print_document(document, ast);
-		free_blocks(document);
+		stmd_free_nodes(document);
 	} else {
 		for (i = 0; i < numfps; i++) {
 			FILE *fp = fopen(argv[files[i]], "r");
@@ -67,7 +67,7 @@ int main(int argc, char *argv[])
 
 			document = stmd_parse_file(fp);
 			print_document(document, ast);
-			free_blocks(document);
+			stmd_free_nodes(document);
 			fclose(fp);
 		}
 	}
diff --git a/src/print.c b/src/print.c
index 36140a8..83f8daa 100644
--- a/src/print.c
+++ b/src/print.c
@@ -32,14 +32,69 @@ static void print_str(const unsigned char *s, int len)
 	putchar('"');
 }
 
+// Prettyprint an inline list, for debugging.
+static void print_inlines(node_inl* ils, int indent)
+{
+	while(ils != NULL) {
+		for (int i=0; i < indent; i++) {
+			putchar(' ');
+		}
+		switch(ils->tag) {
+		case INL_STRING:
+			printf("str ");
+			print_str(ils->content.literal.data, ils->content.literal.len);
+			putchar('\n');
+			break;
+		case INL_LINEBREAK:
+			printf("linebreak\n");
+			break;
+		case INL_SOFTBREAK:
+			printf("softbreak\n");
+			break;
+		case INL_CODE:
+			printf("code ");
+			print_str(ils->content.literal.data, ils->content.literal.len);
+			putchar('\n');
+			break;
+		case INL_RAW_HTML:
+			printf("html ");
+			print_str(ils->content.literal.data, ils->content.literal.len);
+			putchar('\n');
+			break;
+		case INL_LINK:
+		case INL_IMAGE:
+			printf("%s url=", ils->tag == INL_LINK ? "link" : "image");
+
+			if (ils->content.linkable.url)
+				print_str(ils->content.linkable.url, -1);
+
+			if (ils->content.linkable.title) {
+				printf(" title=");
+				print_str(ils->content.linkable.title, -1);
+			}
+			putchar('\n');
+			print_inlines(ils->content.linkable.label, indent + 2);
+			break;
+		case INL_STRONG:
+			printf("strong\n");
+			print_inlines(ils->content.linkable.label, indent + 2);
+			break;
+		case INL_EMPH:
+			printf("emph\n");
+			print_inlines(ils->content.linkable.label, indent + 2);
+			break;
+		}
+		ils = ils->next;
+	}
+}
+
 // Functions to pretty-print inline and node_block lists, for debugging.
 // Prettyprint an inline list, for debugging.
-extern void print_blocks(node_block* b, int indent)
+static void print_blocks(node_block* b, int indent)
 {
 	struct ListData *data;
 
 	while(b != NULL) {
-		// printf("%3d %3d %3d| ", b->start_line, b->start_column, b->end_line);
 		for (int i=0; i < indent; i++) {
 			putchar(' ');
 		}
@@ -115,58 +170,7 @@ extern void print_blocks(node_block* b, int indent)
 	}
 }
 
-// Prettyprint an inline list, for debugging.
-extern void print_inlines(node_inl* ils, int indent)
+void stmd_debug_print(node_block *root)
 {
-	while(ils != NULL) {
-		for (int i=0; i < indent; i++) {
-			putchar(' ');
-		}
-		switch(ils->tag) {
-		case INL_STRING:
-			printf("str ");
-			print_str(ils->content.literal.data, ils->content.literal.len);
-			putchar('\n');
-			break;
-		case INL_LINEBREAK:
-			printf("linebreak\n");
-			break;
-		case INL_SOFTBREAK:
-			printf("softbreak\n");
-			break;
-		case INL_CODE:
-			printf("code ");
-			print_str(ils->content.literal.data, ils->content.literal.len);
-			putchar('\n');
-			break;
-		case INL_RAW_HTML:
-			printf("html ");
-			print_str(ils->content.literal.data, ils->content.literal.len);
-			putchar('\n');
-			break;
-		case INL_LINK:
-		case INL_IMAGE:
-			printf("%s url=", ils->tag == INL_LINK ? "link" : "image");
-
-			if (ils->content.linkable.url)
-				print_str(ils->content.linkable.url, -1);
-
-			if (ils->content.linkable.title) {
-				printf(" title=");
-				print_str(ils->content.linkable.title, -1);
-			}
-			putchar('\n');
-			print_inlines(ils->content.linkable.label, indent + 2);
-			break;
-		case INL_STRONG:
-			printf("strong\n");
-			print_inlines(ils->content.linkable.label, indent + 2);
-			break;
-		case INL_EMPH:
-			printf("emph\n");
-			print_inlines(ils->content.linkable.label, indent + 2);
-			break;
-		}
-		ils = ils->next;
-	}
+	print_blocks(root, 0);
 }
diff --git a/src/references.c b/src/references.c
index 300bbcc..3e54b48 100644
--- a/src/references.c
+++ b/src/references.c
@@ -1,6 +1,7 @@
 #include "stmd.h"
 #include "utf8.h"
 #include "references.h"
+#include "inlines.h"
 
 static unsigned int
 refhash(const unsigned char *link_ref)
diff --git a/src/stmd.h b/src/stmd.h
index 4e21e6c..c6473a6 100644
--- a/src/stmd.h
+++ b/src/stmd.h
@@ -104,28 +104,12 @@ struct node_block {
 
 typedef struct node_block node_block;
 
-node_inl* parse_inlines(strbuf *input, reference_map *refmap);
-void free_inlines(node_inl* e);
+node_block *stmd_parse_document(const unsigned char *buffer, size_t len);
+node_block *stmd_parse_file(FILE *f);
 
-int parse_reference_inline(strbuf *input, reference_map *refmap);
-void unescape_buffer(strbuf *buf);
+void stmd_free_nodes(node_block *e);
 
-extern node_block* make_document();
-extern node_block* add_child(node_block* parent,
-                        int block_type, int start_line, int start_column);
-void free_blocks(node_block* e);
-
-extern node_block *stmd_parse_document(const unsigned char *buffer, size_t len);
-extern node_block *stmd_parse_file(FILE *f);
-
-void print_inlines(node_inl* ils, int indent);
-void print_blocks(node_block* blk, int indent);
-
-void blocks_to_html(strbuf *html, node_block *b, bool tight);
-void inlines_to_html(strbuf *html, node_inl *b);
-
-unsigned char *clean_url(chunk *url);
-unsigned char *clean_autolink(chunk *url, int is_email);
-unsigned char *clean_title(chunk *title);
+void stmd_debug_print(node_block *root);
+void stmd_render_html(strbuf *html, node_block *root);
 
 #endif
-- 
cgit v1.2.3