diff
patches/markdown.patch | 394 ++++++++++++++++++++++++-------------------------
1 file changed, 197 insertions(+), 197 deletions(-)
diff --git a/patches/markdown.patch b/patches/markdown.patch
index 50fc8cd..5f12ed4 100644
--- a/patches/markdown.patch
+++ b/patches/markdown.patch
@@ -1,14 +1,14 @@
--- a/sgit.c
+++ b/sgit.c
-@@ -6,6 +6,7 @@
+@@ -4,6 +4,7 @@
+ #include <sys/stat.h>
+
#include <dirent.h>
- #include <err.h>
- #include <errno.h>
+#include <ctype.h>
+ #include <errno.h>
#include <limits.h>
#include <stdarg.h>
- #include <stdio.h>
-@@ -60,16 +61,19 @@
+@@ -62,16 +63,19 @@
static void make_files_page(const struct repo *, const char *);
static char *read_git_meta_file(const struct repo *, const char *);
static void make_index(const char *, struct repo *, size_t);
@@ -20,7 +20,7 @@
+static void render_markdown(FILE *, const char *);
+static void render_markdown_inline(FILE *, const char *);
static void render_tree(FILE *, const struct repo *, struct tree_node *,
- int);
+ int);
static int repo_cmp(const void *, const void *);
static char *read_cache_head(const char *);
static char *read_cmd_output(const char *, ...);
@@ -30,15 +30,15 @@
static char *repo_basename(const char *);
static int repo_changed(struct repo *);
static int repo_has_git(const char *);
-@@ -85,6 +89,7 @@
+@@ -87,6 +91,7 @@
static void usage(void);
static void write_cache_head(const char *, const char *);
static void write_html_escaped(FILE *, const char *);
+static void write_html_escaped_len(FILE *, const char *, size_t);
static char *write_shell_quoted(const char *);
- /*
-@@ -584,7 +589,15 @@
+ int
+@@ -642,7 +647,15 @@
static void
write_html_escaped(FILE *fp, const char *s)
{
@@ -52,38 +52,20 @@
+ size_t i;
+
+ for (i = 0; i < len; i++) {
- switch (*s) {
- case '&':
- fputs("&", fp);
-@@ -602,6 +615,7 @@
- fputc((unsigned char)*s, fp);
- break;
- }
-+ s++;
+ switch (*s) {
+ case '&':
+ fputs("&", fp);
+@@ -660,6 +673,7 @@
+ fputc((unsigned char)*s, fp);
+ break;
+ }
++ s++;
}
}
-@@ -676,14 +690,32 @@
- free(qpath);
+@@ -735,13 +749,14 @@
}
-+static int
-+has_suffix_ci(const char *s, const char *suffix)
-+{
-+ size_t i, ls, lsf;
-+
-+ ls = strlen(s);
-+ lsf = strlen(suffix);
-+ if (lsf > ls)
-+ return 0;
-+ for (i = 0; i < lsf; i++) {
-+ if (tolower((unsigned char)s[ls - lsf + i]) !=
-+ tolower((unsigned char)suffix[i]))
-+ return 0;
-+ }
-+ return 1;
-+}
-+
static char *
-read_readme(const struct repo *r)
+read_readme(const struct repo *r, int *is_md)
@@ -96,65 +78,81 @@
+ *is_md = 0;
qpath = write_shell_quoted(r->path);
if (qpath == NULL)
- err(1, "malloc");
-@@ -697,6 +729,7 @@
- if (out == NULL)
- continue;
- if (out[0] != '\0') {
-+ *is_md = has_suffix_ci(names[i], ".md");
- free(qpath);
- return out;
- }
-@@ -741,8 +774,167 @@
+ err(1, "malloc");
+@@ -755,6 +770,7 @@
+ if (out == NULL)
+ continue;
+ if (out[0] != '\0') {
++ *is_md = has_suffix_ci(names[i], ".md");
+ free(qpath);
+ return out;
+ }
+@@ -764,6 +780,177 @@
+ return str_dup("");
}
- static void
--make_readme_page(const struct repo *r, const char *readme)
++static int
++has_suffix_ci(const char *s, const char *suffix)
++{
++ size_t i, ls, lsf;
++
++ ls = strlen(s);
++ lsf = strlen(suffix);
++ if (lsf > ls)
++ return 0;
++ for (i = 0; i < lsf; i++) {
++ if (tolower((unsigned char)s[ls - lsf + i]) !=
++ tolower((unsigned char)suffix[i]))
++ return 0;
++ }
++ return 1;
++}
++
++static void
+render_markdown_inline(FILE *fp, const char *line)
- {
-+ const char *code_end, *link_close, *link_end, *url_start;
-+ const char *p;
++{
++ const char *code_end, *link_close, *link_end, *p, *url_start;
+ size_t n;
+
+ p = line;
+ while (*p != '\0') {
-+ if (*p == '`') {
-+ code_end = strchr(p + 1, '`');
-+ if (code_end != NULL) {
-+ fputs("<code>", fp);
-+ write_html_escaped_len(fp, p + 1,
-+ (size_t)(code_end - (p + 1)));
-+ fputs("</code>", fp);
-+ p = code_end + 1;
-+ continue;
-+ }
-+ }
-+ if (*p == '[') {
-+ link_close = strchr(p + 1, ']');
-+ if (link_close != NULL && *(link_close + 1) == '(') {
-+ url_start = link_close + 2;
-+ link_end = strchr(url_start, ')');
-+ if (link_end != NULL) {
-+ fputs("<a href=\"", fp);
-+ write_html_escaped_len(fp, url_start,
-+ (size_t)(link_end - url_start));
-+ fputs("\">", fp);
-+ write_html_escaped_len(fp, p + 1,
-+ (size_t)(link_close - (p + 1)));
-+ fputs("</a>", fp);
-+ p = link_end + 1;
-+ continue;
-+ }
-+ }
-+ }
-+ n = strcspn(p, "`[");
-+ if (n == 0) {
-+ write_html_escaped_len(fp, p, 1);
-+ p++;
-+ } else {
-+ write_html_escaped_len(fp, p, n);
-+ p += n;
-+ }
++ if (*p == '`') {
++ code_end = strchr(p + 1, '`');
++ if (code_end != NULL) {
++ fputs("<code>", fp);
++ write_html_escaped_len(fp, p + 1,
++ (size_t)(code_end - (p + 1)));
++ fputs("</code>", fp);
++ p = code_end + 1;
++ continue;
++ }
++ }
++ if (*p == '[') {
++ link_close = strchr(p + 1, ']');
++ if (link_close != NULL && *(link_close + 1) == '(') {
++ url_start = link_close + 2;
++ link_end = strchr(url_start, ')');
++ if (link_end != NULL) {
++ fputs("<a href=\"", fp);
++ write_html_escaped_len(fp, url_start,
++ (size_t)(link_end - url_start));
++ fputs("\">", fp);
++ write_html_escaped_len(fp, p + 1,
++ (size_t)(link_close - (p + 1)));
++ fputs("</a>", fp);
++ p = link_end + 1;
++ continue;
++ }
++ }
++ }
++ n = strcspn(p, "`[");
++ if (n == 0) {
++ write_html_escaped_len(fp, p, 1);
++ p++;
++ } else {
++ write_html_escaped_len(fp, p, n);
++ p += n;
++ }
+ }
+}
+
@@ -163,135 +161,137 @@
+{
+ char *line;
+ char *save;
-+ char *text;
-+ int in_code, in_list, in_p;
++ int in_code, in_list, in_p, level;
+
+ save = str_dup(md);
+ if (save == NULL)
-+ err(1, "malloc");
++ err(1, "malloc");
+ in_code = 0;
+ in_list = 0;
+ in_p = 0;
+ line = strtok(save, "\n");
+ while (line != NULL) {
-+ while (*line == ' ' || *line == '\t')
-+ line++;
-+ if (strcmp(line, "```") == 0) {
-+ if (in_p) {
-+ fputs("</p>\n", fp);
-+ in_p = 0;
-+ }
-+ if (in_list) {
-+ fputs("</ul>\n", fp);
-+ in_list = 0;
-+ }
-+ if (!in_code)
-+ fputs("<pre>", fp);
-+ else
-+ fputs("</pre>\n", fp);
-+ in_code = !in_code;
-+ line = strtok(NULL, "\n");
-+ continue;
-+ }
-+ if (in_code) {
-+ write_html_escaped(fp, line);
-+ fputc('\n', fp);
-+ line = strtok(NULL, "\n");
-+ continue;
-+ }
-+ if (*line == '\0') {
-+ if (in_p) {
-+ fputs("</p>\n", fp);
-+ in_p = 0;
-+ }
-+ if (in_list) {
-+ fputs("</ul>\n", fp);
-+ in_list = 0;
-+ }
-+ line = strtok(NULL, "\n");
-+ continue;
-+ }
-+ if (*line == '#') {
-+ int level;
-+
-+ if (in_p) {
-+ fputs("</p>\n", fp);
-+ in_p = 0;
-+ }
-+ if (in_list) {
-+ fputs("</ul>\n", fp);
-+ in_list = 0;
-+ }
-+ level = 0;
-+ while (*line == '#' && level < 6) {
-+ level++;
-+ line++;
-+ }
-+ while (*line == ' ')
-+ line++;
-+ fprintf(fp, "<h%d>", level == 0 ? 1 : level);
-+ render_markdown_inline(fp, line);
-+ fprintf(fp, "</h%d>\n", level == 0 ? 1 : level);
-+ line = strtok(NULL, "\n");
-+ continue;
-+ }
-+ if ((line[0] == '-' || line[0] == '*') && line[1] == ' ') {
-+ if (in_p) {
-+ fputs("</p>\n", fp);
-+ in_p = 0;
-+ }
-+ if (!in_list) {
-+ fputs("<ul>\n", fp);
-+ in_list = 1;
-+ }
-+ fputs("<li>", fp);
-+ render_markdown_inline(fp, line + 2);
-+ fputs("</li>\n", fp);
-+ line = strtok(NULL, "\n");
-+ continue;
-+ }
-+ if (!in_p) {
-+ fputs("<p>", fp);
-+ in_p = 1;
-+ } else
-+ fputs("<br>\n", fp);
-+ text = line;
-+ render_markdown_inline(fp, text);
-+ line = strtok(NULL, "\n");
++ while (*line == ' ' || *line == '\t')
++ line++;
++ if (strncmp(line, "```", 3) == 0) {
++ if (in_p) {
++ fputs("</p>\n", fp);
++ in_p = 0;
++ }
++ if (in_list) {
++ fputs("</ul>\n", fp);
++ in_list = 0;
++ }
++ if (!in_code)
++ fputs("<pre>", fp);
++ else
++ fputs("</pre>\n", fp);
++ in_code = !in_code;
++ line = strtok(NULL, "\n");
++ continue;
++ }
++ if (in_code) {
++ write_html_escaped(fp, line);
++ fputc('\n', fp);
++ line = strtok(NULL, "\n");
++ continue;
++ }
++ if (*line == '\0') {
++ if (in_p) {
++ fputs("</p>\n", fp);
++ in_p = 0;
++ }
++ if (in_list) {
++ fputs("</ul>\n", fp);
++ in_list = 0;
++ }
++ line = strtok(NULL, "\n");
++ continue;
++ }
++ if (*line == '#') {
++ if (in_p) {
++ fputs("</p>\n", fp);
++ in_p = 0;
++ }
++ if (in_list) {
++ fputs("</ul>\n", fp);
++ in_list = 0;
++ }
++ level = 0;
++ while (*line == '#' && level < 6) {
++ level++;
++ line++;
++ }
++ while (*line == ' ')
++ line++;
++ fprintf(fp, "<h%d>", level == 0 ? 1 : level);
++ render_markdown_inline(fp, line);
++ fprintf(fp, "</h%d>\n", level == 0 ? 1 : level);
++ line = strtok(NULL, "\n");
++ continue;
++ }
++ if ((line[0] == '-' || line[0] == '*') && line[1] == ' ') {
++ if (in_p) {
++ fputs("</p>\n", fp);
++ in_p = 0;
++ }
++ if (!in_list) {
++ fputs("<ul>\n", fp);
++ in_list = 1;
++ }
++ fputs("<li>", fp);
++ render_markdown_inline(fp, line + 2);
++ fputs("</li>\n", fp);
++ line = strtok(NULL, "\n");
++ continue;
++ }
++ if (!in_p) {
++ fputs("<p>", fp);
++ in_p = 1;
++ } else
++ fputs("<br>\n", fp);
++ render_markdown_inline(fp, line);
++ line = strtok(NULL, "\n");
+ }
+ if (in_code)
-+ fputs("</pre>\n", fp);
++ fputs("</pre>\n", fp);
+ if (in_p)
-+ fputs("</p>\n", fp);
++ fputs("</p>\n", fp);
+ if (in_list)
-+ fputs("</ul>\n", fp);
++ fputs("</ul>\n", fp);
+ free(save);
+}
+
-+static void
+ static char *
+ read_git_meta_file(const struct repo *r, const char *name)
+ {
+@@ -799,7 +986,7 @@
+ }
+
+ static void
+-make_readme_page(const struct repo *r, const char *readme)
+make_readme_page(const struct repo *r, const char *readme, int readme_md)
-+{
+ {
char *html;
FILE *fp;
-
-@@ -784,9 +976,13 @@
+@@ -842,9 +1029,13 @@
if (readme[0] != '\0') {
- fputs("<h2>readme</h2>\n", fp);
-- fputs("<pre>", fp);
-- write_html_escaped(fp, readme);
-- fputs("</pre>\n", fp);
-+ if (readme_md)
-+ render_markdown(fp, readme);
-+ else {
-+ fputs("<pre>", fp);
-+ write_html_escaped(fp, readme);
-+ fputs("</pre>\n", fp);
-+ }
+ fputs("<h2>readme</h2>\n", fp);
+- fputs("<pre>", fp);
+- write_html_escaped(fp, readme);
+- fputs("</pre>\n", fp);
++ if (readme_md)
++ render_markdown(fp, readme);
++ else {
++ fputs("<pre>", fp);
++ write_html_escaped(fp, readme);
++ fputs("</pre>\n", fp);
++ }
}
fputs("</body>\n</html>\n", fp);
-@@ -1089,6 +1285,7 @@
+@@ -1146,6 +1337,7 @@
char *commits, *files, *full_hash, *history, *line, *qpath, *readme;
char *short_hash, *author, *date, *subject;
FILE *fp;
@@ -299,14 +299,14 @@
qpath = write_shell_quoted(r->path);
if (qpath == NULL)
-@@ -1106,11 +1303,11 @@
- err(1, "malloc");
+@@ -1163,11 +1355,11 @@
+ err(1, "malloc");
free(qpath);
- readme = read_readme(r);
+ readme = read_readme(r, &readme_md);
if (readme == NULL)
- err(1, "malloc");
+ err(1, "malloc");
- make_readme_page(r, readme);
+ make_readme_page(r, readme, readme_md);