Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gregors/sax_html
SAX-like parser for HTML
https://github.com/gregors/sax_html
sax sax-parser
Last synced: 2 months ago
JSON representation
SAX-like parser for HTML
- Host: GitHub
- URL: https://github.com/gregors/sax_html
- Owner: gregors
- Created: 2021-02-14T03:05:57.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-02-21T19:05:30.000Z (almost 4 years ago)
- Last Synced: 2024-09-23T16:17:39.167Z (4 months ago)
- Topics: sax, sax-parser
- Language: Elixir
- Homepage:
- Size: 10.7 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SaxHtml
Simple SAX-like parser for HTML.
It accepts an html string and an anonymous callback function that fires for every event```elixir
html_text = """
Gregors
Howdy!!!
"""
handler = fn {event, data, state} -> IO.puts("#{event} - #{data}") end
SaxHtml.parse(html_text, handler)
start_tag - html
characters -start_tag - head
characters -start_tag - title
characters - Gregors
end_tag - title
characters -end_tag - head
characters -start_tag - body
characters -start_tag - div
characters -start_tag - p
characters - Howdy!!!
end_tag - p
characters -end_tag - div
characters -end_tag - body
characters -end_tag - html
characters -
```Or with `state`. State must be returned from the callback
```
handler = fn
{ :characters, chars, state} -> [ chars | state ]
{ _, _, state} -> state
endSaxHtml.parse(html_text, handler, []) |> Enum.join("") |> IO.inspect
"\n\n\n \n Howdy!!!\n \n \n \n Gregors\n \n "
```## Installation
The package is [available in Hex](https://hex.pm/packages/sax_html), the package can be installed by adding `sax_html` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:sax_html, "~> 0.2.0"}
]
end
```