You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
319 lines
8.0 KiB
319 lines
8.0 KiB
#include <stdbool.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
/*
|
|
* This is free and unencumbered software released into the public domain.
|
|
*
|
|
* Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
* distribute this software, either in source code form or as a compiled
|
|
* binary, for any purpose, commercial or non-commercial, and by any
|
|
* means.
|
|
*
|
|
* In jurisdictions that recognize copyright laws, the author or authors
|
|
* of this software dedicate any and all copyright interest in the
|
|
* software to the public domain. We make this dedication for the benefit
|
|
* of the public at large and to the detriment of our heirs and
|
|
* successors. We intend this dedication to be an overt act of
|
|
* relinquishment in perpetuity of all present and future rights to this
|
|
* software under copyright law.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
*
|
|
* For more information, please refer to <http://unlicense.org/>
|
|
*
|
|
*/
|
|
|
|
#define VERSION "0.45"
|
|
|
|
typedef struct {
|
|
size_t position;
|
|
size_t size;
|
|
char *data;
|
|
} Buffer;
|
|
|
|
struct closeThings {
|
|
bool Bold;
|
|
bool Italics;
|
|
bool Strikethrough;
|
|
bool Monospace;
|
|
bool Underline;
|
|
};
|
|
|
|
void reallocBuffer(Buffer *buffer) {
|
|
buffer->size += 1024;
|
|
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++;
|
|
checkBufSafety(buffer);
|
|
}
|
|
}
|
|
|
|
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++;
|
|
}
|
|
}
|
|
|
|
void appendCharToBuffer(char character, Buffer *buffer) {
|
|
buffer->data[buffer->position] = character;
|
|
buffer->position++;
|
|
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;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
(void)argv; /* this stops it from yelling at me :) */
|
|
|
|
if (argc > 1) {
|
|
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 = {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;
|
|
|
|
getFormattingAndPrint(character,&close);
|
|
}
|
|
puts("");
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
}
|