scorpius/src/gemparse.c

265 lines
4.7 KiB
C

#include <stdio.h>
#include <string.h>
#ifdef TESTGEMPARSE
// specific test functions
void addH1 (const char *line);
void addH2 (const char *line);
void addH3 (const char *line);
void addLink (const char *url, const char *text);
void addCode (const char *meta, const char *code);
void addQuote (const char *text);
void addUList (const char *text);
// general functions
int parseFile (char *path);
#endif
int
parseFile (char *path)
{
FILE *fileToParse = fopen (path, "r");
char line[4096];
if (fileToParse == NULL)
return 1;
while (fgets (line, 4096, fileToParse) != NULL)
{
int size = strlen (line);
if (size > 3 && line[0] == '#' && line[1] == '#' && line[2] == '#')
{
int i = 3;
//Remove whitespace before the text
while (i < size && line[i] == ' ')
i++;
//Remove tailing '\n'
line[size - 1] = '\0';
addH3 (line + i);
}
else if (size > 2 && line[0] == '#' && line [1] == '#')
{
int i = 2;
//Remove whitespace before the text
while (i < size && line[i] == ' ')
i++;
//Remove tailing '\n'
line[size - 1] = '\0';
addH2 (line + i);
}
else if (size > 1 && line[0] == '#')
{
int i = 1;
//Remove whitespace before the text
while (i < size && line[i] == ' ')
i++;
//Remove tailing '\n'
line[size - 1] = '\0';
addH1 (line + i);
}
else if (size > 2 && line[0] == '=' && line[1] == '>')
{
int beginUrl = 2;
int urlLen = 0;
int beginText;
//Remove whitespace before the url
while (beginUrl < size && line[beginUrl] == ' ')
beginUrl++;
//Get URL lenght
while (beginUrl + urlLen < size && line[beginUrl + urlLen] != ' ')
urlLen++;
//mark end of url
line[beginUrl + urlLen] = '\0';
// text begin after the whitespace after url
beginText = urlLen + beginUrl + 1;
//Remove whitespace between URL and text
while (beginText < size && line[beginText] == ' ')
beginText++;
//If not text, text = url
if (beginText == size)
addLink (line + beginUrl, line + beginUrl);
else
{
//Remove tailing '\n'
line[size - 1] = '\0';
addLink (line + beginUrl, line + beginText);
}
}
else if (size > 3 && line[0] == '`' && line[1] == '`' && line[2] == '`')
{
char code[4096] = "", codeline[4096];
//Remove tailing '\n' for meta
line[size - 1] = '\0';
do
{
if (fgets (codeline, 4096, fileToParse) == NULL)
break;
size = strlen (codeline);
if (size < 3 || codeline[0] != '`' || codeline[1] != '`' || codeline[2] != '`')
strcat (code, codeline);
}
while (codeline[0] != '`' || codeline[1] != '`' || codeline[2] != '`');
// Remove tailing '\n' for both meta and code
size = strlen (code);
code[size - 1] = '\0';
addCode (line + 3, code);
}
else if (size > 1 && line[0] == '*')
{
char text[4096] = "";
do
{
char buffer[4096] = "";
int linePos = 1;
int bufferPos;
strcat (buffer, "");
bufferPos = strlen (buffer);
buffer[bufferPos++] = ' ';
// remove heading space
while (line[linePos] == ' ' && linePos < size)
linePos++;
while (line[linePos] != '\n' && linePos < size)
{
buffer[bufferPos] = line[linePos];
linePos++;
bufferPos++;
}
buffer[bufferPos] = '\n';
buffer[bufferPos + 1] = '\0';
strcat (text, buffer);
if (fgets (line, 4096, fileToParse) == NULL)
break;
size = strlen (line);
}
while (line[0] == '*');
//we seek the previous line
if (! feof (fileToParse))
fseek (fileToParse, -size, SEEK_CUR);
// Remove tailing '\n' for both meta and code
size = strlen(text);
text[size - 1] = '\0';
addUList (text);
}
else if (size > 1 && line[0] == '>')
{
int i = 1;
//Remove whitespace before the text
while (i < size && line[i] == ' ')
i++;
//Remove tailing '\n'
line[size - 1] = '\0';
addQuote (line + i);
}
else
printf ("%s", line);
}
fclose (fileToParse);
return 0;
}
#ifdef TESTGEMPARSE
int
main (int argc,
char **argv)
{
if (argc >= 2)
return parseFile (argv[1]);
else
{
fprintf (stderr, "USAGE: %s FILE\n", argv[0]);
return 1;
}
}
void
addH1 (const char *line)
{
printf ("h1 {\n%s\n}\n", line);
}
void
addH2 (const char *line)
{
printf ("h2 {\n%s\n}\n", line);
}
void
addH3 (const char *line)
{
printf ("h3 {\n%s\n}\n", line);
}
void
addLink (const char *url,
const char *text)
{
printf ("link {\n%s\n%s\n}\n", url, text);
}
void
addCode (const char *meta,
const char *code)
{
int i = 0;
printf ("code {\n%s\n%s\n}\n", meta, code);
}
void
addQuote (const char *text)
{
printf ("quote {\n%s\n}\n", text);
}
void
addUList (const char *text)
{
printf ("ulist {\n%s\n}\n", text);
}
#endif