X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=src%2Fstagit.c;h=93127ef9bb9dacbdbbf2508a1ea6d597058c68fa;hb=515bec49452f7691c022be27e2cf80c7e517cf41;hp=247fe704d42aac14c2b78348849f7c4d11974680;hpb=516f65ffea797d22346a1acf596e69673e1c9a1d;p=stagit.git diff --git a/src/stagit.c b/src/stagit.c index 247fe70..93127ef 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; @@ -53,6 +56,7 @@ struct commitinfo { static git_repository *repo; +static const char *baseurl = ""; /* base URL to make absolute RSS/Atom URI */ static const char *relpath = ""; static const char *repodir; @@ -68,6 +72,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 +281,69 @@ 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); + } + } +} + +/* 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); + } + } +} + +/* 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; +<<<<<<< HEAD:src/stagit.c + case '\r': break; /* ignore CR */ + case '\n': break; /* ignore LF */ +======= +>>>>>>> 722f836 (micro-optimization: fputc (function) -> putc (macro/inline function)):stagit.c + default: putc(*s, fp); + } + } +} + int mkdirp(const char *path) { @@ -364,7 +432,7 @@ writeheader(FILE *fp, const char *title) fprintf(fp, "\n", relpath); fprintf(fp, "\n", relpath); fputs("\n\n
", fp); - fprintf(fp, "\"Go", + fprintf(fp, "\"Home\"", relpath, relpath); fputs("

", fp); xmlencode(fp, strippedname, strlen(strippedname)); @@ -414,12 +482,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 +522,9 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len) } else { fprintf(fp, "%s", html); } +#else + fprintf(fp, "
%s
", s); +#endif } void @@ -482,12 +557,12 @@ printcommit(FILE *fp, struct commitinfo *ci) xmlencode(fp, ci->author->email, strlen(ci->author->email)); fputs(">\nDate: ", fp); printtime(fp, &(ci->author->when)); - fputc('\n', fp); + putc('\n', fp); } if (ci->msg) { - fputc('\n', fp); + putc('\n', fp); xmlencode(fp, ci->msg, strlen(ci->msg)); - fputc('\n', fp); + putc('\n', fp); } } @@ -606,8 +681,9 @@ printshowfile(FILE *fp, struct commitinfo *ci) fprintf(fp, "-", 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("", fp); } @@ -732,8 +808,8 @@ printcommitatom(FILE *fp, struct commitinfo *ci) xmlencode(fp, ci->summary, strlen(ci->summary)); fputs("\n", fp); } - fprintf(fp, "\n", - ci->oid); + fprintf(fp, "\n", + baseurl, ci->oid); if (ci->author) { fputs("\n", fp); @@ -754,10 +830,10 @@ printcommitatom(FILE *fp, struct commitinfo *ci) xmlencode(fp, ci->author->email, strlen(ci->author->email)); fputs(">\nDate: ", fp); printtime(fp, &(ci->author->when)); - fputc('\n', fp); + putc('\n', fp); } if (ci->msg) { - fputc('\n', fp); + putc('\n', fp); xmlencode(fp, ci->msg, strlen(ci->msg)); } fputs("\n\n\n", fp); @@ -838,7 +914,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("


", fp); +#ifdef HAS_CMARK + char newfpath[PATH_MAX]; + char newfilename[PATH_MAX]; + if (strcmp(get_ext(filename), "md") == 0) { + fprintf(fp, " View raw", 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, " View rendered", oldfilename); + } +#endif + + fputs(".


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

Binary file.

\n", fp); @@ -1103,7 +1198,8 @@ err: void usage(char *argv0) { - fprintf(stderr, "%s [-c cachefile | -l commits] repodir\n", argv0); + fprintf(stderr, "%s [-c cachefile | -l commits] " + "[-u baseurl] repodir\n", argv0); exit(1); } @@ -1136,6 +1232,10 @@ main(int argc, char *argv[]) if (argv[i][0] == '\0' || *p != '\0' || nlogcommits <= 0 || errno) usage(argv[0]); + } else if (argv[i][1] == 'u') { + if (i + 1 >= argc) + usage(argv[0]); + baseurl = argv[++i]; } } if (!repodir)