myhtml

forked from kostya/myhtml

Crystal wrapper for HTML5 Parser (lexborisov/myhtml)

MyHTML

Build Status

Crystal wrapper for HTML5 Parser https://github.com/lexborisov/myhtml

Installation

Add this to your application's shard.yml:

dependencies:
  myhtml:
    github: kostya/myhtml
    branch: master

And run crystal deps

Development Setup:

  git clone https://github.com/kostya/myhtml.git
  cd myhtml
  make

  crystal spec

Usage

# Example: print all html tree

require "myhtml"

def walk(node, level = 0)
  puts "#{" " * level}#{node.tag_name}#{node.attributes}(#{node.tag_text.strip})"
  node.children.each { |child| walk(child, level + 1) }
end

str = if filename = ARGV[0]?
        File.read(filename, "UTF-8", invalid: :skip)
      else
        "<html><Div><span class='test'>HTML</span></div></html>"
      end

parser = Myhtml::Parser.new
parser.parse(str)
walk(parser.root!)

Output:

html{}()
 head{}()
 body{}()
  div{}()
   span{"class" => "test"}()
    -text{}(HTML)

More Examples

examples

Github statistic:
  • 0
  • 0
  • 0
  • 1
  • 0
  • about 3 years ago

License:

MIT License

Links: