htmlq

repository·master·Indexed 27 days ago

https://github.com/mgdm/htmlq

A command-line tool for querying and extracting data from HTML using CSS selectors, similar to jq for JSON. Version 0.5.0 supports extracting raw HTML, plain text, and attribute values, with features for rewriting relative URLs, detecting base URLs from documents, and pretty-printing HTML nodes.

Tokens
672
Snippets
1
Records
5
Agent score
93%

What's inside htmlq

  1. Rewrite relative URLs in HTML nodes

    master

    Use rewrite_relative_url to transform relative href attributes into absolute URLs within specific HTML elements. This function targets <a>, <link>, and <area> elements. It uses the provided base URL to resolve relative paths.

    Special handling: If an href starts with ////, the leading slashes are trimmed, and the remaining path is used.

  2. Pretty-print HTML nodes with `pretty_print`

    master
    Use the pretty_print function to convert a kuchiki::NodeRef into a formatted, human-readable HTML string. This function handles indentation and line breaks, distinguishing between block-level and inline elements (such as a, span, strong, etc.) to ensure the output is properly structured.
  3. Detect the base URL from a document

    master

    Use detect_base to find the <base> element within an HTML document and extract its href attribute as a Url. This is useful for determining the starting point for resolving relative links in a document. Returns None if no <base> element is found or if the href attribute is missing or invalid.

    use kuchikiki::NodeRef;
    use url::Url;
    
    // document: The root node of the HTML document
    pub fn detect_base(document: &NodeRef) -> Option<Url> {
        // ... implementation
    }
  4. Reference the htmlq CLI flags and options

    master

    The following flags and options are available for the htmlq CLI tool:

    FlagLong NameDescription
    (positional)selector: The CSS selector used to filter nodes (defaults to html)
    -f, --filename--filenamePath to the input HTML file (defaults to - for stdin)
    -o, --output--outputPath to the output file (defaults to - for stdout)
    -b, --base--baseURL to prepend to relative links
    -B, --detect-base--detect-baseLook for the <base> tag in the input HTML to determine the base URL
    -t, --text--textOutput only the contained text of the filtered nodes
    -i, --ignore-whitespace--ignore-whitespaceSkip text nodes that consist solely of whitespace
    -p, --pretty--prettyReformat the HTML to be more human-readable
    -r, --remove-nodes--remove-nodesA list of selectors for nodes that should be excluded from the output
    -a, --attributes--attributesA list of attribute names to extract from the matched nodes
  5. Use the htmlq CLI to filter HTML with CSS selectors

    master
    The htmlq command-line tool allows you to parse HTML and extract specific elements using CSS selectors. It supports reading from files or stdin and writing to files or stdout. You can extract raw HTML, plain text, specific attribute values, or pretty-printed HTML.