A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/AdamNiederer/elquery below:

AdamNiederer/elquery: Read and manipulate HTML in emacs

elquery: parse, query, and format HTML with Emacs Lisp

Write things. Do things.

elquery is a library that lets you parse, query, set, and format HTML using Emacs Lisp. It implements (most of) the querySelector API with elquery-$, and can get and set HTML attributes.

Given the below HTML file, some usage examples include:

<html style="height: 100vh">
  <head class="baz"><title class="baz" data-bar="foo">Complex HTML Page</title></head>
  <body class="baz bur" style="height: 100%">
    <h1 id="bar" class="baz wow">Wow this is an <em>example</em></h1>
    <input id="quux" class="baz foo"/>
    <iframe sandbox="allow-same-origin allow-scripts allow-popups allow-forms"
            width="100%" height="100%" src="example.org">
    </iframe>
  </body>
</html>
(let ((html (elquery-read-file "~/baz.html")))
  ;;; Query elements
  (elquery-el (car (elquery-$ ".baz#quux" html)))
  ;; => "input"
  (mapcar 'elquery-el (elquery-$ ".baz" html))
  ;; => ("input" "h1" "body" "title" "head")
  (mapcar (lambda (el) (elquery-el (elquery-parent el))) (elquery-$ ".baz" html))
  ;; => ("body" "body" "html" "head" "html")
  (mapcar (lambda (el) (mapcar 'elquery-el (elquery-siblings el))) (elquery-$ ".baz" html))
  ;; => (("h1" "input" "iframe") ("h1" "input" "iframe") ("head" "body") ("title") ("head" "body"))

  ;;; Read properties of elements
  (elquery-classes (car (elquery-$ ".baz#quux" html)))
  ;; => ("baz" "foo")
  (elquery-prop (car (elquery-$ "iframe" html)) "sandbox")
  ;; => "allow-same-origin allow-scripts allow-popups allow-forms"
  (elquery-text (car (elquery-$ "h1" html)))
  ;; => "Wow this is an"
  (elquery-full-text (car (elquery-$ "h1" html)) " ")
  ;; => "Wow this is an example"

  ;;; Write parsed HTML
  (elquery-write html nil)
  ;; => "<html style=\"height: 100vh\"> ... </html>"
  )

Because HTML is a large tree representation, elq includes some general tree manipulation functions which it uses internally, and may be useful to you when dealing with the DOM.

We support a significant subset of jQuery’s selector syntax. If I ever decide to make this project even more web-scale, I’ll add colon selectors and more property equality tests.

All permutations of union, intersection, child, next-child, and sibling relationships are supported.

Each element is a plist, which is guaranteed to have at least one key-value pair, and an :el key. All elements of this plist are accessible with the above functions, but the internal representation of a document node is below for anybody brave enough to hack on this:

The data structure used in queries via (elquery-$) is very similar, although it doesn’t have :text keyword (PRs welcome!) and has an extra :rel keyword, which specifies the relationship between the query and its :children. :rel may be one of :next-child, :child, next-sibling, and :sibling. This is used by the internal function (elquery--$) which must determine whether it can continue recursion down the tree based on the relationship of two intersections in a selector.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4