2017-10-16 18:09:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2020-01-21 13:58:06 +01:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
#define KiB * 1024
|
2020-01-21 13:58:06 +01:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
void read_line(FILE *file, char *line_buffer) {
|
2018-01-27 21:36:11 +01:00
|
|
|
int ch, count;
|
2017-10-16 18:09:39 +02:00
|
|
|
if (file == NULL || line_buffer == NULL)
|
|
|
|
{
|
|
|
|
printf("Error: file or line buffer pointer is null.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-01-27 21:36:11 +01:00
|
|
|
ch = getc(file);
|
|
|
|
count = 0;
|
2017-10-16 18:09:39 +02:00
|
|
|
while ((ch != '\n') && (ch != EOF)) {
|
|
|
|
line_buffer[count] = ch;
|
|
|
|
count++;
|
|
|
|
ch = getc(file);
|
|
|
|
}
|
|
|
|
line_buffer[count] = '\0';
|
|
|
|
}
|
2020-01-21 13:58:06 +01:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
char source[16 KiB];
|
2020-01-21 13:58:06 +01:00
|
|
|
|
|
|
|
int fenced(char *s, int include_tests)
|
2017-10-16 18:09:39 +02:00
|
|
|
{
|
|
|
|
int a = strcmp(s, "```");
|
|
|
|
int b = strcmp(s, "~~~");
|
2020-01-21 13:58:06 +01:00
|
|
|
if (a == 0 && include_tests == 1) return 1;
|
2017-10-16 18:09:39 +02:00
|
|
|
if (b == 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-21 13:58:06 +01:00
|
|
|
|
|
|
|
void extract(char *fname, int include_tests) {
|
2017-10-16 18:09:39 +02:00
|
|
|
char *buffer = (char *)source;
|
|
|
|
char fence[4];
|
|
|
|
FILE *fp;
|
|
|
|
int inBlock;
|
|
|
|
inBlock = 0;
|
|
|
|
fp = fopen(fname, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
return;
|
|
|
|
while (!feof(fp)) {
|
|
|
|
read_line(fp, buffer);
|
|
|
|
strncpy(fence, buffer, 3);
|
|
|
|
fence[3] = '\0';
|
2020-01-21 13:58:06 +01:00
|
|
|
if (fenced(fence, include_tests)) {
|
2017-10-16 18:09:39 +02:00
|
|
|
if (inBlock == 0)
|
|
|
|
inBlock = 1;
|
|
|
|
else
|
|
|
|
inBlock = 0;
|
|
|
|
} else {
|
|
|
|
if ((inBlock == 1) && (strlen(buffer) != 0))
|
|
|
|
printf("%s\n", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2020-01-21 13:58:06 +01:00
|
|
|
|
2017-10-16 18:09:39 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i = 1;
|
2020-01-21 13:58:06 +01:00
|
|
|
int tests = 0;
|
2017-10-16 18:09:39 +02:00
|
|
|
if (argc > 1) {
|
|
|
|
while (i < argc) {
|
2020-01-21 13:58:06 +01:00
|
|
|
if (strcmp(argv[i], "-t") == 0) {
|
|
|
|
tests = 1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
extract(argv[i++], tests);
|
2017-10-16 18:09:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2020-01-21 13:58:06 +01:00
|
|
|
printf("unu\n(c) 2013-2020 charles childers\n\nTry:\n %s filename\n", argv[0]);
|
2017-10-16 18:09:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|