Permalink
Browse files

initial checkin

  • Loading branch information...
tenderlove committed Jul 14, 2008
0 parents commit e7f98b6cb8e4b49da26aa3bd70f415fac2af5ac3
Showing with 197 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 History.txt
  3. +7 −0 Manifest.txt
  4. +48 −0 README.txt
  5. +13 −0 Rakefile
  6. +55 −0 lib/nokogiri.rb
  7. +26 −0 lib/nokogiri_lib.rb
  8. +41 −0 test/test_nokogiri.rb
@@ -0,0 +1 @@
+.*.swp
@@ -0,0 +1,6 @@
+=== 1.0.0 / 2008-07-13
+
+* 1 major enhancement
+
+ * Birthday!
+
@@ -0,0 +1,7 @@
+History.txt
+Manifest.txt
+README.txt
+Rakefile
+bin/kapow
+lib/kapow.rb
+test/test_kapow.rb
@@ -0,0 +1,48 @@
+= Nokogiri
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 FIX
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,13 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+
+$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), 'lib'))
+require 'nokogiri'
+
+Hoe.new('nokogiri', Nokogiri::VERSION) do |p|
+ p.developer('Aaron Patterson', 'aaronp@rubyforge.org')
+end
+
+# vim: syntax=Ruby
@@ -0,0 +1,55 @@
+require 'dl/import'
+require 'nokogiri_lib'
+
+class Nokogiri
+ VERSION = '1.0.0'
+
+ class << self
+ def parse(string, url = nil, encoding = nil, options = 1)
+ doc = NokogiriLib.htmlReadMemory(
+ string,
+ string.length,
+ url || 0,
+ encoding || 0,
+ options
+ )
+ Document.new(doc)
+ end
+ end
+
+ class Document
+ def initialize(ptr)
+ @ptr = ptr
+ end
+
+ def root
+ Node.new(NokogiriLib::Tree.xmlDocGetRootElement(@ptr))
+ end
+ end
+
+ class Node
+ TYPE = ['PISPPPPPPPP',
+ :private, :type, :name, :children, :last, :parent, :next, :prev, :doc, :ns, :content]
+ def initialize(ptr)
+ @ptr = ptr
+ @ptr.struct!(*TYPE)
+ end
+
+ def name; @ptr[:name].to_s; end
+ def child; Node.new(@ptr[:children]); end
+ def next; Node.new(@ptr[:next]); end
+ def content; @ptr[:content].to_s; end
+
+ def [](property)
+ property = NokogiriLib::Node.xmlGetProp(
+ @ptr,
+ NokogiriLib.xmlCharStrdup(property.to_s)
+ )
+ property && property.to_s
+ end
+
+ def blank?
+ 1 == NokogiriLib::Node.xmlIsBlankNode(@ptr)
+ end
+ end
+end
@@ -0,0 +1,26 @@
+module NokogiriLib
+ extend DL::Importable
+ dlload 'libxml2.dylib'
+ extern "P xmlNewParserCtxt()"
+ extern "I xmlParseDocument (P)"
+
+ extern "P htmlCtxtReadMemory (P, c, I, c, c, I)"
+ extern "P htmlReadMemory (S, I, c, c, I)"
+ extern "I htmlParseDocument (P)"
+ extern "P htmlCreateMemoryParserCtxt(c, I)"
+ extern "P xmlCharStrdup(S)"
+
+ module Tree
+ extend DL::Importable
+ dlload 'libxml2.dylib'
+ extern "P xmlDocGetRootElement (P)"
+ end
+
+ module Node
+ extend DL::Importable
+ dlload 'libxml2.dylib'
+ extern "P xmlHasProp (P, P)"
+ extern "P xmlGetProp (P, P)"
+ extern "I xmlIsBlankNode (P)"
+ end
+end
@@ -0,0 +1,41 @@
+require 'test/unit'
+require 'nokogiri'
+
+class NokogiriTest < Test::Unit::TestCase
+ def test_new
+ kp = Nokogiri.new
+ assert kp
+ end
+
+ def test_read_memory
+ assert Nokogiri.parse('<html><body></body></html>')
+ end
+
+ def test_root
+ html_doc = Nokogiri.parse('<html><head><meta name=keywords content=nasty></head><body>Hello<br>World</html>')
+
+ root = html_doc.root
+ assert root
+ assert_equal 'html', root.name
+ head = root.child
+ assert_equal 'head', head.name
+ meta = head.child
+ assert_equal 'meta', meta.name
+ assert_equal 'keywords', meta[:name]
+ assert_nil meta[:foo]
+
+ body = head.next
+ assert_equal 'body', body.name
+
+ hello = body.child
+ hello = hello.child if hello.name == 'p'
+ assert_equal 'Hello', hello.content
+
+ br = hello.next
+ assert br
+ assert_equal 'br', br.name
+
+ world = br.next
+ assert_equal 'World', world.content
+ end
+end

0 comments on commit e7f98b6

Please sign in to comment.