{"thread":{"id":"37694","subject":"[PATCH] gitweb: add HTML ids for categories and projects","startedAt":"2014-10-09T02:16:31Z","lastAt":"2014-10-09T02:16:31Z","messageCount":1,"participants":["Alexandre Erwin Ittner"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"250403","messageId":"20141008231631.8466676f4f808cb919c677e4@ittner.com.br","threadId":"37694","inReplyTo":null,"subject":"[PATCH] gitweb: add HTML ids for categories and projects","fromName":"Alexandre Erwin Ittner","fromEmail":"alexandre@ittner.com.br","sentAt":"2014-10-09T02:16:31Z","receivedAt":"2014-10-09T02:16:31Z","isPatch":true,"sender":{"key":"alexandre@ittner.com.br","avatar":null},"body":"Add the attribute \"id\" to category headers and project rows in the\nweb interface. These ids are intended to work as anchors, allowing\nlinks with the fragment component pointing to a given category, which\nmay be useful when dealing with long project lists.\n\nIDs are generated from the category name and project paths prefixed\nwith \"category-\" and \"project-\", respectively, so links may follow\nthe format http://example.com/gitweb/#category-drivers. Spaces are\nreplaced by an \"-\" and special characters are escaped but, to ensure\nuniqueness, no other changes are made.\n\nSigned-off-by: Alexandre Erwin Ittner <alexandre@ittner.com.br>\n---\n gitweb/gitweb.perl |   21 ++++++++++++++++++---\n 1 file changed, 18 insertions(+), 3 deletions(-)\n\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex a9f57d6..07f03b1 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -1718,6 +1718,19 @@ sub project_in_list {\n \treturn @list && scalar(grep { $_->{'path'} eq $project } @list);\n }\n \n+\n+# Make a XHTML entity ID from the given string(s).\n+# All arguments are concatenated with an \"-\", spaces are replaced by \"-\",\n+# and other characters are escaped so the name can be used within a XML\n+# attribute.\n+# Eg. make_xhtml_id(\"project\", \"foo.git\")   -->  \"project-foo.git\"\n+sub make_xhtml_id {\n+\tmy $id = join(\"-\", @_);\n+\t$id =~ s/\\s/-/g;\n+\treturn esc_attr($id);\n+}\n+\n+\n ## ----------------------------------------------------------------------\n ## HTML aware string manipulation\n \n@@ -5681,11 +5694,12 @@ sub git_project_list_rows {\n \tmy $alternate = 1;\n \tfor (my $i = $from; $i <= $to; $i++) {\n \t\tmy $pr = $projlist->[$i];\n+\t\tmy $pr_id = make_xhtml_id(\"project\", $pr->{'path'});\n \n \t\tif ($alternate) {\n-\t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n+\t\t\tprint \"<tr id=\\\"$pr_id\\\" class=\\\"dark\\\">\\n\";\n \t\t} else {\n-\t\t\tprint \"<tr class=\\\"light\\\">\\n\";\n+\t\t\tprint \"<tr id=\\\"$pr_id\\\" class=\\\"light\\\">\\n\";\n \t\t}\n \t\t$alternate ^= 1;\n \n@@ -5802,7 +5816,8 @@ sub git_project_list_body {\n \t\t\t\tif ($check_forks) {\n \t\t\t\t\tprint \"<td></td>\\n\";\n \t\t\t\t}\n-\t\t\t\tprint \"<td class=\\\"category\\\" colspan=\\\"5\\\">\".esc_html($cat).\"</td>\\n\";\n+\t\t\t\tmy $cat_id = make_xhtml_id(\"category\", $cat);\n+\t\t\t\tprint \"<td id=\\\"$cat_id\\\" class=\\\"category\\\" colspan=\\\"5\\\">\".esc_html($cat).\"</td>\\n\";\n \t\t\t\tprint \"</tr>\\n\";\n \t\t\t}\n \n-- \n1.7.9.5\n"}]}