<commit_before>// Copyright 2013 <NAME>
//
// 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.github.jleyba.dossier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Iterator;

import javax.annotation.Nullable;

/**
 * Utilities for generating links to {@link Descriptor types} in generated documentation.
 */
class Linker {

  private final Config config;
  private final Path outputRoot;
  private final DocRegistry docRegistry;

  /**
   * @param config The current runtime configuration.
   * @param docRegistry The documented type registry.
   */
  Linker(Config config, DocRegistry docRegistry) {
    this.config = checkNotNull(config);
    this.outputRoot = config.getOutput();
    this.docRegistry = checkNotNull(docRegistry);
  }

  private static String getTypePrefix(Descriptor descriptor) {
    if (descriptor.isInterface()) {
      return "interface_";
    } else if (descriptor.isConstructor()) {
      return "class_";
    } else if (descriptor.isEnum()) {
      return "enum_";
    } else {
      return "namespace_";
    }
  }

  /**
   * Returns the display name for the given {@code descriptor}.
   */
  String getDisplayName(ModuleDescriptor descriptor) {
    if (descriptor.getAttribute("displayName") != null) {
      return descriptor.getAttribute("displayName");
    }

    Path modulePath = descriptor.getPath();
    modulePath = modulePath.resolveSibling(
        com.google.common.io.Files.getNameWithoutExtension(modulePath.toString()));

    Path displayPath = config.getModulePrefix().relativize(modulePath);
    if (displayPath.getFileName().toString().equals("index")
        && displayPath.getParent() != null) {
      displayPath = displayPath.getParent();
    }

    String displayName = displayPath.toString()
        .replace(modulePath.getFileSystem().getSeparator(), "/");  // Oh windows...

    descriptor.setAttribute("displayName", displayName);
    return displayName;
  }

  /**
   * Returns the display name for the given {@code descriptor}. If this is the descriptor for the
   * {@link Descriptor#isModuleExports() exported API} of a CommonJS module, this will return
   * that module's {@link #getDisplayName(ModuleDescriptor) display name}. Otherwise, this simply
   * returns the descriptor's {@link Descriptor#getFullName() fully qualified name}.
   */
  String getDisplayName(Descriptor descriptor) {
    if (descriptor.isModuleExports()) {
      return getDisplayName(descriptor.getModule().get());
    }
    return descriptor.getFullName();
  }

  /**
   * Returns the path of the generated document file for the given module.
   */
  Path getFilePath(ModuleDescriptor module) {
    String name = getDisplayName(module).replace('/', '_') + ".html";
    return outputRoot.resolve("module_" + name);
  }

  /**
   * Returns the path of the generated document file for the given descriptor.
   */
  Path getFilePath(Descriptor descriptor) {
    String name = descriptor.getFullName().replace('.', '_') + ".html";
    name = getTypePrefix(descriptor) + name;

    if (descriptor.getModule().isPresent()) {
      ModuleDescriptor module = descriptor.getModule().get();
      String moduleFileName = getFilePath(module).getFileName().toString();
      name = com.google.common.io.Files.getNameWithoutExtension(moduleFileName)
          + "_" + name;
    }

    return outputRoot.resolve(name);
  }<commit_msg>Fix to work with injected file systems.<commit_after>// Copyright 2013 <NAME>
//
// 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.github.jleyba.dossier;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Iterator;

import javax.annotation.Nullable;

/**
 * Utilities for generating links to {@link Descriptor types} in generated documentation.
 */
class Linker {

  private final Config config;
  private final Path outputRoot;
  private final DocRegistry docRegistry;

  /**
   * @param config The current runtime configuration.
   * @param docRegistry The documented type registry.
   */
  Linker(Config config, DocRegistry docRegistry) {
    this.config = checkNotNull(config);
    this.outputRoot = config.getOutput();
    this.docRegistry = checkNotNull(docRegistry);
  }

  private static String getTypePrefix(Descriptor descriptor) {
    if (descriptor.isInterface()) {
      return "interface_";
    } else if (descriptor.isConstructor()) {
      return "class_";
    } else if (descriptor.isEnum()) {
      return "enum_";
    } else {
      return "namespace_";
    }
  }

  /**
   * Returns the display name for the given {@code descriptor}.
   */
  String getDisplayName(ModuleDescriptor descriptor) {
    if (descriptor.getAttribute("displayName") != null) {
      return descriptor.getAttribute("displayName");
    }

    Path modulePath = stripExtension(descriptor.getPath());

    Path displayPath = config.getModulePrefix().relativize(modulePath);
    if (displayPath.getFileName().toString().equals("index")
        && displayPath.getParent() != null) {
      displayPath = displayPath.getParent();
    }

    String displayName = displayPath.toString()
        .replace(modulePath.getFileSystem().getSeparator(), "/");  // Oh windows...

    descriptor.setAttribute("displayName", displayName);
    return displayName;
  }

  private static Path stripExtension(Path path) {
    String name = path.getFileName().toString();
    int index = name.lastIndexOf('.');
    return index == -1 ? path : path.resolveSibling(name.substring(0, index));
  }

  /**
   * Returns the display name for the given {@code descriptor}. If this is the descriptor for the
   * {@link Descriptor#isModuleExports() exported API} of a CommonJS module, this will return
   * that module's {@link #getDisplayName(ModuleDescriptor) display name}. Otherwise, this simply
   * returns the descriptor's {@link Descriptor#getFullName() fully qualified name}.
   */
  String getDisplayName(Descriptor descriptor) {
    if (descriptor.isModuleExports()) {
      return getDisplayName(descriptor.getModule().get());
    }
    return descriptor.getFullName();
  }

  /**
   * Returns the path of the generated document file for the given module.
   */
  Path getFilePath(ModuleDescriptor module) {
    String name = getDisplayName(module).replace('/', '_') + ".html";
    return outputRoot.resolve("module_" + name);
  }

  /**
   * Returns the path of the generated document file for the given descriptor.
   */
  Path getFilePath(Descriptor descriptor) {
    String name = descriptor.getFullName().replace('.', '_') + ".html";
    name = getTypePrefix(descriptor) + name;

    if (descriptor.getModule().isPresent()) {
      ModuleDescriptor module = descriptor.getModule().get();
      name = stripExtension(getFilePath(module)).getFileName().toString() + "_" + name;
    }

    return outputRoot.resolve(name);
  }
{'fraction_non_alphanumeric': 0.11551392891450528, 'fraction_numerical': 0.002161383285302594, 'mean_word_length': 3.7323863636363637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5390406', 'n_tokens_mistral': 2220, 'n_tokens_neox': 2181, 'n_words': 667}