class Markdown in Markdown 8.2
Same name and namespace in other branches
- 3.0.x src/Markdown.php \Drupal\markdown\Markdown
Markdown service.
Hierarchy
- class \Drupal\markdown\Markdown implements MarkdownInterface uses StringTranslationTrait
Expanded class hierarchy of Markdown
2 files declare their use of Markdown
- markdown.module in ./
markdown.module - Markdown module.
- ParserConfigurationForm.php in src/
Form/ ParserConfigurationForm.php
4 string references to 'Markdown'
1 service uses Markdown
File
- src/
Markdown.php, line 22
Namespace
Drupal\markdownView source
class Markdown implements MarkdownInterface {
use StringTranslationTrait;
/**
* The cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* The Config Factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The File System service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The HTTP Client service.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* The MarkdownParser Plugin Manager.
*
* @var \Drupal\markdown\PluginManager\ParserManagerInterface
*/
protected $parserManager;
/**
* Markdown constructor.
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The Config Factory service.
* @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The File System service.
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP Client service.
* @param \Drupal\markdown\PluginManager\ParserManagerInterface $parserManager
* The Markdown Parser Plugin Manager service.
*/
public function __construct(CacheBackendInterface $cache, ConfigFactoryInterface $configFactory, FileSystemInterface $fileSystem, ClientInterface $httpClient, ParserManagerInterface $parserManager) {
$this->cache = $cache;
$this->configFactory = $configFactory;
$this->fileSystem = $fileSystem;
$this->httpClient = $httpClient;
$this->parserManager = $parserManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container = NULL) {
if (!isset($container)) {
$container = \Drupal::getContainer();
}
return new static($container
->get('cache.markdown'), $container
->get('config.factory'), $container
->get('file_system'), $container
->get('http_client'), $container
->get('plugin.manager.markdown.parser'));
}
/**
* {@inheritdoc}
*/
public function load($id) {
if ($id && ($cache = $this->cache
->get($id)) && $cache->data instanceof ParsedMarkdownInterface) {
return $cache->data;
}
}
/**
* {@inheritdoc}
*/
public function loadFile($path, $id = NULL, LanguageInterface $language = NULL) {
$realpath = $this->fileSystem
->realpath($path) ?: $path;
if (!file_exists($realpath)) {
throw new MarkdownFileNotExistsException($realpath);
}
if (!$id) {
$id = $this->fileSystem
->basename($realpath) . Crypt::hashBase64($realpath);
}
// Append the file modification time as a cache buster in case it changed.
$id = "{$id}:" . filemtime($realpath);
return $this
->load($id) ?: $this
->save($id, $this
->parse(file_get_contents($realpath) ?: '', $language));
}
/**
* {@inheritdoc}
*
* @deprecated in markdown:8.x-2.0 and is removed from markdown:3.0.0.
* Use \Drupal\markdown\MarkdownInterface::loadFile instead.
* @see https://www.drupal.org/project/markdown/issues/3142418
*/
public function loadPath($path, $id = NULL, LanguageInterface $language = NULL) {
return $this
->loadFile($path, $id, $language);
}
/**
* {@inheritdoc}
*/
public function loadUrl($url, $id = NULL, LanguageInterface $language = NULL) {
if ($url instanceof Url) {
$url = $url
->setAbsolute()
->toString();
}
else {
$url = (string) $url;
}
if (!$id) {
$id = $url;
}
if (!($parsed = $this
->load($id))) {
$response = $this->httpClient
->get($url);
if ($response
->getStatusCode() < 200 || $response
->getStatusCode() >= 400) {
throw new MarkdownUrlNotExistsException($url);
}
$parsed = $this
->save($id, $this
->parse($response
->getBody()
->getContents(), $language));
}
return $parsed;
}
/**
* {@inheritdoc}
*/
public function parse($markdown, LanguageInterface $language = NULL) {
return $this
->getParser()
->parse($markdown, $language);
}
/**
* {@inheritdoc}
*/
public function getParser($parserId = NULL, array $configuration = []) {
if (empty($parserId)) {
return $this->parserManager
->getDefaultParser($configuration);
}
return $this->parserManager
->createInstance($parserId, $configuration);
}
/**
* {@inheritdoc}
*/
public function save($id, ParsedMarkdownInterface $parsed) {
$this->cache
->set($id, $parsed, $parsed
->getExpire(), $parsed
->getCacheTags());
return $parsed;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Markdown:: |
protected | property | The cache backend. | |
Markdown:: |
protected | property | The Config Factory service. | |
Markdown:: |
protected | property | The File System service. | |
Markdown:: |
protected | property | The HTTP Client service. | |
Markdown:: |
protected | property | The MarkdownParser Plugin Manager. | |
Markdown:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
Markdown:: |
public | function |
Retrieves a MarkdownParser plugin. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Loads a cached ParsedMarkdown object. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Loads a cached ParsedMarkdown object based on system file. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Loads a cached ParsedMarkdown object based on a URL. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Parses markdown into HTML. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function |
Saves a parsed markdown object. Overrides MarkdownInterface:: |
|
Markdown:: |
public | function | Markdown constructor. | |
MarkdownInterface:: |
constant | The base URL for Markdown documentation. | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |