2 Commits fb5e325024 ... 4871e26924

Author SHA1 Message Date
  Bence DOMONKOS 4871e26924 spit into multiple files 1 year ago
  Bence DOMONKOS 2e78f7cc7a implement some basic html tags 1 year ago
6 changed files with 259 additions and 63 deletions
  1. 186 0
      html.c
  2. 15 0
      html.h
  3. 2 2
      makefile
  4. 18 0
      utils.c
  5. 7 0
      utils.h
  6. 31 61
      webgen.c

+ 186 - 0
html.c

@@ -0,0 +1,186 @@
+#include <stdio.h>
+
+#include "html.h"
+#include "utils.h"
+
+/* globals */
+extern char *line;
+extern char *params[9];
+extern int nparams;
+extern int inparagraph;
+
+
+int
+spitheader(void)
+{
+	const char htmlheader[] =
+		"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
+		"\"http://www.w3.org/TR/html4/loose.dtd\">\n"
+		"<html>\n"
+		"<head>\n"
+		"<meta name=\"generator\" content=\"webgen, see domonkos.tk\">\n"
+		"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=US-ASCII\">\n"
+		"<meta name=\"Content-Style\" content=\"text/css\">\n"
+		"<title>Personal website</title>\n"
+		"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
+		"</head>\n"
+		"<body>\n";
+
+	return fputs(htmlheader, stdout);
+}
+
+int spitfooter(void)
+{
+	const char htmlfooter[] =
+		"<hr>\n"
+		"</body>\n"
+		"</html>\n";
+
+	return fputs(htmlfooter, stdout);
+
+}
+
+
+int
+spithref(void)
+{
+	const char htmlrefstring[] = "<a href=\"%s\">%s</a>";
+
+	if (nparams < 1) {
+		err("href requires at least one parameter!");
+		return 1;
+	} else if (nparams == 1) {
+		printf(htmlrefstring, params[0], params[0]);
+	} else if (nparams == 2) {
+		printf(htmlrefstring, params[0], params[1]);
+	} else if (nparams > 2) {
+		err("href takes two parameters at max!");
+		return 1;
+	}
+
+	return 0;
+}
+
+int
+spitimg(void)
+{
+	const char htmlimgstring[] = "<img src=\"%s\" alt=\"Image %s\">";
+
+	if (nparams < 1) {
+		err("img requires at least one parameter!");
+		return 1;
+	} else if (nparams == 1) {
+		printf(htmlimgstring, params[0], params[0]);
+	} else if (nparams == 2) {
+		printf(htmlimgstring, params[0], params[1]);
+	} else if (nparams > 2) {
+		err("img takes two parameters at max!");
+		return 1;
+	}
+
+	return 0;
+}
+
+int
+spititalic(void)
+{
+	if (nparams < 1) {
+		err("italic requires at least one parameter!");
+		return 1;
+	} else if (nparams == 1) {
+		printf("<em>%s</em>\n", params[0]);
+	} else if (nparams == 2) {
+		printf("<em>%s</em>%s\n", params[0], params[1]);
+	} else if (nparams == 3) {
+		printf("%s<em>%s</em>%s\n", params[2], params[0], params[1]);
+	} else if (nparams > 3) {
+		err("italic takes three parameters at max!");
+		return 1;
+	}
+
+	return 0;
+}
+
+int
+spitbold(void)
+{
+	if (nparams < 1) {
+		err("bold requires at least one parameter!");
+		return 1;
+	} else if (nparams == 1) {
+		printf("<b>%s</b>\n", params[0]);
+	} else if (nparams == 2) {
+		printf("<b>%s</b>%s\n", params[0], params[1]);
+	} else if (nparams == 3) {
+		printf("%s<b>%s</b>%s\n", params[2], params[0], params[1]);
+	} else if (nparams > 3) {
+		err("bold takes three parameters at max!");
+		return 1;
+	}
+
+	return 0;
+}
+
+int
+spitmonospace(void)
+{
+	if (nparams < 1) {
+		err("monospace requires at least one parameter!");
+		return 1;
+	} else if (nparams == 1) {
+		printf("<tt>%s</tt>\n", params[0]);
+	} else if (nparams == 2) {
+		printf("<tt>%s</tt>%s\n", params[0], params[1]);
+	} else if (nparams == 3) {
+		printf("%s<tt>%s</tt>%s\n", params[2], params[0], params[1]);
+	} else if (nparams > 3) {
+		err("monospace takes three parameters at max!");
+		return 1;
+	}
+
+	return 0;
+}
+
+int
+spitlinebreak(void)
+{
+	if (nparams >= 1) {
+		err("br takes no params!");
+		return 1;
+	} else {
+		puts("<br>");
+	}
+
+	return 0;
+}
+
+int
+newparagraph(void)
+{
+	/* if (nparams >= 1) { //TODO
+		err("paragraph takes no params!");
+		return 1;
+	} */
+	if (inparagraph) {
+		puts("</p><p>");
+	} else {
+		puts("<p>");
+	}
+
+	inparagraph = 1;
+
+	return 0;
+}
+
+int
+spithorizontalrule(void)
+{
+	if (nparams >= 1) {
+		err("hr takes no params!");
+		return 1;
+	} else {
+		puts("<hr>");
+	}
+
+	return 0;
+}

