Skip to content

Commit

Permalink
adjusted asciidoc confluence page tests to use production api (instea…
Browse files Browse the repository at this point in the history
…d of input stream-based api just for testing - required writing of temporary files for asciidoc pages in order to create proper path to asciidoc page and copying of example documentation structure to temporary folder in order to avoid creation of non-versioned files in project sources)
  • Loading branch information
cstettler committed Aug 23, 2017
1 parent ca25b54 commit ea1a23b
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import org.asciidoctor.ast.Title;
import org.sahli.asciidoc.confluence.publisher.converter.AsciidocPagesStructureProvider.AsciidocPage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
Expand All @@ -38,6 +36,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.nio.file.Files.exists;
import static java.nio.file.Files.isDirectory;
import static java.nio.file.Files.newInputStream;
import static java.util.Arrays.stream;
import static java.util.Collections.unmodifiableMap;
Expand Down Expand Up @@ -86,21 +86,22 @@ public Map<String, String> attachments() {
return unmodifiableMap(this.attachments);
}

public static AsciidocConfluencePage newAsciidocConfluencePage(AsciidocPage asciidocPage, Path templatesDir, Path pageAssetsFolder) throws IOException {
return newAsciidocConfluencePage(newInputStream(asciidocPage.path()), templatesDir.toString(), pageAssetsFolder.toString(), asciidocPage.path());
}

public static AsciidocConfluencePage newAsciidocConfluencePage(InputStream adoc, String templatesDir, String imagesOutDir, Path pagePath) {
Map<String, String> attachmentCollector = new HashMap<>();
public static AsciidocConfluencePage newAsciidocConfluencePage(AsciidocPage asciidocPage, Path templatesDir, Path pageAssetsFolder) {
try {
Path asciidocPagePath = asciidocPage.path();
String asciidocContent = readFull(newInputStream(asciidocPagePath));

String adocContent = readFull(adoc);
Map<String, String> attachmentCollector = new HashMap<>();

Options options = options(templatesDir, parentFolder(pagePath), imagesOutDir);
String pageContent = convertedContent(adocContent, options, pagePath, attachmentCollector);
Options options = options(templatesDir, asciidocPagePath.getParent(), pageAssetsFolder);
String pageContent = convertedContent(asciidocContent, options, asciidocPagePath, attachmentCollector);

String pageTitle = pageTitle(adocContent);
String pageTitle = pageTitle(asciidocContent);

return new AsciidocConfluencePage(pageTitle, pageContent, attachmentCollector);
return new AsciidocConfluencePage(pageTitle, pageContent, attachmentCollector);
} catch (IOException e) {
throw new RuntimeException("Could not create asciidoc confluence page", e);
}
}

private static String deriveAttachmentName(String path) {
Expand Down Expand Up @@ -157,30 +158,24 @@ private static String pageTitle(String pageContent) {
.orElseThrow(() -> new RuntimeException("top-level heading or title meta information must be set"));
}

private static File parentFolder(Path pagePath) {
return pagePath.getParent().toFile();
}

private static Options options(String templateDir, File baseDir, String imagesOutDir) {
File templateDirFolder = new File(templateDir);

if (!templateDirFolder.exists()) {
private static Options options(Path templatesFolder, Path baseFolder, Path generatedAssetsTargetFolder) {
if (!(exists(templatesFolder))) {
throw new RuntimeException("templateDir folder does not exist");
}

if (!templateDirFolder.isDirectory()) {
if (!(isDirectory(templatesFolder))) {
throw new RuntimeException("templateDir folder is not a folder");
}

Map<String, Object> attributes = new HashMap<>();
attributes.put("imagesoutdir", imagesOutDir);
attributes.put("outdir", imagesOutDir);
attributes.put("imagesoutdir", generatedAssetsTargetFolder.toString());
attributes.put("outdir", generatedAssetsTargetFolder.toString());

return OptionsBuilder.options()
.backend("html")
.safe(UNSAFE)
.baseDir(baseDir)
.templateDirs(templateDirFolder)
.baseDir(baseFolder.toFile())
.templateDirs(templatesFolder.toFile())
.attributes(attributes)
.get();
}
Expand Down

0 comments on commit ea1a23b

Please sign in to comment.