X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=src%2Fstagit.c;h=6b45b3601b9f86b36f18aa3655992f02103b6e28;hb=1426ca963932ab7521eb40914f98ff0cc9e00ffb;hp=b5580b8cc772f7bebd6533cd437d0aae98ef6766;hpb=0b5890cac52e7dbdeb5c3a559f23c6b205ffa06b;p=stagit.git diff --git a/src/stagit.c b/src/stagit.c index b5580b8..6b45b36 100644 --- a/src/stagit.c +++ b/src/stagit.c @@ -14,10 +14,13 @@ #include #include + +#ifdef HAS_CMARK #include +#endif -#include "cp.h" #include "compat.h" +#include "cp.h" struct deltainfo { git_patch *patch; @@ -68,6 +71,7 @@ static char *readme; static long long nlogcommits = -1; /* < 0 indicates not used */ bool htmlized; /* true if markdoown converted to HTML */ +static char oldfilename[PATH_MAX]; /* filename of the last file */ /* cache */ static git_oid lastoid; @@ -276,6 +280,26 @@ xmlencode(FILE *fp, const char *s, size_t len) } } +/* Escape characters below as HTML 2.0 / XML 1.0, ignore printing '\n', '\r' */ +void +xmlencodeline(FILE *fp, const char *s, size_t len) +{ + size_t i; + + for (i = 0; *s && i < len; s++, i++) { + switch(*s) { + case '<': fputs("<", fp); break; + case '>': fputs(">", fp); break; + case '\'': fputs("'", fp); break; + case '&': fputs("&", fp); break; + case '"': fputs(""", fp); break; + case '\r': break; /* ignore CR */ + case '\n': break; /* ignore LF */ + default: putc(*s, fp); + } + } +} + int mkdirp(const char *path) { @@ -347,7 +371,7 @@ void writeheader(FILE *fp, const char *title) { fputs("\n" - "\n\n" + "\n\n" "\n" "\n" "", fp); @@ -364,7 +388,7 @@ writeheader(FILE *fp, const char *title) 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)); @@ -414,12 +438,16 @@ void call_chroma(const char *filename, FILE *fp, const char *s, size_t len) { htmlized = false; + char *html = ""; // Flush HTML-file fflush(fp); - char *html = cmark_markdown_to_html(s, len, CMARK_OPT_DEFAULT); +#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); @@ -450,6 +478,9 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len) } else { fprintf(fp, "%s", html); } +#else + fprintf(fp, "<pre>%s</pre>", s); +#endif } void @@ -606,8 +637,9 @@ printshowfile(FILE *fp, struct commitinfo *ci) fprintf(fp, "<a href=\"#h%zu-%zu-%zu\" id=\"h%zu-%zu-%zu\" class=\"d\">-", i, j, k, i, j, k); else - fputc(' ', fp); - xmlencode(fp, line->content, line->content_len); + putc(' ', fp); + xmlencodeline(fp, line->content, line->content_len); + putc('\n', fp); if (line->old_lineno == -1 || line->new_lineno == -1) fputs("</a>", fp); } @@ -838,7 +870,26 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi xmlencode(fp, filename, strlen(filename)); fprintf(fp, " (%s)", convertbytes((int)filesize)); - fputs("</p><hr/>", fp); +#ifdef HAS_CMARK + char newfpath[PATH_MAX]; + char newfilename[PATH_MAX]; + if (strcmp(get_ext(filename), "md") == 0) { + fprintf(fp, " <a href=\"%s.html-raw\">View raw</a>", filename); + strcpy(newfpath, fpath); + strcat(newfpath, "-raw"); + + strcpy(newfilename, filename); + strcat(newfilename, "-raw"); + strcpy(oldfilename, filename); + + /* NOTE: recurses */ + writeblob(obj, newfpath, newfilename, filesize); + } else if (strcmp(get_ext(filename), "md-raw" ) == 0) { + fprintf(fp, " <a href=\"%s.html\">View rendered</a>", oldfilename); + } +#endif + + fputs(".</p><hr/>", fp); if (git_blob_is_binary((git_blob *)obj)) { fputs("<p>Binary file.</p>\n", fp);