Skip to content

Commit

Permalink
Handling multiple base dirs, closes #114
Browse files Browse the repository at this point in the history
- not very cool solved, but when files are not
  in current base dir for project they will
  be rendered just as done formerly. Means
  of course that maybe images are not correctly
  shown, but at least content is there.
  
  Also users got the possiblity to close all 
  editors  open file and do refresh - this will
  reset the base directory
  • Loading branch information
de-jcup committed Dec 18, 2018
1 parent 75ea4b0 commit 3c716e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
package de.jcup.asciidoctoreditor;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.asciidoctor.Asciidoctor;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -63,7 +61,7 @@ public void convertToHTML(File asciiDocFile, long editorId, boolean useHiddenFil

context.setAsciidocFile(asciiDocFile);
if (useHiddenFile){
context.setFileToRender(AsciiDocFileUtils.createHiddenEditorFile(asciiDocFile,editorId,context.getBaseDir(), getTempFolder()));
context.setFileToRender(AsciiDocFileUtils.createHiddenEditorFile(logAdapter, asciiDocFile,editorId,context.getBaseDir(), getTempFolder()));
}else{
context.setFileToRender(asciiDocFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,27 @@ protected static File createSelfDeletingTempSubFolder(String tempId, String pare
return newTempSubFolder;
}

public static File createHiddenEditorFile(File asciidoctorFile, long editorId, File baseDir, Path tempFolder) throws IOException {
public static File createHiddenEditorFile(LogAdapter logAdapter, File asciidoctorFile, long editorId, File baseDir, Path tempFolder) throws IOException {
File hiddenEditorFile = new File(tempFolder.toFile(), editorId + "_hidden-editorfile_" + asciidoctorFile.getName());

String relativePath= calculatePathToFileFromBase(asciidoctorFile,baseDir);

StringBuilder sb= new StringBuilder();
sb.append("// origin :").append(asciidoctorFile.getAbsolutePath()).append("\n");
sb.append("// editor :").append(editorId).append("\n");
sb.append("// basedir:").append(baseDir.getAbsolutePath()).append("\n");

sb.append("include::").append(relativePath).append("[]\n");
try{
String relativePath= calculatePathToFileFromBase(asciidoctorFile,baseDir);
StringBuilder sb= new StringBuilder();
sb.append("// origin :").append(asciidoctorFile.getAbsolutePath()).append("\n");
sb.append("// editor :").append(editorId).append("\n");
sb.append("// basedir:").append(baseDir.getAbsolutePath()).append("\n");

sb.append("include::").append(relativePath).append("[]\n");

FileUtils.writeStringToFile(hiddenEditorFile, sb.toString(), "UTF-8",false);
hiddenEditorFile.deleteOnExit();
}catch(NotInsideCurrentBaseDirException e){
/* fallback to orign file - maybe something does not work but at least
* content will be shown!
*/
logAdapter.logWarn("File not in current base dir so copied origin as hidden file:"+asciidoctorFile.getAbsolutePath());
FileUtils.copyFile(asciidoctorFile, hiddenEditorFile);
}

FileUtils.writeStringToFile(hiddenEditorFile, sb.toString(), "UTF-8",false);
hiddenEditorFile.deleteOnExit();
return hiddenEditorFile;
}

Expand All @@ -98,7 +105,18 @@ static String calculatePathToFileFromBase(File asciidoctorFile, File baseDir) {
return unixAsciiDocFilePath.substring(unixBasePath.length());
}

return "pathProblems:"+unixAsciiDocFilePath+" not in "+unixBasePath;
throw new NotInsideCurrentBaseDirException("pathProblems:"+unixAsciiDocFilePath+" not in "+unixBasePath);
}


public static class NotInsideCurrentBaseDirException extends RuntimeException{

private static final long serialVersionUID = 1L;

public NotInsideCurrentBaseDirException(String string) {
super(string);
}


}
}

0 comments on commit 3c716e3

Please sign in to comment.