You are here

class ParsedMarkdown in Markdown 8.2

The end result of parsing markdown into HTML.

Hierarchy

Expanded class hierarchy of ParsedMarkdown

2 files declare their use of ParsedMarkdown
BaseParser.php in src/Plugin/Markdown/BaseParser.php
MissingParser.php in src/Plugin/Markdown/MissingParser.php

File

src/Render/ParsedMarkdown.php, line 13

Namespace

Drupal\markdown\Render
View source
class ParsedMarkdown implements ParsedMarkdownInterface {
  use RefinableCacheableDependencyTrait;

  /**
   * A UNIX timestamp of when this object is to expire.
   *
   * @var int
   */
  protected $expire = ParsedMarkdownInterface::PERMANENT;

  /**
   * The parsed HTML.
   *
   * @var string
   */
  protected $html;

  /**
   * A unique identifier.
   *
   * @var string
   */
  protected $id;

  /**
   * A human-readable label.
   *
   * @var string
   */
  protected $label;

  /**
   * The raw markdown.
   *
   * @var string
   */
  protected $markdown;

  /**
   * The language of the parsed markdown, if known.
   *
   * @var \Drupal\Core\Language\LanguageInterface|null
   */
  protected $language;

  /**
   * The byte size of the rendered HTML.
   *
   * @var int
   */
  protected $size;

  /**
   * ParsedMarkdown constructor.
   *
   * @param string $markdown
   *   The raw markdown.
   * @param string $html
   *   The parsed HTML from $markdown.
   * @param \Drupal\Core\Language\LanguageInterface|null $language
   *   Optional. The language of the parsed markdown, if known.
   */
  public function __construct($markdown = '', $html = '', LanguageInterface $language = NULL) {
    $this->html = trim($html);
    $this->markdown = trim($markdown);
    $this->language = $language;
  }

  /**
   * {@inheritdoc}
   */
  public function __toString() {
    return $this
      ->getHtml();
  }

  /**
   * {@inheritdoc}
   */
  public static function create($markdown = '', $html = '', LanguageInterface $language = NULL) {
    return new static($markdown, $html, $language);
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    return $this
      ->getSize();
  }

  /**
   * {@inheritdoc}
   */
  public function getExpire($from_time = NULL) {
    $expire = $this->expire;

    // Handle relative time.
    if (is_string($expire)) {
      $expire = strtotime($expire, $from_time ?: REQUEST_TIME);
    }
    return $expire;
  }

