<commit_before>/*
 * Copyright 2012-2019 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.io.config

import cats.effect.Async
import laika.config.Config.IncludeMap
import laika.io.runtime.Runtime
import laika.parse.hocon.IncludeResource

/**
  * @author <NAME>
  */
object IncludeHandler {
  
  case class RequestedInclude(resource: IncludeResource, parent: Option[IncludeResource])
  
  def load[F[_]: Async : Runtime] (includes: Seq[RequestedInclude]): F[IncludeMap] =
    Async[F].pure(Map.empty) // TODO - 0.13 - implement
  
}<commit_msg>implement IncludeHandler (without resolving relative paths) - #114<commit_after>/*
 * Copyright 2012-2019 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.io.config

import cats.implicits._
import cats.effect.Async
import laika.config.Config.IncludeMap
import laika.config.{ConfigParser, ConfigResourceError}
import laika.io.runtime.Runtime
import laika.parse.hocon.{IncludeAny, IncludeClassPath, IncludeFile, IncludeResource, IncludeUrl}

/**
  * @author <NAME>
  */
object IncludeHandler {
  
  case class RequestedInclude(resource: IncludeResource, parent: Option[IncludeResource])
  case class LoadedInclude(resource: IncludeResource, result: Either[ConfigResourceError, String])
  
  def load[F[_]: Async : Runtime] (includes: Seq[RequestedInclude]): F[IncludeMap] = 
    
    if (includes.isEmpty) Async[F].pure(Map.empty) else {
      
      def prepareFile(include: IncludeFile, parent: Option[IncludeResource]): F[(IncludeResource, IncludeResource)] = Async[F].pure((include, include))
      
      def prepareClasspath(include: IncludeClassPath, parent: Option[IncludeResource]): F[(IncludeResource, IncludeResource)] = Async[F].pure((include, include))
      
      def prepareUrl(include: IncludeUrl, parent: Option[IncludeResource]): F[(IncludeResource, IncludeResource)] = Async[F].pure((include, include))

      def prepareAny(include: IncludeAny, parent: Option[IncludeResource]): F[(IncludeResource, IncludeResource)] = Async[F].pure((include, include))
      
      val preparedIncludes = includes.map {
        case RequestedInclude(i: IncludeFile, parent)      => prepareFile(i, parent)
        case RequestedInclude(i: IncludeClassPath, parent) => prepareClasspath(i, parent)
        case RequestedInclude(i: IncludeUrl, parent)       => prepareUrl(i, parent)
        case RequestedInclude(i: IncludeAny, parent)       => prepareAny(i, parent)
      }.toVector.sequence
    
      def result(resource: IncludeResource, result: F[Option[Either[ConfigResourceError, String]]]): F[Option[LoadedInclude]] =
        result.map(_.map(res => LoadedInclude(resource, res)))
      
      preparedIncludes.flatMap { includes =>
        Runtime[F].runParallel(
          includes.map {
            case (IncludeFile(resourceId, _), orig)      => result(orig, ResourceLoader.loadFile(resourceId.value))
            case (IncludeClassPath(resourceId, _), orig) => result(orig, ResourceLoader.loadClasspathResource(resourceId.value))
            case (IncludeUrl(resourceId, _), orig)       => result(orig, ResourceLoader.loadClasspathResource(resourceId.value))
            case _                                       => Async[F].pure(Option.empty[LoadedInclude])
          }
        )
      }.flatMap { loadedResources =>
        val configParsers = loadedResources.unite.map(loaded => (loaded.resource, loaded.result.map(ConfigParser.parse)))
        val recursiveIncludes = configParsers.flatMap { 
          case (res, Right(parser)) => parser.includes.map(RequestedInclude(_, Some(res)))
          case _ => Nil
        }
        val includeMap = configParsers.map {
          case (resource, result) => (resource, result.flatMap(_.unresolved))
        }.toMap
        load(recursiveIncludes).map(_ ++ includeMap)
      }
      
    }
  
}
{'fraction_non_alphanumeric': 0.10672900996137426, 'fraction_numerical': 0.006098800569221387, 'mean_word_length': 3.6723646723646723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3867404', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1299, 'n_words': 379}