md toc generator

This commit is contained in:
olikraus 2015-12-25 17:48:37 +01:00
parent d062ba07de
commit de10df3f24
1 changed files with 113 additions and 5 deletions

View File

@ -8,6 +8,7 @@
#define TOC_ENTRIES 512
#define TOC_ENTRY_SIZE 1024
char toc_start[] = "[tocstart]: # (toc start)";
char toc_end[] = "[tocend]: # (toc end)";
@ -15,13 +16,66 @@ char toc[TOC_ENTRIES][TOC_ENTRY_SIZE];
int toc_level[TOC_ENTRIES];
int toc_cnt = 0;
char *getentry(const char *s)
{
static char buf[TOC_ENTRY_SIZE];
char *t = buf;
for(;;)
{
if ( *s == '\0' )
break;
if ( *s >= ' ' )
{
*t++ = *s++;
}
else
{
s++;
}
}
*t = '\0';
return buf;
}
char *getref(const char *s)
{
static char buf[TOC_ENTRY_SIZE];
char *t = buf;
for(;;)
{
if ( *s == '\0' )
break;
if ( *s == ' ' )
{
*t++ = '-';
s++;
}
else if ( *s >= '0' && *s <= '9' )
{
*t++ = *s++;
}
else if ( *s < 'A' )
{
s++;
}
else
{
*t = tolower(*s);
t++;
s++;
}
}
*t = '\0';
return buf;
}
void add_toc(int level, char *entry)
{
strncpy( toc[toc_cnt], entry, TOC_ENTRY_SIZE);
toc[toc_cnt][TOC_ENTRY_SIZE-1] = '\0';
toc_level[toc_cnt] = level;
toc_cnt++;
printf("%d: %s\n", level, entry);
// printf("%d: %s [%s](#%s)\n", level, entry, getentry(entry), getref(entry));
}
void read_md_fp(FILE *in, FILE *out)
@ -37,22 +91,21 @@ void read_md_fp(FILE *in, FILE *out)
return;
if ( is_inside_toc != 0 )
{
if ( strcmp(s, toc_end) == 0 )
if ( strncmp(s, toc_end, strlen(toc_end)) == 0 )
{
is_inside_toc = 0;
}
}
else
{
if ( buf[0] == '\0' )
if ( buf[0] == '\0' || buf[0] == '\n' )
{
/* skip empty lines at the beginning of the file */
if ( is_start_of_file == 0 )
fputs(buf, out);
}
else if ( strcmp(s, toc_start) == 0 )
else if ( strncmp(s, toc_start, strlen(toc_start)) == 0 )
{
is_start_of_file = 0;
is_inside_toc = 1;
}
else if ( strncmp(s, "# ", 2) == 0 )
@ -101,6 +154,59 @@ void read_md(const char *in_name, const char *out_name)
fclose(out);
}
void copy_to_md_fp(FILE *tmp, FILE *out)
{
static char buf[TOC_ENTRY_SIZE];
char *s;
int i, j;
fprintf(out, "\n");
fprintf(out, "%s\n", toc_start);
fprintf(out, "\n");
for( i = 0; i < toc_cnt; i++ )
{
for( j = 0; j < toc_level[i]; j++)
fprintf(out, " ");
fprintf(out, "* [%s](#%s)\n", getentry(toc[i]), getref(toc[i]));
}
fprintf(out, "\n");
fprintf(out, "%s\n", toc_end);
fprintf(out, "\n");
for(;;)
{
s = fgets(buf, TOC_ENTRY_SIZE, tmp);
if ( s == NULL )
return;
fputs(buf, out);
}
}
void copy_to_md(const char *tmp_name, const char *out_name)
{
FILE *tmp;
FILE *out;
tmp = fopen(tmp_name, "r");
if ( tmp == NULL )
{
perror(tmp_name);
return;
}
out = fopen(out_name, "w");
if ( out == NULL )
{
perror(out_name);
return;
}
copy_to_md_fp(tmp, out);
fclose(tmp);
fclose(out);
}
int main(int argc, char **argv)
{
if ( argc <= 1 )
@ -113,6 +219,8 @@ int main(int argc, char **argv)
{
toc_cnt = 0;
read_md(*argv, "tmp.md");
copy_to_md("tmp.md", *argv);
printf("%s: added %d toc entries\n", *argv, toc_cnt);
argv++;
}
}