@Override
  public void execute() throws BuildException {
    checkMandatoryParameter("sourceDirectory", sourceDirectory);
    checkMandatoryParameter("outputDirectory", outputDirectory);

    ensureOutputExists();

    Asciidoctor asciidoctor = Asciidoctor.Factory.create(getClass().getClassLoader());

    AttributesBuilder attributesBuilder = buildAttributes();
    OptionsBuilder optionsBuilder = buildOptions();
    optionsBuilder.attributes(attributesBuilder.get());

    if (sourceDocumentName == null) {
      log(
          "Render asciidoc files from "
              + sourceDirectory
              + " to "
              + outputDirectory
              + " with backend="
              + backend);
      for (File file : scanSourceFiles()) {
        setDestinationPaths(optionsBuilder, file);
        asciidoctor.renderFile(file, optionsBuilder.get());
      }
    } else {
      log(
          "Render "
              + sourceDocumentName
              + " from "
              + sourceDirectory
              + " to "
              + outputDirectory
              + " with backend="
              + backend);
      File file = new File(sourceDirectory, sourceDocumentName);
      setDestinationPaths(optionsBuilder, file);
      asciidoctor.renderFile(file, optionsBuilder.get());
    }

    try {
      for (FileSet resource : resources) {
        File resourceDir = resource.getDir();
        String destPath =
            resourceDir
                .getCanonicalPath()
                .substring(new File(sourceDirectory).getCanonicalPath().length());
        File destResourceDir = new File(outputDirectory, destPath);
        destResourceDir.mkdirs();
        String[] includedFiles = resource.getDirectoryScanner(getProject()).getIncludedFiles();
        FileUtils.copyDirectory(
            resourceDir, destResourceDir, new ResourceFileFilter(includedFiles), false);
      }
    } catch (IOException e) {
      throw new BuildException("Error copying resources", e);
    }
  }
 private AttributesBuilder getAttributes(ConfigSection cfg, String revision) {
   AttributesBuilder ab =
       AttributesBuilder.attributes()
           .tableOfContents(cfg.getBoolean(KEY_INCLUDE_TOC, true))
           .sourceHighlighter("prettify");
   for (String name : attributes.stringPropertyNames()) {
     ab.attribute(name, attributes.getProperty(name));
   }
   ab.attribute("last-update-label!");
   ab.attribute("revnumber", revision);
   return ab;
 }
Example #3
0
 private Options getAsciiDocOptionsAndAttributes(ParserContext context) {
   Configuration config = context.getConfig();
   final AttributesBuilder attributes =
       attributes(config.getStringArray(Keys.ASCIIDOCTOR_ATTRIBUTES));
   if (config.getBoolean(Keys.ASCIIDOCTOR_ATTRIBUTES_EXPORT, false)) {
     final String prefix = config.getString(Keys.ASCIIDOCTOR_ATTRIBUTES_EXPORT_PREFIX, "");
     for (final Iterator<String> it = config.getKeys(); it.hasNext(); ) {
       final String key = it.next();
       if (!key.startsWith("asciidoctor")) {
         attributes.attribute(prefix + key.replace(".", "_"), config.getProperty(key));
       }
     }
   }
   final Configuration optionsSubset = config.subset(Keys.ASCIIDOCTOR_OPTION);
   final Options options = options().attributes(attributes.get()).get();
   for (final Iterator<String> iterator = optionsSubset.getKeys(); iterator.hasNext(); ) {
     final String name = iterator.next();
     options.setOption(name, guessTypeByContent(optionsSubset.getString(name)));
   }
   options.setBaseDir(context.getFile().getParentFile().getAbsolutePath());
   options.setSafe(UNSAFE);
   return options;
 }
 private AttributesBuilder buildAttributes() {
   AttributesBuilder attributesBuilder = AttributesBuilder.attributes();
   attributesBuilder.imagesDir(imagesDir);
   if (sourceHighlighter != null) {
     attributesBuilder.sourceHighlighter(sourceHighlighter);
   }
   if (embedAssets) {
     attributesBuilder.linkCss(false);
     attributesBuilder.dataUri(true);
   }
   attributesBuilder.copyCss(false);
   // TODO Figure out how to reliably set other values (like boolean values, dates, times, etc)
   for (Attribute attribute : attributes) {
     if ("true".equals(attribute.getValue()) || "false".equals(attribute.getValue())) {
       attributesBuilder.attribute(
           attribute.getKey(),
           Attributes.toAsciidoctorFlag(Boolean.valueOf(attribute.getValue())));
       continue;
     }
     // Can't do anything about dates and times because all that logic is private in Attributes
     attributesBuilder.attribute(attribute.getKey(), attribute.getValue());
   }
   return attributesBuilder;
 }