functions.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /* Copyright (c) 2019 Geert Bergman (geert@scrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. namespace HighlightUtilities;
  29. require_once __DIR__ . '/_internals.php';
  30. require_once __DIR__ . '/_themeColors.php';
  31. /**
  32. * Get a list of available stylesheets.
  33. *
  34. * By default, a list of filenames without the `.css` extension will be returned.
  35. * This can be configured with the `$filePaths` argument.
  36. *
  37. * @api
  38. *
  39. * @since 9.15.8.1
  40. *
  41. * @param bool $filePaths Return absolute paths to stylesheets instead
  42. *
  43. * @return string[]
  44. */
  45. function getAvailableStyleSheets($filePaths = false)
  46. {
  47. $results = array();
  48. $folder = getStyleSheetFolder();
  49. $dh = @dir($folder);
  50. if ($dh) {
  51. while (($entry = $dh->read()) !== false) {
  52. if (substr($entry, -4, 4) !== ".css") {
  53. continue;
  54. }
  55. if ($filePaths) {
  56. $results[] = implode(DIRECTORY_SEPARATOR, array($folder, $entry));
  57. } else {
  58. $results[] = basename($entry, ".css");
  59. }
  60. }
  61. $dh->close();
  62. }
  63. return $results;
  64. }
  65. /**
  66. * Get the RGB representation used for the background of a given theme as an
  67. * array of three numbers.
  68. *
  69. * @api
  70. *
  71. * @since 9.18.1.1
  72. *
  73. * @param string $name The stylesheet name (with or without the extension)
  74. *
  75. * @throws \DomainException when no stylesheet with this name exists
  76. *
  77. * @return float[] An array representing RGB numerical values
  78. */
  79. function getThemeBackgroundColor($name)
  80. {
  81. return _getThemeBackgroundColor(_getNoCssExtension($name));
  82. }
  83. /**
  84. * Get the contents of the given stylesheet.
  85. *
  86. * @api
  87. *
  88. * @since 9.15.8.1
  89. *
  90. * @param string $name The stylesheet name (with or without the extension)
  91. *
  92. * @throws \DomainException when the no stylesheet with this name exists
  93. *
  94. * @return false|string The CSS content of the stylesheet or FALSE when
  95. * the stylesheet content could be read
  96. */
  97. function getStyleSheet($name)
  98. {
  99. $path = getStyleSheetPath($name);
  100. return file_get_contents($path);
  101. }
  102. /**
  103. * Get the absolute path to the folder containing the stylesheets distributed in this package.
  104. *
  105. * @api
  106. *
  107. * @since 9.15.8.1
  108. *
  109. * @return string An absolute path to the folder
  110. */
  111. function getStyleSheetFolder()
  112. {
  113. $paths = array(__DIR__, '..', 'styles');
  114. return implode(DIRECTORY_SEPARATOR, $paths);
  115. }
  116. /**
  117. * Get the absolute path to a given stylesheet distributed in this package.
  118. *
  119. * @api
  120. *
  121. * @since 9.15.8.1
  122. *
  123. * @param string $name The stylesheet name (with or without the extension)
  124. *
  125. * @throws \DomainException when the no stylesheet with this name exists
  126. *
  127. * @return string The absolute path to the stylesheet with the given name
  128. */
  129. function getStyleSheetPath($name)
  130. {
  131. $name = _getNoCssExtension($name);
  132. $path = implode(DIRECTORY_SEPARATOR, array(getStyleSheetFolder(), $name)) . ".css";
  133. if (!file_exists($path)) {
  134. throw new \DomainException("There is no stylesheet with by the name of '$name'.");
  135. }
  136. return $path;
  137. }
  138. /**
  139. * Convert the HTML generated by Highlighter and split it up into an array of lines.
  140. *
  141. * @api
  142. *
  143. * @since 9.15.6.1
  144. *
  145. * @param string $html An HTML string generated by `Highlighter::highlight()`
  146. *
  147. * @throws \RuntimeException when the DOM extension is not available
  148. * @throws \UnexpectedValueException when the given HTML could not be parsed
  149. *
  150. * @return string[]|false An array of lines of code as strings. False if an error occurred in splitting up by lines
  151. */
  152. function splitCodeIntoArray($html)
  153. {
  154. if (!extension_loaded("dom")) {
  155. throw new \RuntimeException("The DOM extension is not loaded but is required.");
  156. }
  157. $dom = new \DOMDocument();
  158. if (!$dom->loadHTML($html)) {
  159. throw new \UnexpectedValueException("The given HTML could not be parsed correctly.");
  160. }
  161. $spans = $dom->getElementsByTagName("span");
  162. /** @var \DOMElement $span */
  163. foreach ($spans as $span) {
  164. $classes = $span->getAttribute("class");
  165. $renderedSpan = $dom->saveHTML($span);
  166. if (preg_match('/\R/', $renderedSpan)) {
  167. $finished = preg_replace(
  168. '/\R/',
  169. sprintf('</span>%s<span class="%s">', PHP_EOL, $classes),
  170. $renderedSpan
  171. );
  172. $html = str_replace($renderedSpan, $finished, $html);
  173. }
  174. }
  175. return preg_split('/\R/', $html);
  176. }