2
0

docset.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?
  2. // Returns an array of sub-directories for $directory
  3. function sub_directories ($directory, &$directories) {
  4. if (file_exists($directory)) {
  5. $directories[] = $directory;
  6. } else {
  7. $directories = array();
  8. }
  9. if ($handle = @opendir($directory)) {
  10. while (($file = readdir($handle)) !== false) {
  11. if (($file != '.') && ($file != '..') && ($file[0] != '.')) {
  12. $path = "$directory/$file";
  13. if (is_dir($path)) sub_directories($path, $directories);
  14. }
  15. }
  16. closedir($handle);
  17. }
  18. }
  19. class DocSetParser {
  20. var $docset;
  21. var $methods = array();
  22. // Parse a single reference file
  23. private function parse ($path, $class) {
  24. $method = null;
  25. $methods = array();
  26. if ($lines = @file($path)) {
  27. foreach ($lines as $line) {
  28. // Parse instance method description
  29. if ($method) {
  30. if (preg_match("/<span.*data-abstract='(.*)'>/i", $line, $captures)) {
  31. $methods[$method] = strip_tags($captures[1]);
  32. $method = null;
  33. }
  34. }
  35. // Found type definition tag
  36. if (preg_match("/<h3 class=\"tight jump typeDef\">(\w+)<\/h3><p class=\"abstract\">(.*)<\/p>/i", $line, $captures)) {
  37. // Read until the end </p> because I can't get preg_match to do this
  38. $text = substr($captures[2], 0, (stripos($captures[2], "</p>")));
  39. if (!$text) $text = $captures[2];
  40. $methods[$captures[1]] = strip_tags($text);
  41. }
  42. // Found constant tag
  43. if (preg_match("/<code class=\"jump constantName\">(\w+)<\/code><\/dt><dd><p>(.*)<\/p>/i", $line, $captures)) {
  44. //print_r($captures);
  45. // Read until the end </p> because I can't get preg_match to do this
  46. $text = substr($captures[2], 0, (stripos($captures[2], "</p>")));
  47. if (!$text) $text = $captures[2];
  48. $methods[$captures[1]] = strip_tags($text);
  49. }
  50. // Found instance method tag
  51. if (preg_match("<a href=\"#//apple_ref/occ/(instm|intfm|intfp|clm)+/([a-zA-Z:_]+)/([a-zA-Z:_]+)\">", $line, $captures)) {
  52. //print_r($captures);
  53. if ($captures[2] == $class) {
  54. $method = $captures[3];
  55. continue;
  56. }
  57. }
  58. }
  59. } else {
  60. print("* Warning: Can't find the docset at $path.\n");
  61. }
  62. return $methods;
  63. }
  64. // Parses the reference index.html file for the path to the reference html file
  65. private function parse_reference_index ($path) {
  66. $lines = file($path);
  67. foreach ($lines as $line) {
  68. if (preg_match("/<meta id=\"refresh\" http-equiv=\"refresh\" CONTENT=\"0; URL=(.*)\">/i", $line, $captures)) {
  69. return $captures[1];
  70. }
  71. }
  72. }
  73. public function parse_directory ($paths) {
  74. foreach ($paths as $path) {
  75. $path = $this->docset."/Contents/Resources/Documents/documentation".$path;
  76. if (file_exists($path)) {
  77. sub_directories($path, $sub_directories);
  78. foreach ($sub_directories as $directory) {
  79. $name = basename($directory);
  80. if (preg_match("/^(\w+)_(class|protocol)$/i", $name, $captures)) {
  81. $class = $captures[1];
  82. $sub_path = $this->parse_reference_index("$directory/index.html");
  83. if ($methods = $this->parse("$directory/$sub_path", $class)) {
  84. $this->methods[$class] = $methods;
  85. }
  86. }
  87. }
  88. } else {
  89. print("* Warning: The docset at $path can't be found.\n");
  90. }
  91. }
  92. return true;
  93. }
  94. function __construct ($docset) {
  95. $this->docset = $docset;
  96. }
  97. }
  98. // Cocoa
  99. //$path = "/Users/ryanjoseph/Desktop/com.apple.adc.documentation.AppleSnowLeopard.CoreReference.docset";
  100. //$folders = array("/Cocoa/Reference");
  101. // UIKIT
  102. // $path = "/Users/ryanjoseph/Desktop/com.apple.adc.documentation.AppleiOS4_2.iOSLibrary.docset";
  103. //$folders = array("/UIKit/Reference");
  104. //$parser = new DocSetParser($path);
  105. //$parser->parse_directory($folders);
  106. //print_r($parser->methods);
  107. ?>