  /**
   * {@inheritdoc}
   */
  public function getHtml() {
    return $this->html;
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    if ($this->id === NULL) {
      $this->id = Crypt::hashBase64($this
        ->getMarkdown() . $this
        ->getHtml());
    }
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel() {
    return $this->label ?: $this
      ->getId();
  }

  /**
   * {@inheritdoc}
   */
  public function getMarkdown() {
    return static::normalizeMarkdown($this->markdown);
  }

  /**
   * {@inheritdoc}
   */
  public function getSize($formatted = FALSE, $decimals = 2) {
    if ($this->size === NULL) {
      $this->size = Unicode::strlen($this
        ->getHtml());
    }
    return $formatted ? number_format($this->size, $decimals) : $this->size;
  }

  /**
   * {@inheritdoc}
   */
  public function jsonSerialize() {
    return $this
      ->__toString();
  }

  /**
   * {@inheritdoc}
   */
  public function matches($markdown) {
    if ($markdown instanceof static) {
      return $markdown
        ->getMarkdown() === $this
        ->getMarkdown();
    }
    return static::normalizeMarkdown($markdown) === $this
      ->getMarkdown();
  }

  /**
   * {@inheritdoc}
   */
  public static function normalizeMarkdown($markdown) {
    return $markdown === '' ? '' : preg_replace('/\\r\\n|\\n/', "\n", (string) $markdown);
  }

  /**
   * {@inheritdoc}
   */
  public function serialize() {
    $data['object'] = serialize(get_object_vars($this));

    // Determine if PHP has gzip capabilities.
    $data['gzip'] = extension_loaded('zlib');

    // Compress and encode the markdown and html output.
    if ($data['gzip']) {

      /* @noinspection PhpComposerExtensionStubsInspection */
      $data['object'] = base64_encode(gzencode($data['object'], 9));
    }
    return serialize($data);
  }

  /**
   * {@inheritdoc}
   */
  public function setExpire($expire = ParsedMarkdownInterface::PERMANENT) {
    $this->expire = $expire;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setId($id) {
    $this->id = $id;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setLabel($label) {
    $this->label = $label;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function unserialize($serialized) {
    $data = unserialize($serialized);

    // Data was gzipped.
    if ($data['gzip']) {

      // Decompress data if PHP has gzip capabilities.
      if (extension_loaded('zlib')) {

        /* @noinspection PhpComposerExtensionStubsInspection */
        $data['object'] = gzdecode(base64_decode($data['object']));
      }
      else {
        $this->markdown = sprintf('This cached %s object was stored using gzip compression. Unable to decompress. The PHP on this server must have the "zlib" extension installed.', static::class);
        $this->html = $this->markdown;
        return;
      }
    }
    $object = unserialize($data['object']);
    foreach ($object as $prop => $value) {
      $this->{$prop} = $value;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function 3
CacheableDependencyTrait::getCacheMaxAge public function 3
CacheableDependencyTrait::getCacheTags public function 3
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
ParsedMarkdown::$expire protected property A UNIX timestamp of when this object is to expire.
ParsedMarkdown::$html protected property The parsed HTML.
ParsedMarkdown::$id protected property A unique identifier.
ParsedMarkdown::$label protected property A human-readable label.
ParsedMarkdown::$language protected property The language of the parsed markdown, if known.
ParsedMarkdown::$markdown protected property The raw markdown.
ParsedMarkdown::$size protected property The byte size of the rendered HTML.
ParsedMarkdown::count public function
ParsedMarkdown::create public static function Creates new ParsedMarkdown object. Overrides ParsedMarkdownInterface::create
ParsedMarkdown::getExpire public function Retrieves the UNIX timestamp for when this object should expire. Overrides ParsedMarkdownInterface::getExpire
ParsedMarkdown::getHtml public function Retrieves the parsed HTML. Overrides ParsedMarkdownInterface::getHtml
ParsedMarkdown::getId public function Retrieves the identifier for this object. Overrides ParsedMarkdownInterface::getId
ParsedMarkdown::getLabel public function Retrieves the human-readable label for this object. Overrides ParsedMarkdownInterface::getLabel
ParsedMarkdown::getMarkdown public function Retrieves the raw markdown source. Overrides ParsedMarkdownInterface::getMarkdown
ParsedMarkdown::getSize public function Retrieves the file size of the parsed HTML. Overrides ParsedMarkdownInterface::getSize
ParsedMarkdown::jsonSerialize public function
ParsedMarkdown::matches public function Compares whether the provided markdown matches this object. Overrides ParsedMarkdownInterface::matches
ParsedMarkdown::normalizeMarkdown public static function Normalizes markdown. Overrides ParsedMarkdownInterface::normalizeMarkdown
ParsedMarkdown::serialize public function
ParsedMarkdown::setExpire public function Sets the object's expiration timestamp. Overrides ParsedMarkdownInterface::setExpire
ParsedMarkdown::setId public function Sets the object's identifier. Overrides ParsedMarkdownInterface::setId
ParsedMarkdown::setLabel public function Sets the object's label. Overrides ParsedMarkdownInterface::setLabel
ParsedMarkdown::unserialize public function
ParsedMarkdown::__construct public function ParsedMarkdown constructor.
ParsedMarkdown::__toString public function Returns markup. Overrides MarkupInterface::__toString
ParsedMarkdownInterface::PERMANENT constant Indicates the item should never be removed unless explicitly deleted.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function