2017-10-16 18:09:39 +02:00
|
|
|
# Unu
|
|
|
|
|
|
|
|
unu
|
|
|
|
(verb) (-hia) pull out, withdraw, draw out, extract.
|
|
|
|
|
2018-11-04 22:57:09 +01:00
|
|
|
Unu is a tool for extracting fenced code blocks from Markdown
|
|
|
|
documents.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:57:09 +01:00
|
|
|
I always found documenting my projects annoying. Eventually I
|
|
|
|
decided to start mixing the code and commentary using Markdown.
|
|
|
|
Unu is the tool I use to extract the sources from the original
|
|
|
|
files. I've found that this makes it easier for me to keep the
|
|
|
|
commentary up to date, and has lead to better commented code.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
## The Code
|
|
|
|
|
2018-11-04 22:57:09 +01:00
|
|
|
First, headers:
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
I use this for readability purposes.
|
|
|
|
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
#define KiB * 1024
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:53:44 +01:00
|
|
|
The `read_line` function is intended to read in a single line
|
|
|
|
into a target buffer. This considers a line to end with either
|
|
|
|
a `\n` or EOF.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 15:02:14 +02: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';
|
|
|
|
}
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:53:44 +01:00
|
|
|
The line buffer needs to be big enough for the longest lines in
|
|
|
|
your source files. Here it's set to 16KiB, which suffices for
|
|
|
|
everything I've used Unu with so far.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
char source[16 KiB];
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:53:44 +01:00
|
|
|
Unu looks for Markdown style fenced blocks of code. It supports
|
|
|
|
both backticks and tildes for this. This will return `1` if the
|
|
|
|
line appears to be a start/stop of a fence, or `0` otherwise.
|
|
|
|
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
int fenced(char *s)
|
|
|
|
{
|
|
|
|
int a = strcmp(s, "```");
|
|
|
|
int b = strcmp(s, "~~~");
|
|
|
|
if (a == 0) return 1;
|
|
|
|
if (b == 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-11-04 22:53:44 +01:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:53:44 +01:00
|
|
|
The actual `extract` function is straightforward.
|
|
|
|
|
|
|
|
* open the file
|
|
|
|
* read each line
|
|
|
|
* if in a fenced region, write the lines to stdout
|
|
|
|
* close the file
|
|
|
|
|
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
void extract(char *fname) {
|
|
|
|
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';
|
|
|
|
if (fenced(fence)) {
|
|
|
|
if (inBlock == 0)
|
|
|
|
inBlock = 1;
|
|
|
|
else
|
|
|
|
inBlock = 0;
|
|
|
|
} else {
|
|
|
|
if ((inBlock == 1) && (strlen(buffer) != 0))
|
|
|
|
printf("%s\n", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2018-11-04 22:53:44 +01:00
|
|
|
And finally, the main routine, which just runs `extract` on
|
|
|
|
each specified file.
|
2017-10-16 18:09:39 +02:00
|
|
|
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|
2017-10-16 18:09:39 +02:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int i = 1;
|
|
|
|
if (argc > 1) {
|
|
|
|
while (i < argc) {
|
|
|
|
extract(argv[i++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-01-03 15:13:05 +01:00
|
|
|
printf("unu\n(c) 2013-2019 charles childers\n\nTry:\n %s filename\n", argv[0]);
|
2017-10-16 18:09:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-20 15:02:14 +02:00
|
|
|
~~~
|