#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <git2.h>
+#ifdef HAS_CMARK
+#include <cmark-gfm.h>
+#endif
+
+#include "cp.h"
#include "compat.h"
struct deltainfo {
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 */
static FILE *rcachefp, *wcachefp;
static const char *cachefile;
-int cp(char fileSource[], char fileDestination[])
+int
+cp(const char fileSource[], const char fileDestination[])
{
- int c;
- FILE *stream_R, *stream_W;
-
- stream_R = fopen(fileSource, "r");
- if (stream_R == NULL)
- return -1;
- stream_W = fopen(fileDestination, "w"); //create and write to file
- if (stream_W == NULL)
- {
- fclose(stream_R);
- return -2;
- }
- while ((c = fgetc(stream_R)) != EOF)
- fputc(c, stream_W);
- fclose(stream_R);
- fclose(stream_W);
-
- return 0;
+ int c;
+ FILE *stream_R, *stream_W;
+
+ stream_R = fopen(fileSource, "r");
+ if (stream_R == NULL)
+ return -1;
+ stream_W = fopen(fileDestination, "w"); //create and write to file
+ if (stream_W == NULL)
+ {
+ fclose(stream_R);
+ return -2;
+ }
+ while ((c = fgetc(stream_R)) != EOF)
+ fputc(c, stream_W);
+ fclose(stream_R);
+ fclose(stream_W);
+
+ return 0;
}
void
writeheader(FILE *fp, const char *title)
{
fputs("<!DOCTYPE html>\n"
- "<html>\n<head>\n"
+ "<html lang=\"en\">\n<head>\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n"
"<title>", fp);
fprintf(fp, "</title>\n<link rel=\"icon\" type=\"image/png\" href=\"%sfavicon.png\" />\n", relpath);
fprintf(fp, "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"%s Atom Feed\" href=\"%satom.xml\" />\n",
name, relpath);
- fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.min.css\" />\n", relpath);
+ fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%sstyle.css\" />\n", relpath);
fprintf(fp, "<link rel=\"stylesheet\" type=\"text/css\" href=\"%ssyntax.css\" />\n", relpath);
fputs("</head>\n<body>\n<table><tr><td>", fp);
- fprintf(fp, "<a href=\"../%s\"><img src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
+ fprintf(fp, "<a href=\"../%s\"><img alt=\"Home\" src=\"%slogo.png\" alt=\"\" width=\"32\" height=\"32\" /></a>",
relpath, relpath);
fputs("</td><td><h1>", fp);
xmlencode(fp, strippedname, strlen(strippedname));
- fputs("</h1><span class=\"desc\">", fp);
+ fputs("</h1></td></tr><tr><td></td><td><span class=\"desc\">", fp);
xmlencode(fp, description, strlen(description));
fputs("</span></td></tr>", fp);
if (cloneurl[0]) {
if (license)
fprintf(fp, " | <a href=\"%sfile/%s.html\">LICENSE</a>",
relpath, license);
+ fprintf(fp, " | <a href=\"%s%s.tar.gz\">Download</a>",
+ relpath, strippedname);
fputs("</td></tr></table>\n<hr/>\n<div id=\"content\">\n", fp);
}
fputs("</div>\n</body>\n</html>\n", fp);
}
-int
-call_py(const char *filename, FILE *fp, const char *s, size_t len)
+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;
+ char *html = "";
// Flush HTML-file
fflush(fp);
- // Copy STDOUT
- int stdout_copy = dup(1);
- // Redirect STDOUT
- dup2(fileno(fp), 1);
-
- // Python Pygments script for syntax highlighting.
- FILE *child = popen("/usr/local/share/stagit/highlight.py", "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;
+#ifdef HAS_CMARK
+ html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT);
+ if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
+#endif
+
+
+#ifdef HAS_CHROMA
+ 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);
+ }
+#else
+ fprintf(fp, "<pre>%s</pre>", s);
+#endif
}
-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_py(filename, fp, s, len);
+ call_chroma(filename, fp, s, len);
}
-
- return lc;
}
void
printcommit(FILE *fp, struct commitinfo *ci)
{
fprintf(fp, "<b>commit</b> <a href=\"%scommit/%s.html\">%s</a>\n",
- relpath, ci->oid, ci->oid);
+ relpath, ci->oid, ci->oid);
if (ci->parentoid[0])
fprintf(fp, "<b>parent</b> <a href=\"%scommit/%s.html\">%s</a>\n",
- relpath, ci->parentoid, ci->parentoid);
+ relpath, ci->parentoid, ci->parentoid);
if (ci->author) {
fputs("<b>Author:</b> ", 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))
writeheader(fp, filename);
fputs("<p> ", fp);
xmlencode(fp, filename, strlen(filename));
- fprintf(fp, " (%juB)", (uintmax_t)filesize);
+ fprintf(fp, " (%s)", convertbytes((int)filesize));
fputs("</p><hr/>", fp);
if (git_blob_is_binary((git_blob *)obj)) {
fputs("<p>Binary file.</p>\n", fp);
} else {
- lc = writeblobhtml(filename, fp, (git_blob *)obj);
+ writeblobhtml(filename, fp, (git_blob *)obj);
if (ferror(fp))
err(1, "fwrite");
+ else if (htmlized) {
+ /* NOTE: recurses */
+ char newfpath[PATH_MAX];
+ strcat(newfpath, fpath);
+ char newfilename[PATH_MAX];
+ strcat(newfilename, filename);
+ writeblob(obj, strcat(newfpath, "-raw"), strcat(newfilename, "-raw"), filesize);
+ // TODO: Add view-raw button
+ }
}
+
writefooter(fp);
fclose(fp);
relpath = "";
-
- return lc;
}
const char *
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++) {
}
filesize = git_blob_rawsize((git_blob *)obj);
- lc = writeblob(obj, filepath, entryname, filesize);
+ writeblob(obj, filepath, entryname, filesize);
fputs("<tr><td>", fp);
fputs(filemode(git_tree_entry_filemode(entry)), fp);
fputs("\">", fp);
xmlencode(fp, entrypath, strlen(entrypath));
fputs("</a></td><td class=\"num\" align=\"right\">", fp);
- if (lc > 0)
- fprintf(fp, "%dL", lc);
- else
- fprintf(fp, "%juB", (uintmax_t)filesize);
+ fprintf(fp, "%s", convertbytes((int)filesize));
fputs("</td></tr>\n", fp);
git_object_free(obj);
} else if (!git_submodule_lookup(&module, repo, entryname)) {
strcpy(cwd, getcwd(cwd, sizeof(cwd)));
cp("/usr/local/share/stagit/syntax.css", strcat(cwd, "/syntax.css"));
strcpy(cwd, getcwd(cwd, sizeof(cwd)));
- cp("/usr/local/share/stagit/style.min.css", strcat(cwd, "/style.min.css"));
+ cp("/usr/local/share/stagit/style.css", strcat(cwd, "/style.css"));
/* strip .git suffix */
if (!(strippedname = strdup(name)))
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 = "";
writefooter(fp);
fclose(fp);
+ cp("files.html", "index.html");
+
/* summary page with branches and tags */
fp = efopen("refs.html", "w");
writeheader(fp, "Refs");