aboutsummaryrefslogtreecommitdiff
path: root/Converter
diff options
context:
space:
mode:
authordx <aryadevchavali1@gmail.com>2020-05-20 14:36:35 +0100
committerdx <aryadevchavali1@gmail.com>2020-05-20 14:36:35 +0100
commit243ab962355323484fd29502851409bff2ab51b6 (patch)
tree6a5281297e9bdf3e16ec1d13c6314d655eb80f24 /Converter
parentc1ac0746a0f3a6c465c5d4e11397b63c6e5522f7 (diff)
downloadmdhtml-243ab962355323484fd29502851409bff2ab51b6.tar.gz
mdhtml-243ab962355323484fd29502851409bff2ab51b6.tar.bz2
mdhtml-243ab962355323484fd29502851409bff2ab51b6.zip
~!refactored compile_line code
Similar to previous commit, make it more general and easier to adapt through generalizing state and algorithms.
Diffstat (limited to 'Converter')
-rw-r--r--Converter/src/compiler.c63
1 files changed, 32 insertions, 31 deletions
diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c
index 6411604..2fb5607 100644
--- a/Converter/src/compiler.c
+++ b/Converter/src/compiler.c
@@ -136,44 +136,45 @@ string compile_line(string line, size_t size_of_line, string line_or_source)
string compiled_line;
size_t size_of_content;
- if (line[0] == '#') // Compile a header
+ if (line[0] == '#' || line[0] == '-') // Compile a header or list item
{
- // 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);
- string close_tag = malloc(sizeof(*close_tag) * 6);
- sprintf(tag, "<h%d>", depth);
- sprintf(close_tag, "</h%d>", depth);
+ int start_of_content;
+ string tag, close_tag;
+ if (line[0] == '#')
+ {
+ // Get the level of header (h1, h2, etc)
+ int depth;
+ for (depth = 1; line[depth] == '#'; ++depth) continue;
+ start_of_content = depth;
+ size_of_content = size_of_line - depth; // remove hashes
+
+ // Generate the header tags based on depth
+ tag = malloc(sizeof(*tag) * 5);
+ close_tag = malloc(sizeof(*close_tag) * 6);
+ sprintf(tag, "<h%d>", depth);
+ sprintf(close_tag, "</h%d>", depth);
+ }
+ else
+ {
+ tag = "<li>";
+ close_tag = "</li>";
+ size_of_content = size_of_line - 1;
+ start_of_content = 1;
+ }
- // allocate buffer with extra 9 characters for the tags
- compiled_line = malloc(sizeof(*compiled_line) *
- (size_of_line + tag_size + close_tag_size + 1));
- // write h1 to start of compiled_line
+ // allocate buffer with space for content and opposing tags
+ compiled_line =
+ malloc(sizeof(*compiled_line) * (size_of_content + (2 * tag_size) + 2));
+ // write start tag to 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);
+ strncpy(compiled_line + tag_size, line + start_of_content, size_of_line);
// write the end tags
strncpy(compiled_line + size_of_content + tag_size, close_tag,
- close_tag_size);
- compiled_line[size_of_content + tag_size + close_tag_size] = '\0';
- }
- else if (line[0] == '-') // Compile a list item
- {
- string tag = "<li>";
- string close_tag = "</li>";
- size_of_content = size_of_line - 1;
-
- compiled_line = malloc(sizeof(*compiled_line) * (size_of_content + 10));
- strncpy(compiled_line, tag, 4);
- strncpy(compiled_line + 4, line + 1, size_of_line);
- strncpy(compiled_line + size_of_content + 4, close_tag, 5);
- compiled_line[size_of_content + 9] = '\0'; // terminate string
+ tag_size + 1);
+ // write the terminator
+ compiled_line[size_of_content + (2 * tag_size) + 1] = '\0';
}
else // Compile a standard piece of text
{