-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHTMLArticleHelper.m
252 lines (246 loc) · 9.67 KB
/
HTMLArticleHelper.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//
// HTMLArticleHelper.m
// inspire
//
// Created by Yuji on 2015/08/29.
//
//
#import "HTMLArticleHelper.h"
#import "Article.h"
#import "JournalEntry.h"
#import "RegexKitLite.h"
#import "NSString+magic.h"
#import "ArxivHelper.h"
#import "SpiresHelper.h"
@implementation HTMLArticleHelper
{
Article*article;
}
-(instancetype)initWithArticle:(Article*)a
{
self=[super init];
article=a;
return self;
}
-(NSString*)authors
{
NSArray*names=[article.longishAuthorListForEA componentsSeparatedByString:@"; "];
NSString*collaboration=nil;
NSMutableArray* a=[NSMutableArray array];
for(NSString*x in names){
if(![x isEqualToString:@""]){
if([x rangeOfString:@"collaboration"].location!=NSNotFound){
collaboration=x;
}else{
[a addObject:x];
}
}
}
// [a sortUsingSelector:@selector(caseInsensitiveCompare:)];
NSMutableArray*b=[NSMutableArray array];
NSArray*particles=[[NSUserDefaults standardUserDefaults] objectForKey:@"particles"];
for(NSString*s in a){
NSString* searchString=[NSString stringWithFormat:@"spires-search://a %@",s];
searchString=[searchString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSMutableString*result=[NSMutableString stringWithFormat:@"<a href=\"%@\">",searchString];
NSArray* c=[s componentsSeparatedByString:@", "];
NSString* last=c[0];
if([c count]>1){
NSArray* d=[c[1] componentsSeparatedByString:@" "];
if([last hasPrefix:@"collaboration"]){
[result appendString:[[d lastObject] uppercaseString]];
last=@" Collaboration";
}else if([c[1] hasPrefix:@"for the"]){
[result appendString:[last uppercaseString]];
last=@" Collaboration";
}else if([last isEqualToString:@"group"]||
[last isEqualToString:@"groups"]||
[last isEqualToString:@"physics"]){
[result appendFormat:@"%@ ",[c[1] capitalizedString]];
}else{
for(NSString*i in d){
if(!i || [i isEqualToString:@""]) continue;
if(![particles containsObject:i]){
[result appendString:[[i substringToIndex:1] capitalizedStringForName]];
[result appendString:@". "];
}else{
[result appendString:i];
[result appendString:@" "];
}
}
}
}
[result appendString:[last capitalizedStringForName]];
[result appendString:@"</a>"];
[b addObject: result];
}
if(collaboration){
collaboration=[collaboration uppercaseString];
collaboration=[collaboration stringByReplacingOccurrencesOfRegex:@"COLLABORATION(S?)" withString:@"Collaboration$1"];
NSString* searchString=[collaboration stringByReplacingOccurrencesOfRegex:@"Collaborations?" withString:@""];
searchString=[NSString stringWithFormat:@"spires-search://cn %@",searchString];
searchString=[searchString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSMutableString*result=[NSMutableString stringWithFormat:@"<a href=\"%@\">",searchString];
[result appendFormat:@"%@</a>",collaboration];
if([b count]>0 && [b count]<10){
[result appendFormat:@" (%@)",[b componentsJoinedByString:@", "]];
}
return result;
}else{
return [b componentsJoinedByString:@", "];
}
}
-(NSString*)abstract
{
if(article.abstract==nil)
return nil;
// the code here is to trim excessive & escapes introduced in older versions
NSString*trim=[article.abstract stringByReplacingOccurrencesOfRegex:@"&(amp;)+" withString:@"&"];
for(NSString*tag in @[@"i",@"sub",@"sup"]){
NSString*from=[NSString stringWithFormat:@"<(/*)%@>",tag];
NSString*to=[NSString stringWithFormat:@"<$1%@>",tag];
trim=[trim stringByReplacingOccurrencesOfRegex:from withString:to];
}
if(![trim isEqualToString:article.abstract]){
article.abstract=trim;
}
// trim=[trim stringByReplacingOccurrencesOfRegex:@"<img.+>" withString:@"_"];
// up to this point.
NSString* result= [[trim stringByConvertingTeXintoHTML] stringByReplacingOccurrencesOfString:@"href=\"" withString:@"href=\"spires-lookup-eprint://"];
// NSLog(@"%@",result);
return result;
}
-(NSString*)arxivCategory
{
NSString* category=article.arxivCategory;
if(category && ![category isEqualToString:@""] && [article.eprint rangeOfString:@"/"].location==NSNotFound){
return [NSString stringWithFormat:@"[%@]",category];
}else{
return nil;
}
}
-(NSString*)comments
{
if(!article.comments)
return nil;
return [article.comments stringByReplacingOccurrencesOfString:@"href=\"" withString:@"href=\"spires-lookup-eprint://"];
//[NSString stringWithFormat:@"<b>Comments:</b> %@",article.comments];
}
-(NSString*)title
{
NSString*s=article.quieterTitle;
s=[s stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
s=[s stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
s=[s stringByReplacingOccurrencesOfString:@">" withString:@">"];
s=[s stringByConvertingTeXintoHTML];
return s;
}
-(NSString*)eprint
{
if(![article isEprint])
return nil;
NSString* eprint= article.eprint;
//return [NSString stringWithFormat:@"[%@] ", eprint];
NSString*path=[[ArxivHelper sharedHelper] arXivAbstractPathForID:eprint];
return [NSString stringWithFormat:@"[<a class=\"nonloudlink\" href=\"%@\">%@</a>] ",path, eprint];
}
-(NSString*)pdfPath
{
if([article isEprint]){
// if(article.hasPDFLocally){
return [NSString stringWithFormat:@"<a href=\"spires-open-pdf-internal://%@\">pdf</a>",article.objectID.URIRepresentation];
// }else{
// return @"get <a href=\"spires-download-and-open-pdf-internal://\">pdf</a>";
// }
}
if(article.hasPDFLocally){
NSString* path=[article.pdfPath stringByAbbreviatingWithTildeInPath];
NSString* dir=[[NSUserDefaults standardUserDefaults] stringForKey:@"pdfDir"];
if([path hasPrefix:dir]){
path=@"pdf";
}
return [NSString stringWithFormat:@"<a href=\"spires-open-pdf-internal://%@\">%@</a>",article.objectID.URIRepresentation,path];
}else{
return @"<del>pdf</del>";
}
}
-(NSString*)spires
{
NSString* target=[article uniqueInspireQueryString];
if(target){
NSURL*url=[[SpiresHelper sharedHelper] newInspireWebpageURLForQuery:target];
NSString* urlString=[url absoluteString];
return [NSString stringWithFormat:@"<a href=\"%@\">inspire</a>",urlString];
}else{
return [NSString stringWithFormat:@"<del>spires</del>"];
}
return nil;
}
-(NSString*)journalNumber:(JournalEntry*)j
{
if(j.volume){
return [NSString stringWithFormat:@"<span class=\"vol\">%@</span> (%@) %@",j.volume,j.year,j.page];
}else{
return @"";
}
}
-(NSString*)journal{
if(!article.journal)return nil;
JournalEntry*j=article.journal;
NSString* str=[NSString stringWithFormat:@"%@ %@",j.name,[self journalNumber:j]];
if((article.eprint && ![article.eprint isEqualToString:@""]) || article.hasPDFLocally){
str=[NSString stringWithFormat:@"<a class=\"nonloudlink\" href=\"spires-open-journal://\">%@</a>",str];
return str;
}
if(article.doi && ![article.doi isEqualToString:@""]){
// NSString* doiURL=[@"http://dx.doi.org/" stringByAppendingString:article.doi];
str=[NSString stringWithFormat:@"<a href=\"spires-open-journal://\">%@</a>",str];
}
return str;
}
-(NSString*)flagUnflag{
if(article.flag&AFIsFlagged){
return [NSString stringWithFormat:@"<a href=\"spires-unflag://%@\">(un)flag</a>",article.objectID.URIRepresentation];
}else{
return [NSString stringWithFormat:@"<a href=\"spires-flag://%@\">(un)flag</a>",article.objectID.URIRepresentation];
}
}
-(NSString*)flagged{
if(article.flag&AFIsFlagged){
return @"⭐️";
}else{
return @"";
}
}
-(NSString*)texKey{
NSString* x=article.texKey;
if(x &&[[[NSUserDefaults standardUserDefaults] stringForKey:@"bibType"] isEqualToString:@"harvmac"]){
x=[article extraForKey:@"harvmacKey"];
}
if(!x || [x isEqualToString:@""]){
x=@"\\bibitem{?}";
}
return [NSString stringWithFormat:@"<a href=\"spires-get-bib-entry://\">%@</a>",x];
}
-(NSString*)citedBy{
if(article.eprint && ![article.eprint isEqualToString:@""]){
return [NSString stringWithFormat:@"<a href=\"spires-search://c %@\">cited by</a>",article.eprint];
}
// if(article.spicite && ![article.spicite isEqualToString:@""]){
// return [NSString stringWithFormat:@"<a href=\"spires-search://c %@\">cited by</a>",article.spicite];
// }
if(article.inspireKey && [article.inspireKey integerValue]!=0){
return [NSString stringWithFormat:@"<a href=\"spires-search://c recid:%@\">cited by</a>",article.inspireKey];
}
return @"<del>cited by</del>";
}
-(NSString*)refersTo{
if(article.eprint && ![article.eprint isEqualToString:@""]){
return [NSString stringWithFormat:@"<a href=\"spires-search://r %@\">refers to</a>",article.eprint];
}
if(article.inspireKey && [article.inspireKey integerValue]!=0){
return [NSString stringWithFormat:@"<a href=\"spires-search://r recid:%@\">refers to</a>",article.inspireKey];
}
return @"<del>refers to</del>";
}
@end