Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #9 from mxcl/master

[HTMLParser head]
  • Loading branch information...
commit 172a033c945cbe72bb5effda3d061445d8930aac 2 parents c0c9b95 + 8daf1ff
Ben Reeves authored
Showing with 55 additions and 6 deletions.
  1. +3 −0  HTMLParser.h
  2. +10 −3 HTMLParser.m
  3. +0 −3  README
  4. +42 −0 README.md
3  HTMLParser.h
View
@@ -31,4 +31,7 @@
//Returns the html tag
-(HTMLNode*)html;
+//Returns the head tag
+- (HTMLNode*)head;
+
@end
13 HTMLParser.m
View
@@ -27,6 +27,14 @@ -(HTMLNode*)html
return [[self doc] findChildTag:@"html"];
}
+-(HTMLNode*)head
+{
+ if (_doc == NULL)
+ return NULL;
+
+ return [[self doc] findChildTag:@"head"];
+}
+
-(HTMLNode*)body
{
if (_doc == NULL)
@@ -47,8 +55,7 @@ -(id)initWithString:(NSString*)string error:(NSError**)error
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
// _doc = htmlParseDoc((xmlChar*)[string UTF8String], enc);
- int optionsHtml = 0;
- optionsHtml = optionsHtml | HTML_PARSE_RECOVER;
+ int optionsHtml = HTML_PARSE_RECOVER;
optionsHtml = optionsHtml | HTML_PARSE_NOERROR; //Uncomment this to see HTML errors
optionsHtml = optionsHtml | HTML_PARSE_NOWARNING;
_doc = htmlReadDoc ((xmlChar*)[string UTF8String], NULL, enc, optionsHtml);
@@ -106,7 +113,7 @@ -(id)initWithContentsOfURL:(NSURL*)url error:(NSError**)error
return nil;
}
- [self initWithData:_data error:error];
+ self = [self initWithData:_data error:error];
[_data release];
3  README
View
@@ -1,3 +0,0 @@
-1. Open Your project in XCode and drag and drop all .h & .m Files into an appropriate folder
-2. In the project settings add "/usr/include/libxml2" to the "header search paths" field
-3. Ctrl Click the Frameworks group choose Add -> Existing Frameworks and from the list choose libxml2.dylib
42 README.md
View
@@ -0,0 +1,42 @@
+1. Open Your project in XCode and drag and drop all .h & .m Files into an appropriate folder
+2. In the project settings add "/usr/include/libxml2" to the "header search paths" field
+3. Ctrl Click the Frameworks group choose Add -> Existing Frameworks and from the list choose libxml2.dylib
+
+Example Usage
+=============
+```objc
+NSError *error = nil;
+NSString *html =
+ @"<ul>"
+ "<li><input type='image' name='input1' value='string1value' /></li>"
+ "<li><input type='image' name='input2' value='string2value' /></li>"
+ "</ul>"
+ "<span class='spantext'><b>Hello World 1</b></span>"
+ "<span class='spantext'><b>Hello World 2</b></span>";
+HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];
+
+if (error) {
+ NSLog(@"Error: %@", error);
+ return;
+}
+
+HTMLNode *bodyNode = [parser body];
+
+NSArray *inputNodes = [bodyNode findChildTags:@"input"];
+
+for (HTMLNode *inputNode in inputNodes) {
+ if ([[inputNode getAttributeNamed:@"name"] isEqualToString:@"input2"]) {
+ NSLog(@"%@", [inputNode getAttributeNamed:@"value"]); //Answer to first question
+ }
+}
+
+NSArray *spanNodes = [bodyNode findChildTags:@"span"];
+
+for (HTMLNode *spanNode in spanNodes) {
+ if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"spantext"]) {
+ NSLog(@"%@", [spanNode rawContents]); //Answer to second question
+ }
+}
+
+[parser release];
+```

1 comment on commit 172a033

satoshi ootake

Please add semantic version tags.

I’ve recently added to the CocoaPods package manager repo.

CocoaPods is a tool for managing dependencies for OSX and iOS Xcode projects and provides a central repository for iOS/OSX libraries. This makes adding libraries to a project and updating them extremely easy and it will help users to resolve dependencies of the libraries they use.

However, doesn't have any version tags. I’ve added the current HEAD as version 0.0.1, but a version tag will make dependency resolution much easier.

Semantic version tags (instead of plain commit hashes/revisions) allow for resolution of cross-dependencies.

In case you didn’t know this yet; you can tag the current HEAD as, for instance, version 1.0.0, like so:

$ git tag -a 1.0.0 -m "Tag release 1.0.0"
$ git push --tags

Please sign in to comment.
Something went wrong with that request. Please try again.