1
0

MarkDown.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Preview;
  8. use OCP\Files\File;
  9. use OCP\IImage;
  10. class MarkDown extends TXT {
  11. /**
  12. * {@inheritDoc}
  13. */
  14. public function getMimeType(): string {
  15. return '/text\/(x-)?markdown/';
  16. }
  17. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  18. $content = $file->fopen('r');
  19. if ($content === false) {
  20. return null;
  21. }
  22. $content = stream_get_contents($content, 3000);
  23. //don't create previews of empty text files
  24. if (trim($content) === '') {
  25. return null;
  26. }
  27. // Merge text paragraph lines that might belong together
  28. $content = preg_replace('/^(\s*)\*\s/mU', '$1- ', $content);
  29. $content = preg_replace('/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU', '$1 $3', $content);
  30. // Remove markdown symbols that we cannot easily represent in rendered text in the preview
  31. $content = preg_replace('/\*\*(.*)\*\*/U', '$1', $content);
  32. $content = preg_replace('/\*(.*)\*/U', '$1', $content);
  33. $content = preg_replace('/\_\_(.*)\_\_/U', '$1', $content);
  34. $content = preg_replace('/\_(.*)\_/U', '$1', $content);
  35. $content = preg_replace('/\~\~(.*)\~\~/U', '$1', $content);
  36. $content = preg_replace('/\!?\[((.|\n)*)\]\((.*)\)/mU', '$1 ($3)', $content);
  37. $content = preg_replace('/\n\n+/', "\n", $content);
  38. $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content);
  39. $lines = preg_split("/\r\n|\n|\r/", $content);
  40. // Define text size of text file preview
  41. $fontSize = $maxX ? (int) ((1 / ($maxX >= 512 ? 60 : 40) * $maxX)) : 10;
  42. $image = imagecreate($maxX, $maxY);
  43. imagecolorallocate($image, 255, 255, 255);
  44. $textColor = imagecolorallocate($image, 0, 0, 0);
  45. $fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
  46. $fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf';
  47. $canUseTTF = function_exists('imagettftext');
  48. $textOffset = (int)min($maxX * 0.05, $maxY * 0.05);
  49. $nextLineStart = 0;
  50. $y = $textOffset;
  51. foreach ($lines as $line) {
  52. $actualFontSize = $fontSize;
  53. if (mb_strpos($line, '# ') === 0) {
  54. $actualFontSize *= 2;
  55. }
  56. if (mb_strpos($line, '## ') === 0) {
  57. $actualFontSize *= 1.8;
  58. }
  59. if (mb_strpos($line, '### ') === 0) {
  60. $actualFontSize *= 1.6;
  61. }
  62. if (mb_strpos($line, '#### ') === 0) {
  63. $actualFontSize *= 1.4;
  64. }
  65. if (mb_strpos($line, '##### ') === 0) {
  66. $actualFontSize *= 1.2;
  67. }
  68. if (mb_strpos($line, '###### ') === 0) {
  69. $actualFontSize *= 1.1;
  70. }
  71. // Add spacing before headlines
  72. if ($actualFontSize !== $fontSize && $y !== $textOffset) {
  73. $y += (int)($actualFontSize * 2);
  74. }
  75. $x = $textOffset;
  76. $y += (int)($nextLineStart + $actualFontSize);
  77. if ($canUseTTF === true) {
  78. $wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX);
  79. // Get rid of markdown symbols that we still needed for the font size
  80. $line = preg_replace('/^#*\s/', '', $line);
  81. $wrappedText = wordwrap($line, $wordWrap, "\n");
  82. $linesWrapped = count(explode("\n", $wrappedText));
  83. imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText);
  84. $nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2));
  85. if ($actualFontSize !== $fontSize && $y !== $textOffset) {
  86. $nextLineStart -= $actualFontSize;
  87. }
  88. } else {
  89. $y -= $fontSize;
  90. imagestring($image, 1, $x, $y, $line, $textColor);
  91. $nextLineStart = $fontSize;
  92. }
  93. if ($y >= $maxY) {
  94. break;
  95. }
  96. }
  97. $imageObject = new \OCP\Image();
  98. $imageObject->setResource($image);
  99. return $imageObject->valid() ? $imageObject : null;
  100. }
  101. }