purescript-cheerio/README.md

80 lines
1.5 KiB
Markdown
Raw Normal View History

2018-01-16 04:35:48 +00:00
[![Build Status](https://travis-ci.org/icyrockcom/purescript-cheerio.svg?branch=master)](https://travis-ci.org/icyrockcom/purescript-cheerio)
## Description
Basic bindings for [cheerio](https://cheerio.js.org/). Only includes read-only functionality for now.
## Installation
Install [cheerio](https://www.npmjs.com/package/cheerio) dependency:
2020-04-17 17:23:07 +00:00
```bash
$ npm install --save cheerio
```
2018-01-16 04:35:48 +00:00
2020-04-17 17:23:07 +00:00
Install this package using [spago](https://github.com/purescript/spago):
2018-01-16 04:35:48 +00:00
2020-04-17 17:23:07 +00:00
* Add package to your `spago.dhall`:
2018-01-16 04:35:48 +00:00
2020-04-17 17:23:07 +00:00
```dhall
...
dependencies = [ ..., "cheerio" ]
...
```
2018-01-16 04:35:48 +00:00
2020-04-17 17:23:07 +00:00
* Install packages by running:
2018-01-16 04:35:48 +00:00
2020-04-17 17:23:07 +00:00
```bash
$ spago install
```
2018-01-16 04:35:48 +00:00
## Example
From [basic example](examples/Basic.purs).
Imports:
2020-04-17 17:23:07 +00:00
```purescript
import Cheerio (Cheerio, find, length)
import Cheerio.Static (loadRoot)
```
2018-01-16 04:35:48 +00:00
Example html:
2020-04-17 17:23:07 +00:00
```purescript
htmlEx :: String
htmlEx = """
<ul id="fruits">
<li class="apple">Apple</li>
<li class="orange">Orange</li>
<li class="pear">Pear</li>
</ul>
"""
```
2018-01-16 04:35:48 +00:00
Load it and get the root element:
2020-04-17 17:23:07 +00:00
```purescript
root :: Cheerio
root = loadRoot htmlEx
```
2018-01-16 04:35:48 +00:00
Use the query functions:
2020-04-17 17:23:07 +00:00
```purescript
let fruitCount = root # find "#fruits" # find "li" # length
in log $ "Number of fruits: " <> show fruitCount
```
2018-01-16 04:35:48 +00:00
For more examples, please take a look at the [unit tests](test/Test/Main.purs). They cover most of the read-only cheerio functions.
## Issues or suggestions
If you run into any issues or have suggestions, please open an issue or submit a pull request. Both are welcome!
Be prepared to wait more than a couple of days for a response though :)
## License
MIT