@@ -28,117 +28,117 @@ |
||
28 | 28 | use OCP\IImage; |
29 | 29 | |
30 | 30 | class MarkDown extends TXT { |
31 | - /** |
|
32 | - * {@inheritDoc} |
|
33 | - */ |
|
34 | - public function getMimeType(): string { |
|
35 | - return '/text\/(x-)?markdown/'; |
|
36 | - } |
|
37 | - |
|
38 | - public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
39 | - $content = $file->fopen('r'); |
|
40 | - |
|
41 | - if ($content === false) { |
|
42 | - return null; |
|
43 | - } |
|
44 | - |
|
45 | - $content = stream_get_contents($content,3000); |
|
46 | - |
|
47 | - //don't create previews of empty text files |
|
48 | - if (trim($content) === '') { |
|
49 | - return null; |
|
50 | - } |
|
51 | - |
|
52 | - // Merge text paragraph lines that might belong together |
|
53 | - $content = preg_replace('/^(\s*)\*\s/mU', '$1- ', $content); |
|
54 | - |
|
55 | - $content = preg_replace('/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU', '$1 $3', $content); |
|
56 | - |
|
57 | - // Remove markdown symbols that we cannot easily represent in rendered text in the preview |
|
58 | - $content = preg_replace('/\*\*(.*)\*\*/U', '$1', $content); |
|
59 | - $content = preg_replace('/\*(.*)\*/U', '$1', $content); |
|
60 | - $content = preg_replace('/\_\_(.*)\_\_/U', '$1', $content); |
|
61 | - $content = preg_replace('/\_(.*)\_/U', '$1', $content); |
|
62 | - $content = preg_replace('/\~\~(.*)\~\~/U', '$1', $content); |
|
63 | - |
|
64 | - $content = preg_replace('/\!?\[((.|\n)*)\]\((.*)\)/mU', '$1 ($3)', $content); |
|
65 | - $content = preg_replace('/\n\n+/', "\n", $content); |
|
66 | - |
|
67 | - $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content); |
|
68 | - |
|
69 | - $lines = preg_split("/\r\n|\n|\r/", $content); |
|
70 | - |
|
71 | - // Define text size of text file preview |
|
72 | - $fontSize = $maxX ? (int) ((1 / ($maxX >= 512 ? 60 : 40) * $maxX)) : 10; |
|
73 | - |
|
74 | - $image = imagecreate($maxX, $maxY); |
|
75 | - imagecolorallocate($image, 255, 255, 255); |
|
76 | - $textColor = imagecolorallocate($image, 0, 0, 0); |
|
77 | - |
|
78 | - $fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf'; |
|
79 | - $fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf'; |
|
80 | - |
|
81 | - $canUseTTF = function_exists('imagettftext'); |
|
82 | - |
|
83 | - $textOffset = (int)min($maxX * 0.05, $maxY * 0.05); |
|
84 | - $nextLineStart = 0; |
|
85 | - $y = $textOffset; |
|
86 | - foreach ($lines as $line) { |
|
87 | - $actualFontSize = $fontSize; |
|
88 | - if (mb_strpos($line, '# ') === 0) { |
|
89 | - $actualFontSize *= 2; |
|
90 | - } |
|
91 | - if (mb_strpos($line, '## ') === 0) { |
|
92 | - $actualFontSize *= 1.8; |
|
93 | - } |
|
94 | - if (mb_strpos($line, '### ') === 0) { |
|
95 | - $actualFontSize *= 1.6; |
|
96 | - } |
|
97 | - if (mb_strpos($line, '#### ') === 0) { |
|
98 | - $actualFontSize *= 1.4; |
|
99 | - } |
|
100 | - if (mb_strpos($line, '##### ') === 0) { |
|
101 | - $actualFontSize *= 1.2; |
|
102 | - } |
|
103 | - if (mb_strpos($line, '###### ') === 0) { |
|
104 | - $actualFontSize *= 1.1; |
|
105 | - } |
|
106 | - |
|
107 | - // Add spacing before headlines |
|
108 | - if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
|
109 | - $y += (int)($actualFontSize * 2); |
|
110 | - } |
|
111 | - |
|
112 | - $x = $textOffset; |
|
113 | - $y += (int)($nextLineStart + $actualFontSize); |
|
114 | - |
|
115 | - if ($canUseTTF === true) { |
|
116 | - $wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX); |
|
117 | - |
|
118 | - // Get rid of markdown symbols that we still needed for the font size |
|
119 | - $line = preg_replace('/^#*\s/', '', $line); |
|
120 | - |
|
121 | - $wrappedText = wordwrap($line, $wordWrap,"\n"); |
|
122 | - $linesWrapped = count(explode("\n", $wrappedText)); |
|
123 | - imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText); |
|
124 | - $nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2)); |
|
125 | - if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
|
126 | - $nextLineStart -= $actualFontSize; |
|
127 | - } |
|
128 | - } else { |
|
129 | - $y -= (int)$fontSize; |
|
130 | - imagestring($image, 1, $x, $y, $line, $textColor); |
|
131 | - $nextLineStart = $fontSize; |
|
132 | - } |
|
133 | - |
|
134 | - if ($y >= $maxY) { |
|
135 | - break; |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - $imageObject = new \OC_Image(); |
|
140 | - $imageObject->setResource($image); |
|
141 | - |
|
142 | - return $imageObject->valid() ? $imageObject : null; |
|
143 | - } |
|
31 | + /** |
|
32 | + * {@inheritDoc} |
|
33 | + */ |
|
34 | + public function getMimeType(): string { |
|
35 | + return '/text\/(x-)?markdown/'; |
|
36 | + } |
|
37 | + |
|
38 | + public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage { |
|
39 | + $content = $file->fopen('r'); |
|
40 | + |
|
41 | + if ($content === false) { |
|
42 | + return null; |
|
43 | + } |
|
44 | + |
|
45 | + $content = stream_get_contents($content,3000); |
|
46 | + |
|
47 | + //don't create previews of empty text files |
|
48 | + if (trim($content) === '') { |
|
49 | + return null; |
|
50 | + } |
|
51 | + |
|
52 | + // Merge text paragraph lines that might belong together |
|
53 | + $content = preg_replace('/^(\s*)\*\s/mU', '$1- ', $content); |
|
54 | + |
|
55 | + $content = preg_replace('/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU', '$1 $3', $content); |
|
56 | + |
|
57 | + // Remove markdown symbols that we cannot easily represent in rendered text in the preview |
|
58 | + $content = preg_replace('/\*\*(.*)\*\*/U', '$1', $content); |
|
59 | + $content = preg_replace('/\*(.*)\*/U', '$1', $content); |
|
60 | + $content = preg_replace('/\_\_(.*)\_\_/U', '$1', $content); |
|
61 | + $content = preg_replace('/\_(.*)\_/U', '$1', $content); |
|
62 | + $content = preg_replace('/\~\~(.*)\~\~/U', '$1', $content); |
|
63 | + |
|
64 | + $content = preg_replace('/\!?\[((.|\n)*)\]\((.*)\)/mU', '$1 ($3)', $content); |
|
65 | + $content = preg_replace('/\n\n+/', "\n", $content); |
|
66 | + |
|
67 | + $content = preg_replace('/[\x{10000}-\x{10FFFF}]/u', '', $content); |
|
68 | + |
|
69 | + $lines = preg_split("/\r\n|\n|\r/", $content); |
|
70 | + |
|
71 | + // Define text size of text file preview |
|
72 | + $fontSize = $maxX ? (int) ((1 / ($maxX >= 512 ? 60 : 40) * $maxX)) : 10; |
|
73 | + |
|
74 | + $image = imagecreate($maxX, $maxY); |
|
75 | + imagecolorallocate($image, 255, 255, 255); |
|
76 | + $textColor = imagecolorallocate($image, 0, 0, 0); |
|
77 | + |
|
78 | + $fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf'; |
|
79 | + $fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf'; |
|
80 | + |
|
81 | + $canUseTTF = function_exists('imagettftext'); |
|
82 | + |
|
83 | + $textOffset = (int)min($maxX * 0.05, $maxY * 0.05); |
|
84 | + $nextLineStart = 0; |
|
85 | + $y = $textOffset; |
|
86 | + foreach ($lines as $line) { |
|
87 | + $actualFontSize = $fontSize; |
|
88 | + if (mb_strpos($line, '# ') === 0) { |
|
89 | + $actualFontSize *= 2; |
|
90 | + } |
|
91 | + if (mb_strpos($line, '## ') === 0) { |
|
92 | + $actualFontSize *= 1.8; |
|
93 | + } |
|
94 | + if (mb_strpos($line, '### ') === 0) { |
|
95 | + $actualFontSize *= 1.6; |
|
96 | + } |
|
97 | + if (mb_strpos($line, '#### ') === 0) { |
|
98 | + $actualFontSize *= 1.4; |
|
99 | + } |
|
100 | + if (mb_strpos($line, '##### ') === 0) { |
|
101 | + $actualFontSize *= 1.2; |
|
102 | + } |
|
103 | + if (mb_strpos($line, '###### ') === 0) { |
|
104 | + $actualFontSize *= 1.1; |
|
105 | + } |
|
106 | + |
|
107 | + // Add spacing before headlines |
|
108 | + if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
|
109 | + $y += (int)($actualFontSize * 2); |
|
110 | + } |
|
111 | + |
|
112 | + $x = $textOffset; |
|
113 | + $y += (int)($nextLineStart + $actualFontSize); |
|
114 | + |
|
115 | + if ($canUseTTF === true) { |
|
116 | + $wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX); |
|
117 | + |
|
118 | + // Get rid of markdown symbols that we still needed for the font size |
|
119 | + $line = preg_replace('/^#*\s/', '', $line); |
|
120 | + |
|
121 | + $wrappedText = wordwrap($line, $wordWrap,"\n"); |
|
122 | + $linesWrapped = count(explode("\n", $wrappedText)); |
|
123 | + imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText); |
|
124 | + $nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2)); |
|
125 | + if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
|
126 | + $nextLineStart -= $actualFontSize; |
|
127 | + } |
|
128 | + } else { |
|
129 | + $y -= (int)$fontSize; |
|
130 | + imagestring($image, 1, $x, $y, $line, $textColor); |
|
131 | + $nextLineStart = $fontSize; |
|
132 | + } |
|
133 | + |
|
134 | + if ($y >= $maxY) { |
|
135 | + break; |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + $imageObject = new \OC_Image(); |
|
140 | + $imageObject->setResource($image); |
|
141 | + |
|
142 | + return $imageObject->valid() ? $imageObject : null; |
|
143 | + } |
|
144 | 144 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | return null; |
43 | 43 | } |
44 | 44 | |
45 | - $content = stream_get_contents($content,3000); |
|
45 | + $content = stream_get_contents($content, 3000); |
|
46 | 46 | |
47 | 47 | //don't create previews of empty text files |
48 | 48 | if (trim($content) === '') { |
@@ -75,12 +75,12 @@ discard block |
||
75 | 75 | imagecolorallocate($image, 255, 255, 255); |
76 | 76 | $textColor = imagecolorallocate($image, 0, 0, 0); |
77 | 77 | |
78 | - $fontFile = __DIR__ . '/../../../core/fonts/NotoSans-Regular.ttf'; |
|
79 | - $fontFileBold = __DIR__ . '/../../../core/fonts/NotoSans-Bold.ttf'; |
|
78 | + $fontFile = __DIR__.'/../../../core/fonts/NotoSans-Regular.ttf'; |
|
79 | + $fontFileBold = __DIR__.'/../../../core/fonts/NotoSans-Bold.ttf'; |
|
80 | 80 | |
81 | 81 | $canUseTTF = function_exists('imagettftext'); |
82 | 82 | |
83 | - $textOffset = (int)min($maxX * 0.05, $maxY * 0.05); |
|
83 | + $textOffset = (int) min($maxX * 0.05, $maxY * 0.05); |
|
84 | 84 | $nextLineStart = 0; |
85 | 85 | $y = $textOffset; |
86 | 86 | foreach ($lines as $line) { |
@@ -106,27 +106,27 @@ discard block |
||
106 | 106 | |
107 | 107 | // Add spacing before headlines |
108 | 108 | if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
109 | - $y += (int)($actualFontSize * 2); |
|
109 | + $y += (int) ($actualFontSize * 2); |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | $x = $textOffset; |
113 | - $y += (int)($nextLineStart + $actualFontSize); |
|
113 | + $y += (int) ($nextLineStart + $actualFontSize); |
|
114 | 114 | |
115 | 115 | if ($canUseTTF === true) { |
116 | - $wordWrap = (int)((1 / $actualFontSize * 1.3) * $maxX); |
|
116 | + $wordWrap = (int) ((1 / $actualFontSize * 1.3) * $maxX); |
|
117 | 117 | |
118 | 118 | // Get rid of markdown symbols that we still needed for the font size |
119 | 119 | $line = preg_replace('/^#*\s/', '', $line); |
120 | 120 | |
121 | - $wrappedText = wordwrap($line, $wordWrap,"\n"); |
|
121 | + $wrappedText = wordwrap($line, $wordWrap, "\n"); |
|
122 | 122 | $linesWrapped = count(explode("\n", $wrappedText)); |
123 | 123 | imagettftext($image, $actualFontSize, 0, $x, $y, $textColor, $actualFontSize === $fontSize ? $fontFile : $fontFileBold, $wrappedText); |
124 | - $nextLineStart = (int)($linesWrapped * ceil($actualFontSize * 2)); |
|
124 | + $nextLineStart = (int) ($linesWrapped * ceil($actualFontSize * 2)); |
|
125 | 125 | if ($actualFontSize !== $fontSize && $y !== $textOffset) { |
126 | 126 | $nextLineStart -= $actualFontSize; |
127 | 127 | } |
128 | 128 | } else { |
129 | - $y -= (int)$fontSize; |
|
129 | + $y -= (int) $fontSize; |
|
130 | 130 | imagestring($image, 1, $x, $y, $line, $textColor); |
131 | 131 | $nextLineStart = $fontSize; |
132 | 132 | } |