godoc: add on-hover permalink anchors across the site

We already have anchors for godoc types and functions. Add them for
every heading (and "dl") on every other page on the site.

Change-Id: Ibad77f04442e48dd5a41de759010625f89b48604
Reviewed-on: https://go-review.googlesource.com/33641
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/godoc/static/godocs.js b/godoc/static/godocs.js
index 10183cb..7e353d9 100644
--- a/godoc/static/godocs.js
+++ b/godoc/static/godocs.js
@@ -305,9 +305,36 @@
   }
 }
 
+function addPermalinks() {
+  function addPermalink(source, parent) {
+    var id = source.attr("id");
+    if (id == "" || id.indexOf("tmp_") === 0) {
+      // Auto-generated permalink.
+      return;
+    }
+    if (parent.find("> .permalink").length) {
+      // Already attached.
+      return;
+    }
+    parent.append(" ").append($("<a class='permalink'>&#xb6;</a>").attr("href", "#" + id));
+  }
+
+  $("#page .container").find("h2[id], h3[id]").each(function() {
+    var el = $(this);
+    addPermalink(el, el);
+  });
+
+  $("#page .container").find("dl[id]").each(function() {
+    var el = $(this);
+    // Add the anchor to the "dt" element.
+    addPermalink(el, el.find("> dt").first());
+  });
+}
+
 $(document).ready(function() {
   bindSearchEvents();
   generateTOC();
+  addPermalinks();
   bindToggles(".toggle");
   bindToggles(".toggleVisible");
   bindToggleLinks(".exampleLink", "example_");
diff --git a/godoc/static/static.go b/godoc/static/static.go
index f6e267e..2d5fbe4 100644
--- a/godoc/static/static.go
+++ b/godoc/static/static.go
@@ -875,9 +875,36 @@
   }
 }
 
+function addPermalinks() {
+  function addPermalink(source, parent) {
+    var id = source.attr("id");
+    if (id.indexOf("tmp_") === 0) {
+      // Auto-generated permalink.
+      return;
+    }
+    if (parent.find("> .permalink").length) {
+      // Already attached.
+      return;
+    }
+    parent.append(" ").append($("&nbsp;<a class='permalink'>&#xb6;</a>").attr("href", "#" + id));
+  }
+
+  $("#page .container").find("h2[id], h3[id]").each(function() {
+    var el = $(this);
+    addPermalink(el, el);
+  });
+
+  $("#page .container").find("dl[id]").each(function() {
+    var el = $(this);
+    // Add the anchor to the "dt" element.
+    addPermalink(el, el.find("> dt").first());
+  });
+}
+
 $(document).ready(function() {
   bindSearchEvents();
   generateTOC();
+  addPermalinks();
   bindToggles(".toggle");
   bindToggles(".toggleVisible");
   bindToggleLinks(".exampleLink", "example_");
@@ -1599,8 +1626,8 @@
 	</script>
 
 	{{if $.IsMain}}
-		{{comment_html .Doc}}
 		{{/* command documentation */}}
+		{{comment_html .Doc}}
 	{{else}}
 		{{/* package documentation */}}
 		<div id="short-nav">
@@ -2849,7 +2876,7 @@
 .permalink {
 	display: none;
 }
-h2:hover .permalink, h3:hover .permalink {
+:hover > .permalink {
 	display: inline;
 }
 
diff --git a/godoc/static/style.css b/godoc/static/style.css
index fd26bd8..b21bc24 100644
--- a/godoc/static/style.css
+++ b/godoc/static/style.css
@@ -46,7 +46,7 @@
 .permalink {
 	display: none;
 }
-h2:hover .permalink, h3:hover .permalink {
+:hover > .permalink {
 	display: inline;
 }