2014-11-28 11:16:35 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2014-11-28 11:16:35 +03:00
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2014-11-28 11:16:35 +03:00
|
|
|
namespace OC\Preview;
|
|
|
|
|
2021-01-19 17:53:37 +03:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\IImage;
|
|
|
|
|
2014-11-28 11:16:35 +03:00
|
|
|
class MarkDown extends TXT {
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2019-06-04 16:25:25 +03:00
|
|
|
public function getMimeType(): string {
|
2014-11-28 11:16:35 +03:00
|
|
|
return '/text\/(x-)?markdown/';
|
|
|
|
}
|
2021-01-19 17:53:37 +03:00
|
|
|
|
|
|
|
public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
|
|
|
|
$content = $file->fopen('r');
|
|
|
|
|
|
|
|
if ($content === false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = stream_get_contents($content,3000);
|
|
|
|
|
|
|
|
//don't create previews of empty text files
|
|
|
|
if (trim($content) === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge text paragraph lines that might belong together
|
|
|
|
$content = preg_replace('/^(\s*)\*\s/mU', '$1- ', $content);
|
|
|
|
|
|
|
|
$content = preg_replace('/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU', '$1 $3', $content);
|
|
|
|
|
2021-01-20 17:02:32 +03:00
|
|
|
// Remove markdown symbols that we cannot easily represent in rendered text in the preview
|
2021-01-19 17:53:37 +03:00
|
|
|
$content = preg_replace('/\*\*(.*)\*\*/U', '$1', $content);
|
|
|
|
$content = preg_replace('/\*(.*)\*/U', '$1', $content);
|
|
|
|
$content = preg_replace('/\_\_(.*)\_\_/U', '$1', $content);
|
|
|
|
$content = preg_replace('/\_(.*)\_/U', '$1', $content);
|
|
|
|
$content = preg_replace('/\~\~(.*)\~\~/U', '$1', $content);
|
|
|
|
|
|
|
|
$content = preg_replace('/\!?\[((.|\n)*)\]\((.*)\)/mU', '$1 ($3)', $content);
|
|
|
|
$content = preg_replace('/\n\n+/', "\n", $content);
|
|
|
|
|
|
|
|
$content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content);
|
|
|
|
|
|
|
|
$lines = preg_split("/\r\n|\n|\r/", $content);
|
|
|
|
|
|
|
|
// Define text size of text file preview
|
|
|
|
$fontSize = $maxX ? (int) ((1 / ($maxX >= 512 ? 60 : 40) * $maxX)) : 10;
|
|
|
|
|
|
|
|
$image = imagecreate($maxX, $maxY);
|
|
|
|
imagecolorallocate($image, 255, 255, 255);
|
|
|
|
$textColor = imagecolorallocate($image, 0, 0, 0);
|
|
|
|
|
2021-01-12 12:15:48 +03:00
|
|
|
$fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf';
|
|
|
|
$fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf';
|
2021-01-19 17:53:37 +03:00
|
|
|
|
|
|
|
$canUseTTF = function_exists('imagettftext');
|
|
|
|
|
|
|
|
$textOffset = (int)min($maxX * 0.05, $maxY * 0.05);
|
|
|
|
$nextLineStart = 0;
|
|
|
|
$y = $textOffset;
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$actualFontSize = $fontSize;
|
|
|
|
if (mb_strpos($line, '# ') === 0) {
|
|
|
|
$actualFontSize *= 2;
|
|
|
|
}
|
|
|
|
if (mb_strpos($line, '## ') === 0) {
|
|
|
|
$actualFontSize *= 1.8;
|
|
|
|
}
|
|
|
|
if (mb_strpos($line, '### ') === 0) {
|
|
|
|
$actualFontSize *= 1.6;
|
|
|
|
}
|
|
|
|
if (mb_strpos($line, '#### ') === 0) {
|
|
|
|
$actualFontSize *= 1.4;
|
|
|
|
}
|
|
|
|
if (mb_strpos($line, '##### ') === 0) {
|
|
|
|
$actualFontSize *= 1.2;
|
|
|
|
}
|
|
|
|
if (mb_strpos($line, '###### ') === 0) {
|
|
|
|
$actualFontSize *= 1.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add spacing before headlines
|
|
|
|
if ($actualFontSize !== $fontSize && $y !== $textOffset) {
|
|
|
|
$y += (int)($actualFontSize * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
$x = $textOffset;
|
|
|
|
$y += (int)($nextLineStart + $actualFontSize);
|
|
|
|
|
|
|
|
if ($canUseTTF === true) {
|
|
|
|
$wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX);
|
|
|
|
|
|
|
|
// Get rid of markdown symbols that we still needed for the font size
|
|
|
|
$line = preg_replace('/^#*\s/', '', $line);
|
|
|
|
|
|
|
|
$wrappedText = wordwrap($line, $wordWrap,"\n");
|
|
|
|
$linesWrapped = count(explode("\n", $wrappedText));
|
|
|
|
imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText);
|
|
|
|
$nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2));
|
|
|
|
if ($actualFontSize !== $fontSize && $y !== $textOffset) {
|
|
|
|
$nextLineStart -= $actualFontSize;
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 12:29:54 +03:00
|
|
|
$y -= $fontSize;
|
2021-01-19 17:53:37 +03:00
|
|
|
imagestring($image, 1, $x, $y, $line, $textColor);
|
|
|
|
$nextLineStart = $fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($y >= $maxY) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$imageObject = new \OC_Image();
|
|
|
|
$imageObject->setResource($image);
|
|
|
|
|
|
|
|
return $imageObject->valid() ? $imageObject : null;
|
|
|
|
}
|
2014-11-28 11:16:35 +03:00
|
|
|
}
|