Skip to content

Commit

Permalink
Fix #98 and refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Clement Escoffier <clement.escoffier@gmail.com>
  • Loading branch information
cescoffier committed Apr 7, 2014
1 parent 0f013d3 commit a0bc4e4
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 45 deletions.
5 changes: 5 additions & 0 deletions extensions/wisdom-asciidoc-maven-plugin/pom.xml
Expand Up @@ -108,6 +108,11 @@
<artifactId>maven-shared-incremental</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -19,25 +19,29 @@
*/
package org.wisdom.asciidoc;

import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.NameFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.shared.filtering.MavenFileFilter;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.asciidoctor.*;
import org.wisdom.maven.Constants;
import org.wisdom.maven.WatchingException;
import org.wisdom.maven.mojos.AbstractWisdomWatcherMojo;
import org.wisdom.maven.utils.WatcherUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;


/**
* Transform Asciidoc files.
* Compiles Asciidoc files to HTML documents.
*/
@Mojo(name = "compile-asciidoc", threadSafe = false,
requiresDependencyResolution = ResolutionScope.COMPILE,
Expand Down Expand Up @@ -67,24 +71,29 @@ public class AsciidocMojo extends AbstractWisdomWatcherMojo implements Constants
@Parameter(property = "sourceHighlighter", required = false)
protected String sourceHighlighter = "";
@Parameter(property = "extensions")
protected List<String> extensions = new ArrayList<String>();
protected List<String> extensions = new ArrayList<>();
@Parameter(property = "embedAssets")
protected boolean embedAssets = false;
@Parameter
protected String stylesheet;
@Parameter
protected String stylesheetDir;
private File internalSources;
private File destinationForInternals;
private File externalSources;
private File destinationForExternals;
private Asciidoctor instance;
@Component
private MavenFileFilter mavenFileFilter;

File internalSources;
File destinationForInternals;
File externalSources;
File destinationForExternals;

Asciidoctor instance;

public void execute()
throws MojoExecutionException {
this.internalSources = new File(basedir, MAIN_RESOURCES_DIR);

if (extensions == null || extensions.isEmpty()) {
extensions = ImmutableList.of("ad", "asciidoc", "adoc");
}

this.internalSources = new File(basedir, MAIN_RESOURCES_DIR + "/assets");
this.destinationForInternals = new File(buildDirectory, "classes/assets");

this.externalSources = new File(basedir, ASSETS_SRC_DIR);
Expand All @@ -95,8 +104,7 @@ public void execute()
}

final OptionsBuilder optionsBuilderExternals = OptionsBuilder.options().compact(compact)
.safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter)
.inPlace(true);
.safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter).inPlace(true);

final OptionsBuilder optionsBuilderInternals = OptionsBuilder.options().compact(compact)
.safe(SafeMode.UNSAFE).eruby(eruby).backend(backend).docType(doctype).headerFooter(headerFooter).inPlace(true);
Expand Down Expand Up @@ -154,31 +162,46 @@ protected Asciidoctor getAsciidoctorInstance() throws MojoExecutionException {
}

private List<File> scanSourceFiles(File root) {
final List<File> files;
if (extensions == null || extensions.isEmpty()) {
final DirectoryWalker directoryWalker = new AsciiDocDirectoryWalker(root.getAbsolutePath());
files = directoryWalker.scan();
} else {
final DirectoryWalker directoryWalker = new CustomExtensionDirectoryWalker(root.getAbsolutePath(), extensions);
files = directoryWalker.scan();
}
return files;
final DirectoryWalker directoryWalker = new CustomExtensionDirectoryWalker(root.getAbsolutePath(), extensions);
return directoryWalker.scan();
}

