implement --fragment option

Mon, 03 Feb 2025 23:13:19 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 03 Feb 2025 23:13:19 +0100
changeset 20
8639ccd855ba
parent 19
2c128952f198
child 21
bc8b76ca9ee9

implement --fragment option

src/html.cpp file | annotate | diff | comparison | revisions
src/html.h file | annotate | diff | comparison | revisions
src/main.cpp file | annotate | diff | comparison | revisions
src/settings.h file | annotate | diff | comparison | revisions
--- a/src/html.cpp	Sat Feb 01 17:15:14 2025 +0100
+++ b/src/html.cpp	Mon Feb 03 23:13:19 2025 +0100
@@ -67,9 +67,12 @@
     }
 }
 
-void html::open() {
-    puts(
-R"(<html>
+void html::open(bool fragment) {
+    if (fragment) {
+        puts("<div class=\"heatmap-content\">");
+        indentation = 1;
+    } else {
+        puts(R"(<html>
     <head>
         <style>
             table.heatmap {
@@ -116,12 +119,18 @@
             }
         </style>
     </head>
-    <body>)");
-    indentation = 2;
+    <body>
+        <div class=\"heatmap-content\">)");
+        indentation = 3;
+    }
 }
 
-void html::close() {
-    puts("\t</body>\n</html>");
+void html::close(bool fragment) {
+    if (fragment) {
+        puts("</div>");
+    } else {
+        puts("\t\t</div>\n\t</body>\n</html>");
+    }
 }
 
 void html::h1(const std::string& heading) {
--- a/src/html.h	Sat Feb 01 17:15:14 2025 +0100
+++ b/src/html.h	Mon Feb 03 23:13:19 2025 +0100
@@ -32,8 +32,8 @@
 
     static constexpr unsigned columns = 53;
 
-    void open();
-    void close();
+    void open(bool fragment);
+    void close(bool fragment);
 
     void h1(const std::string& heading);
     void h2(const std::string& heading);
--- a/src/main.cpp	Sat Feb 01 17:15:14 2025 +0100
+++ b/src/main.cpp	Mon Feb 03 23:13:19 2025 +0100
@@ -43,8 +43,9 @@
         "   -a, --author <name>       Only report this author\n"
         "                             (repeat option to report multiple authors)\n"
         "   -A, --authormap <file>    Apply an author mapping file\n"
+        "   -d, --depth <num>         The search depth (default: 1, max: 255)\n"
+        "   -f, --fragment            Output as fragment\n"
         "   -h, --help                Print this help message\n"
-        "   -d, --depth <num>         The search depth (default: 1, max: 255)\n"
         "   -n, --no-pull             Do not pull the repositories\n"
         "   -s, --separate            Output a separate heat map for each repository\n"
         "   -y, --year <year>         The year for which to create the heat map\n"
@@ -78,7 +79,9 @@
         "Finally, this tool prints an HTML page to stdout. A separate heap map is\n"
         "generated for each author showing commits across all repositories, unless the\n"
         "\033[1m--separate\033[22m option is specified in which case each repository is displayed with\n"
-        "its own heat map.\n"
+        "its own heat map. By using the \033[1m--fragment\033[22m option, the tool only outputs a\n"
+        "single HTML div container without any header or footer that can be embedded in\n"
+        "your custom web page.\n"
         , stderr);
 }
 
@@ -124,6 +127,8 @@
             }
         } else if (chk_arg(argv[i], "-n", "--no-pull")) {
             settings.update_repos = false;
+        } else if (chk_arg(argv[i], "-f", "--fragment")) {
+            settings.fragment = true;
         } else if (chk_arg(argv[i], "-s", "--separate")) {
             settings.separate = true;
         } else if (chk_arg(argv[i], "-A", "--authormap")) {
@@ -249,7 +254,7 @@
         }
     }
 
-    html::open();
+    html::open(settings.fragment);
     for (const auto &[repo, authors] : heatmap.data()) {
         html::h1(repo);
         for (const auto &[author, entries] : authors) {
@@ -313,7 +318,7 @@
             html::table_end();
         }
     }
-    html::close();
+    html::close(settings.fragment);
 
     return EXIT_SUCCESS;
 }
--- a/src/settings.h	Sat Feb 01 17:15:14 2025 +0100
+++ b/src/settings.h	Mon Feb 03 23:13:19 2025 +0100
@@ -44,6 +44,7 @@
     unsigned char depth = 1;
     bool update_repos = true;
     bool separate = false;
+    bool fragment = false;
     unsigned short year = settings_current_year;
 
     int parse_authormap(const std::string& path);

mercurial