Skip to content

Instantly share code, notes, and snippets.

@daijo
Created September 2, 2011 02:08
Show Gist options
  • Save daijo/1187781 to your computer and use it in GitHub Desktop.
Save daijo/1187781 to your computer and use it in GitHub Desktop.
UIWebView+Markdown category.
//
// UIWebView+Markdown.m
// DropTextwiki
//
// Created by Daniel Hjort on 6/5/11.
// Copyright 2011 Patchwork Solutions AB. All rights reserved.
//
#import "UIWebView+Markdown.h"
#import "html.h"
@implementation UIWebView (UIWebView_Markdown)
-(void)loadMarkdownString:(NSString *)string baseURL:(NSURL *)baseURL
{
NSString *htmlString = [UIWebView htmlFromMarkdown:string];
[self loadHTMLString:htmlString baseURL:baseURL];
}
+(NSString *)htmlFromMarkdown:(NSString *)markdown {
struct mkd_renderer renderer;
const char *c = [markdown UTF8String];
char *html = malloc(256);
VOLATILE_BUF(ib, c);
struct buf ob;
memset(&ob, 0, sizeof(struct buf));
ob.data = html;
ob.asize = 256;
upshtml_renderer(&renderer, 0);
ups_markdown(&ob, &ib, &renderer, 0);
NSString *htmlString = [[[NSString alloc] initWithData:[NSData dataWithBytes:ob.data length:ob.size] encoding:NSUTF8StringEncoding] autorelease];
upshtml_free_renderer(&renderer);
free(html);
return htmlString;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment