Homepage

Aggiornamenti vari

English automatic translation: Various Updates

Ho dato due esami negli scorsi due giorni e posso dirmi soddisfatto dei risultati. Ho anche portato degli aggiornamenti al blog: ho automatizzato la creazione dei post. Ho scritto il seguente 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);

L'invocazione e l'output standard del programma saranno le seguenti:

./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 pratica, prende in input le informazioni fondamentali sul post (id, titolo, data, tags), genera il post in formato html, e stampa una rappresentazione JSON delle informazioni, di questo tipo:

 30   {
 31     "id": 5,
 32     "title": "Exam day!",
 33     "date": "20/04/2023",
 34     "tags": ["general", "uneventful"]
 35   }

All'inizio ho pensato di fare uno script in bash, ma poi ho deciso di usare C, con cui mi trovo più a mio agio. Nel particolare, scrivo in una stringa chiamata command il comando da invocare, e poi uso system() per eseguirlo come sottoprocesso. In particolare:

"markdown %s > %s", infile, temp_name

Prende in input il file markdown, genera un file html che viene salvato in "temp".

"cat p_start.html %s p_end.html > %s", temp_name, outfile

concatena il template di inizio post, il file html temporaneo, e il file di fine post, per ottenere outfile (il file html definitivo).

"ex -s -c '%%s/<title>InsertTitleHere<\\/title>/<title>%s<\\/title>/|x' %s", title, outfile

Per inserire il titolo all'interno dell'HTML, chiama l'editor di testo vim in background, con l'opzione -s che richiede la silent mode, e l'opzione -c che indica che sto scrivendo un comando. Il comando eseguito da vim è :%s/<title>InsertTitleHere<\/title>/<title>Titolo inserito<\/title/. |x richiede che, se la sostituzione è stata effettuata, allora si salvi il file. Alla fine si rimuove il file temporaneo.

L'oggetto javascript che viene stampato verrà poi copiato e incollato da me su postlist.js.

A presto,

21/04/2023