+ 15 - 0
html.h

@@ -0,0 +1,15 @@
+#ifndef HTML_H__
+#define HTML_H__
+
+int spitheader(void);
+int spitfooter(void);
+int spithref(void);
+int spitimg(void);
+int spititalic(void);
+int spitbold(void);
+int spitmonospace(void);
+int spitlinebreak(void);
+int newparagraph(void);
+int spithorizontalrule(void);
+
+#endif

+ 2 - 2
makefile

@@ -1,6 +1,6 @@
 default: webgen
 
-webgen: webgen.c
-	gcc -ggdb -o $@ $<
+webgen: webgen.c html.c utils.c
+	gcc -ggdb -o $@ $^
 
 .PHONY: default

+ 18 - 0
utils.c

@@ -0,0 +1,18 @@
+#include <stdio.h>
+
+extern char *line;
+extern unsigned linenum;
+
+int
+err(char *msg)
+{
+	return fprintf(stderr, "line: %u: %s; line: %s\n", linenum, msg, line);
+}
+
+int
+notimplemented(void)
+{
+	err("this function is not yet implemented :(");
+
+	return 1;
+}

+ 7 - 0
utils.h

@@ -0,0 +1,7 @@
+#ifndef UTIL_H__
+#define UTIL_H__
+
+int err(char *msg);
+int notimplemented(void);
+
+#endif

+ 31 - 61
webgen.c

@@ -4,71 +4,32 @@
 #include <string.h>
 #include <ctype.h>
 
-#define LENGTH(x) (sizeof (x) / sizeof (*x))
-
-const char htmlheader[] =
-"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"
-"\"http://www.w3.org/TR/html4/loose.dtd\">\n"
-"<html>\n"
-"<head>\n"
-"<meta name=\"generator\" content=\"webgen, see domonkos.tk\">\n"
-"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=US-ASCII\">\n"
-"<meta name=\"Content-Style\" content=\"text/css\">\n"
-"<title>Personal website</title>\n"
-"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">\n"
-"</head>\n"
-"<body>\n";
-
-const char htmlfooter[] =
-"<hr>\n"
-"</body>\n"
-"</html>\n";
-
-const char htmlrefstring[] = "<a href=\"%s\">%s</a>";
-const char htmlimgstring[] = "<img src=\"%s\" alt=\"Image %s\">";
+#include "html.h"
+#include "utils.h"
 
+#define LENGTH(x) (sizeof (x) / sizeof (*x))
 
 char *line = NULL;
-char * params[9] = { 0 };
+unsigned linenum = 0;
+size_t linelen;
+char *params[9] = { 0 };
 int nparams;
 
-int
-notimplemented(void)
-{
-	fprintf(stderr, "this function is not yet implemented :(");
-
-	return 1;
-}
-
-int
-spithref(void)
-{
-	if (nparams < 1) {
-		fprintf(stderr, "href requires at least one parameter!\n");
-		return 1;
-	} else if (nparams == 1) {
-		printf(htmlrefstring, params[0], params[0]);
-	} else if (nparams == 2) {
-		printf(htmlrefstring, params[0], params[1]);
-	} else if (nparams > 2) {
-		fprintf(stderr, "href takes two parameters at max!\n");
-		return 1;
-	}
-
-	return 0;
-}
+int inparagraph = 0;
 
 struct {
 	char *name;
 	int(*fun)(void);
-} tokens[] = {
-	{ ".b", notimplemented },
-	{ ".br", notimplemented },
-	{ ".cw", notimplemented },
-	{ ".i", notimplemented },
-	{ ".pp", notimplemented },
+} tokens[] = { //TODO: make the recognition not dependent on length+order
+	{ ".img", spitimg },
 	{ ".ref", spithref },
-	{ ".img", notimplemented },
+	{ ".br", spitlinebreak },
+	{ ".cw", spitmonospace },
+	{ ".hr", spithorizontalrule },
+	{ ".pp", newparagraph },
+	{ ".b", spitbold },
+	{ ".i", spititalic },
+	//{ "", newparagraph },
 };
 
 char *
@@ -111,15 +72,14 @@ paramize(void)
 		p = w;
 	}
 
-	//printf("n: %d; last---\n", nparams);
-
 	return 0;
 }
 
 int
 spit(void)
 {
-	return puts(line);
+	// TODO: should we always spit the original?
+	return fputs(line, stdout);
 }
 
 int
@@ -127,6 +87,12 @@ tokenp(void)
 {
 	int i;
 
+	// implicit new paragraph via an empty line is a special case
+	if (line[0] == '\n') {
+		newparagraph();
+		return 0;
+	}
+
 	for (i = 0; i < LENGTH(tokens); i++) {
 		const char * t = tokens[i].name;
 		if (!strncmp(line, t, strlen(t))) {
@@ -148,16 +114,20 @@ main(int argc, char *argv[])
 	size_t len = 0;
 	ssize_t nread;
 
-	fputs(htmlheader, stdout);
+	spitheader();
 
 	while ((nread = getline(&line, &len, stream)) != -1) {
+		linenum++;
 		tokenp();
 	}
 
 	free(line);
 
-	fputs(htmlfooter, stdout);
-	//fclose(stream);
+	if (inparagraph) {
+		puts("</p>");
+	}
+
+	spitfooter();
 
 	return 0;
 }