com.metreeca.mark.steps.Pug Maven / Gradle / Ivy
/*
* Copyright © 2019-2023 Metreeca srl
*
* 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 com.metreeca.mark.steps;
import com.metreeca.mark.Mark;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import de.neuland.pug4j.PugConfiguration;
import de.neuland.pug4j.exceptions.ExpressionException;
import de.neuland.pug4j.expression.ExpressionHandler;
import de.neuland.pug4j.model.PugModel;
import de.neuland.pug4j.template.TemplateLoader;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Pug template evaluation.
*/
public final class Pug {
private static final Pattern ExpressionPattern=Pattern.compile("\\\\?\\$\\{([.\\w]+)}");
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private final PugConfiguration pug;
public Pug(final Mark mark) {
if ( mark == null ) {
throw new NullPointerException("null mark");
}
this.pug=new PugConfiguration();
pug.setPrettyPrint(false); // minified output
pug.setTemplateLoader(new Loader(mark));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public Path write(final Path target, final Map model) {
if ( target == null ) {
throw new NullPointerException("null target");
}
if ( model == null ) {
throw new NullPointerException("null model");
}
final String layout=layout(model).toString();
try ( final BufferedWriter writer=Files.newBufferedWriter(target, UTF_8) ) {
pug.renderTemplate(pug.getTemplate(layout), set(model), writer);
} catch ( final IOException e ) {
throw new UncheckedIOException(e);
}
return target;
}
//// !!! refactor //////////////////////////////////////////////////////////////////////////////////////////////////
@SuppressWarnings("unchecked") private Object layout(final Map model) {
final Object page=model.get("page");
if ( page instanceof Map ) {
return ((Map)page).getOrDefault("layout", "");
} else {
return "";
}
}
@SuppressWarnings("unchecked") private Map set(final Map model) {
final Map _model=new HashMap<>(model);
_model.computeIfPresent("page", (p, page) -> {
if ( page instanceof Map ) {
final Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy