2014-04-12 6 views
0

내용의 이전을 마쳤으며 새 구조와 일치시키기 위해 많은 HTML을 구문 분석해야합니다.Nokogiri로 img 태그 주위에 그림 래퍼 추가

인라인 스타일이 많은 이미지가 있고 모든 태그 주위에 래퍼를 추가하는 스타일을 제거해야합니다.

이 내 방법 : 내 이미지 태그 주위에 <figure class="center-image"> </figure>를 추가 할 필요가 * 내가 *를 추가

def convert_imgtag(html) 
    html_body = Nokogiri::HTML::fragment(html) 
    html_body.xpath('.//img').each do |img| 

     **** 

     img.xpath('.//@style').remove 
    end 
    html_body.to_html 
    end 

. 나는 "랩"방법으로 시도하지만 작동하지 않습니다.

답변

0

랩은 NodeSets에서만 작동한다고 생각합니다. 이 변경 사항을 img-node를 통해 두 번 실행하도록 분할하십시오.

html_body = Nokogiri::HTML::fragment(html) 

nodes = html_body.xpath('.//img') 
nodes.wrap('<figure class="center-image"> </figure>') 
nodes.each do |img| 
    img.xpath('.//@style').remove 
end 
+0

감사합니다. –