mobley

C Git HTTP server
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/mobley

commit 7ff418e42d419a3017ea27872ebbdcc227782faa
parent 2f675489c2084fe4a145045bc165e0a868e743b1
Author: Louis Solofrizzo <lsolofrizzo@online.net>
Date:   Fri, 28 Dec 2018 17:47:19 +0100

Add line number on raw files display

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>

Diffstat:
Mfile.c | 2+-
Mhtml.c | 19+++++++++++++++++--
Mhtml.h | 2+-
Mstyle/main.css | 6++++--
4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/file.c b/file.c @@ -37,7 +37,7 @@ static bool file_show_markdown(mobley_t *ctx, server_req_t *r, git_blob *blob) static bool file_show_raw(mobley_t *ctx, server_req_t *r, git_blob *blob) { html_open("pre", .class = "file-raw") { - string_raw_format_html(r, git_blob_rawcontent(blob), git_blob_rawsize(blob)); + string_raw_format_html(ctx, r, git_blob_rawcontent(blob), git_blob_rawsize(blob), true); } html_close("pre"); return true; } diff --git a/html.c b/html.c @@ -32,7 +32,7 @@ void _html(server_req_t *r, const mobley_t *ctx, const char *b, const char *d, evbuffer_add_printf(r->req->buffer_out, " href=\"http://%s:%d%s\"", ctx->ip, ctx->port, a->href); } - else if (strncmp(a->href, "http", sizeof("http") - 1) == 0) + else if (strncmp(a->href, "http", sizeof("http") - 1) == 0 || a->href[0] == '#') { evbuffer_add_printf(r->req->buffer_out, " href=\"%s\"", a->href); } @@ -99,10 +99,25 @@ char *__format(const char *s, ...) } -void string_raw_format_html(server_req_t *r, const char *data, size_t len) +void string_raw_format_html(mobley_t *ctx, server_req_t *r, const char *data, size_t len, bool with_lines) { + size_t line_num = 1; + char link[30] = { 0 }; + for (size_t i = 0; i < len; i++) { + if ((i == 0 || data[i - 1] == '\n') && with_lines) + { + snprintf(link, sizeof(link), "#%zu", line_num); + + html_open("a", .name = __format("%zu", line_num), .href = link) { + html("span", __format("%zu:", line_num), .class = "linenum"); + } html_close("a"); + html_raw(" "); + + line_num++; + } + if (data[i] == '<') evbuffer_add(r->req->buffer_out, "&lt;", 3); else if (data[i] == '>') diff --git a/html.h b/html.h @@ -58,6 +58,6 @@ void _html(server_req_t *r, const mobley_t *ctx, __PRINTF_FUNCTION(1, 2) char *__format(const char *s, ...); -void string_raw_format_html(server_req_t *r, const char *data, size_t len); +void string_raw_format_html(mobley_t *ctx, server_req_t *r, const char *data, size_t len, bool with_lines); #endif /* HTML_H */ diff --git a/style/main.css b/style/main.css @@ -197,7 +197,7 @@ li .url { /* Code highlight */ -.file-content pre { +.file-content pre, .file-content .file-raw { background-color: #101010; color: #ffffff; border: 0; @@ -205,6 +205,9 @@ li .url { margin-left: -10px; } +.file-content .file-raw { + padding: 5px 7px !important; +} .file-content pre a:hover { text-decoration: none; @@ -218,7 +221,6 @@ li .url { .file-content pre a span:hover { text-decoration: underline; - color: white; } .keyword {