protected void renderFile(Map<String, Object> options, File f) throws IOException {
getLog().info("Compiling Asciidoc file >> " + f.getName());
File filtered;
boolean unfiltered;

// Check whether the source was already copied to the destination directories (by the resource copy).
if (FilenameUtils.directoryContains(internalSources.getCanonicalPath(), f.getCanonicalPath())) {
filtered = findFileInDirectory(f, destinationForInternals);
} else {
filtered = findFileInDirectory(f, destinationForExternals);
}

if (filtered == null) {
// It was not copied.
getLog().error("Cannot find the filtered version of " + f.getAbsolutePath() + ", " +
"using unprocessed file.");
filtered = f;
unfiltered = true;
} else {
// It was copied.
unfiltered = false;
}

instance.renderFile(filtered, options);

// Move the file to the expected place if not filtered
if (unfiltered) {
String name = filtered.getName().substring(0, filtered.getName().lastIndexOf(".")) + ".html";
File output = new File(filtered.getParentFile(), name);
if (output.isFile()) {
// Move...
File finalFile = getOutputHTMLFile(filtered);
FileUtils.moveFile(output, finalFile);
} else {
getLog().error("Cannot find the output file for " + filtered.getAbsolutePath());
}
}
}

/**
Expand All @@ -189,6 +212,10 @@ protected void renderFile(Map<String, Object> options, File f) throws IOExceptio
* @return the found file or {@code null} if not found
*/
private File findFileInDirectory(File file, File directory) {
if (!directory.isDirectory()) {
return null;
}

Collection<File> files = FileUtils.listFiles(directory, new NameFileFilter(file.getName()),
TrueFileFilter.INSTANCE);

Expand All @@ -204,18 +231,7 @@ private File findFileInDirectory(File file, File directory) {

@Override
public boolean accept(File file) {
if (extensions == null || extensions.isEmpty()) {
return file.getName().endsWith(".ad") || file.getName().endsWith(".adoc") ||
file.getName().endsWith(".asciidoc");
} else {
for (String ext : extensions) {
if (file.getName().endsWith(ext)) {
return true;
}
}
return false;
}

return WatcherUtils.hasExtension(file, extensions);
}

@Override
Expand All @@ -235,7 +251,28 @@ public boolean fileUpdated(File file) throws WatchingException {

@Override
public boolean fileDeleted(File file) throws WatchingException {
// TODO Fix this, by deleting the previously generated files.
File output = getOutputHTMLFile(file);
if (output != null && output.isFile()) {
FileUtils.deleteQuietly(output);
}
return fileCreated(file);
}

private File getOutputHTMLFile(File input) {
File source;
File destination;
if (input.getAbsolutePath().startsWith(internalSources.getAbsolutePath())) {
source = internalSources;
destination = destinationForInternals;
} else if (input.getAbsolutePath().startsWith(externalSources.getAbsolutePath())) {
source = externalSources;
destination = destinationForExternals;
} else {
return null;
}

String fileName = input.getName().substring(0, input.getName().lastIndexOf(".")) + ".html";
String path = input.getParentFile().getAbsolutePath().substring(source.getAbsolutePath().length());
return new File(destination, path + "/" + fileName);
}
}
Expand Up @@ -19,6 +19,7 @@
*/
package org.wisdom.asciidoc;

import org.apache.commons.io.FilenameUtils;
import org.asciidoctor.AbstractDirectoryWalker;

import java.io.File;
Expand All @@ -36,13 +37,8 @@ public CustomExtensionDirectoryWalker(final String root, final List<String> exte
}

@Override
protected boolean isAcceptedFile(final File filename) {
final String name = filename.getName();
for (final String extension : extensions) {
if (name.endsWith(extension)) {
return true;
}
}
return false;
protected boolean isAcceptedFile(final File file) {
String extension = FilenameUtils.getExtension(file.getName());
return extensions.contains(extension);
}
}
@@ -0,0 +1,137 @@
/*
* #%L
* Wisdom-Framework
* %%
* Copyright (C) 2013 - 2014 Wisdom Framework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.wisdom.asciidoc;

import com.google.common.collect.ImmutableList;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Checks the behavior of the Asciidoc Mojo
*/
public class AsciidocMojoTest {

File basedir = new File("target/workbench/project");

@Before
public void setUp() {
FileUtils.deleteQuietly(basedir);
}

@Test
public void testInitializationWithNoFiles() throws MojoExecutionException {
AsciidocMojo mojo = new AsciidocMojo();
mojo.basedir = basedir;
mojo.buildDirectory = new File(mojo.basedir, "target");
mojo.execute();

assertThat(mojo.instance).isNotNull();
assertThat(mojo.getAsciidoctorInstance()).isNotNull();
assertThat(mojo.internalSources).isEqualTo(new File(mojo.basedir, "src/main/resources/assets"));
assertThat(mojo.externalSources).isEqualTo(new File(mojo.basedir, "src/main/assets"));
assertThat(mojo.destinationForInternals).isEqualTo(new File(mojo.buildDirectory, "classes/assets"));
assertThat(mojo.destinationForExternals).isEqualTo(new File(mojo.buildDirectory, "wisdom/assets"));
}

@Test
public void testInitializationWithUnfilteredInternalAndExternalFilesUsingRegularExtensions() throws
MojoExecutionException, IOException {
AsciidocMojo mojo = new AsciidocMojo();
mojo.basedir = basedir;
mojo.buildDirectory = new File(mojo.basedir, "target");
mojo.doctype = "article";
mojo.backend = "html5";
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"src/main/resources/assets/doc/hello.ad"));
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"src/main/assets/doc/hello.asciidoc"));
mojo.execute();

