improve link formatting, add more error handling to link formatting

This commit is contained in:
opFez 2021-07-31 17:37:42 +02:00
parent 8efb91e4b4
commit 010f4bdf58
1 changed files with 35 additions and 18 deletions

View File

@ -93,18 +93,40 @@ internal_implicit_link_p(FILE *input)
return ret;
}
void
get_linkname(FILE *input, char *out)
{
char c;
while ((c = fgetc(input)) != '}') {
if (c == EOF)
die("End of file encountered while parsing link name.");
else if (c == '\n')
c = ' ';
*(out++) = c;
}
*out = '\0';
}
void
get_link(FILE *input, char *out)
{
char c;
while (((c = fgetc(input)) != ' ') && c != '\n') {
if (c == EOF)
die("End of file encountered while parsing link.");
*(out)++ = c;
}
*out = '\0';
}
void
emit_external_link(FILE *input, FILE *output)
{
char link[1024] = {0};
char *link_ptr = link;
while ((*(link_ptr++) = fgetc(input)) != ' ');
*(link_ptr - 1) = '\0';
char link[1024];
get_link(input, link);
char linkname[1024] = {0};
char *linkname_ptr = linkname;
while ((*(linkname_ptr++) = fgetc(input)) != '}');
*(linkname_ptr - 1) = '\0';
char linkname[1024];
get_linkname(input, linkname);
fprintf(output, "<a href=\"%s\">[%s]</a>", link, linkname);
}
@ -114,7 +136,7 @@ emit_internal_implicit_link(FILE *input, FILE *output)
{
fseek(input, -1, SEEK_CUR);
char page[1024] = {0};
char page[1024];
char *page_ptr = page;
while ((*(page_ptr++) = fgetc(input)) != '}');
@ -128,16 +150,11 @@ emit_internal_explicit_link(FILE *input, FILE *output)
{
fseek(input, -1, SEEK_CUR);
char page[1024] = {0};
char *page_ptr = page;
char page[1024];
get_link(input, page);
while ((*(page_ptr++) = fgetc(input)) != ' ');
*(page_ptr - 1) = '\0';
char linkname[1024] = {0};
char *linkname_ptr = linkname;
while ((*(linkname_ptr++) = fgetc(input)) != '}');
*(linkname_ptr - 1) = '\0';
char linkname[1024];
get_linkname(input, linkname);
fprintf(output, "<a href=\"%s.html\">{%s}</a>", page, linkname);
}