Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / diviner / renderer / DivinerDefaultRenderer.php
bloba4c551e93d0828ca49436e9fc21398d03a64e134
1 <?php
3 final class DivinerDefaultRenderer extends DivinerRenderer {
5 public function renderAtom(DivinerAtom $atom) {
6 $out = array(
7 $this->renderAtomTitle($atom),
8 $this->renderAtomProperties($atom),
9 $this->renderAtomDescription($atom),
12 return phutil_tag(
13 'div',
14 array(
15 'class' => 'diviner-atom',
17 $out);
20 protected function renderAtomTitle(DivinerAtom $atom) {
21 $name = $this->renderAtomName($atom);
22 $type = $this->renderAtomType($atom);
24 return phutil_tag(
25 'h1',
26 array(
27 'class' => 'atom-title',
29 array($name, ' ', $type));
32 protected function renderAtomName(DivinerAtom $atom) {
33 return phutil_tag(
34 'div',
35 array(
36 'class' => 'atom-name',
38 $this->getAtomName($atom));
41 protected function getAtomName(DivinerAtom $atom) {
42 if ($atom->getDocblockMetaValue('title')) {
43 return $atom->getDocblockMetaValue('title');
46 return $atom->getName();
49 protected function renderAtomType(DivinerAtom $atom) {
50 return phutil_tag(
51 'div',
52 array(
53 'class' => 'atom-name',
55 $this->getAtomType($atom));
58 protected function getAtomType(DivinerAtom $atom) {
59 return ucwords($atom->getType());
62 protected function renderAtomProperties(DivinerAtom $atom) {
63 $props = $this->getAtomProperties($atom);
65 $out = array();
66 foreach ($props as $prop) {
67 list($key, $value) = $prop;
69 $out[] = phutil_tag('dt', array(), $key);
70 $out[] = phutil_tag('dd', array(), $value);
73 return phutil_tag(
74 'dl',
75 array(
76 'class' => 'atom-properties',
78 $out);
81 protected function getAtomProperties(DivinerAtom $atom) {
82 $properties = array();
83 $properties[] = array(
84 pht('Defined'),
85 $atom->getFile().':'.$atom->getLine(),
88 return $properties;
91 protected function renderAtomDescription(DivinerAtom $atom) {
92 $text = $this->getAtomDescription($atom);
93 $engine = $this->getBlockMarkupEngine();
95 $this->pushAtomStack($atom);
96 $description = $engine->markupText($text);
97 $this->popAtomStack();
99 return phutil_tag(
100 'div',
101 array(
102 'class' => 'atom-description',
104 $description);
107 protected function getAtomDescription(DivinerAtom $atom) {
108 return $atom->getDocblockText();
111 public function renderAtomSummary(DivinerAtom $atom) {
112 $text = $this->getAtomSummary($atom);
113 $engine = $this->getInlineMarkupEngine();
115 $this->pushAtomStack($atom);
116 $summary = $engine->markupText($text);
117 $this->popAtomStack();
119 return phutil_tag(
120 'span',
121 array(
122 'class' => 'atom-summary',
124 $summary);
127 public function getAtomSummary(DivinerAtom $atom) {
128 if ($atom->getDocblockMetaValue('summary')) {
129 return $atom->getDocblockMetaValue('summary');
132 $text = $this->getAtomDescription($atom);
133 return PhabricatorMarkupEngine::summarize($text);
136 public function renderAtomIndex(array $refs) {
137 $refs = msort($refs, 'getSortKey');
139 $groups = mgroup($refs, 'getGroup');
141 $out = array();
142 foreach ($groups as $group_key => $refs) {
143 $out[] = phutil_tag(
144 'h1',
145 array(
146 'class' => 'atom-group-name',
148 $this->getGroupName($group_key));
150 $items = array();
151 foreach ($refs as $ref) {
152 $items[] = phutil_tag(
153 'li',
154 array(
155 'class' => 'atom-index-item',
157 array(
158 $this->renderAtomRefLink($ref),
159 ' - ',
160 $ref->getSummary(),
164 $out[] = phutil_tag(
165 'ul',
166 array(
167 'class' => 'atom-index-list',
169 $items);
172 return phutil_tag(
173 'div',
174 array(
175 'class' => 'atom-index',
177 $out);
180 protected function getGroupName($group_key) {
181 return $group_key;
184 protected function getBlockMarkupEngine() {
185 $engine = PhabricatorMarkupEngine::newMarkupEngine(array());
187 $engine->setConfig('preserve-linebreaks', false);
188 $engine->setConfig('viewer', new PhabricatorUser());
189 $engine->setConfig('diviner.renderer', $this);
190 $engine->setConfig('header.generate-toc', true);
192 return $engine;
195 protected function getInlineMarkupEngine() {
196 return $this->getBlockMarkupEngine();
199 public function normalizeAtomRef(DivinerAtomRef $ref) {
200 if (!strlen($ref->getBook())) {
201 $ref->setBook($this->getConfig('name'));
204 if ($ref->getBook() != $this->getConfig('name')) {
205 // If the ref is from a different book, we can't normalize it.
206 // Just return it as-is if it has enough information to resolve.
207 if ($ref->getName() && $ref->getType()) {
208 return $ref;
209 } else {
210 return null;
214 $atom = $this->getPublisher()->findAtomByRef($ref);
215 if ($atom) {
216 return $atom->getRef();
219 return null;
222 protected function getAtomHrefDepth(DivinerAtom $atom) {
223 if ($atom->getContext()) {
224 return 4;
225 } else {
226 return 3;
230 public function getHrefForAtomRef(DivinerAtomRef $ref) {
231 $depth = 1;
233 $atom = $this->peekAtomStack();
234 if ($atom) {
235 $depth = $this->getAtomHrefDepth($atom);
238 $href = str_repeat('../', $depth);
240 $book = $ref->getBook();
241 $type = $ref->getType();
242 $name = $ref->getName();
243 $context = $ref->getContext();
245 $href .= $book.'/'.$type.'/';
246 if ($context !== null) {
247 $href .= $context.'/';
249 $href .= $name.'/index.html';
251 return $href;
254 protected function renderAtomRefLink(DivinerAtomRef $ref) {
255 return phutil_tag(
256 'a',
257 array(
258 'href' => $this->getHrefForAtomRef($ref),
260 $ref->getTitle());