History

ui-blame: Allow syntax highlighting

Place file contents into a single block so that syntax highlighting can be applied in the usual fashion. Place the alternating color bars behind the file contents. Force the default syntax highlighting background to transparent.

Jeff Smith committed 9 years ago

Commit dbaee261 parent aafc42d3 files · +62 −13Browse files

Unified · Split

3 files
  1. cgit.css +10 −0
  2. filters/syntax-highlighting.py +1 −1
  3. ui-blame.c +51 −12
cgit.css+10−0
@@ -353,6 +353,16 @@ div#cgit table.blame div.alt:nth-child(odd) { · 352 unmodified lines
353353 background: white;
354354 }
355355
356+div#cgit table.blame td.lines > div {
357+ position: relative;
358+}
359+
360+div#cgit table.blame td.lines > div > pre {
361+ padding: 0 0 0 0.5em;
362+ position: absolute;
363+ top: 0;
364+}
365+
356366 div#cgit table.bin-blob {
357367 margin-top: 0.5em;
358368 border: solid 1px black;
filters/syntax-highlighting.py+1−1
@@ -34,7 +34,7 @@ sys.stdin = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8', errors='replace · 33 unmodified lines
3434 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
3535 data = sys.stdin.read()
3636 filename = sys.argv[1]
37−formatter = HtmlFormatter(style='pastie')
37+formatter = HtmlFormatter(style='pastie', nobackground=True)
3838
3939 try:
4040 lexer = guess_lexer_for_filename(filename, data)
ui-blame.c+51−12
@@ -67,15 +67,29 @@ static void emit_blame_entry_linenumber(struct blame_entry *ent) · 66 unmodified lines
6767 htmlf(numberfmt, ++lineno);
6868 }
6969
70−static void emit_blame_entry_line(struct blame_scoreboard *sb,
71− struct blame_entry *ent)
70+static void emit_blame_entry_line_background(struct blame_scoreboard *sb,
71+ struct blame_entry *ent)
7272 {
73− const char *cp, *cpend;
73+ unsigned long line;
74+ size_t len, maxlen = 2;
75+ const char* pos, *endpos;
7476
75− cp = blame_nth_line(sb, ent->lno);
76− cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
77+ for (line = ent->lno; line < ent->lno + ent->num_lines; line++) {
78+ html("\n");
79+ pos = blame_nth_line(sb, line);
80+ endpos = blame_nth_line(sb, line + 1);
81+ len = 0;
82+ while (pos < endpos) {
83+ len++;
84+ if (*pos++ == '\t')
85+ len = (len + 7) & ~7;
86+ }
87+ if (len > maxlen)
88+ maxlen = len;
89+ }
7790
78− html_ntxt(cp, cpend - cp);
91+ for (len = 0; len < maxlen - 1; len++)
92+ html(" ");
7993 }
8094
8195 struct walk_tree_context {
@@ -88,6 +102,7 @@ static void print_object(const unsigned char *sha1, const char *path, · 6 unmodified lines
88102 const char *basename, const char *rev)
89103 {
90104 enum object_type type;
105+ char *buf;
91106 unsigned long size;
92107 struct argv_array rev_argv = ARGV_ARRAY_INIT;
93108 struct rev_info revs;
@@ -102,6 +117,13 @@ static void print_object(const unsigned char *sha1, const char *path, · 8 unmodified lines
102117 return;
103118 }
104119
120+ buf = read_sha1_file(sha1, &type, &size);
121+ if (!buf) {
122+ cgit_print_error_page(500, "Internal server error",
123+ "Error reading object %s", sha1_to_hex(sha1));
124+ return;
125+ }
126+
105127 argv_array_push(&rev_argv, "blame");
106128 argv_array_push(&rev_argv, rev);
107129 init_revisions(&revs, NULL);
@@ -157,20 +179,37 @@ static void print_object(const unsigned char *sha1, const char *path, · 49 unmodified lines
157179 html("</td>\n");
158180 }
159181
160− /* Lines */
161− html("<td class='lines'>");
182+ html("<td class='lines'><div>");
183+
184+ /* Colored bars behind lines */
185+ html("<div>");
162186 for (ent = sb.ent; ent; ) {
163187 struct blame_entry *e = ent->next;
164− html("<div class='alt'><pre><code>");
165− emit_blame_entry_line(&sb, ent);
166− html("</code></pre></div>");
188+ html("<div class='alt'><pre>");
189+ emit_blame_entry_line_background(&sb, ent);
190+ html("</pre></div>");
167191 free(ent);
168192 ent = e;
169193 }
170− html("</td>\n");
194+ html("</div>");
171195
172196 free((void *)sb.final_buf);
173197
198+ /* Lines */
199+ html("<pre><code>");
200+ if (ctx.repo->source_filter) {
201+ char *filter_arg = xstrdup(basename);
202+ cgit_open_filter(ctx.repo->source_filter, filter_arg);
203+ html_raw(buf, size);
204+ cgit_close_filter(ctx.repo->source_filter);
205+ free(filter_arg);
206+ } else {
207+ html_txt(buf);
208+ }
209+ html("</code></pre>");
210+
211+ html("</div></td>\n");
212+
174213 html("</tr>\n</table>\n");
175214
176215 cgit_print_layout_end();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t