]> git.armaanb.net Git - stagit.git/blobdiff - src/stagit.c
add function to print a single line, ignoring \r and \n
[stagit.git] / src / stagit.c
index eef927ea8ded24b6dc5ca59fb3fa181adb4b66f4..6b45b3601b9f86b36f18aa3655992f02103b6e28 100644 (file)
@@ -19,8 +19,8 @@
 #include <cmark-gfm.h>
 #endif
 
-#include "cp.h"
 #include "compat.h"
+#include "cp.h"
 
 struct deltainfo {
        git_patch *patch;
@@ -71,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;
@@ -78,29 +79,6 @@ static char lastoidstr[GIT_OID_HEXSZ + 2]; /* id + newline + NUL byte */
 static FILE *rcachefp, *wcachefp;
 static const char *cachefile;
 
-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;
-}
-
 void
 joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
 {
@@ -302,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("&lt;",   fp); break;
+               case '>':  fputs("&gt;",   fp); break;
+               case '\'': fputs("&#39;",  fp); break;
+               case '&':  fputs("&amp;",  fp); break;
+               case '"':  fputs("&quot;", fp); break;
+               case '\r': break; /* ignore CR */
+               case '\n': break; /* ignore LF */
+               default:   putc(*s, fp);
+               }
+       }
+}
+
 int
 mkdirp(const char *path)
 {
@@ -449,7 +447,6 @@ call_chroma(const char *filename, FILE *fp, const char *s, size_t len)
        if (strcmp(get_ext(filename), "md") == 0) htmlized = true;
 #endif
 
-
 #ifdef HAS_CHROMA
        if (!htmlized) {
                // Copy STDOUT
@@ -640,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);
                        }
@@ -871,7 +869,27 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
        fputs("<p> ", fp);
        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);
@@ -879,15 +897,6 @@ writeblob(git_object *obj, const char *fpath, const char *filename, git_off_t fi
                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);