Break out jd lists into lang specific files. Rescope page.type and add page.category to hold subtabs.

Change-Id: I60ccd7c280551fb7940f6bf7b239199e6da9193e
diff --git a/src/com/google/doclava/ClearPage.java b/src/com/google/doclava/ClearPage.java
index ed1f069..853e49d 100644
--- a/src/com/google/doclava/ClearPage.java
+++ b/src/com/google/doclava/ClearPage.java
@@ -107,7 +107,9 @@
       }
     }
     data.setValue("toroot", toroot);
-
+    if (Doclava.USE_UPDATED_TEMPLATES) {
+      data.setValue("useUpdatedTemplates", "true");
+    }
     data.setValue("filename", filename);
 
     if (!fullPath) {
@@ -157,7 +159,7 @@
     }
   }
 
-  public static void copyFile(boolean allowExcepted, File from, String toPath) {
+  public static void copyFile(boolean allowExcepted, File from, String toPath, Boolean append) {
     File to = new File(outputDir + "/" + toPath);
     FileInputStream in;
     FileOutputStream out;
@@ -172,9 +174,9 @@
     }
     ensureDirectory(to);
     try {
-      out = new FileOutputStream(to);
+      out = new FileOutputStream(to, append);
     } catch (IOException e) {
-      System.err.println(from.getAbsolutePath() + ": Error opening file");
+      System.err.println(to.getAbsolutePath() + ": Error opening file");
       return;
     }
     if (!isValidContentType(allowExcepted, toPath, DROIDDOC_VALID_CONTENT_TYPES)) {
diff --git a/src/com/google/doclava/DocFile.java b/src/com/google/doclava/DocFile.java
index cd3d17c..aad49b4 100644
--- a/src/com/google/doclava/DocFile.java
+++ b/src/com/google/doclava/DocFile.java
@@ -183,86 +183,231 @@
       // Strip out the intl and lang id substr and get back just the
       // guide, design, distribute, etc.
       filename = getPathRoot(filename);
-      if (filename.indexOf("design") == 0) {
-        hdf.setValue("design", "true");
-        hdf.setValue("page.type", "design");
-      } else if (filename.indexOf("develop") == 0) {
-        hdf.setValue("develop", "true");
-        hdf.setValue("page.type", "develop");
-      } else if (filename.indexOf("guide") == 0) {
-        hdf.setValue("guide", "true");
-        hdf.setValue("page.type", "guide");
-      } else if (filename.indexOf("training") == 0) {
-        hdf.setValue("training", "true");
-        hdf.setValue("page.type", "training");
-      } else if (filename.indexOf("more") == 0) {
-        hdf.setValue("more", "true");
-      } else if (filename.indexOf("google") == 0) {
-        hdf.setValue("google", "true");
-        hdf.setValue("page.type", "google");
-      } else if (filename.indexOf("samples") == 0) {
-        hdf.setValue("samples", "true");
-        hdf.setValue("page.type", "samples");
-        if (Doclava.samplesNavTree != null) {
-          hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", ""));
-        }
-      } else if (filename.indexOf("distribute") == 0) {
-        hdf.setValue("distribute", "true");
-        hdf.setValue("page.type", "distribute");
-        if (filename.indexOf("distribute/googleplay") == 0) {
-          hdf.setValue("googleplay", "true");
-        } else if (filename.indexOf("distribute/essentials") == 0) {
-          hdf.setValue("essentials", "true");
-        } else if (filename.indexOf("distribute/users") == 0) {
-          hdf.setValue("users", "true");
-        } else if (filename.indexOf("distribute/engage") == 0) {
-          hdf.setValue("engage", "true");
-        } else if (filename.indexOf("distribute/monetize") == 0) {
-          hdf.setValue("monetize", "true");
-        } else if (filename.indexOf("distribute/analyze") == 0) {
-          hdf.setValue("analyze", "true");
-        } else if (filename.indexOf("distribute/tools") == 0) {
-          hdf.setValue("essentials", "true");
-        } else if (filename.indexOf("distribute/stories") == 0) {
-          hdf.setValue("stories", "true");
-        }
-      } else if (filename.indexOf("about") == 0) {
-        hdf.setValue("about", "true");
-        hdf.setValue("page.type", "about");
-      } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
-        hdf.setValue("tools", "true");
-        hdf.setValue("page.type", "tools");
-        fromTemplate = hdf.getValue("page.template", "");
-      } else if (filename.indexOf("devices") == 0) {
-        hdf.setValue("devices", "true");
-        hdf.setValue("page.type", "devices");
-      } else if (filename.indexOf("source") == 0) {
-        hdf.setValue("source", "true");
-      } else if (filename.indexOf("accessories") == 0) {
-        hdf.setValue("accessories", "true");
-      } else if (filename.indexOf("compatibility") == 0) {
-        hdf.setValue("compatibility", "true");
-      } else if (filename.indexOf("wear") == 0) {
-        hdf.setValue("wear", "true");
-      } else if (filename.indexOf("preview") == 0) {
-        hdf.setValue("preview", "true");
-      } else if (filename.indexOf("auto") == 0) {
-        hdf.setValue("auto", "true");
-      } else if (filename.indexOf("tv") == 0) {
-        hdf.setValue("tv", "true");
-      } else if (filename.indexOf("ndk") == 0) {
-        hdf.setValue("ndk", "true");
-        hdf.setValue("page.type", "ndk");
-        if (filename.indexOf("ndk/guides") == 0) {
-          hdf.setValue("guide", "true");
-        } else if (filename.indexOf("ndk/reference") == 0) {
-          hdf.setValue("reference", "true");
-        } else if (filename.indexOf("ndk/samples") == 0) {
-          hdf.setValue("samples", "true");
-        } else if (filename.indexOf("ndk/downloads") == 0) {
-          hdf.setValue("downloads", "true");
-          fromTemplate = hdf.getValue("page.template", "");
 
+      if (Doclava.USE_UPDATED_TEMPLATES) {
+        //remap types to design, dev, distribute etc.
+        if (filename.indexOf("design") == 0) {
+          hdf.setValue("design", "true");
+          hdf.setValue("page.type", "design");
+          hdf.setValue("page.category", "design");
+        } else if (filename.indexOf("develop") == 0) {
+          hdf.setValue("develop", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "develop");
+        } else if (filename.indexOf("guide") == 0) {
+          hdf.setValue("guide", "true");
+          hdf.setValue("page.type", "develop");
+          if (filename.indexOf("guide/topics/manif") == 0) {
+            hdf.setValue("page.category", "app manifest");
+          } else {
+            hdf.setValue("page.category", "guide");
+          }
+        } else if (filename.indexOf("training") == 0) {
+          hdf.setValue("training", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "training");
+        } else if (filename.indexOf("more") == 0) {
+          hdf.setValue("more", "true");
+        } else if (filename.indexOf("google") == 0) {
+          hdf.setValue("google", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "google");
+        } else if (filename.indexOf("samples") == 0) {
+          hdf.setValue("samples", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "samples");
+          if (Doclava.samplesNavTree != null) {
+            hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", ""));
+          }
+        } else if (filename.indexOf("distribute") == 0) {
+          hdf.setValue("distribute", "true");
+          hdf.setValue("page.type", "distribute");
+          hdf.setValue("page.category", "distribute");
+          if (filename.indexOf("distribute/googleplay") == 0) {
+            hdf.setValue("page.category", "googleplay");
+            hdf.setValue("page.type", "distribute");
+            hdf.setValue("googleplay", "true");
+          } else if (filename.indexOf("distribute/essentials") == 0) {
+            hdf.setValue("page.category", "essentials");
+            hdf.setValue("essentials", "true");
+          } else if (filename.indexOf("distribute/users") == 0) {
+            hdf.setValue("page.category", "users");
+            hdf.setValue("users", "true");
+          } else if (filename.indexOf("distribute/engage") == 0) {
+            hdf.setValue("page.category", "engage");
+            hdf.setValue("engage", "true");
+          } else if (filename.indexOf("distribute/monetize") == 0) {
+            hdf.setValue("page.category", "monetize");
+            hdf.setValue("monetize", "true");
+          } else if (filename.indexOf("distribute/analyze") == 0) {
+            hdf.setValue("page.category", "analyze");
+            hdf.setValue("analyze", "true");
+          } else if (filename.indexOf("distribute/tools") == 0) {
+            hdf.setValue("page.category", "essentials");
+            hdf.setValue("essentials", "true");
+          } else if (filename.indexOf("distribute/stories") == 0) {
+            hdf.setValue("page.category", "stories");
+            hdf.setValue("stories", "true");
+          }
+        } else if (filename.indexOf("about") == 0) {
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+          hdf.setValue("page.category", "about");
+          if ((filename.indexOf("about/versions") == 0)) {
+            hdf.setValue("versions", "true");
+            hdf.setValue("page.category", "versions");
+          } else if ((filename.indexOf("wear") == 0)) {
+            hdf.setValue("wear", "true");
+            hdf.setValue("page.category", "wear");
+          } else if ((filename.indexOf("tv") == 0)) {
+            hdf.setValue("tv", "true");
+            hdf.setValue("page.category", "tv");
+          } else if ((filename.indexOf("auto") == 0)) {
+            hdf.setValue("auto", "true");
+            hdf.setValue("page.category", "auto");
+          }
+        } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
+          hdf.setValue("tools", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "tools");
+          fromTemplate = hdf.getValue("page.template", "");
+        } else if (filename.indexOf("devices") == 0) {
+          hdf.setValue("devices", "true");
+          hdf.setValue("page.type", "devices");
+        } else if (filename.indexOf("source") == 0) {
+          hdf.setValue("source", "true");
+        } else if (filename.indexOf("accessories") == 0) {
+          hdf.setValue("accessories", "true");
+        } else if (filename.indexOf("compatibility") == 0) {
+          hdf.setValue("compatibility", "true");
+        } else if (filename.indexOf("wear") == 0) {
+          hdf.setValue("wear", "true");
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+          hdf.setValue("page.category", "wear");
+        } else if (filename.indexOf("preview") == 0) {
+          hdf.setValue("preview", "true");
+          hdf.setValue("page.type", "develop");
+          hdf.setValue("page.category", "preview");
+        } else if (filename.indexOf("auto") == 0) {
+          hdf.setValue("auto", "true");
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+          hdf.setValue("page.category", "auto");
+        } else if (filename.indexOf("tv") == 0) {
+          hdf.setValue("tv", "true");
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+          hdf.setValue("page.category", "tv");
+        } else if (filename.indexOf("ndk") == 0) {
+          hdf.setValue("ndk", "true");
+          hdf.setValue("page.type", "ndk");
+          hdf.setValue("page.category", "ndk");
+          if (filename.indexOf("ndk/guides") == 0) {
+            hdf.setValue("guide", "true");
+            hdf.setValue("page.type", "ndk");
+            hdf.setValue("page.category", "guide");
+          } else if (filename.indexOf("ndk/reference") == 0) {
+            hdf.setValue("reference", "true");
+            hdf.setValue("page.type", "ndk");
+            hdf.setValue("page.category", "reference");
+          } else if (filename.indexOf("ndk/samples") == 0) {
+            hdf.setValue("samples", "true");
+            hdf.setValue("page.type", "ndk");
+            hdf.setValue("page.category", "samples");
+          } else if (filename.indexOf("ndk/downloads") == 0) {
+            hdf.setValue("downloads", "true");
+            hdf.setValue("page.type", "ndk");
+            hdf.setValue("page.category", "downloads");
+            fromTemplate = hdf.getValue("page.template", "");
+          }
+        } else {
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+          hdf.setValue("page.category", "about");
+        }
+      } else {
+        //support the old mappings
+        if (filename.indexOf("design") == 0) {
+          hdf.setValue("design", "true");
+          hdf.setValue("page.type", "design");
+        } else if (filename.indexOf("develop") == 0) {
+          hdf.setValue("develop", "true");
+          hdf.setValue("page.type", "develop");
+        } else if (filename.indexOf("guide") == 0) {
+          hdf.setValue("guide", "true");
+          hdf.setValue("page.type", "guide");
+        } else if (filename.indexOf("training") == 0) {
+          hdf.setValue("training", "true");
+          hdf.setValue("page.type", "training");
+        } else if (filename.indexOf("more") == 0) {
+          hdf.setValue("more", "true");
+        } else if (filename.indexOf("google") == 0) {
+          hdf.setValue("google", "true");
+          hdf.setValue("page.type", "google");
+        } else if (filename.indexOf("samples") == 0) {
+          hdf.setValue("samples", "true");
+          hdf.setValue("page.type", "samples");
+          if (Doclava.samplesNavTree != null) {
+            hdf.setValue("samples_toc_tree", Doclava.samplesNavTree.getValue("samples_toc_tree", ""));
+          }
+        } else if (filename.indexOf("distribute") == 0) {
+          hdf.setValue("distribute", "true");
+          hdf.setValue("page.type", "distribute");
+          if (filename.indexOf("distribute/googleplay") == 0) {
+            hdf.setValue("googleplay", "true");
+          } else if (filename.indexOf("distribute/essentials") == 0) {
+            hdf.setValue("essentials", "true");
+          } else if (filename.indexOf("distribute/users") == 0) {
+            hdf.setValue("users", "true");
+          } else if (filename.indexOf("distribute/engage") == 0) {
+            hdf.setValue("engage", "true");
+          } else if (filename.indexOf("distribute/monetize") == 0) {
+            hdf.setValue("monetize", "true");
+          } else if (filename.indexOf("distribute/analyze") == 0) {
+            hdf.setValue("analyze", "true");
+          } else if (filename.indexOf("distribute/tools") == 0) {
+            hdf.setValue("essentials", "true");
+          } else if (filename.indexOf("distribute/stories") == 0) {
+            hdf.setValue("stories", "true");
+          }
+        } else if (filename.indexOf("about") == 0) {
+          hdf.setValue("about", "true");
+          hdf.setValue("page.type", "about");
+        } else if ((filename.indexOf("tools") == 0) || (filename.indexOf("sdk") == 0)) {
+          hdf.setValue("tools", "true");
+          hdf.setValue("page.type", "tools");
+          fromTemplate = hdf.getValue("page.template", "");
+        } else if (filename.indexOf("devices") == 0) {
+          hdf.setValue("devices", "true");
+          hdf.setValue("page.type", "devices");
+        } else if (filename.indexOf("source") == 0) {
+          hdf.setValue("source", "true");
+        } else if (filename.indexOf("accessories") == 0) {
+          hdf.setValue("accessories", "true");
+        } else if (filename.indexOf("compatibility") == 0) {
+          hdf.setValue("compatibility", "true");
+        } else if (filename.indexOf("wear") == 0) {
+          hdf.setValue("wear", "true");
+        } else if (filename.indexOf("preview") == 0) {
+          hdf.setValue("preview", "true");
+        } else if (filename.indexOf("auto") == 0) {
+          hdf.setValue("auto", "true");
+        } else if (filename.indexOf("tv") == 0) {
+          hdf.setValue("tv", "true");
+        } else if (filename.indexOf("ndk") == 0) {
+          hdf.setValue("ndk", "true");
+          hdf.setValue("page.type", "ndk");
+          if (filename.indexOf("ndk/guides") == 0) {
+            hdf.setValue("guide", "true");
+          } else if (filename.indexOf("ndk/reference") == 0) {
+            hdf.setValue("reference", "true");
+          } else if (filename.indexOf("ndk/samples") == 0) {
+            hdf.setValue("samples", "true");
+          } else if (filename.indexOf("ndk/downloads") == 0) {
+            hdf.setValue("downloads", "true");
+            fromTemplate = hdf.getValue("page.template", "");
+          }
         }
       }
       //set metadata for this file in jd_lists_unified
diff --git a/src/com/google/doclava/Doclava.java b/src/com/google/doclava/Doclava.java
index e2f7d88..716d8ae 100644
--- a/src/com/google/doclava/Doclava.java
+++ b/src/com/google/doclava/Doclava.java
@@ -68,7 +68,8 @@
   public static final boolean SORT_BY_NAV_GROUPS = true;
   /* Debug output for PageMetadata, format urls from site root */
   public static boolean META_DBG=false;
-
+  /* Remove after updated templates are launched */
+  public static boolean USE_UPDATED_TEMPLATES = false;
   public static String outputPathBase = "/";
   public static ArrayList<String> inputPathHtmlDirs = new ArrayList<String>();
   public static ArrayList<String> inputPathHtmlDir2 = new ArrayList<String>();
@@ -165,7 +166,12 @@
       if (a[0].equals("-d")) {
         outputPathBase = outputPathHtmlDirs = ClearPage.outputDir = a[1];
       } else if (a[0].equals("-templatedir")) {
-        ClearPage.addTemplateDir(a[1]);
+        if (USE_UPDATED_TEMPLATES) {
+          /* remove with updated templates are launched */
+          ClearPage.addTemplateDir("build/tools/droiddoc/templates-sdk-dev");
+        } else {
+          ClearPage.addTemplateDir(a[1]);
+        }
       } else if (a[0].equals("-hdf")) {
         mHDFData.add(new String[] {a[1], a[2]});
       } else if (a[0].equals("-knowntags")) {
@@ -257,6 +263,8 @@
         offlineMode = true;
       } else if (a[0].equals("-metadataDebug")) {
         META_DBG = true;
+      } else if (a[0].equals("-useUpdatedTemplates")) {
+        USE_UPDATED_TEMPLATES = true;
       } else if (a[0].equals("-federate")) {
         try {
           String name = a[1];
@@ -392,7 +400,11 @@
   }
       // Write metadata for all processed files to jd_lists_unified.js in out dir
       if (!sTaglist.isEmpty()) {
-        PageMetadata.WriteList(sTaglist);
+        if (USE_UPDATED_TEMPLATES) {
+          PageMetadata.WriteListByLang(sTaglist);
+        } else {
+          PageMetadata.WriteList(sTaglist);
+        }
       }
     }
 
@@ -669,6 +681,9 @@
     if (option.equals("-metadataDebug")) {
       return 1;
     }
+    if (option.equals("-useUpdatedTemplates")) {
+      return 1;
+    }
     if (option.equals("-documentannotations")) {
       return 2;
     }
@@ -804,7 +819,8 @@
               Data data = makeHDF();
               String hdfValue = data.getValue("sac") == null ? "" : data.getValue("sac");
               boolean allowExcepted = hdfValue.equals("true") ? true : false;
-              ClearPage.copyFile(allowExcepted, f, templ);
+              boolean append = false;
+              ClearPage.copyFile(allowExcepted, f, templ, append);
         }
       } else if (f.isDirectory()) {
         writeDirectory(f, relative + f.getName() + "/", js);
diff --git a/src/com/google/doclava/PageMetadata.java b/src/com/google/doclava/PageMetadata.java
index 4c2a773..73668e2 100644
--- a/src/com/google/doclava/PageMetadata.java
+++ b/src/com/google/doclava/PageMetadata.java
@@ -109,6 +109,41 @@
   }
 
   /**
+  * Given a list of metadata nodes organized by lang, sort the
+  * root nodes by type name and render the types and their child
+  * metadata nodes to separate lang-specific json files in the out dir.
+  *
+  * @param rootNodesList A list of root metadata nodes, each
+  *        representing a type and it's member child pages.
+  */
+  public static void WriteListByLang(List<Node> rootNodesList) {
+    Collections.sort(rootNodesList, BY_LANG_NAME);
+    for (Node n : rootNodesList) {
+      String langFilename = "";
+      String langname = n.getLang();
+      langFilename = "_" + langname;
+      Collections.sort(n.getChildren(), BY_TYPE_NAME);
+      Node pageMeta = new Node.Builder().setLabel("TOP").setChildren(n.getChildren()).build();
+
+      StringBuilder buf = new StringBuilder();
+      // write the taglist to string format
+      pageMeta.renderLangResources(buf,langname);
+      //pageMeta.renderTypesByTag(buf);
+      // write the taglist to js file
+      Data data = Doclava.makeHDF();
+      data.setValue("reference_tree", buf.toString());
+      data.setValue("metadata.lang", langname);
+      // remove when updated templates are launched
+      String unifiedFilename = "jd_lists_unified" + langFilename + ".js";
+      String extrasFilename = "jd_extras" + langFilename + ".js";
+      // write out jd_lists_unified for each lang
+      ClearPage.write(data, "jd_lists_unified.cs", unifiedFilename);
+      // append jd_extras to jd_lists_unified for each lang, then delete.
+      appendExtrasMetadata(extrasFilename, unifiedFilename);
+    }
+  }
+
+  /**
   * Extract supported metadata values from a page and add them as
   * a child node of a root node based on type. Some metadata values
   * are normalized. Unsupported metadata fields are ignored. See
@@ -137,7 +172,7 @@
     if (!excludeNode) {
       Node pageMeta = new Node.Builder().build();
       pageMeta.setLabel(getTitleNormalized(hdf, "page.title"));
-      pageMeta.setTitleFriendly(hdf.getValue("page.titleFriendly",""));
+      pageMeta.setCategory(hdf.getValue("page.category",""));
       pageMeta.setSummary(hdf.getValue("page.metaDescription",""));
       pageMeta.setLink(getPageUrlNormalized(filename));
       pageMeta.setGroup(getStringValueNormalized(hdf,"sample.group"));
@@ -146,7 +181,12 @@
       pageMeta.setImage(getImageUrlNormalized(hdf.getValue("page.image", "")));
       pageMeta.setLang(getLangStringNormalized(hdf, filename));
       pageMeta.setType(getStringValueNormalized(hdf, "page.type"));
-      appendMetaNodeByType(pageMeta, tagList);
+      pageMeta.setTimestamp(hdf.getValue("page.timestamp",""));
+      if (Doclava.USE_UPDATED_TEMPLATES) {
+        appendMetaNodeByLang(pageMeta, tagList);
+      } else {
+        appendMetaNodeByType(pageMeta, tagList);
+      }
     }
   }
 
@@ -323,7 +363,9 @@
     StringBuilder outString =  new StringBuilder();
     String tagList = hdf.getValue(tag, "");
     tagList.replaceAll("\"", "");
-    if (!tagList.isEmpty()) {
+    if ("".equals(tagList)) {
+      return tagList;
+    } else {
       int end = tagList.indexOf(",");
       if (end != -1) {
         tagList = tagList.substring(0,end);
@@ -333,8 +375,8 @@
         tagList = tagList.toLowerCase();
       }
       outString.append(tagList.trim());
-    }
-    return outString.toString();
+      return outString.toString();
+    } 
   }
 
   /**
@@ -493,6 +535,37 @@
   * @param rootList The current list of root nodes.
   * @return The updated list of root nodes.
   */
