about summary refs log tree commit diff stats
path: root/src/command/cmd_defs.c
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-12-04 17:29:31 +0100
committerMichael Vetter <jubalh@iodoru.org>2020-12-04 17:29:31 +0100
commit5e99a791e62d77e99b3e313947b8c1f00587a7e6 (patch)
treecaa4d8c735c9c3f81976a9ec3996578d67715781 /src/command/cmd_defs.c
parent35aecd425fad6697e9cf72832cb287a156ec7942 (diff)
downloadprofani-tty-5e99a791e62d77e99b3e313947b8c1f00587a7e6.tar.gz
Create cmd to generate man pages for prof commands
`profanity mangen` will create for each command (`/account`, `/roster`)
an own manpage (`profanity-account.1`, `profanity-roster.1`)

See https://github.com/profanity-im/profanity/issues/1444

Needs some polishing formatting wise.
Diffstat (limited to 'src/command/cmd_defs.c')
-rw-r--r--src/command/cmd_defs.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index 53c42bae..e12fdb8a 100644
--- a/src/command/cmd_defs.c
+++ b/src/command/cmd_defs.c
@@ -2833,3 +2833,70 @@ command_docgen(void)
     printf("\nProcessed %d commands.\n\n", g_list_length(cmds));
     g_list_free(cmds);
 }
+
+void
+command_mangen(void)
+{
+    GList* cmds = NULL;
+
+    for (unsigned int i = 0; i < ARRAY_SIZE(command_defs); i++) {
+        Command* pcmd = command_defs + i;
+        cmds = g_list_insert_sorted(cmds, pcmd, (GCompareFunc)_cmp_command);
+    }
+
+    mkdir_recursive("docs");
+
+    GList* curr = cmds;
+    while (curr) {
+        Command* pcmd = curr->data;
+
+        char* filename = NULL;
+        if (asprintf(&filename, "docs/profanity-%s.1", &pcmd->cmd[1]) == -1) {
+            // TODO: error
+            return;
+        }
+        FILE* manpage = fopen(filename, "w");
+        free(filename);
+
+        fputs(".TH man 1 \"2020-07-01\" \""PACKAGE_VERSION"\" \"Profanity XMPP client\"\n", manpage);
+        fputs(".SH NAME\n", manpage);
+        fprintf(manpage, "%s\n", pcmd->cmd);
+
+        fputs("\n.SH DESCRIPTION\n", manpage);
+        fprintf(manpage, "%s\n", pcmd->help.desc);
+
+        fputs("\n.SH SYNOPSIS\n", manpage);
+        int i = 0;
+        while (pcmd->help.synopsis[i]) {
+            fprintf(manpage, "%s\n", pcmd->help.synopsis[i]);
+            fputs("\n.BR\n", manpage);
+            i++;
+        }
+
+        if (pcmd->help.args[0][0] != NULL) {
+            fputs("\n.SH ARGUMENTS\n", manpage);
+            for (i = 0; pcmd->help.args[i][0] != NULL; i++) {
+                fprintf(manpage, "%s\n", pcmd->help.args[i][0]);
+                fprintf(manpage, "%s\n", pcmd->help.args[i][1]);
+                fputs("\n.BR\n", manpage);
+            }
+        }
+
+        if (pcmd->help.examples[0] != NULL) {
+            fputs("\n.SH EXAMPLES\n", manpage);
+            int i = 0;
+            while (pcmd->help.examples[i]) {
+                fprintf(manpage, "%s\n", pcmd->help.examples[i]);
+                fputs("\n.BR\n", manpage);
+                i++;
+            }
+        }
+
+        curr = g_list_next(curr);
+
+        fclose(manpage);
+    }
+
+    printf("\nProcessed %d commands.\n\n", g_list_length(cmds));
+    g_list_free(cmds);
+}