summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c38
-rw-r--r--vmp.c91
-rw-r--r--vmp.h24
3 files changed, 115 insertions, 38 deletions
diff --git a/main.c b/main.c
deleted file mode 100644
index deeff4a..0000000
--- a/main.c
+++ /dev/null
@@ -1,38 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <cmark.h>
-#include <string.h>
-
-#define MAX_SIZE 1024
-#define BUFFER_SIZE 4096
-
-/* TODO: Add custom syntax filter */
-int apply_custom_syntax(char *input);
-
-int main()
-{
- size_t cap = BUFFER_SIZE;
- size_t len = 0;
- char *markdown_input = malloc(cap);
-
- char buffer[BUFFER_SIZE];
- while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
- size_t line_len = strlen(buffer);
- if (len + line_len >= cap) {
- cap *= 2;
- markdown_input = realloc(markdown_input, cap);
- }
- strcpy(markdown_input + len, buffer);
- len += line_len;
- }
- char *html_output =
- cmark_markdown_to_html(markdown_input, len, CMARK_OPT_DEFAULT);
-
- if (html_output) {
- printf("%s", html_output);
- free(html_output);
- }
-
- free(markdown_input);
- return 0;
-}
diff --git a/vmp.c b/vmp.c
new file mode 100644
index 0000000..1480d4b
--- /dev/null
+++ b/vmp.c
@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include "vmp.h"
+
+int main(int argc, char** argv)
+{
+
+ /* Get input and output file name */
+ if (argc != 2) {
+ error("Usage: vmp <INPUT_FILE_NAME>\n", NO_ARG);
+ }
+
+ const char* in_file_name = argv[1];
+ if (sizeof(in_file_name) <= 3) {
+ error("File name is too short\n",IN_NAME_TOO_SHORT);
+ }
+
+ run(in_file_name);
+ return 0;
+}
+
+void error(const char* msg, int code) {
+ printf("%s", msg);
+ exit(code);
+}
+
+void run(const char* in_file_name) {
+ FILE* fd = fopen(in_file_name, "r");
+ if (!fd) {
+ error("Cannot open input file.\n", CANNOT_OPEN_FILE);
+ }
+
+ char line[1024];
+ while(fgets(line, sizeof(line), fd) != NULL) {
+ int len = strlen(line);
+ while (len > 0 && isspace(line[len - 1])) {
+ line[--len] = '\0';
+ }
+
+ parse_and_output(line);
+ }
+
+ fclose(fd);
+}
+
+void parse_head(const char* line) {
+ int level = 0;
+ size_t i = 0;
+
+ while(line[i] == '#' && i < strlen(line)) {
+ level++;
+ i++;
+ }
+
+ if (i < strlen(line) && line[i] == ' ' && level <=6 ) {
+ printf("<h%d>", level);
+
+ int start = i;
+ int length = strlen(line);
+ char head_text[512];
+ strncpy(head_text, line + start + 1, length);
+
+ printf("%s", head_text);
+ printf("</h%d>", level);
+ } else {
+ int start = i;
+ int length = strlen(line);
+ char head_text[512];
+ strncpy(head_text, line + start + 1, length);
+
+ printf("%s", head_text);
+ }
+}
+
+void parse_and_output(const char* line) {
+ int in_empty_line = 0;
+
+ /* Empty */
+ if (strcmp(line, "") == 0) {
+ printf("<br>\n");
+ in_empty_line = 1;
+ } else {
+ in_empty_line = 0;
+ char line_head = line[0];
+
+ if (line_head == '#') {
+ parse_head(line);
+ } else if (strcmp(line, "---") == 0) {
+ printf("<hr>\n");
+ }
+ }
+}
diff --git a/vmp.h b/vmp.h
new file mode 100644
index 0000000..0d81624
--- /dev/null
+++ b/vmp.h
@@ -0,0 +1,24 @@
+#ifndef VMP_H
+#define VMP_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+
+
+#define NO_ARG 1
+#define IN_NAME_TOO_SHORT 2
+#define CANNOT_OPEN_FILE 3
+#define IsSpecial(c) ((c) == '*' || (c) == '_' || (c) == '~' || (c) == '`')
+
+void error(const char* msg, int code);
+void run(const char* in_file_name);
+
+void parse_and_output(const char* line);
+char parse_normal_line(char* line);
+void parse_line(const char* line);
+
+
+#endif