git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Symonds <dsymonds@gmail.com>
To: pasky@suse.cz, spearce@spearce.org
Cc: git@vger.kernel.org, David Symonds <dsymonds@gmail.com>
Subject: [PATCH 1/2] gitweb: Refactor abbreviation-with-title-attribute code.
Date: Tue, 23 Oct 2007 11:31:22 +1000	[thread overview]
Message-ID: <1193103083390-git-send-email-dsymonds@gmail.com> (raw)

Signed-off-by: David Symonds <dsymonds@gmail.com>
---
 gitweb/gitweb.perl |   45 +++++++++++++++++++++------------------------
 1 files changed, 21 insertions(+), 24 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index ea84c75..a835bd1 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -842,6 +842,23 @@ sub chop_str {
 	return "$body$tail";
 }
 
+# takes the same arguments as chop_str, but also wraps a <span> around the
+# result with a title attribute if it does get chopped. Additionally, the
+# string is HTML-escaped.
+sub chop_and_escape_str {
+	my $str = shift;
+	my $len = shift;
+	my $add_len = shift || 10;
+
+	my $chopped = chop_str($str, $len, $add_len);
+	if ($chopped eq $str) {
+		return esc_html($chopped);
+	} else {
+		return qq{<span title="} . esc_html($str) . qq{">} .
+			esc_html($chopped) . qq{</span>};
+	}
+}
+
 ## ----------------------------------------------------------------------
 ## functions returning short strings
 
@@ -3427,12 +3444,7 @@ sub git_shortlog_body {
 			print "<tr class=\"light\">\n";
 		}
 		$alternate ^= 1;
-		my $author = chop_str($co{'author_name'}, 10);
-		if ($author ne $co{'author_name'}) {
-			$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
-		} else {
-			$author = esc_html($author);
-		}
+		my $author = chop_and_escape_str($co{'author_name'}, 10);
 		# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
 		print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
 		      "<td><i>" . $author . "</i></td>\n" .
@@ -3484,12 +3496,7 @@ sub git_history_body {
 		}
 		$alternate ^= 1;
 	# shortlog uses      chop_str($co{'author_name'}, 10)
-		my $author = chop_str($co{'author_name'}, 15, 3);
-		if ($author ne $co{'author_name'}) {
-			"<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
-		} else {
-			$author = esc_html($author);
-		}
+		my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
 		print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
 		      "<td><i>" . $author . "</i></td>\n" .
 		      "<td>";
@@ -3645,12 +3652,7 @@ sub git_search_grep_body {
 			print "<tr class=\"light\">\n";
 		}
 		$alternate ^= 1;
-		my $author = chop_str($co{'author_name'}, 15, 5);
-		if ($author ne $co{'author_name'}) {
-			$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
-		} else {
-			$author = esc_html($author);
-		}
+		my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
 		print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
 		      "<td><i>" . $author . "</i></td>\n" .
 		      "<td>" .
@@ -5165,12 +5167,7 @@ sub git_search {
 						print "<tr class=\"light\">\n";
 					}
 					$alternate ^= 1;
-					my $author = chop_str($co{'author_name'}, 15, 5);
-					if ($author ne $co{'author_name'}) {
-						$author = "<span title=\"" . esc_html($co{'author_name'}) . "\">" . esc_html($author) . "</span>";
-					} else {
-						$author = esc_html($author);
-					}
+					my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
 					print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
 					      "<td><i>" . $author . "</i></td>\n" .
 					      "<td>" .
-- 
1.5.3.1

             reply	other threads:[~2007-10-23  1:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-23  1:31 David Symonds [this message]
2007-10-23  1:31 ` [PATCH 2/2] gitweb: Use chop_and_escape_str in more places David Symonds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1193103083390-git-send-email-dsymonds@gmail.com \
    --to=dsymonds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=pasky@suse.cz \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).