+  public static List<Node> appendMetaNodeByLang(Node gNode, List<Node> rootList) {
+
+    String nodeLang = gNode.getLang();
+    boolean matched = false;
+    for (Node n : rootList) {
+      if (n.getLang().equals(nodeLang)) {  //find any matching lang node
+        appendMetaNodeByType(gNode,n.getChildren());
+        //n.getChildren().add(gNode);
+        matched = true;
+        break; // add to the first root node only
+      } // tag did not match
+    } // end rootnodes matching iterator
+    if (!matched) {
+      List<Node> mlangList = new ArrayList<Node>(); // list of file objects that have a given lang
+      //mlangList.add(gNode);
+      Node tnode = new Node.Builder().setChildren(mlangList).setLang(nodeLang).build();
+      rootList.add(tnode);
+      appendMetaNodeByType(gNode, mlangList);
+    }
+    return rootList;
+  }
+
+  /**
+  * Given a metadata node, add it as a child of a root node based on its
+  * type. If there is no root node that matches the node's type, create one
+  * and add the metadata node as a child node.
+  *
+  * @param gNode The node to attach to a root node or add as a new root node.
+  * @param rootList The current list of root nodes.
+  * @return The updated list of root nodes.
+  */
   public static List<Node> appendMetaNodeByType(Node gNode, List<Node> rootList) {
 
     String nodeTags = gNode.getType();
@@ -548,6 +621,28 @@
     return rootTagNodesList;
   }
 
