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

Commit

Permalink
make the pagination steps a generic command instead of having a speci…
Browse files Browse the repository at this point in the history
…fic handling inside the resource processor
  • Loading branch information
syjer committed May 2, 2015
1 parent f304c8e commit ca80037
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 89 deletions.
6 changes: 2 additions & 4 deletions TODO.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
TODO:


- test embedded webserver
- expose start/stop in serveandwatch
- make the watchers work correctly!
- cover resource bundle / i18n part
- cover data directory support

- refactor processing pipeline: add an additional stage.
Currently for each resource we output directly the result. Instead, it would be better to first generate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (C) 2015 digitalfondue (info@digitalfondue.ch)
*
* 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.
*/
package ch.digitalfondue.stampo.processor;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import ch.digitalfondue.stampo.resource.FileResource;

public class DefaultDirective implements Directive {

@Override
public List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Locale locale,
Path defaultOutputPath) {
return Collections.singletonList(new PathAndModelSupplier(defaultOutputPath,
Collections::emptyMap));
}

@Override
public String name() {
return "default";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import ch.digitalfondue.stampo.taxonomy.Taxonomy;

// TODO: refactor!
public class DirPaginator extends Paginator {
public class DirPaginator extends Paginator implements Directive {

private static final Comparator<FileResource> NEW_FILE_FIRST = Comparator.comparingLong(
FileResource::getCreationTime).reversed();
Expand All @@ -60,7 +60,8 @@ public DirPaginator(



public List<PathAndModelSupplier> handlePagination(FileResource resource, Locale locale,
@Override
public List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Locale locale,
Path defaultOutputPath) {

List<PathAndModelSupplier> outpuPaths = new ArrayList<PathAndModelSupplier>();
Expand Down Expand Up @@ -190,6 +191,8 @@ private List<PathAndModelSupplier> handleStaticDir(FileResource resource, Path d
return toAdd;
}



@Override
public String name() {
return "dir-pagination";
}
}
29 changes: 29 additions & 0 deletions src/main/java/ch/digitalfondue/stampo/processor/Directive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2015 digitalfondue (info@digitalfondue.ch)
*
* 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.
*/
package ch.digitalfondue.stampo.processor;

import java.nio.file.Path;
import java.util.List;
import java.util.Locale;

import ch.digitalfondue.stampo.resource.FileResource;

public interface Directive {
List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Locale locale,
Path defaultOutputPath);

String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import ch.digitalfondue.stampo.ProcessedInputHandler;
import ch.digitalfondue.stampo.StampoGlobalConfiguration;
import ch.digitalfondue.stampo.exception.ConfigurationException;
import ch.digitalfondue.stampo.resource.Directory;
import ch.digitalfondue.stampo.resource.DirPaginationConfiguration;
import ch.digitalfondue.stampo.resource.FileMetadata;
import ch.digitalfondue.stampo.resource.FileResource;
import ch.digitalfondue.stampo.resource.TaxonomyPaginationConfiguration;
import ch.digitalfondue.stampo.taxonomy.Taxonomy;

// TODO: cleanup and break up the class
Expand All @@ -49,11 +47,11 @@ public class ResourceProcessor {
private final Directory root;
private final StampoGlobalConfiguration configuration;
private final Path outputDir;
private final DirPaginator dirPaginator;
private final TaxonomyPaginator taxonomyPaginator;
private final Taxonomy taxonomy;
private final Map<String, Directive> directives;

public ResourceProcessor(Path outputDir, Directory root, StampoGlobalConfiguration configuration, Taxonomy taxonomy) {
public ResourceProcessor(Path outputDir, Directory root, StampoGlobalConfiguration configuration,
Taxonomy taxonomy) {

this.root = root;
this.configuration = configuration;
Expand All @@ -63,11 +61,17 @@ public ResourceProcessor(Path outputDir, Directory root, StampoGlobalConfigurati
this.fileResourceProcessor = new FileResourceProcessor(configuration, outputDir, root);
this.layoutProcessor = new LayoutProcessor(configuration, root, fileResourceProcessor);

this.dirPaginator =
new DirPaginator(root, configuration, this::extractOutputPath,
(locale) -> (f, m) -> fileResourceProcessor.applyProcessors(f, locale, m), taxonomy);
this.taxonomyPaginator = new TaxonomyPaginator(root, configuration, this::extractOutputPath,
(locale) -> (f, m) -> fileResourceProcessor.applyProcessors(f, locale, m), taxonomy);
this.directives =
Arrays
.asList(
new DirPaginator(root, configuration, this::extractOutputPath,
(locale) -> (f, m) -> fileResourceProcessor.applyProcessors(f, locale, m),
taxonomy),//
new TaxonomyPaginator(root, configuration, this::extractOutputPath, (locale) -> (f,
m) -> fileResourceProcessor.applyProcessors(f, locale, m), taxonomy),//
new DefaultDirective())//
.stream()//
.collect(Collectors.toMap(Directive::name, Function.identity()));
}


Expand All @@ -78,30 +82,13 @@ public void process(FileResource resource, Locale locale, ProcessedInputHandler

Locale finalLocale = metadata.getOverrideLocale().orElse(locale);

Optional<DirPaginationConfiguration> dirPagination =
metadata.getDirectoryPaginationConfiguration();
Optional<TaxonomyPaginationConfiguration> taxonomyPagination =
metadata.getTaxonomyPaginationConfiguration();

if (dirPagination.isPresent() && taxonomyPagination.isPresent()) {
throw new ConfigurationException(resource.getPath(), "cannot have both "
+ FileMetadata.METADATA_PAGINATE_OVER_DIRECTORY + " and "
+ FileMetadata.METADATA_PAGINATE_OVER_TAXONOMY + " attribute");
}
List<PathAndModelSupplier> outputPaths;


Path defaultOutputPath = extractOutputPath(resource);

if (dirPagination.isPresent()) {
outputPaths = dirPaginator.handlePagination(resource, finalLocale, defaultOutputPath);
} else if (taxonomyPagination.isPresent()) {
outputPaths = taxonomyPaginator.handlePagination(resource, finalLocale, defaultOutputPath);
} else {
outputPaths =
Collections.singletonList(new PathAndModelSupplier(defaultOutputPath,
Collections::emptyMap));
}
List<PathAndModelSupplier> outputPaths =
directives.get(metadata.getDirective()).generateOutputPaths(resource, finalLocale,
defaultOutputPath);

//
outputPaths.forEach(outputPathAndModel -> processToPath(resource, outputHandler, finalLocale,
outputPathAndModel.getOutputPath(), outputPathAndModel.getModelSupplier()));
Expand Down Expand Up @@ -137,7 +124,8 @@ private void processToPath(FileResource resource, ProcessedInputHandler outputHa
}

Map<String, Object> model =
ModelPreparer.prepare(root, configuration, finalLocale, resource, outputPath, taxonomy, additionalData.get());
ModelPreparer.prepare(root, configuration, finalLocale, resource, outputPath, taxonomy,
additionalData.get());

FileResourceProcessorOutput processed =
fileResourceProcessor.applyProcessors(resource, finalLocale, model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import ch.digitalfondue.stampo.resource.TaxonomyPaginationConfiguration;
import ch.digitalfondue.stampo.taxonomy.Taxonomy;

public class TaxonomyPaginator extends Paginator {
public class TaxonomyPaginator extends Paginator implements Directive {

public TaxonomyPaginator(
Directory root,
Expand All @@ -42,7 +42,9 @@ public TaxonomyPaginator(
}


public List<PathAndModelSupplier> handlePagination(FileResource resource, Locale finalLocale, Path defaultOutputPath) {
@Override
public List<PathAndModelSupplier> generateOutputPaths(FileResource resource, Locale locale,
Path defaultOutputPath) {
Path parentDir = defaultOutputPath.getParent();

List<PathAndModelSupplier> outpuPaths = new ArrayList<>();
Expand All @@ -53,25 +55,31 @@ public List<PathAndModelSupplier> handlePagination(FileResource resource, Locale
Map<String, Map<String, List<FileResource>>> groups = taxonomy.getGroups();

if (groups.containsKey(conf.getTaxonomy())) {
outpuPaths.addAll(handleTaxonomyGroup(resource, finalLocale, parentDir, groups.get(conf.getTaxonomy()), conf));
outpuPaths.addAll(handleTaxonomyGroup(resource, locale, parentDir, groups.get(conf.getTaxonomy()), conf));
}

return outpuPaths;
}


private List<PathAndModelSupplier> handleTaxonomyGroup(FileResource resource, Locale finalLocale,
private List<PathAndModelSupplier> handleTaxonomyGroup(FileResource resource, Locale locale,
Path baseDir, Map<String, List<FileResource>> map, TaxonomyPaginationConfiguration conf) {

List<PathAndModelSupplier> toAdd = new ArrayList<>();

for (Entry<String, List<FileResource>> tagFiles : map.entrySet()) {
String finalDirName = tagFiles.getKey() + "/index.html";
toAdd.addAll(registerPaths(tagFiles.getValue(), baseDir.resolve(finalDirName), conf.getPageSize(),
resource, path -> (f -> toPageContent(f, finalLocale, path))));
resource, path -> (f -> toPageContent(f, locale, path))));
}

return toAdd;
}


@Override
public String name() {
return "taxonomy-pagination";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ public class FileMetadata {
public static final String METADATA_PAGINATE_MATCH = "paginate-match";
public static final String METADATA_PAGINATE_RECURSIVE = "paginate-recursive";
public static final String METADATA_PAGINATE_PAGE_SIZE = "paginate-page-size";
public static final String METADATA_DIRECTIVE = "directive";

public static final String METADATA_DATE = "date";

public static final int DEFAULT_PAGE_SIZE = 10;

private final Map<String, Object> metadata;
//
private final String directive;
private final Optional<Date> date;
private final Optional<List<Locale>> onlyForLocales;
private final Optional<String> overrideOutputToPath;
Expand All @@ -64,6 +66,7 @@ public FileMetadata(Map<String, Object> metadata) {
this.overrideLocale = ofNullable(metadata.get(METADATA_OVERRIDE_LOCALE)).map(Object::toString).map(Locale::forLanguageTag);
this.overrideLayout = ofNullable(metadata.get(METADATA_OVERRIDE_LAYOUT)).map(Object::toString);
this.overrideUseUglyUrl = ofNullable(metadata.get(METADATA_OVERRIDE_USE_UGLY_URL)).map(Boolean.class::cast);
this.directive = ofNullable(metadata.get(METADATA_DIRECTIVE)).map(Object::toString).orElse("default");
//
}

Expand Down Expand Up @@ -106,6 +109,10 @@ public Optional<Boolean> getOverrideUseUglyUrl() {
return overrideUseUglyUrl;
}

public String getDirective() {
return directive;
}

public Optional<DirPaginationConfiguration> getDirectoryPaginationConfiguration() {
return ofNullable(metadata.get(METADATA_PAGINATE_OVER_DIRECTORY)).map(Object::toString).map((dir) -> {
List<String> ignorePattern = ofNullable(metadata.get(METADATA_PAGINATE_MATCH)).map(TO_STRING_LIST).orElse(Collections.emptyList());
Expand Down
16 changes: 0 additions & 16 deletions src/test/java/ch/digitalfondue/stampo/ContentPaginationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,4 @@ public void paginationMissingDirToPaginateTest() throws IOException {
stampo.build();
}
}

/**
* Cannot have paginate-over-taxonomy and paginate-over-directory in the same page.
*
* @throws IOException
*/
@Test(expected = IllegalArgumentException.class)
public void paginationBothConfiguration() throws IOException {
try (InputOutputDirs iod = get()) {
write(iod.inputDir.resolve("content/index.html"),
fromTestResource("pagination/index-both-pagination.html.peb"));
Stampo stampo = new Stampo(iod.inputDir, iod.outputDir);
stampo.build();
}
}

}
1 change: 1 addition & 0 deletions src/test/resources/pagination/gallery.html.peb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
directive: dir-pagination
paginate-over-directory: static/gallery
paginate-page-size: 10
---
Expand Down
25 changes: 0 additions & 25 deletions src/test/resources/pagination/index-both-configuration.html.peb

This file was deleted.

1 change: 1 addition & 0 deletions src/test/resources/pagination/index-recursive.html.peb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
directive: dir-pagination
paginate-over-directory: content/post
paginate-page-size: 5
paginate-recursive: true
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/pagination/index.html.peb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
directive: dir-pagination
paginate-over-directory: content/post
paginate-page-size: 5
---
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/pagination/tags.html.peb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
directive: taxonomy-pagination
paginate-over-taxonomy: tags
paginate-page-size: 5
---
Expand Down

0 comments on commit ca80037

Please sign in to comment.