[RFCv2 PATCH 4/7] ui-blame: html_ntxt with no ellipsis

John Keeping john at keeping.me.uk
Sat Sep 23 17:47:43 CEST 2017


On Fri, Sep 22, 2017 at 10:38:45PM -0500, Jeff Smith wrote:
> For implementing a ui-blame page, there is need for a function that
> outputs a selection from a block of text, transformed for HTML output,
> but with no further modifications or additions.
> 
> Signed-off-by: Jeff Smith <whydoubt at gmail.com>
> ---
>  html.c        | 37 ++++++++++++++++---------------------
>  html.h        |  1 +
>  ui-repolist.c |  2 +-
>  3 files changed, 18 insertions(+), 22 deletions(-)
> 
> diff --git a/html.c b/html.c
> index e7e6e07..345300e 100644
> --- a/html.c
> +++ b/html.c
> @@ -122,10 +122,10 @@ void html_vtxtf(const char *format, va_list ap)
>  	strbuf_release(&buf);
>  }
>  
> -void html_txt(const char *txt)
> +static int _html_txt(int len, const char *txt)

This should use ssize_t not int.

>  {
>  	const char *t = txt;
> -	while (t && *t) {
> +	while (t && *t && len--) {
>  		int c = *t;
>  		if (c == '<' || c == '>' || c == '&') {
>  			html_raw(txt, t - txt);
> @@ -140,29 +140,24 @@ void html_txt(const char *txt)
>  		t++;
>  	}
>  	if (t != txt)
> -		html(txt);
> +		html_raw(txt, t - txt);
> +	return len;
> +}
> +
> +void html_txt(const char *txt)
> +{
> +	if (txt)
> +		_html_txt(strlen(txt), txt);
>  }
>  
>  void html_ntxt(int len, const char *txt)
>  {
> -	const char *t = txt;
> -	while (t && *t && len--) {
> -		int c = *t;
> -		if (c == '<' || c == '>' || c == '&') {
> -			html_raw(txt, t - txt);
> -			if (c == '>')
> -				html(">");
> -			else if (c == '<')
> -				html("<");
> -			else if (c == '&')
> -				html("&");
> -			txt = t + 1;
> -		}
> -		t++;
> -	}
> -	if (t != txt)
> -		html_raw(txt, t - txt);
> -	if (len < 0)
> +	_html_txt(len, txt);

Why bother with this wrapper function?  Just make html_ntxt be _html_txt
and ignore the result everywhere except html_ntxt_ellipsis.

> +}
> +
> +void html_ntxt_ellipsis(int len, const char *txt)
> +{
> +	if (_html_txt(len, txt) < 0)
>  		html("...");
>  }
>  
> diff --git a/html.h b/html.h
> index 1b24e55..de53430 100644
> --- a/html.h
> +++ b/html.h
> @@ -20,6 +20,7 @@ extern void html_attrf(const char *format,...);
>  
>  extern void html_txt(const char *txt);
>  extern void html_ntxt(int len, const char *txt);
> +extern void html_ntxt_ellipsis(int len, const char *txt);
>  extern void html_attr(const char *txt);
>  extern void html_url_path(const char *txt);
>  extern void html_url_arg(const char *txt);
> diff --git a/ui-repolist.c b/ui-repolist.c
> index 7272e87..77c33fd 100644
> --- a/ui-repolist.c
> +++ b/ui-repolist.c
> @@ -329,7 +329,7 @@ void cgit_print_repolist(void)
>  		repourl = cgit_repourl(ctx.repo->url);
>  		html_link_open(repourl, NULL, NULL);
>  		free(repourl);
> -		html_ntxt(ctx.cfg.max_repodesc_len, ctx.repo->desc);
> +		html_ntxt_ellipsis(ctx.cfg.max_repodesc_len, ctx.repo->desc);
>  		html_link_close();
>  		html("</td><td>");
>  		if (ctx.cfg.enable_index_owner) {


More information about the CGit mailing list