<commit_before>#!/usr/bin/env lua
local gumbo = require "gumbo"
local to_html = require "gumbo.serialize.html"

--- Do a recursive, depth-first search and remove any descendant element
--- with the specified id attribute.
local function remove_element_by_id(base, id)
    local function search_and_remove(node, n)
        if node[n].type == "element" then
            if node[n].attr.id == id then
                table.remove(node, n)
            else
                -- This loop must use ipairs, to allow the use of
                -- table.remove. A numeric for loop would overrun if
                -- the length of node[n] changes during iteration.
                for i in ipairs(node[n]) do
                    search_and_remove(node[n], i)
                end
            end
        end
    end
    if base and base.type == "element" or base.type == "document" then
        for i = 1, #base do
            search_and_remove(base, i)
        end
    end
end

if not arg[1] then
    io.stderr:write("Error: No element ID specified\n")
    io.stderr:write("Usage: ", arg[0], " ELEMENT_ID [FILENAME]\n")
    os.exit(1)
end

local document = assert(gumbo.parse_file(arg[2] or io.stdin))
remove_element_by_id(document, arg[1])
to_html(document, io.stdout)<commit_msg>Minor tweak to remove_by_id example<commit_after>#!/usr/bin/env lua
local gumbo = require "gumbo"
local to_html = require "gumbo.serialize.html"

--- Do a recursive, depth-first search and remove any descendant element
--- with the specified id attribute.
local function remove_element_by_id(base, id)
    local function search_and_remove(node, n)
        if node[n].type == "element" then
            if node[n].attr.id == id then
                table.remove(node, n)
            else
                -- This loop must use ipairs, to allow the use of
                -- table.remove. A numeric for loop would overrun if
                -- the length of node[n] changes during iteration.
                for i in ipairs(node[n]) do
                    search_and_remove(node[n], i)
                end
            end
        end
    end
    if base and base.type == "element" or base.type == "document" then
        for i = 1, #base do
            search_and_remove(base, i)
        end
    end
end

if not arg[1] then
    io.stderr:write(
        "Error: No element ID specified\n",
        "Usage: ", arg[0], " ELEMENT-ID [FILENAME]\n"
    )
    os.exit(1)
end

local document = assert(gumbo.parse_file(arg[2] or io.stdin))
remove_element_by_id(document, arg[1])
to_html(document, io.stdout)
{'fraction_non_alphanumeric': 0.10210444271239283, 'fraction_numerical': 0.004676539360872954, 'mean_word_length': 2.1190765492102064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253965', 'n_tokens_mistral': 806, 'n_tokens_neox': 771, 'n_words': 250}