MarkDown.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Preview;
  27. use OCP\Files\File;
  28. use OCP\IImage;
  29. class MarkDown extends TXT {
  30. /**
  31. * {@inheritDoc}
  32. */
  33. public function getMimeType(): string {
  34. return '/text\/(x-)?markdown/';
  35. }
  36. public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
  37. $content = $file->fopen('r');
  38. if ($content === false) {
  39. return null;
  40. }
  41. $content = stream_get_contents($content, 3000);
  42. //don't create previews of empty text files
  43. if (trim($content) === '') {
  44. return null;
  45. }
  46. // Merge text paragraph lines that might belong together
  47. $content = preg_replace('/^(\s*)\*\s/mU', '$1- ', $content);
  48. $content = preg_replace('/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU', '$1 $3', $content);
  49. // Remove markdown symbols that we cannot easily represent in rendered text in the preview
  50. $content = preg_replace('/\*\*(.*)\*\*/U', '$1', $content);
  51. $content = preg_replace('/\*(.*)\*/U', '$1', $content);
  52. $content = preg_replace('/\_\_(.*)\_\_/U', '$1', $content);
  53. $content = preg_replace('/\_(.*)\_/U', '$1', $content);
  54. $content = preg_replace('/\~\~(.*)\~\~/U', '$1', $content);
  55. $content = preg_replace('/\!?\[((.|\n)*)\]\((.*)\)/mU', '$1 ($3)', $content);
  56. $content = preg_replace('/\n\n+/', "\n", $content);
  57. $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content);
  58. $lines = preg_split("/\r\n|\n|\r/", $content);
  59. // Define text size of text file preview
  60. $fontSize = $maxX ? (int) ((1 / ($maxX >= 512 ? 60 : 40) * $maxX)) : 10;
  61. $image = imagecreate($maxX, $maxY);
  62. imagecolorallocate($image, 255, 255, 255);
  63. $textColor = imagecolorallocate($image, 0, 0, 0);
  64. $fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
  65. $fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf';
  66. $canUseTTF = function_exists('imagettftext');
  67. $textOffset = (int)min($maxX * 0.05, $maxY * 0.05);
  68. $nextLineStart = 0;
  69. $y = $textOffset;
  70. foreach ($lines as $line) {
  71. $actualFontSize = $fontSize;
  72. if (mb_strpos($line, '# ') === 0) {
  73. $actualFontSize *= 2;
  74. }
  75. if (mb_strpos($line, '## ') === 0) {
  76. $actualFontSize *= 1.8;
  77. }
  78. if (mb_strpos($line, '### ') === 0) {
  79. $actualFontSize *= 1.6;
  80. }
  81. if (mb_strpos($line, '#### ') === 0) {
  82. $actualFontSize *= 1.4;
  83. }
  84. if (mb_strpos($line, '##### ') === 0) {
  85. $actualFontSize *= 1.2;
  86. }
  87. if (mb_strpos($line, '###### ') === 0) {
  88. $actualFontSize *= 1.1;
  89. }
  90. // Add spacing before headlines
  91. if ($actualFontSize !== $fontSize && $y !== $textOffset) {
  92. $y += (int)($actualFontSize * 2);
  93. }
  94. $x = $textOffset;
  95. $y += (int)($nextLineStart + $actualFontSize);
  96. if ($canUseTTF === true) {
  97. $wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX);
  98. // Get rid of markdown symbols that we still needed for the font size
  99. $line = preg_replace('/^#*\s/', '', $line);
  100. $wrappedText = wordwrap($line, $wordWrap, "\n");
  101. $linesWrapped = count(explode("\n", $wrappedText));
  102. imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText);
  103. $nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2));
  104. if ($actualFontSize !== $fontSize && $y !== $textOffset) {
  105. $nextLineStart -= $actualFontSize;
  106. }
  107. } else {
  108. $y -= $fontSize;
  109. imagestring($image, 1, $x, $y, $line, $textColor);
  110. $nextLineStart = $fontSize;
  111. }
  112. if ($y >= $maxY) {
  113. break;
  114. }
  115. }
  116. $imageObject = new \OCP\Image();
  117. $imageObject->setResource($image);
  118. return $imageObject->valid() ? $imageObject : null;
  119. }
  120. }