Welcome to little lamb

Code » qmdoc » commit 5b41f19

Add -a/--author and some metas + <footer>

author Olivier Brunel
2022-12-28 09:33:36 UTC
committer Olivier Brunel
2022-12-28 09:33:36 UTC
parent 91b3bb994225a90f87eb74d0f4c7f4151f357993

Add -a/--author and some metas + <footer>

main.c +30 -3
qmdoc.h +9 -0

diff --git a/main.c b/main.c
index 8c1405a..9a0b1bf 100644
--- a/main.c
+++ b/main.c
@@ -6,6 +6,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <time.h>
 #include <getopt.h>
 #include <errno.h>
 #include <skalibs/strerr2.h>
@@ -72,6 +73,7 @@ struct ctx {
     int cur_page;
     struct {
         const char *title;
+        const char *author;
         int flags;
     } doc;
     struct {
@@ -264,7 +266,12 @@ enter_block(MD_BLOCKTYPE type, void *details, void *ctx_)
             {
 #define str_title(i)    ctx->sa.s + ctx->pages[i].titleoff
 #define str_file(i)    ctx->sa.s + ctx->pages[i].fileoff
+                size_t authlen = strlen(ctx->doc.author);
                 if (!raw_str(ctx, "<!DOCTYPE html>\n<html><head>")
+                        || !raw_str(ctx, metas)
+                        || (authlen && (!raw_str(ctx, "<meta name=\"author\" content=\"")
+                                        || !escape_text(ctx, ctx->doc.author, authlen)
+                                        || !raw_str(ctx, "\">")))
                         || !raw_str(ctx, "<title>")
                         || !escape_text(ctx, str_title(ctx->cur_page),
                                         strlen(str_title(ctx->cur_page)))
@@ -454,8 +461,21 @@ leave_block(MD_BLOCKTYPE type, void *details, void *ctx_)
 
     switch (type) {
         case MD_BLOCK_DOC:
+            ;
+            char year[UINT32_FMT];
+            struct timespec ts;
+            clock_gettime(CLOCK_REALTIME, &ts);
+            struct tm tm;
+            localtime_r(&ts.tv_sec, &tm);
+            year[uint32_fmt(year, (uint32) 1900 + tm.tm_year)] = '\0';
             if (((ctx->doc.flags & DOC_HAS_TITLE) && !raw_str(ctx, "</section>"))
-                    || !raw_str(ctx, "</body></html>"))
+                    || !raw_str(ctx, "<footer>Copyright &copy; ")
+                    || !raw_str(ctx, year)
+                    || !raw_str(ctx, " ")
+                    || !escape_text(ctx, ctx->doc.author, strlen(ctx->doc.author))
+                    || !raw_str(ctx, " - All Rights Reserved<br>"
+                                "<span class=\"generated\">Generated with qmdoc</span>"
+                                "</footer></body></html>"))
                 return ERR_PARSER_LEAVE_BLOCK;
             break;
 
@@ -920,7 +940,8 @@ load_page_from_file(const char *file, struct page *page, stralloc *sa)
 static void
 help(void)
 {
-    outh(" -C, --no-css                 Do not use CSS\n"
+    outh(" -a, --author AUTHOR          Set AUTHOR as author (in meta in footer)\n"
+         " -C, --no-css                 Do not use CSS\n"
          " -d, --destdir DIR            Write files into DIR\n"
          " -h, --help                   Show this help screen and exit\n"
          " -I, --inline-css             Use inline CSS instead of external files\n"
@@ -944,9 +965,11 @@ main (int argc, char *argv[])
     int options = 0;
     char *destdir = ".";
     const char *title = "Documentation";
+    const char *author = "";
 
     int c;
     struct option opts[] = {
+        { "author",         required_argument,  NULL,   'a' },
         { "no-css",         no_argument,        NULL,   'C' },
         { "destdir",        required_argument,  NULL,   'd' },
         { "help",           no_argument,        NULL,   'h' },
@@ -954,7 +977,10 @@ main (int argc, char *argv[])
         { "title",          required_argument,  NULL,   't' },
         { NULL,             0,                  NULL,    0  },
     };
-    while ((c = getopt_long(argc, argv, "Cd:hIt:", opts, NULL)) != -1) switch (c) {
+    while ((c = getopt_long(argc, argv, "a:Cd:hIt:", opts, NULL)) != -1) switch (c) {
+        case 'a':
+            author = optarg;
+            break;
         case 'C':
             options |= OPT_NO_CSS;
             break;
@@ -988,6 +1014,7 @@ main (int argc, char *argv[])
         .pages = pages,
         .nb_pages = sizeof(pages) / sizeof(*pages),
         .doc.title = title,
+        .doc.author = author,
         .buf.sa = STRALLOC_ZERO,
     };
 
diff --git a/qmdoc.h b/qmdoc.h
index 78730e7..b10c8c2 100644
--- a/qmdoc.h
+++ b/qmdoc.h
@@ -12,4 +12,13 @@ enum {
     ERR_MISC        = -88,
 };
 
+const char metas[] =
+"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"
+"<meta charset=\"utf-8\">"
+"<meta name=\"generator\" content=\"qmdoc\">"
+"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
+"<meta name=\"theme-color\" media=\"(prefers-color-scheme: light)\" content=\"white\">"
+"<meta name=\"theme-color\" media=\"(prefers-color-scheme: dark)\" content=\"gray\">"
+;
+
 #endif /* QMDOC_H */