Documentation/replace_macros: support --site-search option

This makes the search box link to google.com/search with a "site:XYZ"
restrict.

I have also investigated Google Custom Search Engine, but the UI
feels antiquated, and the search results overlaid properly beyond
my CSS/HTML skills.


Tested:
A:
 1. Add this option to _replace_macros_impl in tools/bzl/asciidoc.bzl
 2. bazel build Documentation/cmd-index-activate.html
 3. chrome $PWD/bazel-bin/Documentation/cmd-index-activate.html
 4. Verify that the search box links to google.com
B:
 5. Revert asciidoc.bzl, bazel build release.
 6. Verify that internal search works.

Change-Id: Ia319803d6a9bfa20ea39c035891208bd5a55a162
diff --git a/Documentation/replace_macros.py b/Documentation/replace_macros.py
index 309a135..7d8ea23 100755
--- a/Documentation/replace_macros.py
+++ b/Documentation/replace_macros.py
@@ -87,6 +87,12 @@
   id="searchBox">
   Search
 </button>
+  %s
+</div>
+++++
+"""
+
+BUILTIN_SEARCH = """
 <script type="text/javascript">
 var f = function() {
   window.location = '../#/Documentation/q/' +
@@ -99,11 +105,25 @@
   }
 }
 </script>
-</div>
-++++
-
 """
 
+GOOGLE_SITE_SEARCH = """
+<script type="text/javascript">
+var f = function() {
+  window.location = 'https://www.google.com/search?q=' +
+     encodeURIComponent(document.getElementById("docSearch").value +
+     ' site:@SITE@');
+}
+document.getElementById("searchBox").onclick = f;
+document.getElementById("docSearch").onkeypress = function(e) {
+  if (13 == (e.keyCode ? e.keyCode : e.which)) {
+    f();
+  }
+}
+</script>
+"""
+
+
 LINK_SCRIPT = """
 
 ++++
@@ -227,8 +247,19 @@
                 help="generate the search boxes")
 opts.add_option('--no-searchbox', action="store_false", dest='searchbox',
                 help="don't generate the search boxes")
+opts.add_option('--site-search', action="store", metavar="SITE",
+                help=("generate the search box using google. SITE should " +
+                      "point to the domain/path of the site, eg. " +
+                      "gerrit-review.googlesource.com/Documentation"))
 options, _ = opts.parse_args()
 
+if options.site_search:
+  SEARCH_BOX = (SEARCH_BOX %
+                GOOGLE_SITE_SEARCH.replace("@SITE@", options.site_search))
+else:
+  SEARCH_BOX = SEARCH_BOX % BUILTIN_SEARCH
+
+
 try:
     try:
         out_file = open(options.out, 'w', errors='ignore')