add logic to doclava to generate meta-data JSON for .jd files.
This will be used to provide search suggestions for all docs,
and also automated "see also" links in doc TOC for pages w/ matching tags

Change-Id: I0aefc929bc97ad6eaee77c7094d23c7f57afdf7b
diff --git a/res/assets/templates/jd_lists.cs b/res/assets/templates/jd_lists.cs
new file mode 100644
index 0000000..e205032
--- /dev/null
+++ b/res/assets/templates/jd_lists.cs
@@ -0,0 +1,8 @@
+var JD_DATA = [
+<?cs
+each:page = docs.pages
+?>
+  { label:"<?cs var:page.label ?>", link:"<?cs var:page.link ?>",
+    tags:[<?cs var:page.tags ?>], type:"<?cs var:page.type ?>" }<?cs if:!last(page) ?>,<?cs /if ?><?cs
+/each ?>
+];
diff --git a/src/com/google/doclava/DocFile.java b/src/com/google/doclava/DocFile.java
index dd0a6bc..9e56235 100644
--- a/src/com/google/doclava/DocFile.java
+++ b/src/com/google/doclava/DocFile.java
@@ -24,8 +24,8 @@
 
 
 public class DocFile {
-  private static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
-  private static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
+  public static final Pattern LINE = Pattern.compile("(.*)[\r]?\n", Pattern.MULTILINE);
+  public static final Pattern PROP = Pattern.compile("([^=]+)=(.*)");
 
   public static String readFile(String filename) {
     try {
diff --git a/src/com/google/doclava/Doclava.java b/src/com/google/doclava/Doclava.java
index a239210..33f68c0 100644
--- a/src/com/google/doclava/Doclava.java
+++ b/src/com/google/doclava/Doclava.java
@@ -27,6 +27,7 @@
 
 import java.util.*;
 import java.util.jar.JarFile;
+import java.util.regex.Matcher;
 import java.io.*;
 import java.lang.reflect.Proxy;
 import java.lang.reflect.Array;
@@ -792,7 +793,11 @@
     ClearPage.write(timedata, "timestamp.cs", "timestamp.js");
   }
 
+  /** Go through the docs and generate meta-data about each
+      page to use in search suggestions */
   public static void writeLists() {
+
+    // Write the lists for API references
     Data data = makeHDF();
 
     ClassInfo[] classes = Converter.rootClasses();
@@ -830,8 +835,85 @@
       }
       i++;
     }
-
     ClearPage.write(data, "lists.cs", javadocDir + "lists.js");
+
+
+    // Write the lists for JD documents
+    Data jddata = makeHDF();
+    Iterator counter = new Iterator();
+    for (String htmlDir : inputPathHtmlDirs) {
+      File dir = new File(htmlDir);
+      if (!dir.isDirectory()) {
+        continue;
+      }
+      writeJdDirList(dir, jddata, counter);
+    }
+    ClearPage.write(jddata, "jd_lists.cs", javadocDir + "jd_lists.js");
+  }
+
+  private static class Iterator {
+    int i = 0;
+  }
+
+  /** Write meta-data for a JD file, used for search suggestions */
+  private static void writeJdDirList(File dir, Data data, Iterator counter) {
+    File[] files = dir.listFiles();
+    int i, count = files.length;
+    // Loop all files in given directory
+    for (i = 0; i < count; i++) {
+      File f = files[i];
+      if (f.isFile()) {
+        String filePath = f.getAbsolutePath();
+        String templ = f.getName();
+        int len = templ.length();
+        // If it's a .jd file we want to process
+        if (len > 3 && ".jd".equals(templ.substring(len - 3))) {
+          // remove the directories below the site root
+          String webPath = filePath.substring(filePath.indexOf("docs/html/") + 10, filePath.length());
+          // replace .jd with .html
+          webPath = webPath.substring(0, webPath.length() - 3) + htmlExtension;
+          // Parse the .jd file for properties data at top of page
+          Data hdf = Doclava.makeHDF();
+          String filedata = DocFile.readFile(filePath);
+          Matcher lines = DocFile.LINE.matcher(filedata);
+          String line = null;
+          // Get each line to add the key-value to hdf
+          while (lines.find()) {
+            line = lines.group(1);
+            if (line.length() > 0) {
+              // Stop when we hit the body
+              if (line.equals("@jd:body")) {
+                break;
+              }
+              Matcher prop = DocFile.PROP.matcher(line);
+              if (prop.matches()) {
+                String key = prop.group(1);
+                String value = prop.group(2);
+                hdf.setValue(key, value);
+              } else {
+                break;
+              }
+            }
+          } // done gathering page properties
+
+          // Insert the goods into HDF data (title, link, tags, type)
+          String title = hdf.getValue("page.title", "");
+          String tags = hdf.getValue("page.tags", "");
+          String dirName = (webPath.indexOf("/") != -1)
+                  ? webPath.substring(0, webPath.indexOf("/")) : "";
+
+          if (!"".equals(title) && !"intl".equals(dirName)) {
+            data.setValue("docs.pages." + counter.i + ".label", title.replaceAll("\"", "'"));
+            data.setValue("docs.pages." + counter.i + ".link", webPath);
+            data.setValue("docs.pages." + counter.i + ".tags", tags);
+            data.setValue("docs.pages." + counter.i + ".type", dirName);
+            counter.i++;
+          }
+        }
+      } else if (f.isDirectory()) {
+        writeJdDirList(f, data, counter);
+      }
+    }
   }
 
   public static void cantStripThis(ClassInfo cl, HashSet<ClassInfo> notStrippable) {