@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);
    }
  }
Beispiel #2
0
  @Override
  public Document parse(
      InputStream io,
      Optional<String> fileName,
      SentenceExtractor sentenceExtractor,
      RedPenTokenizer tokenizer)
      throws RedPenException {
    Document.DocumentBuilder documentBuilder = new Document.DocumentBuilder(tokenizer);
    fileName.ifPresent(documentBuilder::setFileName);
    BufferedReader reader = createReader(io);
    try {
      // create an asciidoctor instance
      Asciidoctor asciidoctor = Asciidoctor.Factory.create();

      // register our 'redpen' backend
      InputStream rubySource =
          new ByteArrayInputStream(AsciiDoctorRedPenRubySource.SOURCE_TEXT.getBytes("UTF-8"));
      asciidoctor.rubyExtensionRegistry().loadClass(rubySource);

      // set our 'redpen' backend as the active AsciiDoctor backend
      Map<String, Object> options = new HashMap<>();
      options.put("backend", "redpen");

      // tell AsciiDoctor to record the source line numbers
      options.put("sourcemap", true);
      Map<String, Object> attributes = new HashMap<>();
      attributes.put("sourcemap", true);
      options.put("attributes", attributes);

      // register our documentbuilding TreeProcessor
      asciidoctor
          .javaExtensionRegistry()
          .treeprocessor(new RedPenTreeProcessor(documentBuilder, sentenceExtractor, options));

      try {
        // trigger the tree processor, which will consequently fill the documentBuilder
        asciidoctor.readDocumentStructure(reader, options);
      } catch (Exception e) {
        LOG.error("Asciidoctor parser error: " + e.getMessage());
      }

    } catch (ParsingTimeoutException e) {
      throw new RedPenException("Failed to parse timeout: ", e);
    } catch (Exception e) {
      throw new RedPenException("Exception when configuring AsciiDoctor", e);
    }

    return documentBuilder.build();
  }
  @Override
  public String format(
      String projectName,
      String path,
      String revision,
      String abbrRev,
      ConfigSection globalCfg,
      String raw)
      throws IOException {
    if (!globalCfg.getBoolean(KEY_ALLOW_HTML, false)) {
      raw = suppressHtml(raw);
    }

    ConfigSection projectCfg = formatters.getFormatterConfig(NAME, projectName);
    String html =
        Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
            .convert(raw, createOptions(projectCfg, abbrRev));
    return util.applyCss(html, NAME, projectName);
  }
 private Asciidoctor getEngine() {
   try {
     lock.readLock().lock();
     if (engine == null) {
       lock.readLock().unlock();
       try {
         lock.writeLock().lock();
         if (engine == null) {
           LOGGER.info("Initializing Asciidoctor engine...");
           engine = Asciidoctor.Factory.create();
           LOGGER.info("Asciidoctor engine initialized.");
         }
       } finally {
         lock.readLock().lock();
         lock.writeLock().unlock();
       }
     }
   } finally {
     lock.readLock().unlock();
   }
   return engine;
 }