@purescraps/purehtml 0.1.6 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -0
- package/README.md +4 -72
- package/dist/examples/simple.js +6 -1
- package/dist/examples/simple.js.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.js +8 -1
- package/dist/src/index.js.map +1 -1
- package/package.json +2 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2024 Fatih Kaya <1994274@gmail.com>
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,75 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# PureHTML: Parsing specification for extracting JSON from HTML
|
|
2
2
|
|
|
3
|
-
This
|
|
3
|
+
This is the TypeScript/JavaScript implementation of the PureHTML specification.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
You need to install [NodeJS](https://nodejs.org) first, then follow the steps explained in the documentation.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
import { ConfigFactory, extract } from '@purescraps/purehtml';
|
|
11
|
-
|
|
12
|
-
const yaml = '...';
|
|
13
|
-
const html = '...';
|
|
14
|
-
const config = ConfigFactory.fromYAML(yaml);
|
|
15
|
-
const result = extract(html, config);
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
### `extract($: cheerio.Root | string, config: Config): any`
|
|
19
|
-
|
|
20
|
-
You may pass either `cheerio.Root` or `string` (html contents) to `extract` function.
|
|
21
|
-
If you wish, you can use the same `cheerio.Root` for multiple configs without need to
|
|
22
|
-
call `load(html)` again and again.
|
|
23
|
-
|
|
24
|
-
## Configuration
|
|
25
|
-
|
|
26
|
-
- `selector` allows to filter the current HTML element with a CSS selector query. **optional**.
|
|
27
|
-
- `type` can be `string`, `object` and `array`. **optional**.
|
|
28
|
-
if you set `properties`, then your `type` will be `object`.
|
|
29
|
-
if you set `items`, then your `type` will be `array`
|
|
30
|
-
- `type` might become `number`, `boolean`, `object` and `array` **implicitly** by the `transform` parameter.
|
|
31
|
-
- `transform` can be either:
|
|
32
|
-
- name of one of the core transformers. see `Core Transformers` section below.
|
|
33
|
-
- [not available for YAML] an instance of a class that extends `Transformer` class.
|
|
34
|
-
- array of above
|
|
35
|
-
- `properties: { [key: string]: Config }` implicitly sets `type` to `object`.
|
|
36
|
-
- `items: Config` defines the configuration that will be used to extract data for every match of the selector.
|
|
37
|
-
- `union: ConfigWithSelector[]` extracts data for the first matching configuration only. Extracts `null` if
|
|
38
|
-
none of the given configurations match. Can't be used with any other properties. `selector` IS required in union items.
|
|
39
|
-
- `constant: any` always extracts the provided value. Can't be used with any other properties.
|
|
40
|
-
|
|
41
|
-
## Selectors
|
|
42
|
-
|
|
43
|
-
[cheerio](https://npmjs.com/package/cheerio) is used for matching elements so the format you provided MUST be valid for cheerio. Except the **special selectors**.
|
|
44
|
-
|
|
45
|
-
### Special Selectors
|
|
46
|
-
|
|
47
|
-
- **$self**
|
|
48
|
-
Passes the same element down to the extraction process.
|
|
49
|
-
|
|
50
|
-
## Core Transformers
|
|
51
|
-
|
|
52
|
-
1. **length**:
|
|
53
|
-
Input type: `string | array`
|
|
54
|
-
Output type: `number`
|
|
55
|
-
|
|
56
|
-
2. **trim**:
|
|
57
|
-
Input type: `string`
|
|
58
|
-
Output type: `string`
|
|
59
|
-
|
|
60
|
-
3. **exists**:
|
|
61
|
-
Input type: `$`
|
|
62
|
-
Output type: `number`
|
|
63
|
-
|
|
64
|
-
4. **match-count**:
|
|
65
|
-
Input type: `$`
|
|
66
|
-
Output type: `string`
|
|
67
|
-
Returns matched element count of your selector.
|
|
68
|
-
|
|
69
|
-
5. **exists**:
|
|
70
|
-
Input type: `$`
|
|
71
|
-
Output type: `boolean`
|
|
72
|
-
|
|
73
|
-
6. **html**:
|
|
74
|
-
Input type: `$`
|
|
75
|
-
Output type: `string`
|
|
7
|
+
See [https://purescraps.github.io/purehtml/](https://purescraps.github.io/purehtml/) for documentation and playground.
|
package/dist/examples/simple.js
CHANGED
|
@@ -6812,6 +6812,9 @@ var ConfigWithSelector = class extends Config {
|
|
|
6812
6812
|
if (this.selector === null) {
|
|
6813
6813
|
return $2;
|
|
6814
6814
|
}
|
|
6815
|
+
if ($2.is(this.selector)) {
|
|
6816
|
+
return $2;
|
|
6817
|
+
}
|
|
6815
6818
|
return $2.find(this.selector);
|
|
6816
6819
|
}
|
|
6817
6820
|
};
|
|
@@ -7502,7 +7505,9 @@ var ConfigFactory = class _ConfigFactory {
|
|
|
7502
7505
|
transform
|
|
7503
7506
|
);
|
|
7504
7507
|
case "union":
|
|
7505
|
-
return UnionConfig.generate(
|
|
7508
|
+
return UnionConfig.generate(
|
|
7509
|
+
union.map((cfg) => this.generate(cfg))
|
|
7510
|
+
);
|
|
7506
7511
|
default:
|
|
7507
7512
|
return PrimitiveValueConfig.generate(selector, transform);
|
|
7508
7513
|
}
|