From: Alexandre Erwin Ittner Date: Thu, 09 Oct 2014 02:16:31 GMT Subject: [PATCH] gitweb: add HTML ids for categories and projects Message-ID: <20141008231631.8466676f4f808cb919c677e4@ittner.com.br> 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 --- 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 "\n"; + print "\n"; } else { - print "\n"; + print "\n"; } $alternate ^= 1; @@ -5802,7 +5816,8 @@ sub git_project_list_body { if ($check_forks) { print "\n"; } - print "".esc_html($cat)."\n"; + my $cat_id = make_xhtml_id("category", $cat); + print "".esc_html($cat)."\n"; print "\n"; } -- 1.7.9.5