You are here

public function ReadmeHelpMarkdownConverter::convertMarkdownFile in README Help 8

Converts markdown into HTML markup in a file.

If the second file argument is not passed then any of the README.md or README.txt or README files will be looked for in a module's root directory. The markdown is converted by the "Convert markdown into markup" filter and then run through "Convert line breaks into HTML" and "Convert URLs into links" filters. Finally, \Drupal\Component\Utility\Xss::filter() is applyed using the class::tags property as the set of allowed tags.

The second argument might be either an absolute path to a file or a directory where README files could be found.

Parameters

string $module_name: The name of the module where README file to look for.

string $file: (optional) The alternative directory or file path.

Return value

string|\Drupal\Component\Render\MarkupInterface A safe string.

See also

::convertMarkdownText()

::highlightPhp()

::insertPhpSnippets()

File

src/ReadmeHelpMarkdownConverter.php, line 127

Class

ReadmeHelpMarkdownConverter
Default implementation of the ReadmeHelpMarkdownConverter.

Namespace

Drupal\readmehelp

Code

public function convertMarkdownFile($module_name, $file = NULL) {
  $text = '';
  $root = $this->root;
  $files = static::READMEHELP_FILES;

  // Allow files from directories other than a module root folder.
  if (is_file($file)) {
    $files = ', ' . basename($file);
    $dir = dirname($file);
  }
  if (isset($dir) || is_dir($file) && ($dir = $file)) {
    $path = str_replace($root, '', $dir);

    // Seems that $dir passed is the relative to $root one.
    if ($path == $dir) {
      $path = trim($path, '/');
      $dir = "{$root}/{$path}";
    }
  }
  else {
    $path = $this->moduleHandler
      ->getModule($module_name)
      ->getPath();
    $dir = $this->moduleHandler
      ->getModuleDirectories()[$module_name];
  }
  $path = trim($path, '/');
  foreach (explode(', ', $files) as $readme) {
    if (file_exists("{$dir}/{$readme}")) {
      if ($text = file_get_contents("{$dir}/{$readme}")) {
        break;
      }
    }
  }
  if (!$text) {
    return $this
      ->t('None of the %files files is found in the %dir folder or their content is empty. Please, <a href=":href">README</a>.', [
      '%files' => $files,
      '%dir' => $dir,
      ':href' => '/admin/help/readmehelp',
    ]);
  }
  $text = $this
    ->convertMarkdownText($text, 'en', $path);

  // The snippets should be inserted the last because Xss::filter() strips
  // css style attribute which is inserted by PHP highlight_file() function.
  // Note that output of this function is safe to print on a page because any
  // HTML tags found in the file to highlight are escaped to HTML entities.
  $text = $this
    ->insertPhpSnippets($text, $path);
  $name = Html::getClass($module_name);
  $readme = "<h3 class=\"readmenelp-heading\">{$readme}</h3>";
  $markup = "{$readme}<article class=\"markdown-body {$name}-readmehelp\">{$text}</article>";
  return Markup::create($markup);
}