SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | - | $data = file_get_contents('http://developer.android.com/reference/classes.html'); |
3 | + | // Uncomment to test |
4 | - | $class = $_GET['name']; |
4 | + | // $_GET['name'] = 'FragmentManager.BackStackEntry'; |
5 | - | $keyword = $class . '.html'; |
5 | + | |
6 | - | $lengthKey = strlen($keyword); |
6 | + | $baseUrl = 'http://developer.android.com'; |
7 | $classesPath = '/reference/classes.html'; | |
8 | - | $pos = strpos($data, $keyword); |
8 | + | |
9 | $cacheFile = 'cache.xml'; | |
10 | - | $substring = $keyword; |
10 | + | $cacheTTL = 14400; |
11 | - | $i = 0; |
11 | + | |
12 | // Converts all blocks of whitespace to a single SP | |
13 | - | $firstTen = substr($substring, 0, 10); |
13 | + | function space($str) |
14 | - | $match = "/reference"; |
14 | + | { |
15 | - | while($firstTen != $match) { |
15 | + | return preg_replace('/\s{2,}/', ' ', $str); |
16 | - | $i = $i + 1; |
16 | + | |
17 | - | $substring = substr($data, $pos-$i, $lengthKey+$i); |
17 | + | |
18 | - | $firstTen = substr($substring, 0, 10); |
18 | + | // Attempts to converts a DOMElement to sensible markdown |
19 | function dom_to_markdown(DOMElement $el, $baseUrl, $trim = 0) | |
20 | { | |
21 | $result = ''; | |
22 | - | $finalURL = 'http://developer.android.com' . $substring; |
22 | + | |
23 | foreach ($el->childNodes as $child) { | |
24 | if ($child instanceof DOMText) { | |
25 | - | $final = '[' . $class . ']('.$finalURL.')'; |
25 | + | $result .= space($child->data); |
26 | } else if ($child instanceof DOMElement) { | |
27 | - | if( isset( $_REQUEST['callback'] ) ) { |
27 | + | if (strtolower($child->tagName) === 'em' || strtolower($child->tagName) === 'emb') { |
28 | - | echo $_REQUEST['callback'] . "({src:'" . $final ."'});"; |
28 | + | $result = '*' . dom_to_markdown($child, $baseUrl, 2) . '*'; |
29 | } else if (strtolower($child->tagName) === 'i') { | |
30 | - | echo $final; |
30 | + | $result .= '*' . space($child->firstChild->data) . '*'; |
31 | } else if (strtolower($child->tagName) === 'b' || strtolower($child->tagName) === 'strong') { | |
32 | - | ?> |
32 | + | $result .= '**' . space($child->firstChild->data) . '**'; |
33 | } else if (strtolower($child->tagName) === 'p') { | |
34 | $result = space($result . dom_to_markdown($child, $baseUrl)); | |
35 | } else if (strtolower($child->tagName) === 'tt' || strtolower($child->tagName) === 'blockquote') { | |
36 | $result .= '`' . space($child->firstChild->data) . '`'; | |
37 | } else if (strtolower($child->tagName) === 'br') { | |
38 | $result = space($result . ' '); | |
39 | } else if (strtolower($child->tagName) === 'code') { | |
40 | if ($child->firstChild instanceof DOMText) { | |
41 | $result .= '`' . space($child->firstChild->data) . '`'; | |
42 | } else if ($child->firstChild instanceof DOMElement && strtolower($child->firstChild->tagName) === 'a' && $child->firstChild->firstChild instanceof DOMText) { | |
43 | $result .= '[`' . space($child->firstChild->firstChild->data) . '`](' . $baseUrl . $child->firstChild->getAttribute('href') . ')'; | |
44 | } | |
45 | } else if (strtolower($child->tagName) === 'a' && $child->firstChild instanceof DOMText) { | |
46 | $result .= '[' . space($child->firstChild->data) . '](' . $baseUrl . $child->getAttribute('href') . ')'; | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
51 | return substr(trim($result, " \t\r\n\x00\xC2\xA0"), 0, 350 - $trim); | |
52 | } | |
53 | ||
54 | error_reporting(0); | |
55 | ||
56 | if (!isset($_GET['name'])) { | |
57 | header('HTTP/1.1 400 Bad Request'); | |
58 | exit('No class specified'); | |
59 | } | |
60 | $class = basename($_GET['name']); | |
61 | ||
62 | $cache = new DOMDocument('1.0', 'utf-8'); | |
63 | ||
64 | if (!file_exists($cacheFile) || filemtime($cacheFile) < time() - $cacheTTL) { | |
65 | // Rebuild cache file | |
66 | $remote = new DOMDocument; | |
67 | @$remote->loadHTMLFile($baseUrl . $classesPath); | |
68 | $remoteXPath = new DOMXPath($remote); | |
69 | ||
70 | $cacheRoot = $cache->appendChild($cache->createElement('root')); | |
71 | ||
72 | $result = $remoteXPath->query("//td[@class='jd-linkcol']/a"); | |
73 | foreach ($result as $match) { | |
74 | $el = $cache->createElement(strtolower($match->firstChild->data)); | |
75 | $el->setAttribute('name', $match->firstChild->data); | |
76 | $el->setAttribute('href', $baseUrl . $match->getAttribute('href')); | |
77 | $desc = $remoteXPath->query("./td[@class='jd-descrcol']", $match->parentNode->parentNode)->item(0); | |
78 | $el->setAttribute('desc', dom_to_markdown($desc, $baseUrl)); | |
79 | $cacheRoot->appendChild($el); | |
80 | } | |
81 | ||
82 | unset($remote, $remoteXPath, $result, $match, $cacheRoot, $el); | |
83 | ||
84 | $cache->save($cacheFile); | |
85 | } else { | |
86 | // Load cache | |
87 | $cache->load($cacheFile); | |
88 | } | |
89 | ||
90 | // Find the class we want | |
91 | $xpath = new DOMXPath($cache); | |
92 | if (!$result = $xpath->query('/root/' . strtolower($class))->item(0)) { | |
93 | header('HTTP/1.1 404 Not Found'); | |
94 | exit("Unable to locate class {$class}"); | |
95 | } | |
96 | ||
97 | // Create the markdown | |
98 | $result = '[`' . $result->getAttribute('name') . '`](' . $result->getAttribute('href') . ') - ' . $result->getAttribute('desc'); | |
99 | ||
100 | // JSONP-ify if required | |
101 | if (isset($_GET['callback'])) { | |
102 | $result = $_GET['callback'] . '(' . json_encode(array('src' => $result)) . ')'; | |
103 | } | |
104 | ||
105 | echo $result; |