title: Nokogiri: Merge neighbour text nodes recursively?
body: <p>I have a prepared Nokogiri page where junk is removed... but still the text parts are stored in different nodes...</p>
  
  <p>What I want to do is connecting all direct neighbour text nodes into one single text node...</p>
  
  <p>what I came up with:</p>
  
  <pre><code>#merge neighbour text nodes -&gt; connect content
  def merge_text_nodes(node)
    previoustext = false
    node.children.each_with_index do |item,i|
      if item.name != 'text()'
        merge_text_nodes(item)
        previoustext = false
      else
        if previoustext
          node.children[i-1].inner_html += item.inner_html
          item.remove
        end
        previoustext = true
      end
    end
  end
  </code></pre>
  
  <p>But it doesn't seem to work as expected - it seems to do nothing at all... Can someone tell me how to do it right/show me the error/the correct way to do it?</p>