@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorMemeRemarkupRule extends PhutilRemarkupRule {
4
5 private $images;
6
7 public function getPriority() {
8 return 200.0;
9 }
10
11 public function apply($text) {
12 return preg_replace_callback(
13 '@{meme,((?:[^}\\\\]+|\\\\.)+)}@m',
14 array($this, 'markupMeme'),
15 $text);
16 }
17
18 public function markupMeme(array $matches) {
19 if (!$this->isFlatText($matches[0])) {
20 return $matches[0];
21 }
22
23 $options = array(
24 'src' => null,
25 'above' => null,
26 'below' => null,
27 );
28
29 $parser = new PhutilSimpleOptions();
30 $options = $parser->parse($matches[1]) + $options;
31
32 $engine = id(new PhabricatorMemeEngine())
33 ->setViewer(PhabricatorUser::getOmnipotentUser())
34 ->setTemplate($options['src'])
35 ->setAboveText($options['above'])
36 ->setBelowText($options['below']);
37
38 $asset = $engine->loadCachedFile();
39
40 $is_html_mail = $this->getEngine()->isHTMLMailMode();
41 $is_text = $this->getEngine()->isTextMode();
42 $must_inline = ($is_html_mail || $is_text);
43
44 if ($must_inline) {
45 if (!$asset) {
46 try {
47 $asset = $engine->newAsset();
48 } catch (Exception $ex) {
49 return $matches[0];
50 }
51 }
52 }
53
54 if ($asset) {
55 $uri = $asset->getViewURI();
56 } else {
57 $uri = $engine->getGenerateURI();
58 }
59
60 if ($is_text) {
61 $parts = array();
62
63 $above = $options['above'];
64 if (strlen($above)) {
65 $parts[] = pht('"%s"', $above);
66 }
67
68 $parts[] = $options['src'].' <'.$uri.'>';
69
70 $below = $options['below'];
71 if (strlen($below)) {
72 $parts[] = pht('"%s"', $below);
73 }
74
75 $parts = implode("\n", $parts);
76 return $this->getEngine()->storeText($parts);
77 }
78
79 $alt_text = pht(
80 'Macro %s: %s %s',
81 $options['src'],
82 $options['above'],
83 $options['below']);
84
85 if ($asset) {
86 $img = $this->newTag(
87 'img',
88 array(
89 'src' => $uri,
90 'class' => 'phabricator-remarkup-macro',
91 'alt' => $alt_text,
92 ));
93 } else {
94 $img = id(new PHUIRemarkupImageView())
95 ->setURI($uri)
96 ->addClass('phabricator-remarkup-macro')
97 ->setAlt($alt_text);
98 }
99
100 return $this->getEngine()->storeText($img);
101 }
102
103}