diff options
author | dx <aryadevchavali1@gmail.com> | 2020-05-20 14:31:37 +0100 |
---|---|---|
committer | dx <aryadevchavali1@gmail.com> | 2020-05-20 14:31:37 +0100 |
commit | c1ac0746a0f3a6c465c5d4e11397b63c6e5522f7 (patch) | |
tree | 39d0e5a6d595c58df4ac2a5053bb7cc15f6e548e /Converter/src | |
parent | 701f08fbc9768049cd77160f313cd336dadb984f (diff) | |
download | mdhtml-c1ac0746a0f3a6c465c5d4e11397b63c6e5522f7.tar.gz mdhtml-c1ac0746a0f3a6c465c5d4e11397b63c6e5522f7.tar.bz2 mdhtml-c1ac0746a0f3a6c465c5d4e11397b63c6e5522f7.zip |
~!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.
Diffstat (limited to 'Converter/src')
-rw-r--r-- | Converter/src/compiler.c | 102 |
1 files changed, 55 insertions, 47 deletions
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 = "<strong>"; - close_tag = "</strong>"; - 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 = "<strong>"; + close_tag = "</strong>"; + 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 = "<i>"; + close_tag = "</i>"; + tag_size = 3; + md_tag_size = 1; } + break; } - - // one asterisk - else - { - tag = "<i>"; - close_tag = "</i>"; + case '_': { + tag = "<u>"; + close_tag = "</u>"; + md_tag = "_"; tag_size = 3; md_tag_size = 1; - while ((j < size_of_line) && (src[j] != '*')) ++j; + break; + } + case '~': { + tag = "<s>"; + close_tag = "</s>"; + 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); |