@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
at upstream/main 127 lines 2.9 kB view raw
1<?php 2 3final class PhutilRemarkupNoteBlockRule extends PhutilRemarkupBlockRule { 4 5 public function getMatchingLineCount(array $lines, $cursor) { 6 $num_lines = 0; 7 8 if (preg_match($this->getRegEx(), $lines[$cursor])) { 9 $num_lines++; 10 $cursor++; 11 12 while (isset($lines[$cursor])) { 13 if (trim($lines[$cursor])) { 14 $num_lines++; 15 $cursor++; 16 continue; 17 } 18 break; 19 } 20 } 21 22 return $num_lines; 23 } 24 25 public function markupText($text, $children) { 26 $matches = array(); 27 preg_match($this->getRegEx(), $text, $matches); 28 29 if (idx($matches, 'showword')) { 30 $word = $matches['showword']; 31 $show = true; 32 } else { 33 $word = $matches['hideword']; 34 $show = false; 35 } 36 37 $class_suffix = phutil_utf8_strtolower($word); 38 39 // This is the "(IMPORTANT)" or "NOTE:" part. 40 $word_part = rtrim(substr($text, 0, strlen($matches[0]))); 41 42 // This is the actual text. 43 $text_part = substr($text, strlen($matches[0])); 44 $text_part = $this->applyRules(rtrim($text_part)); 45 46 $text_mode = $this->getEngine()->isTextMode(); 47 $html_mail_mode = $this->getEngine()->isHTMLMailMode(); 48 if ($text_mode) { 49 return $word_part.' '.$text_part; 50 } 51 52 if ($show) { 53 $content = array( 54 phutil_tag( 55 'span', 56 array( 57 'class' => 'remarkup-note-word', 58 ), 59 $word_part), 60 ' ', 61 $text_part, 62 ); 63 } else { 64 $content = $text_part; 65 } 66 67 if ($html_mail_mode) { 68 if ($class_suffix == 'important') { 69 $attributes = array( 70 'style' => 'margin: 16px 0; 71 padding: 12px; 72 border-left: 3px solid #c0392b; 73 background: #f4dddb;', 74 ); 75 } else if ($class_suffix == 'note') { 76 $attributes = array( 77 'style' => 'margin: 16px 0; 78 padding: 12px; 79 border-left: 3px solid #2980b9; 80 background: #daeaf3;', 81 ); 82 } else if ($class_suffix == 'warning') { 83 $attributes = array( 84 'style' => 'margin: 16px 0; 85 padding: 12px; 86 border-left: 3px solid #f1c40f; 87 background: #fdf5d4;', 88 ); 89 } 90 } else { 91 $attributes = array( 92 'class' => 'remarkup-'.$class_suffix, 93 ); 94 } 95 96 return phutil_tag( 97 'div', 98 $attributes, 99 $content); 100 } 101 102 private function getRegEx() { 103 static $regex; 104 105 if ($regex === null) { 106 $words = array( 107 'NOTE', 108 'IMPORTANT', 109 'WARNING', 110 ); 111 112 foreach ($words as $k => $word) { 113 $words[$k] = preg_quote($word, '/'); 114 } 115 $words = implode('|', $words); 116 117 $regex = 118 '/^(?:'. 119 '(?:\((?P<hideword>'.$words.')\))'. 120 '|'. 121 '(?:(?P<showword>'.$words.'):))\s*'. 122 '/'; 123 } 124 125 return $regex; 126 } 127}