final File internal = new File(mojo.destinationForInternals, "doc/hello.html");
final File external = new File(mojo.destinationForExternals, "doc/hello.html");
assertThat(internal).isFile();
assertThat(external).isFile();

assertThat(FileUtils.readFileToString(internal)).contains("<h1>Hello, " +
"Wisdom!</h1>").contains("href=\"http://asciidoc.org\"");
assertThat(FileUtils.readFileToString(external)).contains("<h1>Hello, " +
"Wisdom!</h1>").contains("href=\"http://asciidoc.org\"");
}

@Test
public void testInitializationWithFilteredInternalAndExternalFilesUsingRegularExtensions() throws
MojoExecutionException, IOException {
AsciidocMojo mojo = new AsciidocMojo();
mojo.basedir = basedir;
mojo.buildDirectory = new File(mojo.basedir, "target");
mojo.doctype = "article";
mojo.backend = "html5";
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"src/main/resources/assets/doc/hello.ad"));
// Filtered version:
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"target/classes/assets/doc/hello.ad"));
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"src/main/assets/doc/hello.asciidoc"));
// Filtered version:
FileUtils.copyFile(new File("src/test/resources/hello.ad"), new File(basedir,
"target/wisdom/assets/doc/hello.asciidoc"));

mojo.execute();

final File internal = new File(mojo.destinationForInternals, "doc/hello.html");
final File external = new File(mojo.destinationForExternals, "doc/hello.html");
assertThat(internal).isFile();
assertThat(external).isFile();

assertThat(FileUtils.readFileToString(internal)).contains("<h1>Hello, " +
"Wisdom!</h1>").contains("href=\"http://asciidoc.org\"");
assertThat(FileUtils.readFileToString(external)).contains("<h1>Hello, " +
"Wisdom!</h1>").contains("href=\"http://asciidoc.org\"");
}

@Test
public void testAccept() throws MojoExecutionException {
AsciidocMojo mojo = new AsciidocMojo();
mojo.basedir = basedir;
mojo.buildDirectory = new File(mojo.basedir, "target");
// Regular extensions.
mojo.execute();

assertThat(mojo.accept(new File("hello.ad"))).isTrue();
assertThat(mojo.accept(new File("hello.adoc"))).isTrue();
assertThat(mojo.accept(new File("hello.asciidoc"))).isTrue();
assertThat(mojo.accept(new File("hello.html"))).isFalse();

mojo.extensions = ImmutableList.of("ascii");
// Regular extensions.
mojo.execute();
assertThat(mojo.accept(new File("hello.ad"))).isFalse();
assertThat(mojo.accept(new File("hello.ascii"))).isTrue();

}
}
@@ -0,0 +1,12 @@
= Hello, Wisdom!
Doc Writer <http://wisdom-framework.org>

An introduction to Asciidoc is available http://asciidoc.org[here].

== First Section

* item 1
* item 2

[source,java]
System.out.println("Hello Wisdom");

0 comments on commit a0bc4e4

Please sign in to comment.