336 lines
5.8 KiB
C
336 lines
5.8 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#ifndef TESTGEMPARSE
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <gemgui.h>
|
|
#endif
|
|
|
|
#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 (FILE *fileToParse);
|
|
|
|
#else
|
|
|
|
extern GtkWidget *render;
|
|
extern char links[1024][20];
|
|
|
|
#endif
|
|
|
|
int
|
|
parseFile (FILE *fileToParse)
|
|
{
|
|
char line[4096];
|
|
#ifndef TESTGEMPARSE
|
|
int linkNumber = 0;
|
|
#endif
|
|
|
|
rewind (fileToParse);
|
|
|
|
if (fileToParse == NULL)
|
|
{
|
|
#ifdef TESTGEMPARSE
|
|
printf("error");
|
|
#else
|
|
addH1 (render, "An error occured");
|
|
addText (render, "Can't open the file");
|
|
#endif
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addH3 (line + i);
|
|
#else
|
|
addH3 (render, line + i);
|
|
#endif
|
|
}
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addH2 (line + i);
|
|
#else
|
|
addH2 (render, line + i);
|
|
#endif
|
|
}
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addH1 (line + i);
|
|
#else
|
|
addH1 (render, line + i);
|
|
#endif
|
|
}
|
|
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] != ' ' && line[beginUrl + urlLen] != '\n')
|
|
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++;
|
|
|
|
#ifndef TESTGEMPARSE
|
|
strcpy (links[linkNumber], line + beginUrl);
|
|
#endif
|
|
|
|
//If not text, text = url
|
|
if (beginText == size)
|
|
#ifdef TESTGEMPARSE
|
|
addLink (line + beginUrl, line + beginUrl);
|
|
#else
|
|
addLink (render, links[linkNumber], links[linkNumber]);
|
|
#endif
|
|
else
|
|
{
|
|
//Remove tailing '\n'
|
|
line[size - 1] = '\0';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addLink (line + beginUrl, line + beginText);
|
|
#else
|
|
addLink (render, links[linkNumber], line + beginText);
|
|
#endif
|
|
}
|
|
|
|
#ifndef TESTGEMPARSE
|
|
linkNumber++;
|
|
#endif
|
|
}
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addCode (line + 3, code);
|
|
#else
|
|
addCode (render, code, line + 3);
|
|
#endif
|
|
}
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addUList (text);
|
|
#else
|
|
addUList (render, text);
|
|
#endif
|
|
}
|
|
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';
|
|
|
|
#ifdef TESTGEMPARSE
|
|
addQuote (line + i);
|
|
#else
|
|
addQuote (render, line + i);
|
|
#endif
|
|
}
|
|
else
|
|
#ifdef TESTGEMPARSE
|
|
printf ("%s", line);
|
|
#else
|
|
addText (render, line);
|
|
#endif
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef TESTGEMPARSE
|
|
|
|
int
|
|
main (int argc,
|
|
char **argv)
|
|
{
|
|
if (argc >= 2)
|
|
{
|
|
FILE *file = fopen (argv[1], "r");
|
|
if (file == NULL)
|
|
return 1;
|
|
|
|
return parseFile (file);
|
|
}
|
|
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
|