Excerpts.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * @package Grav\Common\Helpers
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Helpers;
  9. use Grav\Common\Page\Interfaces\PageInterface;
  10. use Grav\Common\Page\Markdown\Excerpts as ExcerptsObject;
  11. use Grav\Common\Page\Medium\Medium;
  12. class Excerpts
  13. {
  14. /**
  15. * Process Grav image media URL from HTML tag
  16. *
  17. * @param string $html HTML tag e.g. `<img src="image.jpg" />`
  18. * @param PageInterface|null $page Page, defaults to the current page object
  19. * @return string Returns final HTML string
  20. */
  21. public static function processImageHtml($html, PageInterface $page = null)
  22. {
  23. $excerpt = static::getExcerptFromHtml($html, 'img');
  24. $original_src = $excerpt['element']['attributes']['src'];
  25. $excerpt['element']['attributes']['href'] = $original_src;
  26. $excerpt = static::processLinkExcerpt($excerpt, $page, 'image');
  27. $excerpt['element']['attributes']['src'] = $excerpt['element']['attributes']['href'];
  28. unset ($excerpt['element']['attributes']['href']);
  29. $excerpt = static::processImageExcerpt($excerpt, $page);
  30. $excerpt['element']['attributes']['data-src'] = $original_src;
  31. $html = static::getHtmlFromExcerpt($excerpt);
  32. return $html;
  33. }
  34. /**
  35. * Get an Excerpt array from a chunk of HTML
  36. *
  37. * @param string $html Chunk of HTML
  38. * @param string $tag A tag, for example `img`
  39. * @return array|null returns nested array excerpt
  40. */
  41. public static function getExcerptFromHtml($html, $tag)
  42. {
  43. $doc = new \DOMDocument();
  44. $doc->loadHTML($html);
  45. $images = $doc->getElementsByTagName($tag);
  46. $excerpt = null;
  47. foreach ($images as $image) {
  48. $attributes = [];
  49. foreach ($image->attributes as $name => $value) {
  50. $attributes[$name] = $value->value;
  51. }
  52. $excerpt = [
  53. 'element' => [
  54. 'name' => $image->tagName,
  55. 'attributes' => $attributes
  56. ]
  57. ];
  58. }
  59. return $excerpt;
  60. }
  61. /**
  62. * Rebuild HTML tag from an excerpt array
  63. *
  64. * @param array $excerpt
  65. * @return string
  66. */
  67. public static function getHtmlFromExcerpt($excerpt)
  68. {
  69. $element = $excerpt['element'];
  70. $html = '<'.$element['name'];
  71. if (isset($element['attributes'])) {
  72. foreach ($element['attributes'] as $name => $value) {
  73. if ($value === null) {
  74. continue;
  75. }
  76. $html .= ' '.$name.'="'.$value.'"';
  77. }
  78. }
  79. if (isset($element['text'])) {
  80. $html .= '>';
  81. $html .= $element['text'];
  82. $html .= '</'.$element['name'].'>';
  83. } else {
  84. $html .= ' />';
  85. }
  86. return $html;
  87. }
  88. /**
  89. * Process a Link excerpt
  90. *
  91. * @param array $excerpt
  92. * @param PageInterface|null $page Page, defaults to the current page object
  93. * @param string $type
  94. * @return mixed
  95. */
  96. public static function processLinkExcerpt($excerpt, PageInterface $page = null, $type = 'link')
  97. {
  98. $excerpts = new ExcerptsObject($page);
  99. return $excerpts->processLinkExcerpt($excerpt, $type);
  100. }
  101. /**
  102. * Process an image excerpt
  103. *
  104. * @param array $excerpt
  105. * @param PageInterface|null $page Page, defaults to the current page object
  106. * @return array
  107. */
  108. public static function processImageExcerpt(array $excerpt, PageInterface $page = null)
  109. {
  110. $excerpts = new ExcerptsObject($page);
  111. return $excerpts->processImageExcerpt($excerpt);
  112. }
  113. /**
  114. * Process media actions
  115. *
  116. * @param Medium $medium
  117. * @param string|array $url
  118. * @param PageInterface|null $page Page, defaults to the current page object
  119. * @return Medium
  120. */
  121. public static function processMediaActions($medium, $url, PageInterface $page = null)
  122. {
  123. $excerpts = new ExcerptsObject($page);
  124. return $excerpts->processMediaActions($medium, $url);
  125. }
  126. }