Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
add page number, prev/next pagination info
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed May 16, 2015
1 parent 1df49c1 commit 4f02c5d
Showing 1 changed file with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;

import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -33,6 +34,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import ch.digitalfondue.stampo.PathUtils;
import ch.digitalfondue.stampo.StampoGlobalConfiguration;
import ch.digitalfondue.stampo.resource.Directory;
import ch.digitalfondue.stampo.resource.DirectoryResource;
Expand Down Expand Up @@ -73,9 +75,10 @@ public List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Loc
String path = ofNullable(resource.getMetadata().getRawMap().get("include-all"))
.map(String.class::cast).orElseThrow(IllegalArgumentException::new);

Path p = configuration.getBaseDirectory().resolve(path);
if (!p.startsWith(configuration.getBaseDirectory())) {
throw new IllegalArgumentException();// cannot be outside
Path baseDirectory = configuration.getBaseDirectory();
Path p = baseDirectory.resolve(path);
if (!p.startsWith(baseDirectory)) {
throw new IllegalArgumentException(p+" must be inside of the basedirectory: " + baseDirectory);// cannot be outside
}

Directory toIncludeAllDir = new LocaleAwareDirectory(locale, new RootResource(resourceFactory, p), FileResourceWithMetadataSection::new);
Expand All @@ -84,11 +87,54 @@ public List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Loc

int maxDepth = (Integer) resource.getMetadata().getRawMap().getOrDefault("paginate-at-depth", 1);

List<PathAndModelSupplier> res = new ArrayList<>();
List<FlattenedStructuredDocument> res = new ArrayList<>();
doc.toOutputPaths(new OutputPathsEnv(maxDepth, locale, resource), res);
return res;

addPaginationInfoToModel(res);

return res.stream().map(f -> new PathAndModelSupplier(f.path, () -> f.model)).collect(toList());
}

private void addPaginationInfoToModel(List<FlattenedStructuredDocument> res) {
final int resCount = res.size();
for (int i = 0; i < resCount; i++) {
FlattenedStructuredDocument current = res.get(i);
String previousPageUrl = i > 0 ? PathUtils.relativePathTo(res.get(i-1).path, current.path) : null;
String nextPageUrl = i < resCount -1 ? PathUtils.relativePathTo(res.get(i+1).path, current.path) : null;
current.model.put("pagination", new Pagination(i + 1, resCount, previousPageUrl, nextPageUrl));
}
}

public static class Pagination {
private final int page;
private final int total;
private final String previousPageUrl;
private final String nextPageUrl;

public Pagination(int page, int total, String previousPageUrl, String nextPageUrl) {
this.page = page;
this.total = total;
this.previousPageUrl = previousPageUrl;
this.nextPageUrl = nextPageUrl;
}

public int getPage() {
return page;
}

public int getTotal() {
return total;
}

public String getPreviousPageUrl() {
return previousPageUrl;
}

public String getNextPageUrl() {
return nextPageUrl;
}
}

private static class OutputPathsEnv {
final int maxDepth;
final Locale locale;
Expand All @@ -101,6 +147,16 @@ public OutputPathsEnv(int maxDepth, Locale locale, FileResource resource) {
}
}

private static class FlattenedStructuredDocument {
final Path path;
final Map<String, Object> model;

FlattenedStructuredDocument(Path path, Map<String, Object> model) {
this.path = path;
this.model = model;
}
}


private class StructuredDocument {
final int depth;
Expand All @@ -126,7 +182,7 @@ private class StructuredDocument {
this.relativeToBasePath = basePath.relativize(resource.getPath());
}

void toOutputPaths(OutputPathsEnv env, List<PathAndModelSupplier> res) {
void toOutputPaths(OutputPathsEnv env, List<FlattenedStructuredDocument> res) {

FileResource v = fileResource.orElseGet(() -> new FileResourcePlaceHolder(relativeToBasePath, configuration));

Expand All @@ -136,22 +192,23 @@ void toOutputPaths(OutputPathsEnv env, List<PathAndModelSupplier> res) {
Path finalOutputPathForResource = outputPathExtractor.apply(virtualResource);

StringBuilder sb = new StringBuilder();

Map<String, Object> model = ModelPreparer.prepare(root, configuration, env.locale, virtualResource, finalOutputPathForResource, taxonomy);

sb.append(renderFile(env.locale, model));

if(depth < env.maxDepth) {

Map<String, Object> modelForSupplier = ModelPreparer.prepare(root, configuration, env.locale, virtualResource, finalOutputPathForResource, taxonomy, Collections.singletonMap("includeAllResult", sb.toString()));
res.add(new FlattenedStructuredDocument(finalOutputPathForResource, modelForSupplier));

childDocuments.forEach(sd -> sd.toOutputPaths(env, res));
} else if (depth == env.maxDepth ) {//cutoff point
renderChildDocuments(env.locale, model).forEach(sb::append);

Map<String, Object> modelForSupplier = ModelPreparer.prepare(root, configuration, env.locale, virtualResource, finalOutputPathForResource, taxonomy, Collections.singletonMap("includeAllResult", sb.toString()));
res.add(new FlattenedStructuredDocument(finalOutputPathForResource, modelForSupplier));
}

Map<String, Object> modelForSupplier = ModelPreparer.prepare(root, configuration, env.locale, virtualResource, finalOutputPathForResource, taxonomy, Collections.singletonMap("includeAllResult", sb.toString()));
res.add(new PathAndModelSupplier(finalOutputPathForResource, () -> modelForSupplier));
}


String renderFile(Locale locale, Map<String, Object> model) {
return fileResource.map(f -> resourceProcessor.apply(locale).apply(f, model).getContent()).orElse("");
}
Expand Down

0 comments on commit 4f02c5d

Please sign in to comment.