All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.varmateo.yawg.html.HtmlPageBaker Maven / Gradle / Ivy

/**************************************************************************
 *
 * Copyright (c) 2016-2020 Yawg project contributors.
 *
 **************************************************************************/

package com.varmateo.yawg.html;

import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Pattern;

import io.vavr.control.Try;

import com.varmateo.yawg.api.Result;
import com.varmateo.yawg.spi.PageBakeResult;
import com.varmateo.yawg.spi.PageBaker;
import com.varmateo.yawg.spi.PageContext;
import com.varmateo.yawg.spi.Template;
import com.varmateo.yawg.spi.TemplateContext;
import com.varmateo.yawg.util.FileUtils;
import com.varmateo.yawg.util.PageBakeResults;
import com.varmateo.yawg.util.Results;


/**
 * A Baker that transforms HTML files into other HTML
 * files.
 */
public final class HtmlPageBaker
        implements PageBaker {


    private static final String NAME = "html";

    private static final Pattern RE_HTML = Pattern.compile(".*\\.html$");

    private static final String TARGET_EXTENSION = ".html";


    private HtmlPageBaker() {
        // Nothing to do.
    }


    /**
     *
     */
    public static PageBaker create() {

        return new HtmlPageBaker();
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public String shortName() {

        return NAME;
    }


    /**
     * Checks if the given file name has one of the known extensions.
     *
     * 

The following extensions will be allowed:

* *
    *
  • .adoc
  • *
* * @return True if the given file name has one of the allowed * extensions. */ @Override public boolean isBakeable(final Path path) { return FileUtils.isNameMatch(path, RE_HTML); } /** * Converts the given text file in HTML format into another HTML * file processed through the template engine. * *

The target directory must already exist. Otherwise an * exception will be thrown.

* * @param sourcePath The file to be baked. * * @param context Provides the template for generating the target * document. If no template is provided, then the source document * is just copied to the target directory. * * @param targetDir The directory where the baked file will be * copied to. * * @return A result signaling success of failure. */ @Override public PageBakeResult bake( final Path sourcePath, final PageContext context, final Path targetDir) { final Try result = doBake(sourcePath, context, targetDir); return PageBakeResults.fromTry(result); } /** * */ private Try doBake( final Path sourcePath, final PageContext context, final Path targetDir) { final Path targetPath = getTargetPath(sourcePath, targetDir); final Optional