diff options
author | dx <aryadevchavali1@gmail.com> | 2020-05-20 14:56:59 +0100 |
---|---|---|
committer | dx <aryadevchavali1@gmail.com> | 2020-05-20 14:58:18 +0100 |
commit | 9042af391b891b0b0b9d9a04d35ac98ea95ca11d (patch) | |
tree | 00d7cf33f273aefa13250cec6a98a622e7eb1cdc /Converter | |
parent | 243ab962355323484fd29502851409bff2ab51b6 (diff) | |
download | mdhtml-9042af391b891b0b0b9d9a04d35ac98ea95ca11d.tar.gz mdhtml-9042af391b891b0b0b9d9a04d35ac98ea95ca11d.tar.bz2 mdhtml-9042af391b891b0b0b9d9a04d35ac98ea95ca11d.zip |
~!refactor of the compile_inner_text function
Set compiled token recipes in the compiler.h file (easier to manage) and
let code handle the rest.
Diffstat (limited to 'Converter')
-rw-r--r-- | Converter/includes/compiler.h | 4 | ||||
-rw-r--r-- | Converter/src/compiler.c | 80 |
2 files changed, 43 insertions, 41 deletions
diff --git a/Converter/includes/compiler.h b/Converter/includes/compiler.h index 6e1eca8..1e8578f 100644 --- a/Converter/includes/compiler.h +++ b/Converter/includes/compiler.h @@ -6,6 +6,8 @@ typedef char *string; const static char TOKENS[] = {'*', '_', '~'}; +const static char *TOKENS_COMPILED[] = { "<i>", "<u>", "<s>" }; +const static char *TOKENS_CLOSE_COMPILED[] = { "</i>", "</u>", "</s>" }; const static size_t N_TOKENS = 3; /* Compile markdown line to HTML line */ @@ -14,5 +16,7 @@ string compile_line(string line, size_t sz_line, string line_or_source); int find_string(string src, string query, size_t sz_src, size_t sz_query); /* Check if a given character is a text token*/ bool is_token(char c); +/* Generate the compiled token pair for a given single sized markdown token */ +char **get_token_pair(char c); #endif // __COMPILER_H_ diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c index 2fb5607..2b65da7 100644 --- a/Converter/src/compiler.c +++ b/Converter/src/compiler.c @@ -21,6 +21,32 @@ bool is_token(char c) return token_check; } +char **get_token_pair(char c) +{ + if (is_token(c)) + { + char **pair = malloc(sizeof(*pair) * 2); + int i; + switch(c) + { + case '*': + i = 0; + break; + case '_': + i = 1; + break; + case '~': + i = 2; + break; + } + pair[0] = TOKENS_COMPILED[i]; + pair[1] = TOKENS_CLOSE_COMPILED[i]; + return pair; + } + + return NULL; +} + void compile_inner_text(string dest, string src, size_t sz_src, string line_or_source) { @@ -50,49 +76,20 @@ void compile_inner_text(string dest, string src, size_t sz_src, { if (is_token(src[i])) { + char **pair = get_token_pair(src[i]); + tag = pair[0]; + close_tag = pair[1]; + md_tag = &src[i]; + md_tag_size = 1; + tag_size = 3; // Information for each tag - // NOTE: Here's where you add new text tags - switch (src[i]) + if (src[i] == '*' && src[i + 1] == '*') { - case '*': { - // Twin asterisks - if (src[i + 1] == '*') - { - md_tag = "**"; - tag = "<strong>"; - close_tag = "</strong>"; - tag_size = 8; - md_tag_size = 2; - } - // one asterisk - else - { - md_tag = "*"; - tag = "<i>"; - close_tag = "</i>"; - tag_size = 3; - md_tag_size = 1; - } - break; - } - case '_': { - tag = "<u>"; - close_tag = "</u>"; - md_tag = "_"; - tag_size = 3; - md_tag_size = 1; - break; - } - case '~': { - tag = "<s>"; - close_tag = "</s>"; - md_tag = "~"; - tag_size = 3; - md_tag_size = 1; - break; - } - default: - break; + md_tag = "**"; + tag = "<strong>"; + close_tag = "</strong>"; + tag_size = 8; + md_tag_size = 2; } // find the corresponding markdown tag, and report errors @@ -122,6 +119,7 @@ void compile_inner_text(string dest, string src, size_t sz_src, cursor += tag_size + size_of_content + tag_size + 1; // shift after operation i += size_of_content + (2 * md_tag_size) - 1; + free(pair); } else { |