### Install CMake Dependencies
Source: https://github.com/serpapi/nokolexbor/blob/master/README.md
Commands to install CMake, which is required for compiling C extensions if pre-compiled gems are not available for your platform.
```bash
# macOS
brew install cmake
# Linux (Debian, Ubuntu, etc.)
sudo apt-get install cmake
```
--------------------------------
### Install Nokolexbor via Gemfile
Source: https://github.com/serpapi/nokolexbor/blob/master/README.md
Add the Nokolexbor gem to your Ruby project's Gemfile to enable high-performance HTML parsing.
```ruby
gem 'nokolexbor'
```
--------------------------------
### Install CSS Function
Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html
A placeholder JavaScript function for installing CSS. It currently does nothing, indicating it might be a stub for future implementation or a feature that is conditionally enabled.
```javascript
function \_F\_installCss(c){}
```
--------------------------------
### Batch Operations on HTML Elements with Nokolexbor
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Illustrates how to perform batch operations on multiple HTML elements simultaneously, such as adding classes, setting attributes, removing attributes, and wrapping nodes. It also covers searching within a NodeSet and getting children of nodes.
```ruby
items.add_class('highlight')
items.attr('data-processed', 'true')
items.attr('data-index') { |node| items.index(node).to_s }
items.remove_attr('data-processed')
nested = items.css('.active')
puts nested.length
items.wrap('
')
children = items.children
puts children.length
```
--------------------------------
### Search Nodes with CSS and XPath
Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet
Provides examples of querying the DOM using CSS selectors and XPath expressions, including methods for finding single nodes or collections of nodes.
```ruby
doc.css('div > span.a')
doc.at_css('div > span.a')
doc.xpath('//div / span[@class="a"]')
doc.at_xpath('//div / div / text()')
```
--------------------------------
### Serializing HTML Nodes and Node Sets with Nokolexbor
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Covers how to serialize Nokolexbor nodes and node sets into HTML strings. This includes getting text content, inner HTML, outer HTML, and writing the serialized HTML to IO objects. It demonstrates various methods like `content`, `text`, `inner_html`, `outer_html`, `to_html`, `serialize`, `to_s`, and `write_to`.
```ruby
doc = Nokolexbor::HTML <<-HTML
HTML
node = doc.at_css('#content')
puts node.content
puts node.text
puts node.inner_html
puts node.outer_html
puts node.to_html
puts node.serialize
puts node.to_s
File.open('output.html', 'w') do |f|
node.write_to(f)
end
```
--------------------------------
### DOM Traversal in Nokolexbor
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Explains how to navigate the DOM tree in Nokolexbor, including moving between parent, child, and sibling nodes. It covers methods for getting ancestors, children, and descendants, as well as filtering these relationships using CSS selectors. Requires the Nokolexbor library.
```ruby
doc = Nokolexbor::HTML <<-HTML
Title
First paragraph
Second paragraph
HTML
article = doc.at_css('article')
# Parent navigation
puts article.parent.name
# => "main"
# Get ancestors
article.ancestors.each { |a| puts a.name }
# => "main"
# => "div"
# => "body"
# => "html"
# Filter ancestors with selector
puts article.ancestors('div').first['id']
# => "root"
# Child navigation
puts article.child.name
# => "h1"
puts article.children.map(&:name).inspect
# => ["h1", "p", "p"]
# Element-specific children (excludes text nodes)
puts article.elements.length
# => 3
puts article.first_element_child.name
# => "h1"
puts article.last_element_child.content
# => "Second paragraph"
# Sibling navigation
nav = doc.at_css('nav')
link = nav.at_css('a')
puts link.next.name
# => "a"
puts link.next.content
# => "About"
puts link.next.previous.content
# => "Home"
# Element siblings only
header = doc.at_css('header')
puts header.next_element.name
# => "main"
# Traverse all descendants
article.traverse do |node|
puts "#{node.name}: #{node.content.strip}" if node.element?
end
# => "article: TitleFirst paragraphSecond paragraph"
# => "h1: Title"
# => "p: First paragraph"
# => "p: Second paragraph"
```
--------------------------------
### Working with Attributes in Nokolexbor
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Details how to get, set, and delete attributes on HTML nodes using Nokolexbor. It also covers iterating over attributes and specifically manipulating CSS classes, including adding and removing them. Requires the Nokolexbor library.
```ruby
doc = Nokolexbor::HTML('')
node = doc.at_css('div')
# Get attribute value
puts node['class']
# => "box primary"
puts node['data-id']
# => "123"
# Set attribute value
node['data-active'] = 'true'
node['id'] = 'main-box'
# Check for attribute
puts node.key?('style')
# => true
# Get all attribute names and values
puts node.keys.inspect
# => ["class", "data-id", "style", "data-active", "id"]
puts node.values.inspect
# => ["box primary", "123", "color:red", "true", "main-box"]
# Delete attribute
node.delete('style')
# Iterate over attributes
node.each do |name, value|
puts "#{name}: #{value}"
end
# Work with CSS classes
puts node.classes
# => ["box", "primary"]
node.add_class('highlight')
# => class="box primary highlight"
node.add_class(['active', 'visible'])
# => class="box primary highlight active visible"
node.remove_class('primary')
# => class="box highlight active visible"
# Get attribute as Attribute object
attr = node.attribute('class')
puts attr.name
# => "class"
puts attr.value
# => "box highlight active visible"
# Navigate attributes
next_attr = attr.next
puts next_attr.name
# => "data-id"
```
--------------------------------
### Initialize Nokolexbor Documents
Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet
Demonstrates various ways to instantiate a Nokolexbor document, including creating blank documents, parsing HTML strings, or reading from external sources.
```ruby
doc = Nokolexbor::Document.new
doc = Nokolexbor::HTML('')
doc = Nokolexbor::HTML('')
doc = Nokolexbor::HTML(URI.open('https://github.com/serpapi/nokolexbor'))
```
--------------------------------
### Building HTML Documents Programmatically with Nokolexbor DSL
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Demonstrates how to build HTML documents programmatically using Nokolexbor's HTML Builder DSL. It covers the no-argument block style, block parameter style, fluent class and ID API, building into existing nodes, and inserting raw HTML like text, comments, and CDATA.
```ruby
html = Nokolexbor do
html do
head do
title 'My Page'
meta charset: 'utf-8'
end
body do
header do
h1 'Welcome'
nav do
ul do
li { a 'Home', href: '/' }
li { a 'About', href: '/about' }
end
end
end
main do
article do
h2 'Article Title'
p 'First paragraph content.'
p 'Second paragraph content.'
end
end
footer do
p '2024 My Site'
end
end
end
end
puts html.to_html
doc = Nokolexbor::Builder.new do |b|
b.div class: 'container' do
b.h1 'Hello World'
b.p 'This is content'
end
end
puts doc.to_html
doc = Nokolexbor do
div.container.main! do
p.intro.highlight 'Styled paragraph'
end
end
puts doc.to_html
existing_doc = Nokolexbor::HTML('')
target = existing_doc.at_css('#target')
Nokolexbor::Builder.with(target) do
span 'Injected content'
ul do
li 'Item 1'
li 'Item 2'
end
end
puts target.to_html
doc = Nokolexbor do
div do
self << 'Raw HTML
'
text 'Plain text node'
comment 'This is a comment'
cdata 'CDATA content'
end
end
puts doc.to_html
```
--------------------------------
### Create and Configure Nodes
Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet
Explains how to create various types of nodes including elements, text nodes, comments, CDATA, and processing instructions within a document context.
```ruby
doc = Nokolexbor::HTML('')
doc.create_element("div", class: "a")
doc.create_text_node("Some text")
doc.create_comment("Some comment")
doc.create_cdata("Some CDATA")
Nokolexbor::ProcessingInstruction.new("xml", "some data", doc)
```
--------------------------------
### HTML Builder DSL
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Illustrates how to programmatically build HTML documents using Nokolexbor's fluent DSL.
```APIDOC
## HTML Builder DSL
### Description
This section explains how to construct HTML documents programmatically using Nokolexbor's Domain Specific Language (DSL).
### No-Argument Block Style
```ruby
html = Nokolexbor do
html do
head do
title 'My Page'
meta charset: 'utf-8'
end
body do
header do
h1 'Welcome'
nav do
ul do
li { a 'Home', href: '/' }
li { a 'About', href: '/about' }
end
end
end
main do
article do
h2 'Article Title'
p 'First paragraph content.'
p 'Second paragraph content.'
end
end
footer do
p '2024 My Site'
end
end
end
end
puts html.to_html
```
### Block Parameter Style
```ruby
doc = Nokolexbor::Builder.new do |b|
b.div class: 'container' do
b.h1 'Hello World'
b.p 'This is content'
end
end
puts doc.to_html
# => Hello World
This is content
```
### Fluent Class and ID API
```ruby
doc = Nokolexbor do
div.container.main! do
p.intro.highlight 'Styled paragraph'
end
end
puts doc.to_html
# =>
```
### Building into Existing Node
```ruby
# Build into existing node
existing_doc = Nokolexbor::HTML('')
target = existing_doc.at_css('#target')
Nokolexbor::Builder.with(target) do
span 'Injected content'
ul do
li 'Item 1'
li 'Item 2'
end
end
puts target.to_html
# =>
```
### Inserting Raw HTML
```ruby
doc = Nokolexbor do
div do
self << 'Raw HTML
'
text 'Plain text node'
comment 'This is a comment'
cdata 'CDATA content'
end
end
puts doc.to_html
```
```
--------------------------------
### Configure LibXML2 Build Options and Features
Source: https://github.com/serpapi/nokolexbor/blob/master/ext/nokolexbor/CMakeLists.txt
This snippet initializes the project, defines user-configurable build options for various LibXML2 modules, and sets up the necessary environment for cross-platform compilation.
```cmake
cmake_minimum_required(VERSION 2.8.12)
IF(CMAKE_VERSION VERSION_LESS "3.0")
project(libxml2)
ELSE()
cmake_policy(SET CMP0048 NEW)
project(libxml2 VERSION "2.11.0")
ENDIF()
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
set(LIBXML2_WITH_HTML ON)
set(LIBXML2_WITH_THREADS ON)
if(LIBXML2_WITH_THREADS)
find_package(Threads REQUIRED)
endif()
```
--------------------------------
### Nokogiri-Compatible CSS Methods in Nokolexbor
Source: https://context7.com/serpapi/nokolexbor/llms.txt
Shows how to use Nokolexbor's `nokogiri_css` and `nokogiri_at_css` methods, which leverage libxml2 for mixed CSS and XPath syntax support, similar to Nokogiri. This is useful for compatibility and advanced selector needs.
```ruby
doc = Nokolexbor::HTML("\n
First paragraph
\n
Second paragraph
\n
")
results = doc.nokogiri_css('div p')
puts results.length
first = doc.nokogiri_at_css('div p')
puts first.content
```
--------------------------------
### Load Configuration Arrays (JavaScript)
Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html
Loads multiple configuration arrays into the 'window.W_jd' object. These arrays likely contain various settings and parameters for different Google features.
```javascript
var m=['CRKV1k','[\x22psy-ab\x22,\x22gws-wiz\x22,\x22Coffee\x22,\x22\x22,null,1,0,null,0,11,\x22en\x22,\x225pdKV-UdTxzHGnKMb4vYfDWTXyE\x22,\x22\x22,\x22ZDOvXdetDYG9hwPfqLfIBA\x22,0,\x22en\x22,null,null,null,null,null,3,null,null,5,null,null,null,null,null,0,0,0,null,null,null,null,-1,null,0,null,0,0,\x22\x22,0,null,0,\x22\x22,null,0,null,0]\n','CRKV1o','[0,0,0,[null,null,[[[3,null,null,[null,[["qdr_ ",1,6]\n,["qdr_h",0,6]\n,["qdr_d",0,6]\n,["qdr_w",0,6]\n,["qdr_m",0,6]\n,["qdr_y",0,6]\n,["cdr_opt",0,1,[1,\x22Custom range...\x22,null,\x22cdr:1,cd_min:x,cd_max:x\x22,\x22\x22,\x22text\x22,\x22\x22,\x22\x22,6,1,[ [ [\x22q\x22,\x22Coffee\x22 ]\n, [\x22num\x22,\x22100\x22 ]\n, [\x22gl\x22,\x22us\x22 ]\n, [\x22hl\x22,\x22en\x22 ]\n ]\n ]\n,\x22cdr_opt\x22,\x225/23/2004\x22,0 ]\n ]\n ],0]\n ],3,null,null,[null,[["li_ ",1,6]\n,["li_1",0,6]\n ]\n,1]\n ]\n ],null,[\x22tbs\x22 ]\n ]\n ],null,null,null,null,1]\n','CRKV1s','[null,null,1,30000,null,null,null,2,null,null,3,null,null,null,null,null,1,null,null,null,null,null,null,[37.09024,-95.712891 ],null,null,null,null,0,null,null,null,null,null,null,null,0,\x221571763044\x22,null,null,null,null,null,1,null,null,[\x2286400000\x22,\x22604800000\x22,2.0 ]\n,null,1]\n','CRKV1w','[1,null,null,1188,1276,0]\n'];
```
```javascript
var a=m;window.W_jd=window.W_jd||{};for(var b=0;b