1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/*
* mktypst - dumb typst document generator.
*
* generates a typst document that imports and applies an mktypst template,
* so you can start writing instead of pasting the same preamble every time.
*
* Author: hachem <im@hachem.wtf>
* Version: 0.1.0
*/
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define mktypst_program "mktypst"
#define mktypst_version "0.1.0"
#define mktypst_tagline "dumb typst document generator"
#define mktypst_package "@local/hachem:0.1.0"
enum Template
{
template_paper,
template_letter,
template_count
};
struct Template_Info
{
const char* key;
const char* name;
};
struct Config
{
const char* name;
enum Template tmpl;
const char* title;
const char* author;
const char* date;
int use_stdout;
int force;
};
static const struct Template_Info templates[template_count] =
{
[template_paper] = { .key = "paper", .name = "paper" },
[template_letter] = { .key = "letter", .name = "letter" },
};
static void print_version(void)
{
(void) printf("%s %s\n", mktypst_program, mktypst_version);
}
static void print_usage(FILE* stream)
{
(void) fprintf(
stream,
"%s %s - %s\n"
"\n"
"generates a typst document that imports and applies an mktypst\n"
"template, so you can start writing instead of pasting a preamble.\n"
"\n"
"usage: %s [-t template] [-T title] [-a author] [-d date] [-f] <name>\n"
" %s [-v | -h]\n"
"\n"
"options:\n"
" -t, --template NAME template to apply (default paper)\n"
" -T, --title TITLE document title (default: the name)\n"
" -a, --author NAME author name (default: git config user.name)\n"
" -d, --date DATE document date (default: current month)\n"
" -f, --force overwrite an existing file\n"
" --stdout write to stdout instead of a .typ file\n"
" -v, --version print version and bail\n"
" -h, --help print this and bail\n"
"\n"
"by hachem <im@hachem.wtf>\n",
mktypst_program, mktypst_version, mktypst_tagline,
mktypst_program, mktypst_program);
}
static void error(const char* message, const char* detail)
{
if (detail != NULL)
(void) fprintf(stderr, "%s: error: %s: %s\n", mktypst_program, message, detail);
else
(void) fprintf(stderr, "%s: error: %s\n", mktypst_program, message);
}
/* match a value-taking option. handles both "--opt val" and "--opt=val".
* returns 1 and sets *value on a match, -1 if the value is missing, else 0. */
static int opt_value(const char* arg, const char* shortname, const char* longname,
int argc, char** argv, int* i, const char** value)
{
if (strcmp(arg, shortname) == 0 || strcmp(arg, longname) == 0)
{
if (*i + 1 >= argc)
return -1;
*value = argv[++(*i)];
return 1;
}
size_t longlen = strlen(longname);
if (strncmp(arg, longname, longlen) == 0 && arg[longlen] == '=')
{
*value = arg + longlen + 1;
return 1;
}
return 0;
}
static int find_template(const char* key, enum Template* out)
{
for (enum Template tmpl = 0; tmpl < template_count; tmpl++)
if (strcmp(templates[tmpl].key, key) == 0)
{
*out = tmpl;
return 1;
}
return 0;
}
/* the project name with a trailing ".typ" trimmed, used as the default title. */
static const char* stem_of(const char* name)
{
static char buf[256];
(void) snprintf(buf, sizeof buf, "%s", name);
size_t len = strlen(buf);
if (len >= 4 && strcmp(buf + len - 4, ".typ") == 0)
buf[len - 4] = '\0';
return buf;
}
/* best-effort author: git's configured name, else $USER. */
static const char* author_name(void)
{
static char buf[256];
buf[0] = '\0';
FILE* pipe = popen("git config user.name", "r");
if (pipe != NULL)
{
if (fgets(buf, (int) sizeof buf, pipe) != NULL)
{
size_t len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
}
(void) pclose(pipe);
}
if (buf[0] == '\0')
{
const char* user = getenv("USER");
(void) snprintf(buf, sizeof buf, "%s", user != NULL ? user : "Author");
}
return buf;
}
static const char* current_date(void)
{
static char buf[64];
time_t now = time(NULL);
struct tm* local = localtime(&now);
if (local == NULL || strftime(buf, sizeof buf, "%B %Y", local) == 0)
(void) snprintf(buf, sizeof buf, "undated");
return buf;
}
static void write_paper(FILE* out, const struct Config* config)
{
(void) fprintf(
out,
"#import \"%s\": paper\n"
"\n"
"#show: paper.with(\n"
" title: \"%s\",\n"
" date: \"%s\",\n"
" authors: (\n"
" (name: \"%s\", location: []),\n"
" ),\n"
")\n"
"\n"
"= Introduction\n",
mktypst_package, config->title, config->date, config->author);
}
static void write_letter(FILE* out, const struct Config* config)
{
(void) fprintf(
out,
"#import \"%s\": letter\n"
"\n"
"#show: letter.with(\n"
" sender: (\n"
" name: [%s],\n"
" address: [Votre adresse \\ Code postal, ville],\n"
" ),\n"
" recipient: (\n"
" name: [Destinataire],\n"
" address: [Son adresse \\ Code postal, ville],\n"
" ),\n"
" place: \"Paris\",\n"
" date: \"%s\",\n"
" subject: [%s],\n"
")\n"
"\n"
"Corps de la lettre.\n",
mktypst_package, config->author, config->date, config->title);
}
static void write_document(FILE* out, const struct Config* config)
{
switch (config->tmpl)
{
case template_paper:
write_paper(out, config);
break;
case template_letter:
write_letter(out, config);
break;
default:
break;
}
}
static void output_path(const char* name, char* buf, size_t size)
{
size_t len = strlen(name);
if (len >= 4 && strcmp(name + len - 4, ".typ") == 0)
(void) snprintf(buf, size, "%s", name);
else
(void) snprintf(buf, size, "%s.typ", name);
}
static int generate(const struct Config* config)
{
if (config->use_stdout)
{
write_document(stdout, config);
return 0;
}
char path[1024];
output_path(config->name, path, sizeof path);
if (!config->force && access(path, F_OK) == 0)
{
error("file already exists (use -f to overwrite)", path);
return -1;
}
FILE* file = fopen(path, "w");
if (file == NULL)
{
error("cannot create file", path);
return -1;
}
write_document(file, config);
if (fclose(file) != 0)
{
error("cannot write file", path);
return -1;
}
(void) printf("created %s (%s)\n", path, templates[config->tmpl].name);
(void) printf(" typst watch %s\n", path);
return 0;
}
static int parse_args(int argc, char** argv, struct Config* config)
{
const char* template_arg = NULL;
const char* name = NULL;
for (int i = 1; i < argc; i++)
{
const char* arg = argv[i];
if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0)
{
print_usage(stdout);
return -1;
}
if (strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0)
{
print_version();
return -1;
}
if (strcmp(arg, "--stdout") == 0)
{
config->use_stdout = 1;
continue;
}
if (strcmp(arg, "-f") == 0 || strcmp(arg, "--force") == 0)
{
config->force = 1;
continue;
}
int matched = 0;
matched = opt_value(arg, "-t", "--template", argc, argv, &i, &template_arg);
if (matched != 0)
{
if (matched < 0) { error("missing argument for", arg); return 1; }
continue;
}
matched = opt_value(arg, "-T", "--title", argc, argv, &i, &config->title);
if (matched != 0)
{
if (matched < 0) { error("missing argument for", arg); return 1; }
continue;
}
matched = opt_value(arg, "-a", "--author", argc, argv, &i, &config->author);
if (matched != 0)
{
if (matched < 0) { error("missing argument for", arg); return 1; }
continue;
}
matched = opt_value(arg, "-d", "--date", argc, argv, &i, &config->date);
if (matched != 0)
{
if (matched < 0) { error("missing argument for", arg); return 1; }
continue;
}
if (arg[0] == '-' && arg[1] != '\0')
{
error("unknown option", arg);
return 1;
}
if (name != NULL)
{
error("unexpected extra argument", arg);
return 1;
}
name = arg;
}
if (name == NULL)
{
error("missing document name", NULL);
return 1;
}
config->name = name;
if (template_arg != NULL && !find_template(template_arg, &config->tmpl))
{
error("unknown template", template_arg);
return 1;
}
if (config->title == NULL)
config->title = stem_of(name);
if (config->author == NULL)
config->author = author_name();
if (config->date == NULL)
config->date = current_date();
return 0;
}
int main(int argc, char** argv)
{
(void) setlocale(LC_TIME, "");
struct Config config =
{
.name = NULL,
.tmpl = template_paper,
.title = NULL,
.author = NULL,
.date = NULL,
.use_stdout = 0,
.force = 0,
};
int status = parse_args(argc, argv, &config);
if (status < 0)
return EXIT_SUCCESS;
if (status > 0)
{
print_usage(stderr);
return EXIT_FAILURE;
}
if (generate(&config) != 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
|