-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathWebContentManager.m
executable file
·141 lines (131 loc) · 6.27 KB
/
WebContentManager.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// 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 *wiki_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:@"wiki" ofType:@"html"];
shared_manager.wiki_pattern_htmlStr = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
DebugLog(@"wiki_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 isEdit:(BOOL)isEdit{
if (!codeFile || !codeFile.file) {
return @"";
}
NSString *dataStr = [codeFile.file.lang isEqualToString:@"markdown"]? codeFile.file.preview: isEdit? codeFile.editData: codeFile.file.data;
if (dataStr.length <= 0) {
return @"";
}
NSString *patternedStr;
if ([codeFile.file.lang isEqualToString:@"markdown"]) {
patternedStr = [self.markdown_pattern_htmlStr stringByReplacingOccurrencesOfString:@"${webview_content}" withString:dataStr];
}else{
patternedStr = [dataStr stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
patternedStr = [patternedStr 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 *)wikiPatternedWithContent:(NSString *)content{
if (!content) {
return @"";
}
NSString *patternedStr = [self.wiki_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 isEdit:(BOOL)isEdit{
return [[self sharedManager] codePatternedWithContent:codeFile isEdit:(BOOL)isEdit];
}
+ (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 *)wikiPatternedWithContent:(NSString *)content{
return [[self sharedManager] wikiPatternedWithContent:content];
}
+ (NSString *)diffPatternedWithContent:(NSString *)content andComments:(NSString *)comments{
return [[self sharedManager] diffPatternedWithContent:content andComments:comments];
}
@end