45 " -l Disable line numbers\n" |
45 " -l Disable line numbers\n" |
46 " -V, -v Prints version and exits\n" |
46 " -V, -v Prints version and exits\n" |
47 "\n"); |
47 "\n"); |
48 } |
48 } |
49 |
49 |
50 #define WRITECONST(stream, out, cstr) out(cstr, 1, sizeof(cstr)-1, stream) |
|
51 int formatfile( |
50 int formatfile( |
52 highlighter_t *highlighter, |
51 highlighter_t *highlighter, |
53 UcxList *in, |
52 UcxList *in, |
54 write_func out, |
53 write_func out, void *stream, |
55 void *stream, |
|
56 int showlineno) { |
54 int showlineno) { |
57 size_t lines = ucx_list_size(in); |
55 |
58 |
56 /* compute width of line numbering */ |
|
57 int lnw; |
|
58 if (showlineno) { |
|
59 size_t lines = ucx_list_size(in); |
|
60 lnw = 1; |
|
61 int p = 1; |
|
62 while ((p*=10) < lines) lnw++; |
|
63 } |
|
64 |
|
65 /* allocate line buffer */ |
59 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
66 UcxBuffer *line = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND); |
60 if(!line) { |
67 if(!line) { |
61 return 1; |
68 return 1; |
62 } |
69 } |
63 WRITECONST(stream, out, "<pre>\n"); |
70 |
64 |
71 /* start monospace formatting */ |
65 int lnw; |
72 out("<pre>\n", 1, 6, stream); |
66 { |
73 |
67 lnw = 1; |
74 /* process lines */ |
68 int p = 1; |
|
69 while ((p*=10) < lines) lnw++; |
|
70 } |
|
71 |
|
72 size_t lineno = 0; |
75 size_t lineno = 0; |
73 UCX_FOREACH(sourceline, in) { |
76 UCX_FOREACH(sourceline, in) { |
|
77 /* increase line number and clean line buffer */ |
74 lineno++; |
78 lineno++; |
|
79 ucx_buffer_clear(line); |
|
80 |
|
81 /* write line number */ |
|
82 if (showlineno) { |
|
83 ucx_bprintf(line, "<span class=\"c2html-lineno\">" |
|
84 "<a name=\"l%d\" href=\"#l%d\">%*d </a></span> ", |
|
85 lineno, lineno, lnw, lineno); |
|
86 } |
|
87 |
75 /* TODO: backwards compatibility: replace line->space in all occasions |
88 /* TODO: backwards compatibility: replace line->space in all occasions |
76 * and use UcxBuffer functions |
89 * and use UcxBuffer functions |
77 */ |
90 */ |
|
91 char *buf = line->space + line->pos; |
78 if (highlighter) { |
92 if (highlighter) { |
79 highlighter->parser(sourceline->data, line->space, highlighter); |
93 highlighter->parser(sourceline->data, buf, highlighter); |
80 } else { |
94 } else { |
81 char *c = sourceline->data; |
95 char *c = sourceline->data; |
82 size_t dp = 0; |
96 size_t dp = 0; |
83 while (*c && *c != '\n') { |
97 while (*c && *c != '\n') { |
84 dp = writeescapedchar(line->space, dp, *c); |
98 dp = writeescapedchar(buf, dp, *c); |
85 c++; |
99 c++; |
86 } |
100 } |
87 line->space[dp++] = '\n'; |
101 buf[dp++] = '\n'; |
88 line->space[dp] = '\0'; |
102 buf[dp] = '\0'; |
89 } |
103 } |
90 |
104 line->size = strlen(line->space); |
91 // write line number |
105 |
92 if (showlineno) { |
106 /* write code line */ |
93 WRITECONST(stream, out, "<span class=\"c2html-lineno\">"); |
107 out(line->space, 1, line->size, stream); |
94 char lnbuf[128]; |
108 } |
95 int len; |
109 |
96 // line number link |
110 /* end monospace formatting */ |
97 len = snprintf(lnbuf, 128, "<a name=\"l%d\" href=\"#l%d\">", |
111 out("</pre>\n", 1, 7, stream); |
98 lineno, lineno); |
112 |
99 out(lnbuf, 1, len, stream); |
113 /* cleanup and return */ |
100 // justified line number |
|
101 len = snprintf(lnbuf, 128, "%*d ", lnw, lineno); |
|
102 out(lnbuf, 1, len, stream); |
|
103 WRITECONST(stream, out, "</a></span> "); |
|
104 } |
|
105 |
|
106 // write formated (or plain) code line |
|
107 out(line->space, 1, strlen(line->space), stream); |
|
108 } |
|
109 |
|
110 WRITECONST(stream, out, "</pre>\n"); |
|
111 ucx_buffer_free(line); |
114 ucx_buffer_free(line); |
112 return 0; |
115 return 0; |
113 } |
116 } |
114 |
117 |
115 void init_c_highlighter(highlighter_t *highlighter) { |
118 void init_c_highlighter(highlighter_t *highlighter) { |