diff options
author | dx <aryadevchavali1@gmail.com> | 2020-05-06 03:21:09 +0100 |
---|---|---|
committer | dx <aryadevchavali1@gmail.com> | 2020-05-06 03:43:13 +0100 |
commit | c57d3886e05fbea9a45b18b069f112ecbae42151 (patch) | |
tree | f0a050aac55aa698d92fee44b57c01e5a1332edc /Converter | |
parent | acb0e6b2bf944c096eee6dd5c3cf54df0e4b7b6e (diff) | |
download | mdhtml-c57d3886e05fbea9a45b18b069f112ecbae42151.tar.gz mdhtml-c57d3886e05fbea9a45b18b069f112ecbae42151.tar.bz2 mdhtml-c57d3886e05fbea9a45b18b069f112ecbae42151.zip |
+introduced compiler.h and compiler.c
compiler.h holds definitions necessary for compiling markdown,
compiler.c the implementations.
Moved implementation of compile_line to compiler.h and compiler.c respectively.
Diffstat (limited to 'Converter')
-rw-r--r-- | Converter/includes/compiler.h | 8 | ||||
-rw-r--r-- | Converter/src/compiler.c | 46 | ||||
-rw-r--r-- | Converter/src/main.c | 46 |
3 files changed, 55 insertions, 45 deletions
diff --git a/Converter/includes/compiler.h b/Converter/includes/compiler.h new file mode 100644 index 0000000..b0b6d43 --- /dev/null +++ b/Converter/includes/compiler.h @@ -0,0 +1,8 @@ +#ifndef __COMPILER_H_ +#define __COMPILER_H_ + +#include <stdlib.h> +typedef char *string; +string compile_line(string line, size_t size_of_line); + +#endif // __COMPILER_H_ diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c new file mode 100644 index 0000000..1c5b728 --- /dev/null +++ b/Converter/src/compiler.c @@ -0,0 +1,46 @@ +#include "../includes/compiler.h" +#include <stdio.h> +#include <string.h> + +string compile_line(string line, size_t size_of_line) +{ + string compiled_line; + size_t size_of_content; + + if (line[0] == '#') + { + // Compile a header + + // Get the level of header (h1, h2, etc) + int depth; + for (depth = 1; line[depth] == '#'; ++depth) continue; + size_of_content = size_of_line - depth; // remove hashes + + // Generate the header tags based on depth + const int tag_size = 4; + const int close_tag_size = 5; + string tag = malloc(sizeof(*tag) * 5); + sprintf(tag, "<h%d>", depth); + string close_tag = malloc(sizeof(*close_tag) * 6); + sprintf(close_tag, "</h%d>", depth); + + // allocate buffer with extra 9 characters for the tags + compiled_line = malloc(sizeof(*compiled_line) * + (size_of_line + tag_size + close_tag_size)); + // write h1 to start of compiled_line + strncpy(compiled_line, tag, tag_size); + // write the rest of the line to the compiled_line + strncpy(compiled_line + tag_size, line + depth, size_of_line); + // write the end tags + strncpy(compiled_line + size_of_content + tag_size, close_tag, + close_tag_size); + } + + else + { + compiled_line = malloc(sizeof(*compiled_line) * size_of_line); + strncpy(compiled_line, line, size_of_line); + } + + return compiled_line; +} diff --git a/Converter/src/main.c b/Converter/src/main.c index 9d0c61f..7ca85e8 100644 --- a/Converter/src/main.c +++ b/Converter/src/main.c @@ -1,52 +1,8 @@ +#include "../includes/compiler.h" #include <malloc.h> #include <stdio.h> #include <string.h> -typedef char *string; - -string compile_line(string line, size_t size_of_line) -{ - string compiled_line; - size_t size_of_content; - - if (line[0] == '#') - { - // Compile a header - - // Get the level of header (h1, h2, etc) - int depth; - for (depth = 1; line[depth] == '#'; ++depth) continue; - size_of_content = size_of_line - depth; // remove hashes - - // Generate the header tags based on depth - const int tag_size = 4; - const int close_tag_size = 5; - string tag = malloc(sizeof(*tag) * 5); - sprintf(tag, "<h%d>", depth); - string close_tag = malloc(sizeof(*close_tag) * 6); - sprintf(close_tag, "</h%d>", depth); - - // allocate buffer with extra 9 characters for the tags - compiled_line = malloc(sizeof(*compiled_line) * - (size_of_line + tag_size + close_tag_size)); - // write h1 to start of compiled_line - strncpy(compiled_line, tag, tag_size); - // write the rest of the line to the compiled_line - strncpy(compiled_line + tag_size, line + depth, size_of_line); - // write the end tags - strncpy(compiled_line + size_of_content + tag_size, close_tag, - close_tag_size); - } - - else - { - compiled_line = malloc(sizeof(*compiled_line) * size_of_line); - strncpy(compiled_line, line, size_of_line); - } - - return compiled_line; -} - int main() { return 0; |