forked from toland/qlmarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.m
33 lines (27 loc) · 1.54 KB
/
markdown.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "markdown.h"
#include "discount-wrapper.h"
NSData* renderMarkdown(NSURL* url)
{
NSString *styles = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier: @"com.fiatdev.QLMarkdown"]
pathForResource:@"styles" ofType:@"css"]
encoding:NSUTF8StringEncoding
error:nil];
NSStringEncoding usedEncoding = 0;
NSError *e = nil;
NSString *source = [NSString stringWithContentsOfURL:url usedEncoding:&usedEncoding error:&e];
if (usedEncoding == 0) {
NSLog(@"Wasn't able to determine encoding for file “%@”", [url path]);
}
char *output = convert_markdown_to_string([source UTF8String]);
NSString *html = [NSString stringWithFormat:@"<html>"
"<head>"
"<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />"
"<style type=\"text/css\">%@</style>"
"<base href=\"%@\"/>"
"</head>"
"<body>%@</body>"
"</html>",
styles, url, [NSString stringWithUTF8String:output]];
free(output);
return [html dataUsingEncoding:NSUTF8StringEncoding];
}