001package org.asciidoctor; 002 003import com.google.common.io.Files; 004import com.google.common.io.Resources; 005import com.sun.javadoc.DocErrorReporter; 006import com.sun.javadoc.Doclet; 007import com.sun.javadoc.LanguageVersion; 008import com.sun.javadoc.RootDoc; 009import org.asciidoctor.asciidoclet.*; 010 011import java.io.File; 012import java.io.IOException; 013import java.net.URL; 014 015/** 016 * = Asciidoclet 017 * 018 * https://github.com/asciidoctor/asciidoclet[Asciidoclet] is a Javadoc Doclet 019 * that uses http://asciidoctor.org[Asciidoctor] (via the 020 * https://github.com/asciidoctor/asciidoctorj[Asciidoctor Java integration]) 021 * to interpet http://asciidoc.org[AsciiDoc] markup within Javadoc comments. 022 * 023 * include::README.adoc[tags=usage] 024 * 025 * == Examples 026 * 027 * Custom attributes:: 028 * `+{project_name}+`;; {project_name} 029 * `+{project_desc}+`;; {project_desc} 030 * `+{project_version}+`;; {project_version} 031 * 032 * Code block (with syntax highlighting added by CodeRay):: 033 * + 034 * [source,java] 035 * -- 036 * /** 037 * * = Asciidoclet 038 * * 039 * * A Javadoc Doclet that uses http://asciidoctor.org[Asciidoctor] 040 * * to render http://asciidoc.org[AsciiDoc] markup in Javadoc comments. 041 * * 042 * * @author https://github.com/johncarl81[John Ericksen] 043 * *\/ 044 * public class Asciidoclet extends Doclet { 045 * private final Asciidoctor asciidoctor = Asciidoctor.Factory.create(); // <1> 046 * 047 * @SuppressWarnings("UnusedDeclaration") 048 * public static boolean start(RootDoc rootDoc) { 049 * new Asciidoclet().render(rootDoc); // <2> 050 * return Standard.start(rootDoc); 051 * } 052 * } 053 * -- 054 * <1> Creates an instance of the Asciidoctor Java integration 055 * <2> Runs Javadoc comment strings through Asciidoctor 056 * 057 * Inline code:: `code()` 058 * 059 * Headings:: 060 * + 061 * -- 062 * [float] 063 * = Heading 1 064 * 065 * [float] 066 * == Heading 2 067 * 068 * [float] 069 * === Heading 3 070 * 071 * [float] 072 * ==== Heading 4 073 * 074 * [float] 075 * ===== Heading 5 076 * -- 077 * 078 * Links:: 079 * Doc Writer <doc@example.com> + 080 * http://asciidoc.org[AsciiDoc] is a lightweight markup language. + 081 * Learn more about it at http://asciidoctor.org. + 082 * 083 * Bullets:: 084 * + 085 * -- 086 * .Unnumbered 087 * * bullet 088 * * bullet 089 * - bullet 090 * - bullet 091 * * bullet 092 * ** bullet 093 * ** bullet 094 * *** bullet 095 * *** bullet 096 * **** bullet 097 * **** bullet 098 * ***** bullet 099 * ***** bullet 100 * **** bullet 101 * *** bullet 102 * ** bullet 103 * * bullet 104 * -- 105 * + 106 * -- 107 * .Numbered 108 * . bullet 109 * . bullet 110 * .. bullet 111 * .. bullet 112 * . bullet 113 * .. bullet 114 * ... bullet 115 * ... bullet 116 * .... bullet 117 * .... bullet 118 * ... bullet 119 * ... bullet 120 * .. bullet 121 * .. bullet 122 * . bullet 123 * -- 124 * 125 * Tables:: 126 * + 127 * .An example table 128 * |=== 129 * |Column 1 |Column 2 |Column 3 130 * 131 * |1 132 * |Item 1 133 * |a 134 * 135 * |2 136 * |Item 2 137 * |b 138 * 139 * |3 140 * |Item 3 141 * |c 142 * |=== 143 * 144 * Sidebar block:: 145 * + 146 * .Optional Title 147 * **** 148 * Usage: Notes in a sidebar, naturally. 149 * **** 150 * 151 * Admonitions:: 152 * + 153 * IMPORTANT: Check this out! 154 * 155 * @author https://github.com/johncarl81[John Ericksen] 156 * @version {project_version} 157 * @see org.asciidoctor.Asciidoclet 158 * @since 0.1.0 159 * @serial (or @serialField or @serialData) 160 */ 161public class Asciidoclet extends Doclet { 162 163 private final RootDoc rootDoc; 164 private final DocletOptions docletOptions; 165 private final DocletIterator iterator; 166 private final Stylesheets stylesheets; 167 168 public Asciidoclet(RootDoc rootDoc) { 169 this.rootDoc = rootDoc; 170 this.docletOptions = new DocletOptions(rootDoc); 171 this.iterator = new DocletIterator(docletOptions); 172 this.stylesheets = new Stylesheets(docletOptions, rootDoc); 173 } 174 175 // test use 176 Asciidoclet(RootDoc rootDoc, DocletIterator iterator, Stylesheets stylesheets) { 177 this.rootDoc = rootDoc; 178 this.docletOptions = new DocletOptions(rootDoc); 179 this.iterator = iterator; 180 this.stylesheets = stylesheets; 181 } 182 183 /** 184 * .Example usage 185 * [source,java] 186 * exampleDeprecated("do not use"); 187 * 188 * @deprecated for example purposes 189 * @exception Exception example 190 * @throws RuntimeException example 191 * @serialData something else 192 * @link Asciidoclet 193 */ 194 public static void exampleDeprecated(String field) throws Exception{ 195 //noop 196 } 197 198 /** 199 * Sets the language version to Java 5. 200 * 201 * _Javadoc spec requirement._ 202 * 203 * @return language version number 204 */ 205 @SuppressWarnings("UnusedDeclaration") 206 public static LanguageVersion languageVersion() { 207 return LanguageVersion.JAVA_1_5; 208 } 209 210 /** 211 * Sets the option length to the standard Javadoc option length. 212 * 213 * _Javadoc spec requirement._ 214 * 215 * @param option input option 216 * @return length of required parameters 217 */ 218 @SuppressWarnings("UnusedDeclaration") 219 public static int optionLength(String option) { 220 return optionLength(option, new StandardAdapter()); 221 } 222 223 /** 224 * The starting point of Javadoc render. 225 * 226 * _Javadoc spec requirement._ 227 * 228 * @param rootDoc input class documents 229 * @return success 230 */ 231 @SuppressWarnings("UnusedDeclaration") 232 public static boolean start(RootDoc rootDoc) { 233 return new Asciidoclet(rootDoc).start(new StandardAdapter()); 234 } 235 236 /** 237 * Processes the input options by delegating to the standard handler. 238 * 239 * _Javadoc spec requirement._ 240 * 241 * @param options input option array 242 * @param errorReporter error handling 243 * @return success 244 */ 245 @SuppressWarnings("UnusedDeclaration") 246 public static boolean validOptions(String[][] options, DocErrorReporter errorReporter) { 247 return validOptions(options, errorReporter, new StandardAdapter()); 248 } 249 250 static int optionLength(String option, StandardAdapter standardDoclet) { 251 return DocletOptions.optionLength(option, standardDoclet); 252 } 253 254 static boolean validOptions(String[][] options, DocErrorReporter errorReporter, StandardAdapter standardDoclet) { 255 return DocletOptions.validOptions(options, errorReporter, standardDoclet); 256 } 257 258 boolean start(StandardAdapter standardDoclet) { 259 return run(standardDoclet) 260 && postProcess(); 261 } 262 263 private boolean run(StandardAdapter standardDoclet) { 264 AsciidoctorRenderer renderer = new AsciidoctorRenderer(docletOptions, rootDoc); 265 try { 266 return iterator.render(rootDoc, renderer) && 267 standardDoclet.start(rootDoc); 268 } finally { 269 renderer.cleanup(); 270 } 271 } 272 273 private boolean postProcess() { 274 if (docletOptions.stylesheet().isPresent()) return true; 275 return stylesheets.copy(); 276 } 277}