/** * Rewrite links with special "api:" prefix, from two possible formats: * 1. [api:DataObject] * 2. (My Title)(api:DataObject) * * Hack: Replaces any backticks with "<code>" blocks, * as the currently used markdown parser doesn't resolve links in backticks, * but does resolve in "<code>" blocks. * * @param String $md * @param DocumentationPage $page * @return String */ public static function rewrite_api_links($md, $page) { // Links with titles $re = '/ `? \\[ (.*?) # link title (non greedy) \\] \\( api:(.*?) # link url (non greedy) \\) `? /x'; preg_match_all($re, $md, $linksWithTitles); if ($linksWithTitles) { foreach ($linksWithTitles[0] as $i => $match) { $title = $linksWithTitles[1][$i]; $subject = $linksWithTitles[2][$i]; $url = sprintf(self::$api_link_base, urlencode($subject), urlencode($page->getVersion()), urlencode($page->getEntity()->getKey())); $md = str_replace($match, sprintf('[%s](%s)', $title, $url), $md); } } // Bare links $re = '/ `? \\[ api:(.*?) \\] `? /x'; preg_match_all($re, $md, $links); if ($links) { foreach ($links[0] as $i => $match) { $subject = $links[1][$i]; $url = sprintf(self::$api_link_base, $subject, $page->getVersion(), $page->getEntity()->getKey()); $md = str_replace($match, sprintf('[%s](%s)', $subject, $url), $md); } } return $md; }