+  /**
+  * Append the contents of jd_extras to jd_lists_unified for each language.
+  *
+  * @param extrasFilename The lang-specific extras file to append.
+  * @param unifiedFilename The lang-specific unified metadata file.
+  */
+  public static void appendExtrasMetadata (String extrasFilename, String unifiedFilename) {
+    File f = new File(ClearPage.outputDir + "/" + extrasFilename);
+    if (f.exists() && !f.isDirectory()) {
+      ClearPage.copyFile(true, f, unifiedFilename, true);
+      try {
+        if (f.delete()) {
+          if (Doclava.META_DBG) System.out.println("    >>>>> Delete succeeded");
+        } else {
+          if (Doclava.META_DBG) System.out.println("    >>>>> Delete failed");
+        }
+      } catch (Exception e) {
+        if (Doclava.META_DBG) System.out.println("    >>>>> Exception: " + e + "\n");
+      }
+    }
+  }
+
   public static final Comparator<Node> BY_TAG_NAME = new Comparator<Node>() {
     public int compare (Node one, Node other) {
       return one.getLabel().compareTo(other.getLabel());
@@ -560,13 +655,19 @@
     }
   };
 
+    public static final Comparator<Node> BY_LANG_NAME = new Comparator<Node>() {
+    public int compare (Node one, Node other) {
+      return one.getLang().compareTo(other.getLang());
+    }
+  };
+
   /**
   * A node for storing page metadata. Use Builder.build() to instantiate.
   */
   public static class Node {
 
     private String mLabel; // holds page.title or similar identifier
-    private String mTitleFriendly; // title for card or similar use
+    private String mCategory; // subtabs, example 'training' 'guides'
     private String mSummary; // Summary for card or similar use
     private String mLink; //link href for item click
     private String mGroup; // from sample.group in _index.jd
@@ -575,11 +676,12 @@
     private String mImage; // holds an href, fully qualified or relative to root
     private List<Node> mChildren;
     private String mLang;
-    private String mType; // can be file, dir, video show, announcement, etc.
+    private String mType; // design, develop, distribute, youtube, blog, etc
+    private String mTimestamp; // optional timestamp eg 1447452827
 
     private Node(Builder builder) {
       mLabel = builder.mLabel;
-      mTitleFriendly = builder.mTitleFriendly;
+      mCategory = builder.mCategory;
       mSummary = builder.mSummary;
       mLink = builder.mLink;
       mGroup = builder.mGroup;
@@ -589,16 +691,17 @@
       mChildren = builder.mChildren;
       mLang = builder.mLang;
       mType = builder.mType;
+      mTimestamp = builder.mTimestamp;
     }
 
     private static class Builder {
-      private String mLabel, mTitleFriendly, mSummary, mLink, mGroup, mImage, mLang, mType;
+      private String mLabel, mCategory, mSummary, mLink, mGroup, mImage, mLang, mType, mTimestamp;
       private List<String> mKeywords = null;
       private List<String> mTags = null;
       private List<Node> mChildren = null;
       public Builder setLabel(String mLabel) { this.mLabel = mLabel; return this;}
-      public Builder setTitleFriendly(String mTitleFriendly) {
-        this.mTitleFriendly = mTitleFriendly; return this;
+      public Builder setCategory(String mCategory) {
+        this.mCategory = mCategory; return this;
       }
       public Builder setSummary(String mSummary) {this.mSummary = mSummary; return this;}
       public Builder setLink(String mLink) {this.mLink = mLink; return this;}
@@ -611,6 +714,7 @@
       public Builder setChildren(List<Node> mChildren) {this.mChildren = mChildren; return this;}
       public Builder setLang(String mLang) {this.mLang = mLang; return this;}
       public Builder setType(String mType) {this.mType = mType; return this;}
+      public Builder setTimestamp(String mTimestamp) {this.mTimestamp = mTimestamp; return this;}
       public Node build() {return new Node(this);}
     }
 
@@ -631,6 +735,24 @@
         }
       }
     }
