Skip to content

Commit ebcea38

Browse files
authored
Merge pull request #21 from GravityPDF/library-info
Refine copy/examples in README and composer.json
2 parents 465627c + 30bc7eb commit ebcea38

File tree

2 files changed

+139
-123
lines changed

2 files changed

+139
-123
lines changed

README.md

Lines changed: 84 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,106 +2,124 @@
22

33
QueryPath is a jQuery-like library for working with XML and HTML(5) documents in PHP. It is stable software, [with the original library garnering 4M+ downloads](https://packagist.org/packages/querypath/querypath) since first published in 2009.
44

5-
**⚠️ This is a fork of a fork. The [original library](https://github.com/technosophos/querypath), and [subsequent fork](https://github.com/arthurkushman/querypath), are no longer being maintained. The aim of `gravitypdf/querypath` is to ensure the library remains compatible with the latest version of PHP, and bug free. 🧑‍💻There is still a lot of legacy code to clean up + modernize, and any assistance given is appreciated.**
5+
**This is a fork of a fork. The [original library](https://github.com/technosophos/querypath), and [subsequent fork](https://github.com/arthurkushman/querypath), are no longer being maintained. The aim of `gravitypdf/querypath` is to ensure the library remains compatible with the latest version of PHP, and bug free. 🧑‍💻There is still a lot of legacy code to clean up + modernize, and any assistance given is appreciated.**
6+
7+
> If you are viewing this file on QueryPath GitHub repository homepage or on Packagist, please note that the default repository branch is `main` which can differ from the last stable release.
68
79
[![Latest Stable Version](https://poser.pugx.org/gravitypdf/querypath/v/stable)](https://packagist.org/packages/gravitypdf/querypath)
810
[![Total Downloads](https://poser.pugx.org/gravitypdf/querypath/downloads)](https://packagist.org/packages/gravitypdf/querypath)
911
[![License](https://poser.pugx.org/gravitypdf/querypath/license)](https://packagist.org/packages/gravitypdf/querypath)
1012

11-
> ⚠ If you are viewing this file on QueryPath GitHub repository homepage or on Packagist, please note that the default repository branch is `main` which can differ from the last stable release.
12-
1313
## Installation
1414
```
1515
composer require gravitypdf/querypath
1616
```
1717

1818
## Basic Usage
1919

20-
You can parse documents like this:
20+
To parse HTML or XML:
2121

2222
```php
2323
<?php
2424
// Assuming you installed from Composer:
25-
require 'vendor/autoload.php';
26-
27-
// HTML5
28-
$qp = html5qp('path/to/file.html');
29-
30-
// Legacy HTML via libxml
31-
$qp = htmlqp('path/to/file.html');
25+
require_once __DIR__.'/vendor/autoload.php';
26+
27+
try {
28+
// Recommended: uses the masterminds/html5 library to process the HTML
29+
$qp = html5qp(__DIR__.'/path/to/file.html'); // load a file from disk
30+
$qp = html5qp('<div>You can pass a string of HTML directly to the function</div>'); // load a string
31+
} catch (\QueryPath\Exception $e) {
32+
// Handle error
33+
}
3234

33-
// XML or XHTML
34-
$qp = qp('path/to/file.html');
35+
try {
36+
// Legacy: uses libxml to parse HTML
37+
$qp = htmlqp(__DIR__.'/path/to/file.html'); // load a file from disk
38+
$qp = htmlqp('<div>You can pass a string of HTML directly to the function</div>'); // load a string
39+
} catch (\QueryPath\Exception $e) {
40+
// Handle error
41+
}
3542

36-
// All of the above can take string markup instead of a file name:
37-
$qp = qp('<?xml version='1.0'?><hello><world/></hello>')
43+
try {
44+
// XML or XHTML
45+
$qp = qp(__DIR__.'/path/to/file.html'); // load a file from disk
46+
$qp = qp("<?xml version='1.0'?><hello><world/></hello>"); // load a string
47+
} catch (\QueryPath\Exception $e) {
48+
// Handle error
49+
}
3850
```
3951

40-
The real power of QueryPath comes from chaining methods together:
52+
The real power of QueryPath comes from chaining methods together. This example will generate a valid HTML5 document and output to the browser:
4153

4254
```php
43-
require 'vendor/autoload.php';
44-
45-
html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title')
46-
// Add some text to the title
47-
->text('Example of QueryPath.')
48-
// Now look for the <body> element
49-
->top('body')
50-
// Inside the body, add a title and paragraph.
51-
->append('<h1>This is a test page</h1><p>Test text</p>')
52-
// Now we select the paragraph we just created inside the body
53-
->children('p')
54-
// Add a 'class="some-class"' attribute to the paragraph
55-
->attr('class', 'some-class')
56-
// And add a style attribute, too, setting the background color.
57-
->css('background-color', '#eee')
58-
// Now go back to the paragraph again
59-
->parent()
60-
// Before the paragraph and the title, add an empty table.
61-
->prepend('<table id="my-table"></table>')
62-
// Now let's go to the table...
63-
->top('#my-table')
64-
// Add a couple of empty rows
65-
->append('<tr></tr><tr></tr>')
66-
// select the rows (both at once)
67-
->children()
68-
// Add a CSS class to both rows
69-
->addClass('table-row')
70-
// Now just get the first row (at position 0)
71-
->eq(0)
72-
// Add a table header in the first row
73-
->append('<th>This is the header</th>')
74-
// Now go to the next row
75-
->next()
76-
// Add some data to this row
77-
->append('<td>This is the data</td>')
78-
// Write it all out as HTML
79-
->writeHTML5();
55+
try {
56+
html5qp(\QueryPath\QueryPath::HTML5_STUB, 'title')
57+
// Add some text to the title
58+
->text('Example of QueryPath.')
59+
// Now look for the <body> element
60+
->top('body')
61+
// Inside the body, add a title and paragraph.
62+
->append('<h1>This is a test page</h1><p>Test text</p>')
63+
// Now we select the paragraph we just created inside the body
64+
->children('p')
65+
// Add a 'class="some-class"' attribute to the paragraph
66+
->attr('class', 'some-class')
67+
// And add a style attribute, too, setting the background color.
68+
->css('background-color', '#eee')
69+
// Now go back to the paragraph again
70+
->parent()
71+
// Before the paragraph and the title, add an empty table.
72+
->prepend('<table id="my-table"></table>')
73+
// Now let's go to the table...
74+
->top('#my-table')
75+
// Add a couple of empty rows
76+
->append('<tr></tr><tr></tr>')
77+
// select the rows (both at once)
78+
->children()
79+
// Add a CSS class to both rows
80+
->addClass('table-row')
81+
// Now just get the first row (at position 0)
82+
->eq(0)
83+
// Add a table header in the first row
84+
->append('<th>This is the header</th>')
85+
// Now go to the next row
86+
->next()
87+
// Add some data to this row
88+
->append('<td>This is the data</td>')
89+
// Write it all out as HTML
90+
->writeHTML5();
91+
} catch (\QueryPath\Exception $e) {
92+
// Handle error
93+
}
8094
```
8195

82-
You can also search for specific elements and loop over any matches:
96+
You can find specific nodes, loop over the matches, and extract information about each element:
8397

8498
```php
85-
$html = '
86-
<ul>
87-
<li>Foo</li>
88-
<li>Bar</li>
89-
<li>FooBar</li>
90-
</ul>';
91-
92-
$qp = html5qp($html);
93-
foreach ($qp->find('li') as $li) {
94-
echo $li->text() .'<br>';
99+
try {
100+
$html = '
101+
<ul>
102+
<li>Foo</li>
103+
<li>Bar</li>
104+
<li>FooBar</li>
105+
</ul>';
106+
107+
$qp = html5qp($html);
108+
foreach ($qp->find('li') as $li) {
109+
echo $li->text() .'<br>';
110+
}
111+
} catch (\QueryPath\Exception $e) {
112+
// Handle error
95113
}
96114
```
97115

98-
See the [examples directory files](https://github.com/GravityPDF/querypath/tree/main/examples) for more usages of QueryPath.
116+
See the [examples directory files](https://github.com/GravityPDF/querypath/tree/main/examples) for more usages.
99117

100118
## Online Manual
101119

102120
The legacy QueryPath manual has been automatically generated from inline DocBlocks using phpDocumentor, and can be found at [http://querypath.org](http://querypath.org/).
103121

104-
> ⚠️ Note: The website querypath.org is not built or maintained by Gravity PDF, and we have no access to manage or change anything. [Help is wanted writing new documentation in the repo's Wiki](https://github.com/GravityPDF/querypath/wiki).
122+
> ⚠️ querypath.org is not built or maintained by Gravity PDF, and we have no access to manage or change the website. [Help writing new documentation in the repo's Wiki is wanted](https://github.com/GravityPDF/querypath/wiki).
105123
106124
## General Troubleshooting
107125

composer.json

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,58 @@
11
{
2-
"name": "gravitypdf/querypath",
3-
"type": "library",
4-
"description": "HTML/XML querying (CSS 4 or XPath) and processing (like jQuery)",
5-
"homepage": "https://github.com/gravitypdf/querypath",
6-
"license": "MIT",
7-
"keywords": [
8-
"xml",
9-
"html",
10-
"css",
11-
"jquery",
12-
"xslt",
13-
"php7"
14-
],
15-
"support": {
16-
"issues": "https://github.com/gravitypdf/querypath/issues",
17-
"source": "https://github.com/gravitypdf/querypath",
18-
"forum": "https://github.com/gravitypdf/querypath/discussion"
19-
},
20-
"funding": [
21-
{
22-
"type": "paypal",
23-
"url": "https://www.paypal.com/donate/?hosted_button_id=VRUVBVM6BZPKN"
24-
}
25-
],
26-
"require": {
27-
"php": "^7.1 || ~8.0.0 || ~8.1.0",
28-
"masterminds/html5": "2.*"
29-
},
30-
"autoload": {
31-
"psr-4": {
32-
"QueryPath\\": "src/",
33-
"QueryPathTests\\": "tests/QueryPath/"
34-
},
35-
"files": [
36-
"src/qp_functions.php"
37-
]
38-
},
39-
"require-dev": {
40-
"mockery/mockery": "^1.1",
41-
"yoast/phpunit-polyfills": "^1.0",
42-
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
43-
"phpcompatibility/php-compatibility": "*"
44-
},
45-
"config": {
46-
"allow-plugins": {
47-
"dealerdirect/phpcodesniffer-composer-installer": true
48-
}
49-
},
50-
"scripts": {
51-
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs",
52-
"lint:fix": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf",
53-
"lint:min-php": "@lint --standard=phpcompat.xml"
2+
"name": "gravitypdf/querypath",
3+
"type": "library",
4+
"description": "PHP library for HTML(5)/XML querying (CSS 4 or XPath) and processing (like jQuery)",
5+
"homepage": "https://github.com/gravitypdf/querypath",
6+
"license": "MIT",
7+
"keywords": [
8+
"xml",
9+
"html",
10+
"css",
11+
"jquery",
12+
"php"
13+
],
14+
"support": {
15+
"issues": "https://github.com/gravitypdf/querypath/issues",
16+
"source": "https://github.com/gravitypdf/querypath",
17+
"forum": "https://github.com/gravitypdf/querypath/discussion"
18+
},
19+
"funding": [
20+
{
21+
"type": "paypal",
22+
"url": "https://www.paypal.com/donate/?hosted_button_id=VRUVBVM6BZPKN"
23+
}
24+
],
25+
"require": {
26+
"php": "^7.1 || ~8.0.0 || ~8.1.0",
27+
"masterminds/html5": "^2.0"
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"QueryPath\\": "src/",
32+
"QueryPathTests\\": "tests/QueryPath/"
5433
},
55-
"replace": {
56-
"arthurkushman/query-path": "3.1.4",
57-
"querypath/querypath": "3.0.5"
34+
"files": [
35+
"src/qp_functions.php"
36+
]
37+
},
38+
"require-dev": {
39+
"mockery/mockery": "^1.1",
40+
"yoast/phpunit-polyfills": "^1.0",
41+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
42+
"phpcompatibility/php-compatibility": "*"
43+
},
44+
"config": {
45+
"allow-plugins": {
46+
"dealerdirect/phpcodesniffer-composer-installer": true
5847
}
59-
60-
}
48+
},
49+
"scripts": {
50+
"lint": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcs",
51+
"lint:fix": "@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf",
52+
"lint:min-php": "@lint --standard=phpcompat.xml"
53+
},
54+
"replace": {
55+
"arthurkushman/query-path": "3.1.4",
56+
"querypath/querypath": "3.0.5"
57+
}
58+
}

0 commit comments

Comments
 (0)