Path: blob/master/src/applications/diviner/renderer/DivinerDefaultRenderer.php
12256 views
<?php12final class DivinerDefaultRenderer extends DivinerRenderer {34public function renderAtom(DivinerAtom $atom) {5$out = array(6$this->renderAtomTitle($atom),7$this->renderAtomProperties($atom),8$this->renderAtomDescription($atom),9);1011return phutil_tag(12'div',13array(14'class' => 'diviner-atom',15),16$out);17}1819protected function renderAtomTitle(DivinerAtom $atom) {20$name = $this->renderAtomName($atom);21$type = $this->renderAtomType($atom);2223return phutil_tag(24'h1',25array(26'class' => 'atom-title',27),28array($name, ' ', $type));29}3031protected function renderAtomName(DivinerAtom $atom) {32return phutil_tag(33'div',34array(35'class' => 'atom-name',36),37$this->getAtomName($atom));38}3940protected function getAtomName(DivinerAtom $atom) {41if ($atom->getDocblockMetaValue('title')) {42return $atom->getDocblockMetaValue('title');43}4445return $atom->getName();46}4748protected function renderAtomType(DivinerAtom $atom) {49return phutil_tag(50'div',51array(52'class' => 'atom-name',53),54$this->getAtomType($atom));55}5657protected function getAtomType(DivinerAtom $atom) {58return ucwords($atom->getType());59}6061protected function renderAtomProperties(DivinerAtom $atom) {62$props = $this->getAtomProperties($atom);6364$out = array();65foreach ($props as $prop) {66list($key, $value) = $prop;6768$out[] = phutil_tag('dt', array(), $key);69$out[] = phutil_tag('dd', array(), $value);70}7172return phutil_tag(73'dl',74array(75'class' => 'atom-properties',76),77$out);78}7980protected function getAtomProperties(DivinerAtom $atom) {81$properties = array();82$properties[] = array(83pht('Defined'),84$atom->getFile().':'.$atom->getLine(),85);8687return $properties;88}8990protected function renderAtomDescription(DivinerAtom $atom) {91$text = $this->getAtomDescription($atom);92$engine = $this->getBlockMarkupEngine();9394$this->pushAtomStack($atom);95$description = $engine->markupText($text);96$this->popAtomStack();9798return phutil_tag(99'div',100array(101'class' => 'atom-description',102),103$description);104}105106protected function getAtomDescription(DivinerAtom $atom) {107return $atom->getDocblockText();108}109110public function renderAtomSummary(DivinerAtom $atom) {111$text = $this->getAtomSummary($atom);112$engine = $this->getInlineMarkupEngine();113114$this->pushAtomStack($atom);115$summary = $engine->markupText($text);116$this->popAtomStack();117118return phutil_tag(119'span',120array(121'class' => 'atom-summary',122),123$summary);124}125126public function getAtomSummary(DivinerAtom $atom) {127if ($atom->getDocblockMetaValue('summary')) {128return $atom->getDocblockMetaValue('summary');129}130131$text = $this->getAtomDescription($atom);132return PhabricatorMarkupEngine::summarize($text);133}134135public function renderAtomIndex(array $refs) {136$refs = msort($refs, 'getSortKey');137138$groups = mgroup($refs, 'getGroup');139140$out = array();141foreach ($groups as $group_key => $refs) {142$out[] = phutil_tag(143'h1',144array(145'class' => 'atom-group-name',146),147$this->getGroupName($group_key));148149$items = array();150foreach ($refs as $ref) {151$items[] = phutil_tag(152'li',153array(154'class' => 'atom-index-item',155),156array(157$this->renderAtomRefLink($ref),158' - ',159$ref->getSummary(),160));161}162163$out[] = phutil_tag(164'ul',165array(166'class' => 'atom-index-list',167),168$items);169}170171return phutil_tag(172'div',173array(174'class' => 'atom-index',175),176$out);177}178179protected function getGroupName($group_key) {180return $group_key;181}182183protected function getBlockMarkupEngine() {184$engine = PhabricatorMarkupEngine::newMarkupEngine(array());185186$engine->setConfig('preserve-linebreaks', false);187$engine->setConfig('viewer', new PhabricatorUser());188$engine->setConfig('diviner.renderer', $this);189$engine->setConfig('header.generate-toc', true);190191return $engine;192}193194protected function getInlineMarkupEngine() {195return $this->getBlockMarkupEngine();196}197198public function normalizeAtomRef(DivinerAtomRef $ref) {199if (!strlen($ref->getBook())) {200$ref->setBook($this->getConfig('name'));201}202203if ($ref->getBook() != $this->getConfig('name')) {204// If the ref is from a different book, we can't normalize it.205// Just return it as-is if it has enough information to resolve.206if ($ref->getName() && $ref->getType()) {207return $ref;208} else {209return null;210}211}212213$atom = $this->getPublisher()->findAtomByRef($ref);214if ($atom) {215return $atom->getRef();216}217218return null;219}220221protected function getAtomHrefDepth(DivinerAtom $atom) {222if ($atom->getContext()) {223return 4;224} else {225return 3;226}227}228229public function getHrefForAtomRef(DivinerAtomRef $ref) {230$depth = 1;231232$atom = $this->peekAtomStack();233if ($atom) {234$depth = $this->getAtomHrefDepth($atom);235}236237$href = str_repeat('../', $depth);238239$book = $ref->getBook();240$type = $ref->getType();241$name = $ref->getName();242$context = $ref->getContext();243244$href .= $book.'/'.$type.'/';245if ($context !== null) {246$href .= $context.'/';247}248$href .= $name.'/index.html';249250return $href;251}252253protected function renderAtomRefLink(DivinerAtomRef $ref) {254return phutil_tag(255'a',256array(257'href' => $this->getHrefForAtomRef($ref),258),259$ref->getTitle());260}261262}263264265