Make the documentation embedding work.

Bug: Issue 2199
Change-Id: Ib0e6d412f3e5523834b4b977728551e2be0d3a86
diff --git a/Documentation/asciidoc.defs b/Documentation/asciidoc.defs
index e2de785..44313d2 100644
--- a/Documentation/asciidoc.defs
+++ b/Documentation/asciidoc.defs
@@ -23,6 +23,7 @@
   EXPN = '.expn'
 
   asciidoc = [
+      'cd $SRCDIR;',
       '$(exe //lib/asciidoctor:asciidoc)',
       '-z', '$OUT',
       '--in-ext', '".txt%s"' % EXPN,
@@ -33,7 +34,7 @@
   for attribute in attributes:
     asciidoc.extend(['-a', attribute])
   asciidoc.append('$SRCS')
-  newsrcs = []
+  newsrcs = ["doc.css"]
   newdeps = deps + ['//lib/asciidoctor:asciidoc']
 
   for src in srcs:
diff --git a/lib/asciidoctor/java/AsciiDoctor.java b/lib/asciidoctor/java/AsciiDoctor.java
index a5bf492..0613ff4 100644
--- a/lib/asciidoctor/java/AsciiDoctor.java
+++ b/lib/asciidoctor/java/AsciiDoctor.java
@@ -29,6 +29,7 @@
 import org.asciidoctor.AttributesBuilder;
 import org.asciidoctor.Options;
 import org.asciidoctor.OptionsBuilder;
+import org.asciidoctor.SafeMode;
 import org.asciidoctor.internal.JRubyAsciidoctor;
 
 import org.kohsuke.args4j.Argument;
@@ -75,15 +76,16 @@
     return basename + outExt;
   }
 
-  private Options createOptions(File tmpFile) {
+  private Options createOptions(File outputFile) {
     OptionsBuilder optionsBuilder = OptionsBuilder.options();
 
-    optionsBuilder.backend(backend).docType(DOCTYPE).eruby(ERUBY);
+    optionsBuilder.backend(backend).docType(DOCTYPE).eruby(ERUBY)
+      .safe(SafeMode.UNSAFE);
     // XXX(fishywang): ideally we should just output to a string and add the
     // content into zip. But asciidoctor will actually ignore all attributes if
     // not output to a file. So we *have* to output to a file then read the
     // content of the file into zip.
-    optionsBuilder.toFile(tmpFile);
+    optionsBuilder.toFile(outputFile);
 
     AttributesBuilder attributesBuilder = AttributesBuilder.attributes();
     attributesBuilder.attributes(getAttributes());
@@ -127,12 +129,18 @@
 
     ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
     for (String inputFile : inputFiles) {
-      File tmp = File.createTempFile("doc", ".html");
-      Options options = createOptions(tmp);
+      if (!inputFile.endsWith(inExt)) {
+        // We have to use UNSAFE mode in order to make embedding work. But in
+        // UNSAFE mode we'll also need css file in the same directory, so we
+        // have to add css files into the SRCS.
+        continue;
+      }
+      String outName = mapInFileToOutFile(inputFile, inExt, outExt);
+      File out = new File(outName);
+      Options options = createOptions(out);
       renderInput(options, inputFile);
 
-      String outputFile = mapInFileToOutFile(inputFile, inExt, outExt);
-      zipFile(tmp, outputFile, zip);
+      zipFile(out, outName, zip);
     }
     zip.close();
   }