Various updates
Original Italian: Aggiornamenti vari (automatic translation)
I took two exams in the past two days and I am satisfied with the results. I also brought updates to the blog: I automated the creation of posts. I wrote the following script in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strdup(const char *s);
int main(int argc, char **argv){
if(argc < 6){
printf("Usage: %s inputfile.md outputfile.html id title date\n", argv[0]);
exit(1);
}
char *infile = argv[1];
char *outfile = argv[2];
int id = atoi(argv[3]);
char *title = argv[4];
char *date = argv[5];
printf("Tags:\n");
char * tags = malloc(sizeof(char)*101);
scanf("%100[^\n]", tags);
char *temp_name = "temp.html";
char command[1000];
sprintf(command, "markdown %s > %s", infile, temp_name);
puts(command);
system(command);
sprintf(command, "cat p_start.html %s p_end.html > %s", temp_name, outfile);
puts(command);
system(command);
sprintf(command, "ex -s -c '%%s/<title>InsertTitleHere<\\/title>/<title>%s<\\/title>/|x' %s", title, outfile);
puts(command);
system(command);
sprintf(command, "rm %s", temp_name);
puts(command);
system(command);
printf("\n{\n\t\"id\": %d,\n\t\"title\": \"%s\",\n\t\"date\": \"%s\",\n\t\"tags\": [", id, title, date);
char *e = strtok(tags, " ");
if(e != NULL){
printf("\"%s\"", e);
e = strtok(NULL, " ");
}
while(e != NULL){
printf(", \"%s\"", e);
e = strtok(NULL, " ");
}
printf("]\n}\n");
free(tags);
The invocation and standard output of the program will be as follows:
./gen.out post1_eng.md post1_eng.html 1 'Looking for "la romanina"' 16/04/2023
Inserisci tags
markdown post1_eng.md > temp.html
cat p_start.html temp.html p_end.html > post1_eng.html
ex -s -c '%s/<title>InsertTitleHere<\/title>/<title>Looking for "la romanina"<\/title>/|x' post1_eng.html
rm temp.html
{
"id": 1,
"title": "Looking for "la romanina"",
"date": "16/04/2023",
"tags": []
}
In practice, it takes the basic information about the post (id, title, date, tags) as input, generates the post in html format, and prints a JSON representation of the information, like this:
30 {
31 "id": 5,
32 "title": "Exam day!",
33 "date": "20/04/2023",
34 "tags": ["general", "uneventful"]
35 }
At first I thought about doing a bash script, but then I decided to use C, which I'm more comfortable with. In particular, I write the command to be invoked in a string called command, and then use system() to execute it as a subprocess. In particular:
"markdown %s > %s", infile, temp_name
It takes the markdown file as input, generates an html file which is saved in "temp".
"cat p_start.html %s p_end.html > %s", temp_name, outfile
This concatenates the start post template, the temporary html file, and the end post file, to get the outfile (the final html file).
"ex -s -c '%%s/<title>InsertTitleHere<\\/title>/<title>%s<\\/title>/|x' %s", title, outfile
To embed the title into the HTML, call the vim text editor in the background, with the -s option requesting silent mode, and the -c option indicating I'm typing a command. The command executed by vim is :%s/<title>InsertTitleHere<\/title>/<title>Insert Title<\/title/. |x requests that, if the replacement was successful, then save the file. Finally you remove the temporary file.
The javascript object that is printed will then be copied and pasted by me to postlist.js.
See you soon,
04/21/2023