Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BROTHFORGE.png
HTML parsing library for Corros.

brothforge 🍲

A BeautifulSoup-style HTML parser for Corros — parse any HTML into a node tree, then query it with find, find_all, CSS-ish selectors, and text extraction. Written entirely in Corros, the from-scratch scripting language. No Python, no dependencies.

Python package brothforge
soup = BeautifulSoup(html, "html.parser") page = broth_parse(html)
soup.find("h1") broth_find(page, "h1")
soup.find_all("p") broth_find_all(page, "p")
soup.select("p.note") broth_select(page, "p.note")
soup.get_text() broth_text(page)
tag["href"] broth_attr(tag, "href")
tag.text broth_text(tag)
tag.children broth_children(tag)
soup.title.string broth_title(page)

Install (one line)

curl -fsSL https://raw.githubusercontent.com/CocoCopi/brothforge/main/install.sh | sh
brothforge https://example.com

Use it in your own Corros programs

adopt "/path/to/brothforge/src/broth.cro"

forge page = broth_parse(html)                      // root node
forge h1   = broth_find(page, "h1")                 // first <h1>
forge ps   = broth_find_all(page, "p")              // every <p>
forge note = broth_select(page, "p.note")           // CSS-ish selectors
forge links = broth_select(page, "a[href]")         // (attribute selectors not supported yet — use broth_links)

speak(broth_text(h1))                               // "Hello", entities decoded
speak(broth_attr(broth_find(page, "img"), "src"))   // "x.png"
speak(broth_pretty(page))                           // indented tree

Selectors

tag, .class, #id, tag.class, tag#id, descendant ("ul li"), child ("div > p"), and comma groups ("h1, footer").

What the parser handles

  • comments (<!-- -->), doctypes, processing instructions
  • self-closing tags (<img src="x.png"> without </img>)
  • void elements (br, img, input, meta, link, hr, …)
  • implied closing: <li>A<li>B → two <li>, block tags close <p>
  • single-quoted, double-quoted, and unquoted attribute values
  • uppercase tags/attrs normalized to lowercase
  • HTML entities in text (&amp;, &lt;, &gt;, &quot;, &apos;, &nbsp;)

Scrape a real page (pairs with courierforge)

adopt "/path/to/brothforge/src/broth.cro"
adopt "/path/to/courierforge/src/courier.cro"

forge r = courier_get("https://example.com", nil)
forge page = broth_parse(r["body"])
speak(broth_title(page))                  // "Example Domain"
each a in broth_links(page) {
  speak(a["text"] + " -> " + str(a["href"]))
}

Tests

corros tests/t_broth.cro

Covers structure, selectors, entities, implied closing, void elements, serialization round-trips, and pretty printing.

License

MIT — see LICENSE. Built with Corros; see the Corros repo for the language.

About

brothforge — a BeautifulSoup-style HTML parser for Corros

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages