commit ba2dc046e649f0f48b0fbe65f12627bd33ec2d38
parent 560f081852a8b04bcb628d7396d13a22c00ffa26
Author: Demonstrandum <moi@knutsen.co>
Date: Wed, 5 Aug 2020 22:04:18 +0100
FileBlob wrapper.
Diffstat:
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/highlight b/highlight
@@ -8,7 +8,27 @@ stdin = ARGF.file
filename = stdin.readline.strip # Read first line (filename).
contents = stdin.read # Read rest (code).
-detected = Linguist::FileBlob.new(filename).language
+class FakeBlob < Linguist::FileBlob
+ def initialize(path, content, base_bath=nil)
+ super(path, base_bath)
+ @content = content
+ end
+
+ def data
+ @content
+ end
+
+ def size
+ @content.bytesize
+ end
+end
+
+blob = FakeBlob.new(filename, contents)
+detected = if blob.language
+ blob.language.name
+ else
+ "Text only"
+ end
# Debugging
#puts "File #{filename}"
@@ -18,7 +38,7 @@ detected = Linguist::FileBlob.new(filename).language
#pp detected
html = Pygments.highlight(contents,
- :lexer => detected.name,
+ :lexer => detected,
:formatter => 'html',
:options => {
:encoding => 'utf-8',
diff --git a/stagit.c b/stagit.c
@@ -394,17 +394,20 @@ writefooter(FILE *fp)
fputs("</div>\n</body>\n</html>\n", fp);
}
-void
-syntax_highlight(const char *fpath, FILE *fp, const char *s, size_t len)
+int
+syntax_highlight(const char *filename, FILE *fp, const char *s, size_t len)
{
// Ruby script for syntax highlighting.
FILE *child = popen("./highlight", "w");
// Give filename:
- fprintf(child, "%s\n", fpath);
+ fprintf(child, "%s\n", filename);
// Give code to highlight:
+ int lc;
size_t i;
- for (i = 0; *s && i < len; s++, i++)
+ for (i = 0; *s && i < len; s++, i++) {
+ if (*s == '\n') lc++;
fputc(*s, child);
+ }
// Write returned HTML to the HTML file.
char c;
@@ -412,24 +415,25 @@ syntax_highlight(const char *fpath, FILE *fp, const char *s, size_t len)
fputc(c, fp);
pclose(child);
+ return lc;
}
int
-writeblobhtml(const char *fpath, FILE *fp, const git_blob *blob)
+writeblobhtml(const char *filename, FILE *fp, const git_blob *blob)
{
- size_t n = 0, i, prev;
+ int lc = 0;
const char *s = git_blob_rawcontent(blob);
git_off_t len = git_blob_rawsize(blob);
fputs("<div id=\"blob\">\n", fp);
if (len > 0) {
- syntax_highlight(fpath, fp, s, len);
+ lc = syntax_highlight(filename, fp, s, len);
}
fputs("</div>\n", fp);
- return n;
+ return lc;
}
void