Skip to content

Commit

Permalink
Made refresh more smart, closes #114
Browse files Browse the repository at this point in the history
- cached basedir in provider is also refreshed now
- created base class for providers
  every registered provider is now doing a reset
  when proviate context reset is triggered
  • Loading branch information
de-jcup committed Dec 18, 2018
1 parent 3c716e3 commit afdc8b6
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.jcup.asciidoctoreditor.provider;

public abstract class AbstractAsciiDoctorProvider {

private AsciiDoctorProviderContext context;

AbstractAsciiDoctorProvider(AsciiDoctorProviderContext context){
if (context==null ){
throw new IllegalArgumentException("context may never be null!");
}
this.context=context;
}

AsciiDoctorProviderContext getContext() {
return context;
}

protected abstract void reset() ;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,34 @@
import org.asciidoctor.DirectoryWalker;
import org.asciidoctor.ast.DocumentHeader;

public class AsciiDoctorAttributesProvider {
public class AsciiDoctorAttributesProvider extends AbstractAsciiDoctorProvider{

private Map<String, Object> cachedAttributes;
private AsciiDoctorProviderContext context;

AsciiDoctorAttributesProvider(AsciiDoctorProviderContext context){
if (context==null ){
throw new IllegalArgumentException("context may never be null!");
}
this.context=context;
super(context);
}

protected Map<String, Object> getCachedAttributes() {
if (cachedAttributes == null) {
cachedAttributes = resolveAttributes(context.getBaseDir());
cachedAttributes = resolveAttributes(getContext().getBaseDir());
}
return cachedAttributes;
}

protected Map<String, Object> resolveAttributes(File baseDir) {
context.getLogAdapter().resetTimeDiff();
getContext().getLogAdapter().resetTimeDiff();
Map<String, Object> map = new HashMap<>();
Set<DocumentHeader> documentIndex = new HashSet<DocumentHeader>();
DirectoryWalker directoryWalker = new AsciiDocDirectoryWalker(baseDir.getAbsolutePath());

for (File file : directoryWalker.scan()) {
documentIndex.add(context.getAsciiDoctor().readDocumentHeader(file));
documentIndex.add(getContext().getAsciiDoctor().readDocumentHeader(file));
}
for (DocumentHeader header : documentIndex) {
map.putAll(header.getAttributes());
}
context.getLogAdapter().logTimeDiff("resolved attributes from base dir:"+baseDir);
getContext().getLogAdapter().logTimeDiff("resolved attributes from base dir:"+baseDir);
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
import java.io.File;
import java.io.FileFilter;

public class AsciiDoctorBaseDirectoryProvider {
public class AsciiDoctorBaseDirectoryProvider extends AbstractAsciiDoctorProvider {
private static FileFilter ADOC_FILE_FILTER = new ADocFilter();
private AsciiDoctorProviderContext context;

AsciiDoctorBaseDirectoryProvider(AsciiDoctorProviderContext context) {
if (context == null) {
throw new IllegalArgumentException("context may never be null!");
}
this.context = context;
super(context);
}

private File cachedBaseDir;
Expand All @@ -39,7 +35,7 @@ private File findBaseDir(File startFrom) {
}

private File findBaseDirNotCached(File startFrom) {
context.getLogAdapter().resetTimeDiff();
getContext().getLogAdapter().resetTimeDiff();
File file = resolveUnSaveBaseDir(startFrom);
File tempFolder = new File(System.getProperty("java.io.tmpdir"));
if (tempFolder.equals(file)){
Expand All @@ -48,7 +44,7 @@ private File findBaseDirNotCached(File startFrom) {
*/
throw new IllegalStateException("Tempfolder may never be the base dir folder!");
}
context.getLogAdapter().logTimeDiff("findBaseDirNotCached, started from:"+startFrom+", result:"+file);
getContext().getLogAdapter().logTimeDiff("findBaseDirNotCached, started from:"+startFrom+", result:"+file);
return file;
}

Expand Down Expand Up @@ -106,7 +102,7 @@ private boolean hasValidFileEnding(File file) {
}

public File findBaseDir() {
File asciiDocFile = context.getAsciiDocFile();
File asciiDocFile = getContext().getAsciiDocFile();
if (asciiDocFile == null) {
throw new IllegalStateException("No asciidoc file set!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@

import java.io.File;

public class AsciiDoctorDiagramProvider {
public class AsciiDoctorDiagramProvider extends AbstractAsciiDoctorProvider{

private AsciiDoctorProviderContext context;

public AsciiDoctorDiagramProvider(AsciiDoctorProviderContext context) {
if (context==null ){
throw new IllegalArgumentException("context may never be null!");
}
this.context = context;
super(context);
}

public File getDiagramRootDirectory() {
return context.getBaseDir();
return getContext().getBaseDir();
}

@Override
protected void reset() {
/* nothing todo */
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,31 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

public class AsciiDoctorImageProvider {
public class AsciiDoctorImageProvider extends AbstractAsciiDoctorProvider{
private static ImageFilesFilter IMAGE_FILTER = new ImageFilesFilter();
private String cachedSourceImagesPath;
private AsciiDoctorProviderContext context;

AsciiDoctorImageProvider(AsciiDoctorProviderContext context) {
if (context==null ){
throw new IllegalArgumentException("context may never be null!");
}
this.context = context;
super(context);
}

private void copyImagesToOutputFolder(String sourcePath, File target) {
context.getLogAdapter().resetTimeDiff();
getContext().getLogAdapter().resetTimeDiff();
File cachedImagesFile = new File(sourcePath);
if (!cachedImagesFile.exists()) {
return;
}
try {
FileUtils.copyDirectory(cachedImagesFile, target, IMAGE_FILTER);
} catch (IOException e) {
context.getLogAdapter().logError("Cannot copy images", e);
getContext().getLogAdapter().logError("Cannot copy images", e);
}
context.getLogAdapter().logTimeDiff("copied images to output folder:"+sourcePath);
getContext().getLogAdapter().logTimeDiff("copied images to output folder:"+sourcePath);

}

public void ensureImages() {
Path outputFolder = context.getOutputFolder();
Path outputFolder = getContext().getOutputFolder();
if (outputFolder==null){
throw new IllegalStateException("no output folder set!");
}
Expand All @@ -61,13 +57,13 @@ public void ensureImages() {
targetImagesDir.deleteOnExit();
}
copyImagesToOutputFolder(getCachedSourceImagesPath(), targetImagesDir);
context.targetImagesDir=targetImagesDir;
getContext().targetImagesDir=targetImagesDir;

}

public String getCachedSourceImagesPath() {
if (cachedSourceImagesPath == null) {
cachedSourceImagesPath = resolveImagesDirPath(context.getBaseDir());
cachedSourceImagesPath = resolveImagesDirPath(getContext().getBaseDir());
}
return cachedSourceImagesPath;
}
Expand Down Expand Up @@ -111,8 +107,8 @@ public boolean accept(File file) {
}

protected String resolveImagesDirPath(File baseDir) {
context.getLogAdapter().resetTimeDiff();
Object imagesDir = context.getAttributesProvider().getCachedAttributes().get("imagesdir");
getContext().getLogAdapter().resetTimeDiff();
Object imagesDir = getContext().getAttributesProvider().getCachedAttributes().get("imagesdir");

String imagesDirPath = null;
if (imagesDir != null) {
Expand All @@ -125,7 +121,7 @@ protected String resolveImagesDirPath(File baseDir) {
/* fallback when not defined - as defined at https://asciidoctor.org/docs/asciidoctor-pdf/#image-paths*/
imagesDirPath = baseDir.getAbsolutePath();
}
context.getLogAdapter().logTimeDiff("resolveImagesDirPath, baseDir:"+baseDir);
getContext().getLogAdapter().logTimeDiff("resolveImagesDirPath, baseDir:"+baseDir);
return imagesDirPath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,34 @@
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;

public class AsciiDoctorOptionsProvider {

private AsciiDoctorProviderContext context;
public class AsciiDoctorOptionsProvider extends AbstractAsciiDoctorProvider {

AsciiDoctorOptionsProvider(AsciiDoctorProviderContext context) {
if (context == null) {
throw new IllegalArgumentException("context may never be null!");
}
this.context = context;
super(context);
}

public Map<String, Object> createDefaultOptions() {
/* @formatter:off*/
Attributes attrs;
Path outputFolder = context.getOutputFolder();
Path outputFolder = getContext().getOutputFolder();
if (outputFolder==null){
throw new IllegalStateException("output folder not defined");
}
context.getImageProvider().ensureImages();
getContext().getImageProvider().ensureImages();

AttributesBuilder attrBuilder = AttributesBuilder.
attributes().
showTitle(true).
sourceHighlighter("coderay").
attribute("eclipse-editor-basedir",context.getBaseDir().getAbsolutePath()).
attribute("imagesoutdir", createAbsolutePath(context.targetImagesDir.toPath())).
attribute("eclipse-editor-basedir",getContext().getBaseDir().getAbsolutePath()).
attribute("imagesoutdir", createAbsolutePath(getContext().targetImagesDir.toPath())).
attribute("icons", "font").
attribute("source-highlighter","coderay").
attribute("coderay-css", "style").
attribute("env", "eclipse").
attribute("env-eclipse");
/* @formatter:on*/
Map<String, Object> cachedAttributes = context.getAttributesProvider().getCachedAttributes();
Map<String, Object> cachedAttributes = getContext().getAttributesProvider().getCachedAttributes();
for (String key : cachedAttributes.keySet()) {
Object value = cachedAttributes.get(key);
if (value != null && value.toString().isEmpty()) {
Expand All @@ -70,23 +65,23 @@ public Map<String, Object> createDefaultOptions() {
attrBuilder.attribute(key, value);
}
}
if (context.isTOCVisible()) {
if (getContext().isTOCVisible()) {
attrBuilder.attribute("toc", "left");
if (context.tocLevels > 0) {
attrBuilder.attribute("toclevels", "" + context.tocLevels);
if (getContext().tocLevels > 0) {
attrBuilder.attribute("toclevels", "" + getContext().tocLevels);
}
}
attrBuilder.imagesDir(context.targetImagesDir.getAbsolutePath());
attrBuilder.imagesDir(getContext().targetImagesDir.getAbsolutePath());

attrs = attrBuilder.get();
attrs.setAttribute("outdir", createAbsolutePath(outputFolder));

File destionationFolder = outputFolder.toFile();

OptionsBuilder opts = OptionsBuilder.options().toDir(destionationFolder).safe(SafeMode.UNSAFE).backend("html5")
.headerFooter(context.isTOCVisible()).
.headerFooter(getContext().isTOCVisible()).

attributes(attrs).option("sourcemap", "true").baseDir(context.getBaseDir());
attributes(attrs).option("sourcemap", "true").baseDir(getContext().getBaseDir());
/* @formatter:on*/
return opts.asMap();
}
Expand All @@ -96,7 +91,7 @@ protected String createAbsolutePath(Path path) {
}

public void reset() {

/* nothing to do*/
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package de.jcup.asciidoctoreditor.provider;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.asciidoctor.Asciidoctor;

import de.jcup.asciidoctoreditor.LogAdapter;
Expand Down Expand Up @@ -92,30 +91,33 @@ public void setAsciidocFile(File asciidocFile) {

protected void init() {
logAdapter.resetTimeDiff();
attributesProvider = new AsciiDoctorAttributesProvider(this);
attributesProvider = register(new AsciiDoctorAttributesProvider(this));
logAdapter.logTimeDiff("time to create attributes provider");
imageProvider = new AsciiDoctorImageProvider(this);
imageProvider = register(new AsciiDoctorImageProvider(this));
logAdapter.logTimeDiff("time to create images provider");
optionsProvider = new AsciiDoctorOptionsProvider(this);
optionsProvider = register(new AsciiDoctorOptionsProvider(this));
logAdapter.logTimeDiff("time to create options provider");
baseDirProvider = new AsciiDoctorBaseDirectoryProvider(this);
baseDirProvider = register(new AsciiDoctorBaseDirectoryProvider(this));
logAdapter.logTimeDiff("time to create base dir provider");
diagramProvider = new AsciiDoctorDiagramProvider(this);
diagramProvider = register(new AsciiDoctorDiagramProvider(this));
logAdapter.logTimeDiff("time to create diagram provider");
}

public void setOutputFolder(Path outputFolder) {
this.outputFolder = outputFolder;
}

/**
* Reset context. After this method is called all cached operations will be recalculated on next rendering time fo editor content
*/
public void reset() {
this.baseDir = null;
this.outputFolder = null;
this.asciidocFile = null;

this.attributesProvider.reset();
this.optionsProvider.reset();
this.imageProvider.reset();
for (AbstractAsciiDoctorProvider provider: providers){
provider.reset();
}
}

public Asciidoctor getAsciiDoctor() {
Expand Down Expand Up @@ -165,4 +167,11 @@ public File getFileToRender() {
return fileToRender;
}

private Set<AbstractAsciiDoctorProvider> providers = new LinkedHashSet<>();

public <T extends AbstractAsciiDoctorProvider> T register(T provider) {
providers.add(provider);
return provider;
}

}

0 comments on commit afdc8b6

Please sign in to comment.