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

laika.helium.generate.LandingPageGenerator.scala Maven / Gradle / Ivy

/*
 * Copyright 2012-2020 the original author or authors.
 *
 * 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 laika.helium.generate

import cats.data.Kleisli
import cats.syntax.all._
import cats.effect.Sync
import laika.ast.Path.Root
import laika.ast.{/, Document, Element, RootElement}
import laika.config.{ConfigException, LaikaKeys}
import laika.helium.config.LandingPage
import laika.rewrite.nav.TitleDocumentConfig
import laika.theme.Theme.TreeProcessor

private[helium] object LandingPageGenerator {

  def generate[F[_]: Sync] (landingPage: LandingPage): TreeProcessor[F] = Kleisli { tree =>
    
    val (landingPageContent, fragments, landingPageConfig) = tree.root.tree.content.collectFirst {
      case d: Document if d.path.withoutSuffix.name == "landing-page" => (d.content, d.fragments, d.config)
    }.getOrElse((RootElement.empty, Map.empty[String, Element], tree.root.config))
    
    val titleDocument = tree.root.titleDocument.fold(
      TitleDocumentConfig.inputName(tree.root.config).map { inputName =>
        Document(
          path = Root / inputName,
          content = landingPageContent, 
          fragments = fragments,
          config = landingPageConfig.withValue(LaikaKeys.versioned, false).build
        )
      }
    ) { titleDoc =>
      Right(titleDoc.copy(
        content = RootElement(titleDoc.content.content ++ landingPageContent.content),
        fragments = titleDoc.fragments ++ fragments,
        config = landingPageConfig.withFallback(titleDoc.config).withValue(LaikaKeys.versioned, false).build
      ))
    }
    
    val result = titleDocument.map { doc =>
      val titleDocWithTemplate =
        if (doc.config.hasKey(LaikaKeys.template)) doc
        else doc.copy(config = doc.config.withValue(LaikaKeys.template, "landing.template.html").build)
        
      // TODO - add API for replaceDocument, removeDocument, appendDocument, prependDocument
      tree.copy(root = tree.root.copy(tree = tree.root.tree.copy(
        titleDocument = Some(titleDocWithTemplate),
        content = tree.root.tree.content.filterNot(_.path.withoutSuffix.name == "landing-page")
      )))
    }
    
    Sync[F].fromEither(result.leftMap(ConfigException.apply))
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy