X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=src%2Fstagit.c;h=b5580b8cc772f7bebd6533cd437d0aae98ef6766;hb=0b5890cac52e7dbdeb5c3a559f23c6b205ffa06b;hp=d1bf33c3eacb72bccab8391a3b10516fcc1c3aa8;hpb=338741f03a073f0963d737d05c15e9d472fa467d;p=stagit.git diff --git a/src/stagit.c b/src/stagit.c index d1bf33c..b5580b8 100644 --- a/src/stagit.c +++ b/src/stagit.c @@ -8,11 +8,13 @@ #include #include #include +#include #include #include #include #include +#include #include "cp.h" #include "compat.h" @@ -65,6 +67,8 @@ static char *readmefiles[] = { "HEAD:README", "HEAD:README.md" }; static char *readme; static long long nlogcommits = -1; /* < 0 indicates not used */ +bool htmlized; /* true if markdoown converted to HTML */ + /* cache */ static git_oid lastoid; static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */ @@ -387,6 +391,8 @@ writeheader(FILE *fp, const char *title) if (license) fprintf(fp, " | LICENSE", relpath, license); + fprintf(fp, " | Download", + relpath, strippedname); fputs("\n
\n
\n", fp); } @@ -396,53 +402,65 @@ writefooter(FILE *fp) fputs("
\n\n\n", fp); } -int +const char * +get_ext(const char *filename) +{ + const char *dot = strrchr(filename, '.'); + if(!dot || dot == filename) return ""; + return dot + 1; +} + +void call_chroma(const char *filename, FILE *fp, const char *s, size_t len) { + htmlized = false; // Flush HTML-file fflush(fp); - // Copy STDOUT - int stdout_copy = dup(1); - // Redirect STDOUT - dup2(fileno(fp), 1); - - char cmd[] = "chroma --html --html-only --html-lines --html-lines-table --filename "; - strcat(cmd, filename); - - FILE *child = popen(cmd, "w"); - if (child == NULL) { - printf("child is null: %s", strerror(errno)); - exit(1); - } - // Give filename through STDIN: - fprintf(child, "%s\n", filename); - // Give code to highlight through STDIN: - int lc; - size_t i; - for (i = 0; *s && i < len; s++, i++) { - if (*s == '\n') lc++; - fprintf(child, "%c", *s); - } - pclose(child); - fflush(stdout); - // Give back STDOUT. - dup2(stdout_copy, 1); - return lc; + char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT); + if (strcmp(get_ext(filename), "md") == 0) htmlized = true; + + if (!htmlized) { + // Copy STDOUT + int stdout_copy = dup(1); + + // Redirect STDOUT + dup2(fileno(fp), 1); + + char cmd[255] = "chroma --html --html-only --html-lines --html-lines-table --filename "; + strncat(cmd, filename, strlen(filename) + 1); + FILE *child = popen(cmd, "w"); + if (child == NULL) { + printf("child is null: %s", strerror(errno)); + exit(1); + } + + // Give code to highlight through STDIN: + size_t i; + for (i = 0; *s && i < len; s++, i++) { + fprintf(child, "%c", *s); + } + + pclose(child); + fflush(stdout); + + // Give back STDOUT. + dup2(stdout_copy, 1); + + } else { + fprintf(fp, "%s", html); + } } -int +void writeblobhtml(const char *filename, FILE *fp, const git_blob *blob) { - int lc = 0; const char *s = git_blob_rawcontent(blob); git_off_t len = git_blob_rawsize(blob); if (len > 0) { - lc = call_chroma(filename, fp, s, len); + call_chroma(filename, fp, s, len); } - - return lc; } void @@ -777,20 +795,36 @@ writeatom(FILE *fp) return 0; } -int +float +rounder(float var) +{ + int value = var * 10 + .5; + return value / 10.0; +} + +const char * +convertbytes(int bytes) +{ + bytes = (float)bytes; + static char outp[25]; + if (bytes < 1024) sprintf(outp, "%u %s", bytes, "B"); + else if (bytes < 1048576) sprintf(outp, "%0.1f %s", rounder(bytes/1024.0), "K"); + else sprintf(outp, "%0.1f %s", rounder(bytes/1048576.0), "M"); + return outp; +} + +void writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t filesize) { char tmp[PATH_MAX] = "", *d; const char *p; - int lc = 0; FILE *fp; if (strlcpy(tmp, fpath, sizeof(tmp)) >= sizeof(tmp)) errx(1, "path truncated: '%s'", fpath); if (!(d = dirname(tmp))) err(1, "dirname"); - if (mkdirp(d)) - return -1; + mkdirp(d); for (p = fpath, tmp[0] = '\0'; *p; p++) { if (*p == '/' && strlcat(tmp, "../", sizeof(tmp)) >= sizeof(tmp)) @@ -802,22 +836,22 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi writeheader(fp, filename); fputs("

", fp); xmlencode(fp, filename, strlen(filename)); - fprintf(fp, " (%juB)", (uintmax_t)filesize); + fprintf(fp, " (%s)", convertbytes((int)filesize)); + fputs("


", fp); if (git_blob_is_binary((git_blob *)obj)) { fputs("

Binary file.

\n", fp); } else { - lc = writeblobhtml(filename, fp, (git_blob *)obj); + writeblobhtml(filename, fp, (git_blob *)obj); if (ferror(fp)) err(1, "fwrite"); } + writefooter(fp); fclose(fp); relpath = ""; - - return lc; } const char * @@ -872,7 +906,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) const char *entryname; char filepath[PATH_MAX], entrypath[PATH_MAX]; size_t count, i; - int lc, r, ret; + int r, ret; count = git_tree_entrycount(tree); for (i = 0; i < count; i++) { @@ -904,7 +938,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) } filesize = git_blob_rawsize((git_blob *)obj); - lc = writeblob(obj, filepath, entryname, filesize); + writeblob(obj, filepath, entryname, filesize); fputs("", fp); fputs(filemode(git_tree_entry_filemode(entry)), fp); @@ -913,10 +947,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path) fputs("\">", fp); xmlencode(fp, entrypath, strlen(entrypath)); fputs("", fp); - if (lc > 0) - fprintf(fp, "%dL", lc); - else - fprintf(fp, "%juB", (uintmax_t)filesize); + fprintf(fp, "%s", convertbytes((int)filesize)); fputs("\n", fp); git_object_free(obj); } else if (!git_submodule_lookup(&module, repo, entryname)) { @@ -1209,6 +1240,12 @@ main(int argc, char *argv[]) submodules = ".gitmodules"; git_object_free(obj); + /* Generate tarball */ + char tarball[255]; + sprintf(tarball, "tar -zcf %s.tar.gz --ignore-failed-read --exclude='.git' %s", + strippedname, repodir); + system(tarball); + /* log for HEAD */ fp = efopen("log.html", "w"); relpath = ""; @@ -1270,6 +1307,8 @@ main(int argc, char *argv[]) writefooter(fp); fclose(fp); + cp("files.html", "index.html"); + /* summary page with branches and tags */ fp = efopen("refs.html", "w"); writeheader(fp, "Refs");