Passed
Push — master ( 28bf1e...61fea9 )
by Arthur
02:09
created

addClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 2
b 0
f 0
1
<?php
2
/**
3
 * Compare jQuery documentation to QueryPath documentation
4
 *
5
 * 
6
 * @author Emily Brand
7
 * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
8
 * @see http://api.jquery.com/api/
9
 * 
10
 * TODO: add to the xml file
11
 */
12
13
require_once '../src/QueryPath/QueryPath.php';
14
15
/**
16
 * Add the link & class to each key to show in the left div.
17
 * 
18
 * @param String $v
19
 */
20
function addClasses($v) {
21
  return "<a href='".$_SERVER['PHP_SELF']."?key=$v'><span class='keyname'>$v</span></a><br />";
22
}
23
24
// The document skeleton
25
$qpdoc = htmlqp('doc.html', 'body');
26
27
$key = $_GET['key'];
28
29
// The jQuery categories that are used in QueryPath
30
$qparray = array('Tree Traversal', 'Child Filter', 'Attribute', 'Content Filter', 'Basic Filter', 
31
'Hierarchy', 'Basic', 'Filtering', 'Miscellaneous Traversing', 'DOM Insertion, Outside', 
32
'DOM Insertion, Inside', 'Attributes', 'Style Properties');
33
34
$jqnames = array();
35
$qpnames = array();
36
37
// Search through the xml file to find any entries of jQuery entities
38
foreach(qp('querypath.xml', 'entry') as $entry) {
39
  $qpnames[$entry->attr('name')] =  
40
      array('desc' => $entry->find('desc')->innerXML(), 
41
            'jquery' => $entry->parent()->find('jquery')->innerXML(), 
42
            'querypath' => $entry->parent()->find('querypath')->innerXML());
43
}
44
45
// Search through the xml file to find all entries of jQuery entities
46
foreach(htmlqp('http://api.jquery.com/api/', 'entry') as $entry) {
47
  $category = false;
48
  $category = array_search($entry->find('category:first')->attr('name'), $qparray);
49
  while($entry->next('category')->html() != null) {
50
    $category = (array_search($entry->attr('name'), $qparray)) ? true : $category;
51
    if($category) break;
52
  }
53
  if($category) {
54
    $jqnames[$entry->parent()->attr('name')] =  
55
      array('longdesc' => $entry->find('longdesc')->innerXML(), 
56
            'name' => $entry->parent()->find('category')->attr('name'));
57
  }
58
}
59
60
// Map the keys & sort them
61
$jqkeys = array_keys($jqnames);
62
$jqkeys = array_map("addClasses", $jqkeys);
63
sort($jqkeys);
64
65
// Add the keys to the nav bar
66
$qpdoc->find('#leftbody');
67
foreach($jqkeys as $k => $v) {
68
  $qpdoc->append($v);
69
}
70
71
// Add the description to the main window if the key exists
72
if(array_key_exists($key, $jqnames)) {
73
  if(array_key_exists($key, $qpnames)) {
74
    $qpdoc->top()->find('#rightfunction')->text('Function: '.ucfirst($key));
75
    $qpdoc->top()->find('#rightdesc')->text($qpnames[$key]['desc']);
76
    $qpdoc->top()->find('#righttitle')->text('How it\'s done in jQuery');
77
    $qpdoc->top()->find('#righttext')->text($qpnames[$key]['jquery']);
78
    $qpdoc->top()->find('#righttitle2')->text('How it\'s done in QueryPath');
79
    $qpdoc->top()->find('#righttext2')->text($qpnames[$key]['querypath']);
80
  }
81
  else {
82
    $qpdoc->top()->find('#rightfunction')->text('Function: '.ucfirst($key));
83
    $qpdoc->top()->find('#rightdesc')->remove();
84
    $qpdoc->top()->find('#righttitle')->text('jQuery Documentation');
85
    $qpdoc->top()->find('#righttext')->append($jqnames[$key]['longdesc']);
86
  }
87
}
88
89
// Write the document
90
$qpdoc->writeHTML();
91
92