summaryrefslogtreecommitdiffstats
path: root/vmp.c
diff options
context:
space:
mode:
authorverdant <im@verdant.ee>2026-05-31 11:38:49 +0800
committerverdant <im@verdant.ee>2026-05-31 11:38:49 +0800
commit7f5ecacd12e7908a3f1575b53920c10dac2e9a56 (patch)
tree71fd2bd6bade95d27c73ee0a2668fc3e1a661798 /vmp.c
parent694a0a85e1abba046787e238c921897ef6b7f0fa (diff)
downloadvmp-7f5ecacd12e7908a3f1575b53920c10dac2e9a56.tar.gz
vmp-7f5ecacd12e7908a3f1575b53920c10dac2e9a56.zip
Remove cmark, refactor allHEADmaster
Implement head and split line parser
Diffstat (limited to 'vmp.c')
-rw-r--r--vmp.c91
1 files changed, 91 insertions, 0 deletions
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");
+ }
+ }
+}