View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.doc.maven.post;
18  
19  import org.apache.commons.io.IOUtils;
20  
21  import org.apache.maven.plugin.MojoExecutionException;
22  import org.forgerock.doc.maven.AbstractDocbkxMojo;
23  import org.forgerock.doc.maven.utils.HtmlUtils;
24  import org.forgerock.doc.maven.utils.BootstrapCopier;
25  import org.forgerock.doc.maven.utils.NameUtils;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.HashMap;
30  
31  /**
32   * HTML post-processor for Bootstrap-formatted HTML formats.
33   */
34  public class Bootstrap {
35  
36      /**
37       * The Mojo that holds configuration and related methods.
38       */
39      private AbstractDocbkxMojo m;
40  
41      /**
42       * Constructor setting the Mojo that holds the configuration.
43       *
44       * @param mojo The Mojo that holds the configuration.
45       */
46      public Bootstrap(final AbstractDocbkxMojo mojo) {
47          m = mojo;
48  
49          outputDirectories = new String[1];
50          outputDirectories[0] = "";
51      }
52  
53      /**
54       * Post-processes Bootstrap formats.
55       *
56       * @throws MojoExecutionException Failed to post-process Bootstrap format.
57       */
58      public void execute() throws MojoExecutionException {
59  
60          // Add HtmlForBootstrap files.
61          final File htmlDir = new File(m.getDocbkxOutputDirectory(),
62                  "bootstrap");
63  
64          String[] outputDirectories = new String[m.getDocNames().size()];
65  
66          int i = 0;
67          for (final String docName : m.getDocNames()) {
68  
69              final File docDir = new File(htmlDir, docName);
70  
71              // If PDFs are also being built, edit Bootstrap HTML with a link
72              if (m.getFormats().contains(AbstractDocbkxMojo.Format.pdf)) {
73                  addPDFLink(docDir.getPath(), docName);
74              }
75  
76              // Example:
77              // ${project.build.directory}/docbkx/html/my-book
78              outputDirectories[i] = docDir.getPath();
79              ++i;
80  
81          }
82          editBuiltHtml(htmlDir.getPath());
83  
84          if (m.isDraftMode().equals("yes")) {
85              addDraftAlert(htmlDir.getPath());
86          }
87  
88          BootstrapCopier copier =
89                  new BootstrapCopier(outputDirectories);
90          try {
91              copier.copy();
92          } catch (IOException e) {
93              throw new MojoExecutionException(
94                      "Failed to copy files: " + e.getMessage(), e);
95          }
96  
97      }
98  
99      /**
100      * Add essentials to the built Bootstrap HTML.
101      *
102      * <p>
103      *
104      * - Add Google Analytics tracking code to the Bootstrap HTML
105      *
106      *
107      * @param htmlDir Directory under which to find Bootstrap output.
108      * @throws MojoExecutionException Something went wrong when updating HTML.
109      */
110     final void editBuiltHtml(final String htmlDir) throws
111             MojoExecutionException {
112         try {
113             HashMap<String, String> replacements = new HashMap<String, String>();
114 
115             String gascript = IOUtils.toString(
116                     getClass().getResourceAsStream("/endhead-ga.txt"), "UTF-8");
117             gascript = gascript.replace("ANALYTICS-ID", m.getGoogleAnalyticsId());
118             replacements.put("</head>", gascript);
119 
120             HtmlUtils.updateHtml(htmlDir, replacements);
121         } catch (IOException e) {
122             throw new MojoExecutionException(
123                     "Failed to update output HTML correctly: " + e.getMessage());
124         }
125     }
126 
127 
128 
129     final void addDraftAlert(final String htmlDir) throws
130         MojoExecutionException {
131         try {
132             HashMap<String, String> replacements = new HashMap<String, String>();
133             String draftAlert = IOUtils.toString(
134                     getClass().getResourceAsStream("/endbody-draftalert.txt"), "UTF-8");
135             replacements.put("</body>", draftAlert);
136             HtmlUtils.updateHtml(htmlDir, replacements);
137         } catch (IOException e) {
138             throw new MojoExecutionException(
139                     "Failed to update output HTML correctly: " + e.getMessage());
140         }
141     }
142 
143     /**
144      * Add a link to the PDF in the built Bootstrap HTML.
145      *
146      * <p>
147      *
148      * If both Bootstrap and PDF formats are being built, link to the PDFs
149      * from the Bootstrap.
150      *
151      *
152      * @param htmlDir Directory under which to find Bootstrap output.
153      * @param docName The short name of the document, for example "dev-guide".
154      * @throws MojoExecutionException Something went wrong when updating HTML.
155      */
156     final void addPDFLink(final String htmlDir, final String docName) throws
157             MojoExecutionException {
158         try {
159             HashMap<String, String> replacements = new HashMap<String, String>();
160 
161             String linkToPdf = getLinkToPdf(docName);
162             replacements.put("<ul id=\"pdf-link\">", linkToPdf);
163 
164             HtmlUtils.updateHtml(htmlDir, replacements);
165         } catch (IOException e) {
166             throw new MojoExecutionException(
167                     "Failed to inject PDF link HTML correctly: " + e.getMessage());
168         }
169     }
170 
171     private String getLinkToPdf(final String docName) {
172         // Note: No closing UL required, it's already in the HTML
173         String link = "<ul id=\"pdf-link\" "
174             + "class=\"nav navbar-nav navbar-right hidden-xs\">"
175             + "<li><a href=\"PDF-URL\" target=\"_blank\">"
176             + "<span class=\"glyphicon glyphicon-save\"></span> "
177             + "Download PDF Version</a></li>";
178 
179         String pdfUrl = "../../" + NameUtils.renameDoc(m.getProjectName(),
180                 docName, "pdf");
181         link = link.replaceFirst("PDF-URL", pdfUrl);
182 
183         return link;
184     }
185 
186     /**
187      * Directories where scripts and CSS are to be added.
188      */
189     private String[] outputDirectories;
190 }