Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Uncomment to test
- // $_GET['name'] = 'FragmentManager.BackStackEntry';
- $baseUrl = 'http://developer.android.com';
- $classesPath = '/reference/classes.html';
- $cacheFile = 'cache.xml';
- $cacheTTL = 14400;
- // Converts all blocks of whitespace to a single SP
- function space($str)
- {
- return preg_replace('/\s{2,}/', ' ', $str);
- }
- // Attempts to converts a DOMElement to sensible markdown
- function dom_to_markdown(DOMElement $el, $baseUrl, $trim = 0)
- {
- $result = '';
- foreach ($el->childNodes as $child) {
- if ($child instanceof DOMText) {
- $result .= space($child->data);
- } else if ($child instanceof DOMElement) {
- if (strtolower($child->tagName) === 'em' || strtolower($child->tagName) === 'emb') {
- $result = '*' . dom_to_markdown($child, $baseUrl, 2) . '*';
- } else if (strtolower($child->tagName) === 'i') {
- $result .= '*' . space($child->firstChild->data) . '*';
- } else if (strtolower($child->tagName) === 'b' || strtolower($child->tagName) === 'strong') {
- $result .= '**' . space($child->firstChild->data) . '**';
- } else if (strtolower($child->tagName) === 'p') {
- $result = space($result . dom_to_markdown($child, $baseUrl));
- } else if (strtolower($child->tagName) === 'tt' || strtolower($child->tagName) === 'blockquote') {
- $result .= '`' . space($child->firstChild->data) . '`';
- } else if (strtolower($child->tagName) === 'br') {
- $result = space($result . ' ');
- } else if (strtolower($child->tagName) === 'code') {
- if ($child->firstChild instanceof DOMText) {
- $result .= '`' . space($child->firstChild->data) . '`';
- } else if ($child->firstChild instanceof DOMElement && strtolower($child->firstChild->tagName) === 'a' && $child->firstChild->firstChild instanceof DOMText) {
- $result .= '[`' . space($child->firstChild->firstChild->data) . '`](' . $baseUrl . $child->firstChild->getAttribute('href') . ')';
- }
- } else if (strtolower($child->tagName) === 'a' && $child->firstChild instanceof DOMText) {
- $result .= '[' . space($child->firstChild->data) . '](' . $baseUrl . $child->getAttribute('href') . ')';
- }
- }
- }
- return substr(trim($result, " \t\r\n\x00\xC2\xA0"), 0, 350 - $trim);
- }
- error_reporting(0);
- if (!isset($_GET['name'])) {
- header('HTTP/1.1 400 Bad Request');
- exit('No class specified');
- }
- $class = basename($_GET['name']);
- $cache = new DOMDocument('1.0', 'utf-8');
- if (!file_exists($cacheFile) || filemtime($cacheFile) < time() - $cacheTTL) {
- // Rebuild cache file
- $remote = new DOMDocument;
- @$remote->loadHTMLFile($baseUrl . $classesPath);
- $remoteXPath = new DOMXPath($remote);
- $cacheRoot = $cache->appendChild($cache->createElement('root'));
- $result = $remoteXPath->query("//td[@class='jd-linkcol']/a");
- foreach ($result as $match) {
- $el = $cache->createElement(strtolower($match->firstChild->data));
- $el->setAttribute('name', $match->firstChild->data);
- $el->setAttribute('href', $baseUrl . $match->getAttribute('href'));
- $desc = $remoteXPath->query("./td[@class='jd-descrcol']", $match->parentNode->parentNode)->item(0);
- $el->setAttribute('desc', dom_to_markdown($desc, $baseUrl));
- $cacheRoot->appendChild($el);
- }
- unset($remote, $remoteXPath, $result, $match, $cacheRoot, $el);
- $cache->save($cacheFile);
- } else {
- // Load cache
- $cache->load($cacheFile);
- }
- // Find the class we want
- $xpath = new DOMXPath($cache);
- if (!$result = $xpath->query('/root/' . strtolower($class))->item(0)) {
- header('HTTP/1.1 404 Not Found');
- exit("Unable to locate class {$class}");
- }
- // Create the markdown
- $result = '[`' . $result->getAttribute('name') . '`](' . $result->getAttribute('href') . ') - ' . $result->getAttribute('desc');
- // JSONP-ify if required
- if (isset($_GET['callback'])) {
- $result = $_GET['callback'] . '(' . json_encode(array('src' => $result)) . ')';
- }
- echo $result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement