Add the attribute "id" to category headers and project rows in the
web interface. These ids are intended to work as anchors, allowing
links with the fragment component pointing to a given category, which
may be useful when dealing with long project lists.

IDs are generated from the category name and project paths prefixed
with "category-" and "project-", respectively, so links may follow
the format http://example.com/gitweb/#category-drivers. Spaces are
replaced by an "-" and special characters are escaped but, to ensure
uniqueness, no other changes are made.

Signed-off-by: Alexandre Erwin Ittner <alexan...@ittner.com.br>
---
 gitweb/gitweb.perl |   21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index a9f57d6..07f03b1 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -1718,6 +1718,19 @@ sub project_in_list {
        return @list && scalar(grep { $_->{'path'} eq $project } @list);
 }
 
+
+# Make a XHTML entity ID from the given string(s).
+# All arguments are concatenated with an "-", spaces are replaced by "-",
+# and other characters are escaped so the name can be used within a XML
+# attribute.
+# Eg. make_xhtml_id("project", "foo.git")   -->  "project-foo.git"
+sub make_xhtml_id {
+       my $id = join("-", @_);
+       $id =~ s/\s/-/g;
+       return esc_attr($id);
+}
+
+
 ## ----------------------------------------------------------------------
 ## HTML aware string manipulation
 
@@ -5681,11 +5694,12 @@ sub git_project_list_rows {
        my $alternate = 1;
        for (my $i = $from; $i <= $to; $i++) {
                my $pr = $projlist->[$i];
+               my $pr_id = make_xhtml_id("project", $pr->{'path'});
 
                if ($alternate) {
-                       print "<tr class=\"dark\">\n";
+                       print "<tr id=\"$pr_id\" class=\"dark\">\n";
                } else {
-                       print "<tr class=\"light\">\n";
+                       print "<tr id=\"$pr_id\" class=\"light\">\n";
                }
                $alternate ^= 1;
 
@@ -5802,7 +5816,8 @@ sub git_project_list_body {
                                if ($check_forks) {
                                        print "<td></td>\n";
                                }
-                               print "<td class=\"category\" 
colspan=\"5\">".esc_html($cat)."</td>\n";
+                               my $cat_id = make_xhtml_id("category", $cat);
+                               print "<td id=\"$cat_id\" class=\"category\" 
colspan=\"5\">".esc_html($cat)."</td>\n";
                                print "</tr>\n";
                        }
 
-- 
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to