From c1ac0746a0f3a6c465c5d4e11397b63c6e5522f7 Mon Sep 17 00:00:00 2001 From: dx Date: Wed, 20 May 2020 14:31:37 +0100 Subject: ~!refactored compile_inner_text code Make it a bit cleaner, uses a switch to setup general variables (tag strings, lengths, etc) then a general algorithm that is guaranteed to work for any single character markdown syntax if those variables are set. --- Converter/src/compiler.c | 102 +++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 47 deletions(-) (limited to 'Converter/src') diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c index 9da3667..6411604 100644 --- a/Converter/src/compiler.c +++ b/Converter/src/compiler.c @@ -39,73 +39,81 @@ void compile_inner_text(string dest, string src, size_t sz_src, */ int cursor, lower, upper; size_t size_of_content, tag_size, md_tag_size; - string tag, close_tag; + string md_tag, tag, close_tag; int i, j; lower = 0; upper = 0; cursor = 0; - for (i = 0; i < size_of_line; ++i) + for (i = 0; i < sz_src; ++i) { - // Asterisk routine - if (src[i] == '*') + if (is_token(src[i])) { - j = i + 1; - - // Twin asterisks - if (src[i + 1] == '*') + // Information for each tag + // NOTE: Here's where you add new text tags + switch (src[i]) { - tag = ""; - close_tag = ""; - tag_size = 8; - md_tag_size = 2; - - // Make j the cursor seeking for the matching asterisk pair - while ((j < size_of_line - 1) && (src[j] != '*' || src[j + 1] != '*')) - ++j; - - // if found - if (src[j] == '*') + case '*': { + // Twin asterisks + if (src[i + 1] == '*') { - lower = i + 1; - upper = j; + md_tag = "**"; + tag = ""; + close_tag = ""; + tag_size = 8; + md_tag_size = 2; } + // one asterisk else { - // ERROR - fprintf(stderr, "No matching asterisk for %s:%d\n", line_or_source, - i); - exit(-1); + md_tag = "*"; + tag = ""; + close_tag = ""; + tag_size = 3; + md_tag_size = 1; } + break; } - - // one asterisk - else - { - tag = ""; - close_tag = ""; + case '_': { + tag = ""; + close_tag = ""; + md_tag = "_"; tag_size = 3; md_tag_size = 1; - while ((j < size_of_line) && (src[j] != '*')) ++j; + break; + } + case '~': { + tag = ""; + close_tag = ""; + md_tag = "~"; + tag_size = 3; + md_tag_size = 1; + break; + } + default: + break; + } - if (src[j] == '*') - { - lower = i; - upper = j; - } - else - { - // error, no matching asterisk - fprintf(stderr, "No matching asterisk for %s:%d\n", line_or_source, - i); - exit(-1); - } + // find the corresponding markdown tag, and report errors + j = find_string(src + i + md_tag_size, md_tag, sz_src - i - md_tag_size, + md_tag_size); + j += i + md_tag_size; + + // error checking the token + if (src[j] == md_tag[0]) + { + lower = i + md_tag_size - 1; + upper = j; + } + else + { + // error, no matching asterisk + fprintf(stderr, "No corresponding %s for %s:%d\n", md_tag, + line_or_source, i + md_tag_size); + exit(0); } - /* - * Copy all - */ size_of_content = upper - lower - 1; strncpy(dest + cursor, tag, tag_size); strncpy(dest + cursor + tag_size, src + lower + 1, size_of_content); -- cgit v1.2.3-13-gbd6f