<commit_before>package org.ambraproject.wombat.freemarker;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSetMultimap;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.ambraproject.wombat.controller.SiteResolver;
import org.ambraproject.wombat.service.EntityNotFoundException;
import org.ambraproject.wombat.service.remote.EditorialContentService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import java.util.Set;

public class FetchHtmlDirective implements TemplateDirectiveModel {
  private static final Logger log = LoggerFactory.getLogger(FetchHtmlDirective.class);

  @Autowired
  private SiteResolver siteResolver;
  @Autowired
  private EditorialContentService editorialContentService;

  private static final String SUBST_ATTR_NAME = "data-subst";

  private static final ImmutableSetMultimap<String, HtmlElementTransformation> elementTransforms =
          ImmutableSetMultimap.of(
                  "homepage", HtmlElementTransformation.IMAGE,
                  "homepage", HtmlElementTransformation.ARTICLE,
                  "siteContent", HtmlElementTransformation.IMAGE,
                  "siteContent", HtmlElementTransformation.LINK,
                  "siteContent", HtmlElementTransformation.ASSET
                  );

  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    Object typeObj = params.get("type");
    if (typeObj == null) {
      throw new TemplateModelException("type parameter required");
    }

    Object pathObj = params.get("path");
    if (pathObj == null) {
      throw new TemplateModelException("path parameter required");
    }

    String pageType = typeObj.toString();

    Set<HtmlElementTransformation> transformations = elementTransforms.get(pageType);
    if (transformations == null) {
      throw new TemplateModelException(String.format("type parameter '%s' is invalid.", pageType));
    }

    ImmutableList<HtmlElementSubstitution> substitutions = HtmlElementSubstitution.buildList(body, SUBST_ATTR_NAME);

    SitePageContext sitePageContext = new SitePageContext(siteResolver, env);

    try (Reader html = editorialContentService.readHtml(sitePageContext, pageType, pathObj.toString(),
            transformations, substitutions)) {
      IOUtils.copy(html, env.getOut());
    } catch (EntityNotFoundException e) {
      // TODO: Allow themes to provide custom, user-visible error blocks
      log.error("Could not retrieve HTML of type \"{}\" at path \"{}\"", typeObj, pathObj);
    }
  }


}<commit_msg>DPRO-333: code format fix<commit_after>package org.ambraproject.wombat.freemarker;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSetMultimap;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.ambraproject.wombat.controller.SiteResolver;
import org.ambraproject.wombat.service.EntityNotFoundException;
import org.ambraproject.wombat.service.remote.EditorialContentService;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import java.util.Set;

public class FetchHtmlDirective implements TemplateDirectiveModel {
  private static final Logger log = LoggerFactory.getLogger(FetchHtmlDirective.class);

  @Autowired
  private SiteResolver siteResolver;
  @Autowired
  private EditorialContentService editorialContentService;

  private static final String SUBST_ATTR_NAME = "data-subst";

  private static final ImmutableSetMultimap<String, HtmlElementTransformation> ELEMENT_TRANSFORMS =
          ImmutableSetMultimap.of(
                  "homepage", HtmlElementTransformation.IMAGE,
                  "homepage", HtmlElementTransformation.ARTICLE,
                  "siteContent", HtmlElementTransformation.IMAGE,
                  "siteContent", HtmlElementTransformation.LINK,
                  "siteContent", HtmlElementTransformation.ASSET
                  );

  @Override
  public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
      throws TemplateException, IOException {
    Object typeObj = params.get("type");
    if (typeObj == null) {
      throw new TemplateModelException("type parameter required");
    }

    Object pathObj = params.get("path");
    if (pathObj == null) {
      throw new TemplateModelException("path parameter required");
    }

    String pageType = typeObj.toString();

    Set<HtmlElementTransformation> transformations = ELEMENT_TRANSFORMS.get(pageType);
    if (transformations == null) {
      throw new TemplateModelException(String.format("type parameter '%s' is invalid.", pageType));
    }

    ImmutableList<HtmlElementSubstitution> substitutions = HtmlElementSubstitution.buildList(body, SUBST_ATTR_NAME);

    SitePageContext sitePageContext = new SitePageContext(siteResolver, env);

    try (Reader html = editorialContentService.readHtml(sitePageContext, pageType, pathObj.toString(),
            transformations, substitutions)) {
      IOUtils.copy(html, env.getOut());
    } catch (EntityNotFoundException e) {
      // TODO: Allow themes to provide custom, user-visible error blocks
      log.error("Could not retrieve HTML of type \"{}\" at path \"{}\"", typeObj, pathObj);
    }
  }


}
{'fraction_non_alphanumeric': 0.08947885939036382, 'fraction_numerical': 0.0011471648639790233, 'mean_word_length': 4.444246208742195, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406407', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1626, 'n_words': 355}