forked from toland/qlmarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown.m
41 lines (32 loc) · 1.6 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
34
35
36
37
38
39
40
41
#include "discount-wrapper.h"
#include "markdown.h"
NSString *renderMarkdown(NSURL *url) {
@autoreleasepool {
NSString *domainName = @"com.fiatdev.QLMarkdown";
NSString *styles = [[NSString alloc]
initWithContentsOfFile:[[NSBundle bundleWithIdentifier:domainName] pathForResource:@"styles" ofType:@"css"]
encoding:NSUTF8StringEncoding
error:nil];
NSStringEncoding usedEncoding = 0;
NSError *e = nil;
NSString *source = [[NSString alloc] initWithContentsOfURL: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:@"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
"<meta charset=\"utf-8\">\n"
"<style type=\"text/css\">\n%@</style>\n"
"<base href=\"%@\"/>\n"
"</head>\n"
"<body>\n"
"%@"
"</body>\n"
"</html>",
styles, url, [NSString stringWithUTF8String:output]];
free(output);
return html;
}
}