Created
June 6, 2018 00:14
-
-
Save havenwood/47c28849df7ab8faed348f77433cf0ad to your computer and use it in GitHub Desktop.
A little SAX parser solution for: https://twitter.com/keystonelemur/status/1003752494797737984
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'open-uri' | |
require 'ox' | |
class FoxHandler < Ox::Sax | |
def initialize | |
@h_tags = [] | |
@h = nil | |
end | |
def start_element name | |
return unless name.match? /\Ah\d+\z/i | |
@h = name[1..-1].to_i | |
if @h_tags.last.to_i < @h | |
@h_tags << @h | |
elsif @h_tags.last > @h | |
@h_tags.pop | |
end | |
print ' ' * @h_tags.size.pred | |
print "[#{name}] " | |
end | |
def end_element _ | |
@h = nil | |
end | |
def text value | |
return unless @h | |
puts value | |
end | |
end | |
html = URI.parse('https://jquery.com').read | |
def header_hierarchy html | |
Ox.sax_parse FoxHandler.new, html | |
end | |
header_hierarchy html | |
#[h2] jQuery | |
# [h3] Lightweight Footprint | |
# [h3] CSS3 Compliant | |
# [h3] Cross-Browser | |
#[h2] What is jQuery? | |
#[h2] Other Related Projects | |
# [h3] Resources | |
#[h2] A Brief Look | |
# [h3] DOM Traversal and Manipulation | |
# [h3] Event Handling | |
# [h3] Ajax | |
# [h3] Books |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment