-
Notifications
You must be signed in to change notification settings - Fork 141
/
markdown.m
32 lines (26 loc) · 1.33 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
#include "markdown.h"
#include "discount-wrapper.h"
NSData* renderMarkdown(NSURL* url)
{
@autoreleasepool {
NSString *styles = [[NSString alloc] initWithContentsOfFile:[[NSBundle bundleWithIdentifier: @"com.fiatdev.QLMarkdown"]
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>"
"<meta charset=utf-8>"
"<style>%@</style>"
"<base href=\"%@\"/>"
"%@",
styles, url, [NSString stringWithUTF8String:output]];
free(output);
return [html dataUsingEncoding:NSUTF8StringEncoding];
}
}