HTML コンテンツを解析するための hpple ライブラリの使用に問題があります。
textkit を使用して、適切な html コンテンツを表示したいと考えています。
だからここに私の問題があります。
すべての画像またはビデオ タグを独自のタグに置き換えてから、UITextview に textkit を挿入したいと考えています。
私のアルゴリズム:
私は自分のhtmlコンテンツを解析します
[要素 raw] に正規表現の a が含まれている場合、正規表現でテストします
ある場合は、ビデオの画像の「src」を抽出し、パーソナライズされた文字列を作成します
次に、 [element raw] 全体を新しい文字列に置き換えたい
たとえば、次のようになります。
<p>
<img src="blablabla.jpg" />
</p>
次のようになります。
\IMG\blablabla.jpg
次に、正規表現を使用してすべての HTML タグを削除します
私の問題は、[element raw] を新しいタグ「\IMG\blablabla.jpg」に置き換えたいときです。
私が使用する正規表現
#define REGEX_IMG_CONTENT @"<img[^>]+src=\"([^\">]+)\""
#define REGEX_IFRAME_CONTENT @"<iframe[^>]+src=\"([^\"]+).*?</iframe>"
#define REGEX_ALL_BALISE @"<(?:\"[^\"]*\"['\"]*|'[^']*'['\"]*|[^'\">])+>"
カスタム NSTextstorage の初期化
_textStorage = [[HTMLTextStorage alloc]init];
NSDictionary *attrs = @{NSFontAttributeName:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[self.textStorage formatedHTMLStringWithString:self.post.wpContent] attributes:attrs];
独自のタグを使用して HTML 文字列をフォーマットする
- (NSString *)formatedHTMLStringWithString:(NSString *)string {
NSData *tutorialsHtmlData = [string dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *tutorialsParser = [TFHpple hppleWithHTMLData:tutorialsHtmlData];
NSString *tutorialsXpathQueryString = @"//p";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
for (TFHppleElement *element in tutorialsNodes) {
NSString *imageRegexString = REGEX_IMG_CONTENT;
NSString *videoRegexString = REGEX_IFRAME_CONTENT;
[self formatElement:element withRegexString:imageRegexString andInitialString:string];
[self formatElement:element withRegexString:videoRegexString andInitialString:string];
}
NSError *error = nil;
NSRange range = NSMakeRange(0, string.length);
NSString *htmlBalisesRegexString = REGEX_ALL_BALISE;
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:htmlBalisesRegexString options:0 error:&error];
NSString *finalString = [reg stringByReplacingMatchesInString:string options:0 range:range withTemplate:@""];
return finalString;
}
- (void)formatElement:(TFHppleElement *)element withRegexString:(NSString *)regexString andInitialString:(NSString *)initialString{
NSString *raw = [[element raw] stringByDecodingHTMLEntities];
NSError *error = nil;
NSRange range = NSMakeRange(0, raw.length);
NSRegularExpression *reg = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:&error];
NSArray *matches = [reg matchesInString:raw options:0 range:range];
if (matches.count != 0) {
NSLog(@"initialString : %@",initialString);
NSLog(@"Paragraph contains regex : %@",raw);
NSArray *firstNode = [element children];
TFHppleElement *child = firstNode[0];
if ([child hasChildren]) child = [child children][0];
[self handleAllRegexFormatWithInitialString:initialString raw:raw child:child andRegexString:regexString];
}
}
- (void)handleAllRegexFormatWithInitialString:(NSString *)initialString raw:(NSString *)raw child:(TFHppleElement *)child andRegexString:(NSString *)regexString {
if ([regexString isEqualToString:REGEX_IMG_CONTENT]) initialString = [self formatedStringForChild:child withRaw:raw andInitialString:initialString andTag:@"IMG"];
else if ([regexString isEqualToString:REGEX_IFRAME_CONTENT]) initialString = [self formatedStringForChild:child withRaw:raw andInitialString:initialString andTag:@"VIDEO"];
}
- (NSString *)formatedStringForChild:(TFHppleElement *)child withRaw:(NSString *)raw andInitialString:(NSString *)initialString andTag:(NSString *)tag {
NSString *string = @"";
NSDictionary *attributes = [child attributes];
NSString *replacementImageString = [NSString stringWithFormat:@"\\%@\\%@",tag,[attributes objectForKey:@"src"]];
NSRange range = [initialString rangeOfString:raw];
NSLog(@"Range : %@",NSStringFromRange(range));
if (range.location != NSNotFound) string = [initialString stringByReplacingOccurrencesOfString:raw withString:replacementImageString];
return string;
}
この方法では、長さの範囲は常に 0 です
ここで私が得るものの例:
最初の html コンテンツ
initialString : <p style="text-align: center;">
<a href="http://www.tuxboard.com/cet-artiste-dessine-en-negatif">
<img class="alignnone size-medium wp-image-150233" alt="dessin negatif 640x398 Cet artiste dessine en négatif" src="http://www.tuxboard.com/photos/2013/12/dessin-negatif-640x398.jpg" width="640" height="398" title="Cet artiste dessine en négatif" />
</a>
</p>
<p>
<span id="more-150230"></span><br />
Réaliser des dessins hyperréalistes, c’est déjà assez costaud, mais savoir les faire <strong>en négatif</strong> se révèle être un exploit que l’artiste malaisien <strong>Brian Lai</strong> maitrise avec perfection.</p>
<p>Voici un exemple. D’un côté, le dessin comme on le verrait à l’œil nu, de l’autre côté, quand il est passé en négatif.</p>
<p style="text-align: center;">
<a href="http://www.tuxboard.com/photos/2013/12/negatif-dessin.jpg">
<img class="alignnone size-full wp-image-150234" alt="negatif dessin Cet artiste dessine en négatif" src="http://www.tuxboard.com/photos/2013/12/negatif-dessin.jpg" width="504" height="729" title="Cet artiste dessine en négatif" />
</a>
</p>
<p style="text-align: center;">
<a href="http://www.tuxboard.com/photos/2013/12/art-negatif.jpg">
<img class="alignnone size-medium wp-image-150235" alt="art negatif 640x428 Cet artiste dessine en négatif" src="http://www.tuxboard.com/photos/2013/12/art-negatif-640x428.jpg" width="640" height="428" title="Cet artiste dessine en négatif" />
</a>
</p>
<p>Vous pensez « fake » ? Vous vous trompez. L’artiste a même réalisé une vidéo pour expliquer sa technique.</p>
<div id="tuxplayer">Chargement du player …</div>
<p><script type="text/javascript">jwplayer("tuxplayer").setup({ flashplayer: "http://medias.tuxboard.com/playerv2.swf", file: "http://medias.tuxboard.com/Brian_Lai_Invert_Drawing.mp4",image: "http://www.tuxboard.com/photos/2013/12/Video-Brian-Lai-negatif.jpg", height: 370,width: '100%', 'plugins': 'sharing-3'});</script></p>
抽出された [要素 raw]
2013-12-19 11:40:50.247 Tuxboard[6279:70b] Paragraph contains regex :
<p style="text-align: center;">
<a href="http://www.tuxboard.com/cet-artiste-dessine-en-negatif">
<img class="alignnone size-medium wp-image-150233" alt="dessin negatif 640x398 Cet artiste dessine en négatif" src="http://www.tuxboard.com/photos/2013/12/dessin-negatif-640x398.jpg" width="640" height="398" title="Cet artiste dessine en négatif"/>
</a>
</p>
範囲
2013-12-19 11:40:50.247 Tuxboard[6279:70b] Range : {2147483647, 0}
編集
アルゴリズムで抽出したい部分を手動で抽出し、それを HTML パーサーによって返された [element raw] と一致させたところ、HTML パーサーが空白文字を削除したようです...