forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebContentManager.m
More file actions
executable file
·119 lines (110 loc) · 5.22 KB
/
WebContentManager.m
File metadata and controls
executable file
·119 lines (110 loc) · 5.22 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// WebContentManager.m
// Coding_iOS
//
// Created by 王 原闯 on 14-9-25.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "WebContentManager.h"
@interface WebContentManager ()
@property (strong, nonatomic) NSString *bubble_pattern_htmlStr;
@property (strong, nonatomic) NSString *topic_pattern_htmlStr;
@property (strong, nonatomic) NSString *code_pattern_htmlStr;
@property (strong, nonatomic) NSString *markdown_pattern_htmlStr;
@property (strong, nonatomic) NSString *diff_pattern_htmlStr;
@end
@implementation WebContentManager
+ (instancetype)sharedManager {
static WebContentManager *shared_manager = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
shared_manager = [[self alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"bubble" ofType:@"html"];
NSError *error = nil;
shared_manager.bubble_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"bubble_pattern_htmlStr fail: %@", error.description);
}
path = [[NSBundle mainBundle] pathForResource:@"topic-ios" ofType:@"html"];
shared_manager.topic_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"topic_pattern_htmlStr fail: %@", error.description);
}
path = [[NSBundle mainBundle] pathForResource:@"code" ofType:@"html"];
shared_manager.code_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"code_pattern_htmlStr fail: %@", error.description);
}
path = [[NSBundle mainBundle] pathForResource:@"markdown" ofType:@"html"];
shared_manager.markdown_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"markdown_pattern_htmlStr fail: %@", error.description);
}
path = [[NSBundle mainBundle] pathForResource:@"diff-ios" ofType:@"html"];
shared_manager.diff_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"diff_pattern_htmlStr fail: %@", error.description);
}
});
return shared_manager;
}
- (NSString *)bubblePatternedWithContent:(NSString *)content{
if (!content) {
return @"";
}
NSString *patternedStr = [self.bubble_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${webview_content}" withString:content];
return patternedStr;
}
- (NSString *)topicPatternedWithContent:(NSString *)content{
if (!content) {
return @"";
}
NSString *patternedStr = [self.topic_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${webview_content}" withString:content];
return patternedStr;
}
- (NSString *)codePatternedWithContent:(CodeFile *)codeFile{
if (!codeFile || !codeFile.file || (!codeFile.file.data && !codeFile.file.preview)) {
return @"";
}
NSString *patternedStr;
if ([codeFile.file.lang isEqualToString:@"markdown"]) {
patternedStr = [self.markdown_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${webview_content}" withString:codeFile.file.preview];
}else{
patternedStr = [codeFile.file.data stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
patternedStr = [patternedStr stringByReplacingOccurrencesOfString:@">" withString:@">"];
patternedStr = [self.code_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${file_code}" withString:patternedStr];
patternedStr = [patternedStr stringByReplacingOccurrencesOfString:@"${file_lang}" withString:codeFile.file.lang];
}
return patternedStr;
}
- (NSString *)markdownPatternedWithContent:(NSString *)content{
if (!content) {
return @"";
}
NSString *patternedStr = [self.markdown_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${webview_content}" withString:content];
return patternedStr;
}
- (NSString *)diffPatternedWithContent:(NSString *)content andComments:(NSString *)comments{
if (!content) {
return @"";
}
NSString *patternedStr = [self.diff_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${diff-content}" withString:content];
patternedStr = [patternedStr stringByReplacingOccurrencesOfString:@"${comments}" withString:comments];
return patternedStr;
}
+ (NSString *)codePatternedWithContent:(CodeFile *)codeFile{
return [[self sharedManager] codePatternedWithContent:codeFile];
}
+ (NSString *)bubblePatternedWithContent:(NSString *)content{
return [[self sharedManager] bubblePatternedWithContent:content];
}
+ (NSString *)topicPatternedWithContent:(NSString *)content{
return [[self sharedManager] topicPatternedWithContent:content];
}
+ (NSString *)markdownPatternedWithContent:(NSString *)content{
return [[self sharedManager] markdownPatternedWithContent:content];
}
+ (NSString *)diffPatternedWithContent:(NSString *)content andComments:(NSString *)comments{
return [[self sharedManager] diffPatternedWithContent:content andComments:comments];
}
@end