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
|
#include <stdio.h>
#include <string.h>
#include "aatree.h"
#include "dawk.h"
#include "make_doc.h"
#define MAX_NAMES 64
static const char *SECTION = "3alleg5";
static const char *MANUAL = "Allegro reference manual";
static const char *SUFFIX = "Allegro 5 API";
static dstr last_header;
static dstr names[MAX_NAMES];
static void man_page_header(const char *name)
{
d_printf("%% %s(%s) %s\n", name, SECTION, MANUAL);
d_print("# NAME");
d_print(name);
d_printf(" \\- %s\n\n", SUFFIX);
d_print("# SYNOPSIS");
d_print("~~~~c");
d_print(last_header);
d_print(lookup_prototype(name));
d_print("~~~~");
d_print("# DESCRIPTION");
}
static void call_pandoc_for_man(const char *input, int name_count)
{
dstr output_filename;
int i;
for (i = 0; i < name_count; i++) {
sprintf(output_filename, "%s.%s", names[i], SECTION);
call_pandoc(input, output_filename, "--standalone");
}
}
void make_man_pages(int argc, char *argv[])
{
int output_level = 0;
int name_count = 0;
dstr line;
d_init(argc, argv);
while (d_getline(line)) {
/* Stop outputting the current man page at the first line beginning with
* the same or few number of hashes, i.e. at the same level or above.
*/
if (d_match(line, "^(#+) ") && output_level > 0) {
int n = strlen(d_submatch(1));
if (n <= output_level) {
d_close_output();
call_pandoc_for_man(tmp_preprocess_output, name_count);
name_count = 0;
output_level = 0;
}
}
if (d_match(line, "^ *(#include.*)") && output_level == 0) {
d_assign(last_header, d_submatch(1));
continue;
}
if (d_match(line, "^(#+) API: *(.*)")) {
const char *hashes = d_submatch(1);
const char *name = d_submatch(2);
if (output_level == 0) {
output_level = strlen(hashes);
d_open_output(tmp_preprocess_output);
man_page_header(name);
}
else {
d_printf("%s %s\n", hashes, name);
}
if (name_count < MAX_NAMES) {
d_assign(names[name_count], name);
name_count++;
}
continue;
}
if (!output_level) {
continue;
}
if (d_match(line, "^\\*?Return [Vv]alue:\\*?")) {
d_print("# RETURN VALUE");
d_assign(line, d_after_match);
}
if (d_match(line, "^\\*?Since:\\*?")) {
d_print("# SINCE");
d_assign(line, d_after_match);
}
if (d_match(line, "^\\*?See [Aa]lso:\\*?")) {
d_print("# SEE ALSO");
d_assign(line, d_after_match);
}
/* Replace [al_foo] and [ALLEGRO_foo] by al_foo(3) and ALLEGRO_foo(3). */
while (d_match(line, "\\[((al_|ALLEGRO_)[^\\]]*)\\]")) {
d_printf("%s", d_before_match);
d_printf("%s(%s)", d_submatch(1), SECTION);
d_assign(line, d_after_match);
}
d_print(line);
}
if (output_level > 0) {
d_close_output();
call_pandoc_for_man(tmp_preprocess_output, name_count);
name_count = 0;
output_level = 0;
}
}
/* vim: set sts=3 sw=3 et: */
|