Permalink
Browse files

Feature: auto-update .html siblings (looks for an element with an "ma…

…rkdownlive" id).
  • Loading branch information...
1 parent c26815c commit 9ec7bf07557850f53f187891cfb4cb15c41e9f38 @rentzsch committed Jul 20, 2011
Showing with 30 additions and 0 deletions.
  1. +30 −0 MyDocument.m
View
@@ -88,6 +88,36 @@ - (BOOL)writeToURL:(NSURL*)absoluteURL_ ofType:(NSString*)typeName_ error:(NSErr
error:error_];
}
+ if (result && ![self hasUnautosavedChanges]) {
+ NSURL *markdownFileURL = [self fileURL];
+ NSURL *htmlFileURL = [[markdownFileURL URLByDeletingPathExtension] URLByAppendingPathExtension:@"html"];
+ if ([[NSFileManager defaultManager] fileExistsAtPath:[htmlFileURL path]]) {
+ NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithContentsOfURL:htmlFileURL
+ options:NSXMLNodePreserveAll|NSXMLDocumentTidyXML
+ error:nil] autorelease];
+ if (doc) {
+ NSArray *nodes = [doc nodesForXPath:@"//*[@id=\"markdownlive\"]" error:nil];
+ if ([nodes count] == 1) {
+ NSXMLElement *node = [nodes objectAtIndex:0];
+ NSXMLDocument *markdownDoc = [[[NSXMLDocument alloc] initWithXMLString:[ORCDiscount markdown2HTML:[markdownSource string]]
+ options:NSXMLDocumentTidyHTML
+ error:nil] autorelease];
+ NSArray *markdownNodes = [markdownDoc nodesForXPath:@"/html/body/*" error:nil];
+ [markdownNodes makeObjectsPerformSelector:@selector(detach)];
+ [node setChildren:markdownNodes];
+ NSString *htmlFileContent = [doc XMLStringWithOptions:NSXMLNodePrettyPrint];
+ if ([htmlFileContent hasPrefix:@"<?xml"]) {
+ NSUInteger index = [htmlFileContent rangeOfString:@"\n"].location;
+ htmlFileContent = [htmlFileContent substringFromIndex:index+1];
+ }
+ [htmlFileContent writeToURL:htmlFileURL
+ atomically:YES
+ encoding:NSUTF8StringEncoding
+ error:nil];
+ }
+ }
+ }
+ }
return result;
}

0 comments on commit 9ec7bf0

Please sign in to comment.