+
+    /**
+    * Render a tree of metadata nodes organized by lang.
+    * @param buf Output buffer to render to.
+    */
+    void renderLangResources(StringBuilder buf, String langname) {
+      List<Node> list = mChildren; //list of type rootnodes
+      if (list == null || list.size() == 0) {
+        buf.append("null");
+      } else {
+        final int n = list.size();
+        for (int i = 0; i < n; i++) {
+          buf.append("METADATA['" + langname + "']." + list.get(i).mType + " = [");
+          list.get(i).renderTypes(buf); //render this lang's children
+          buf.append("\n];\n\n");
+        }
+      }
+    }
     /**
     * Render all metadata nodes for a specific type.
     * @param buf Output buffer to render to.
@@ -643,16 +765,33 @@
         final int n = list.size();
         for (int i = 0; i < n; i++) {
           buf.append("\n      {\n");
-          buf.append("        \"title\":\"" + list.get(i).mLabel + "\",\n" );
-          buf.append("        \"titleFriendly\":\"" + list.get(i).mTitleFriendly + "\",\n" );
-          buf.append("        \"summary\":\"" + list.get(i).mSummary + "\",\n" );
+          buf.append("        \"title\":\"");
+          renderStrWithUcs(buf, list.get(i).mLabel);
+          buf.append("\",\n" );
+          buf.append("        \"summary\":\"");
+          renderStrWithUcs(buf, list.get(i).mSummary);
+          buf.append("\",\n" );
           buf.append("        \"url\":\"" + list.get(i).mLink + "\",\n" );
-          buf.append("        \"group\":\"" + list.get(i).mGroup + "\",\n" );
+          if (!"".equals(list.get(i).mImage)) {
+            buf.append("        \"image\":\"" + list.get(i).mImage + "\",\n" );
+          }
+          if (!"".equals(list.get(i).mGroup)) {
+            buf.append("        \"group\":\"");
+            renderStrWithUcs(buf, list.get(i).mGroup);
+            buf.append("\",\n" );
+          }
+          if (!"".equals(list.get(i).mCategory)) {
+            buf.append("        \"category\":\"" + list.get(i).mCategory + "\",\n" );
+          }
+          if ((list.get(i).mType != null) && (list.get(i).mType != "")) {
+            buf.append("        \"type\":\"" + list.get(i).mType + "\",\n");
+          }
           list.get(i).renderArrayType(buf, list.get(i).mKeywords, "keywords");
           list.get(i).renderArrayType(buf, list.get(i).mTags, "tags");
-          buf.append("        \"image\":\"" + list.get(i).mImage + "\",\n" );
-          buf.append("        \"lang\":\"" + list.get(i).mLang + "\",\n" );
-          buf.append("        \"type\":\"" + list.get(i).mType + "\"");
+          if (!"".equals(list.get(i).mTimestamp)) {
+            buf.append("        \"timestamp\":\"" + list.get(i).mTimestamp + "\",\n");
+          }
+          buf.append("        \"lang\":\"" + list.get(i).mLang + "\"" );
           buf.append("\n      }");
           if (i != n - 1) {
             buf.append(", ");
@@ -727,22 +866,7 @@
         final int n = list.size();
         for (int i = 0; i < n; i++) {
           String tagval = list.get(i).toString();
-          final int L = tagval.length();
-          for (int t = 0; t < L; t++) {
-            char c = tagval.charAt(t);
-            if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_HIGH_SURROGATE ) {
-              // we have a UTF-16 multi-byte character
-              int codePoint = tagval.codePointAt(t);
-              int charSize = Character.charCount(codePoint);
-              t += charSize - 1;
-              buf.append(String.format("\\u%04x",codePoint));
-            } else if (c >= ' ' && c <= '~' && c != '\\') {
-              buf.append(c);
-            } else { 
-              // we are encoding a two byte character
-              buf.append(String.format("\\u%04x", (int) c));
-            }
-          }
+          renderStrWithUcs(buf,tagval);
           if (i != n - 1) {
             buf.append(",");
           }
@@ -750,6 +874,31 @@
       }
     }
 
+    /**
+    * Render a string that can include ucs2 encoded characters.
+    * @param buf Output buffer to render to.
+    * @param chars String to append to buf with any necessary encoding
+    */
+    void renderStrWithUcs(StringBuilder buf, String chars) {
+      String strval = chars;
+      final int L = strval.length();
+      for (int t = 0; t < L; t++) {
+        char c = strval.charAt(t);
+        if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_HIGH_SURROGATE ) {
+          // we have a UTF-16 multi-byte character
+          int codePoint = strval.codePointAt(t);
+          int charSize = Character.charCount(codePoint);
+          t += charSize - 1;
+          buf.append(String.format("\\u%04x",codePoint));
+        } else if (c >= ' ' && c <= '~' && c != '\\') {
+          buf.append(c);
+        } else { 
+          // we are encoding a two byte character
+          buf.append(String.format("\\u%04x", (int) c));
+        }
+      }
+    }
+
     public String getLabel() {
       return mLabel;
     }
