|
|
|
@ -3,7 +3,6 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This is free and unencumbered software released into the public domain.
|
|
|
|
|
*
|
|
|
|
@ -32,16 +31,7 @@
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define VERSION "0.43" /* yes i pulled a number out of my hat please cope */
|
|
|
|
|
|
|
|
|
|
#define checkCloseAndPrint(thing,openTxt,closeTxt) \
|
|
|
|
|
if (close.thing) { \
|
|
|
|
|
fputs(closeTxt,stdout); \
|
|
|
|
|
close.thing = 0; \
|
|
|
|
|
} else { \
|
|
|
|
|
fputs(openTxt,stdout); \
|
|
|
|
|
close.thing = 1; \
|
|
|
|
|
}
|
|
|
|
|
#define VERSION "0.45"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
size_t position;
|
|
|
|
@ -59,21 +49,27 @@ struct closeThings {
|
|
|
|
|
|
|
|
|
|
void reallocBuffer(Buffer *buffer) {
|
|
|
|
|
buffer->size += 1024;
|
|
|
|
|
buffer->data = realloc(buffer->data,buffer->size);
|
|
|
|
|
buffer->data = realloc(buffer->data,buffer->size+1); /* Adding one ensures we always have a null terminator */
|
|
|
|
|
if (!buffer->data) {
|
|
|
|
|
printf("Error realloc-ing buffer!");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Yes I am not using most of these at the moment but I might in the future so they shall remain */
|
|
|
|
|
void checkBufSafety(Buffer *buffer) {
|
|
|
|
|
if ((buffer->size < (buffer->position - 64)) || ((buffer->size - 64) < 0)) {
|
|
|
|
|
reallocBuffer(buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void appendBufferToBuffer(Buffer *buffer, Buffer *dataBuffer) {
|
|
|
|
|
for (size_t i = 0;i < dataBuffer->size;i++) {
|
|
|
|
|
if (dataBuffer->data[i] == 0x00) break; /* stop on NULL */
|
|
|
|
|
buffer->data[buffer->position] = dataBuffer->data[i];
|
|
|
|
|
buffer->position++;
|
|
|
|
|
if ((buffer->position > buffer->size-64) || (buffer->size-64 < 0)) {
|
|
|
|
|
reallocBuffer(buffer);
|
|
|
|
|
}
|
|
|
|
|
checkBufSafety(buffer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -81,6 +77,7 @@ void appendCharPToBuffer(char *string, Buffer *buffer) {
|
|
|
|
|
size_t stringLen = strlen(string);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0;i < stringLen;i++) {
|
|
|
|
|
checkBufSafety(buffer);
|
|
|
|
|
buffer->data[buffer->position+i] = string[i];
|
|
|
|
|
buffer->position++;
|
|
|
|
|
}
|
|
|
|
@ -89,8 +86,193 @@ void appendCharPToBuffer(char *string, Buffer *buffer) {
|
|
|
|
|
void appendCharToBuffer(char character, Buffer *buffer) {
|
|
|
|
|
buffer->data[buffer->position] = character;
|
|
|
|
|
buffer->position++;
|
|
|
|
|
if ((buffer->position > buffer->size-64) || (buffer->size-64 < 0)) {
|
|
|
|
|
reallocBuffer(buffer);
|
|
|
|
|
checkBufSafety(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* I'm gonna leave this as a define for now */
|
|
|
|
|
#define checkCloseAndPrint(thing,openTxt,closeTxt) \
|
|
|
|
|
if (close->thing) { \
|
|
|
|
|
fputs(closeTxt,stdout); \
|
|
|
|
|
close->thing = 0; \
|
|
|
|
|
} else { \
|
|
|
|
|
fputs(openTxt,stdout); \
|
|
|
|
|
close->thing = 1; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void getFormattingAndPrint(char character,struct closeThings *close) {
|
|
|
|
|
int characterInt;
|
|
|
|
|
int cont;
|
|
|
|
|
switch (character) {
|
|
|
|
|
case '\\': /* character escaping */
|
|
|
|
|
putc((char)fgetc(stdin),stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '*': /* bold */
|
|
|
|
|
checkCloseAndPrint(Bold,"<b>","</b>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '/': /* italics */
|
|
|
|
|
checkCloseAndPrint(Italics,"<i>","</i>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '`': /* monospace */
|
|
|
|
|
checkCloseAndPrint(Monospace,"<code>","</code>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '~': /* strikethrough */
|
|
|
|
|
checkCloseAndPrint(Strikethrough,"<del>","</del>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '_': /* underline */
|
|
|
|
|
checkCloseAndPrint(Underline,"<ins>","</ins>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\n': /* convert newlines */
|
|
|
|
|
puts("<br>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '[': /* links */
|
|
|
|
|
;Buffer linkTxt;
|
|
|
|
|
linkTxt.size = 1024;
|
|
|
|
|
linkTxt.position = 0;
|
|
|
|
|
linkTxt.data = calloc(linkTxt.size+1,sizeof(char));
|
|
|
|
|
|
|
|
|
|
Buffer linkLoc;
|
|
|
|
|
linkLoc.size = 1024;
|
|
|
|
|
linkLoc.position = 0;
|
|
|
|
|
linkLoc.data = calloc(linkLoc.size+1,sizeof(char));
|
|
|
|
|
|
|
|
|
|
characterInt = '\n';
|
|
|
|
|
cont = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
characterInt = fgetc(stdin);
|
|
|
|
|
character = (char)characterInt;
|
|
|
|
|
if (characterInt == EOF) {
|
|
|
|
|
puts("Stream ended before link text finished!");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (character == '\\') {
|
|
|
|
|
cont = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (cont) {
|
|
|
|
|
cont = 0;
|
|
|
|
|
checkBufSafety(&linkTxt);
|
|
|
|
|
appendCharToBuffer(character,&linkTxt);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkBufSafety(&linkTxt);
|
|
|
|
|
appendCharToBuffer(character,&linkTxt);
|
|
|
|
|
if (character == ']') break;
|
|
|
|
|
}
|
|
|
|
|
linkTxt.data[--linkTxt.position] = 0;
|
|
|
|
|
fgetc(stdin); /* Get past the '(' in the location */
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
characterInt = fgetc(stdin);
|
|
|
|
|
character = (char)characterInt;
|
|
|
|
|
if (characterInt == EOF) {
|
|
|
|
|
puts("Stream ended before link location finished!");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (character == '\\') {
|
|
|
|
|
cont = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (cont) {
|
|
|
|
|
cont = 0;
|
|
|
|
|
checkBufSafety(&linkLoc);
|
|
|
|
|
appendCharToBuffer(character,&linkLoc);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkBufSafety(&linkLoc);
|
|
|
|
|
appendCharToBuffer(character,&linkLoc);
|
|
|
|
|
if (character == ')') break;
|
|
|
|
|
}
|
|
|
|
|
linkLoc.data[--linkLoc.position] = 0;
|
|
|
|
|
printf("<a href=\"%s\">%s</a>",linkLoc.data,linkTxt.data);
|
|
|
|
|
|
|
|
|
|
free(linkLoc.data);
|
|
|
|
|
free(linkTxt.data);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '{': /* images */
|
|
|
|
|
;Buffer imgTxt;
|
|
|
|
|
imgTxt.size = 1024;
|
|
|
|
|
imgTxt.position = 0;
|
|
|
|
|
imgTxt.data = calloc(imgTxt.size+1,sizeof(char));
|
|
|
|
|
|
|
|
|
|
Buffer imgLoc;
|
|
|
|
|
imgLoc.size = 1024;
|
|
|
|
|
imgLoc.position = 0;
|
|
|
|
|
imgLoc.data = calloc(imgLoc.size+1,sizeof(char));
|
|
|
|
|
|
|
|
|
|
characterInt = '\n';
|
|
|
|
|
cont = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
characterInt = fgetc(stdin);
|
|
|
|
|
character = (char)characterInt;
|
|
|
|
|
if (characterInt == EOF) {
|
|
|
|
|
puts("Stream ended before link text finished!");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (character == '\\') {
|
|
|
|
|
cont = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (cont) {
|
|
|
|
|
cont = 0;
|
|
|
|
|
checkBufSafety(&imgLoc);
|
|
|
|
|
appendCharToBuffer(character,&imgLoc);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkBufSafety(&imgLoc);
|
|
|
|
|
appendCharToBuffer(character,&imgLoc);
|
|
|
|
|
if (character == '}') break;
|
|
|
|
|
}
|
|
|
|
|
imgLoc.data[--imgLoc.position] = 0;
|
|
|
|
|
fgetc(stdin); /* Get past the '(' in the alt text */
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
characterInt = fgetc(stdin);
|
|
|
|
|
character = (char)characterInt;
|
|
|
|
|
if (characterInt == EOF) {
|
|
|
|
|
puts("Stream ended before link location finished!");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
if (character == '\\') {
|
|
|
|
|
cont = 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (cont) {
|
|
|
|
|
cont = 0;
|
|
|
|
|
checkBufSafety(&imgTxt);
|
|
|
|
|
appendCharToBuffer(character,&imgTxt);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkBufSafety(&imgTxt);
|
|
|
|
|
appendCharToBuffer(character,&imgTxt);
|
|
|
|
|
if (character == ')') break;
|
|
|
|
|
}
|
|
|
|
|
imgTxt.data[--imgTxt.position] = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf("<img alt=\"%s\" src=\"%s\">",imgTxt.data,imgLoc.data);
|
|
|
|
|
|
|
|
|
|
free(imgTxt.data);
|
|
|
|
|
free(imgLoc.data);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: /* it is a normal character print to stdout */
|
|
|
|
|
putc(character,stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -98,66 +280,39 @@ int main(int argc, char **argv) {
|
|
|
|
|
(void)argv; /* this stops it from yelling at me :) */
|
|
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
|
printf("txtToHTML V %s\n",VERSION);
|
|
|
|
|
printf(" Usage:\n");
|
|
|
|
|
printf(" cat coolthing.txt | txtToHTML > coolthing.html\n");
|
|
|
|
|
printf(" Special characters:\n");
|
|
|
|
|
printf(" * = bold\n");
|
|
|
|
|
printf(" / = italic\n");
|
|
|
|
|
printf(" ` = monospace\n");
|
|
|
|
|
printf(" ~ = strikethrough\n");
|
|
|
|
|
printf(" _ = underline\n");
|
|
|
|
|
printf(" markdown style links are available\n");
|
|
|
|
|
printf(" you can escape any character with \\ and it will not be used for formatting\n");
|
|
|
|
|
fputs(
|
|
|
|
|
"txtToHTML V"VERSION"\n" /* god I love the way C handles strings in the compiler */
|
|
|
|
|
" Usage:\n"
|
|
|
|
|
" cat coolthing.txt | txtToHTML > coolthing.html\n"
|
|
|
|
|
" Special characters:\n"
|
|
|
|
|
" * = bold\n"
|
|
|
|
|
" / = italic\n"
|
|
|
|
|
" ` = monospace\n"
|
|
|
|
|
" ~ = strikethrough\n"
|
|
|
|
|
" _ = underline\n"
|
|
|
|
|
" Markdown style links are available/\n"
|
|
|
|
|
" You can create images like so:\n"
|
|
|
|
|
" {https://image.location/img.gif}(Required alt text)\n"
|
|
|
|
|
" Alt text is required for images.\n"
|
|
|
|
|
" you can escape any character with \\ and it will not be used for formatting\n",
|
|
|
|
|
stderr);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char character = 'a';
|
|
|
|
|
int characterInt = 'a';
|
|
|
|
|
|
|
|
|
|
struct closeThings close;
|
|
|
|
|
|
|
|
|
|
while(characterInt != EOF) {
|
|
|
|
|
struct closeThings close = {0,0,0,0,0}; /* Make sure it starts as zero so we do not get fucky shit */
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
characterInt = fgetc(stdin);
|
|
|
|
|
character = (char)characterInt;
|
|
|
|
|
if (characterInt == EOF) break;
|
|
|
|
|
|
|
|
|
|
switch (character) {
|
|
|
|
|
case '\\': /* character escaping */
|
|
|
|
|
putc((char)fgetc(stdin),stdout);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '*': /* bold */
|
|
|
|
|
checkCloseAndPrint(Bold,"<b>","</b>");
|
|
|
|
|
break;
|
|
|
|
|
case '/': /* italics */
|
|
|
|
|
checkCloseAndPrint(Bold,"<i>","</i>");
|
|
|
|
|
break;
|
|
|
|
|
case '`': /* monospace */
|
|
|
|
|
checkCloseAndPrint(Bold,"<code>","</code>");
|
|
|
|
|
break;
|
|
|
|
|
case '~': /* strikethrough */
|
|
|
|
|
checkCloseAndPrint(Bold,"<del>","</del>");
|
|
|
|
|
break;
|
|
|
|
|
case '_': /* underline */
|
|
|
|
|
checkCloseAndPrint(Bold,"<ins>","</ins>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '{': /* images */
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '\n': /* convert newlines */
|
|
|
|
|
puts("<br>");
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default: /* it is a normal character append to buffer */
|
|
|
|
|
putc(character,stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
getFormattingAndPrint(character,&close);
|
|
|
|
|
}
|
|
|
|
|
puts("");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|