@@ -758,12 +907,12 @@
        mLabel = label;
     }
 
-    public String getTitleFriendly() {
-      return mTitleFriendly;
+    public String getCategory() {
+      return mCategory;
     }
 
-    public void setTitleFriendly(String title) {
-       mTitleFriendly = title;
+    public void setCategory(String title) {
+       mCategory = title;
     }
 
     public String getSummary() {
@@ -854,8 +1003,16 @@
       return mType;
     }
 
+    public String getTimestamp() {
+      return mTimestamp;
+    }
+
     public void setType(String type) {
       mType = type;
     }
+
+    public void setTimestamp(String timestamp) {
+      mTimestamp = timestamp;
+    }
   }
 }
diff --git a/src/com/google/doclava/SampleCode.java b/src/com/google/doclava/SampleCode.java
index 57f1c54..a16fd09 100644
--- a/src/com/google/doclava/SampleCode.java
+++ b/src/com/google/doclava/SampleCode.java
@@ -206,7 +206,7 @@
         if (inList(path, IMAGES)) {
           type = "img";
           if (f.length() < MAX_FILE_SIZE_BYTES) {
-            ClearPage.copyFile(false, f, path);
+            ClearPage.copyFile(false, f, path, false);
             writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension),
                 relative, type, true);
           } else {
@@ -220,7 +220,7 @@
         } else if (inList(path, VIDEOS)) {
           type = "video";
           if (f.length() < MAX_FILE_SIZE_BYTES) {
-            ClearPage.copyFile(false, f, path);
+            ClearPage.copyFile(false, f, path, false);
             writeImageVideoPage(f, convertExtension(path, Doclava.htmlExtension),
                 relative, type, true);
           } else {