ruby on rails - Nokogiri: Select content between element A and B -


The smartest way to start the nautchuri and select all the contents between the stop element (including Start- / Stop-Elements) What's the way

To understand what I'm seeing, check out the example code below:

  'rubygems' is required 'nokogiri' value = Nokogiri :: HTML.parse (& lt; & lt; -HTML_END) "& lt; html> & lt; body & gt; & lt; p id = 'para-1' & gt; A & lt; / p & gt; ; Div; div class = 'block' id = 'x1' & gt; & lt; p class = "this" & gt; foo  & lt; p id = 'para-2' Gt; B & lt; / p & gt; & lt; / div & gt; & lt; P id = 'para-3' & gt; c & lt; / p & gt; & lt; p class = "That "& Gt; Bar & lt; / p & gt; & lt; p id = 'para-4' & gt; D & lt; / p & gt; & lt; p id = 'para-5' & gt; E & lt; / p & gt; & lt; Div class = 'block' id = 'x2' & gt; & lt; p id = 'para-6' & gt; F & lt; / p & gt; Lt; / div & gt; & lt ; P id = 'para-7' & gt; F & lt; / p & gt; & lt; p id = 'para-8' & gt; G & lt; / p & gt; & lt; / body & Gt; & lt; / html & gt; "HTMLIT Basic = Price CSS ('body'). First # Start Element @ Start_Element = Parent.et ('P # Para-3) #Stop Element @ &_Element = Parent.et (' P # Para-7 ')  

The result (return value) should look like this :

  & lt; P id = 'para-3' & gt; C & lt; / P & gt; & Lt; P class = "this" & gt; Bar & lt; / P & gt; & Lt; P id = 'para-4' & gt; D & lt; / P & gt; & Lt; P id = 'para-5' & gt; E & lt; / P & gt; & Lt; Div class = 'block' id = 'x2' & gt; & Lt; P id = 'para-6' & gt; F. & lt; / P & gt; & Lt; / Div & gt; & Lt; P id = 'para-7' & gt; F. & lt; / P & gt;  

Update: This is my current solution, though I think something should be clever:

  @ my_content = " "@selected_node = true def collect_content (_start) if _start == @end_element @ my_content & lt; & Lt; _start.to_html @selected_node = Incorrect end if @selected_node == true my_content & lt; & Lt; _start.to_html Collect_content_content (_start.next) End End Collection_ content (@start_element) @ my_content  

A very smart inlineer that uses recursive:

  def collect_between (first, last) first == last? [First]: [first, * archive_bateine ​​(first, one, last)]  

an iterative solution:

  DIF archive_with (first, last) result = [First] up to the first == Last First = First.   & Lt; First end result ending  

Edit: Asterix short description

This is called a split operator, it "unrolls an array" ":

  array = [3, 2, 1] [4, array] # = & gt; [4, [3, 2, 1]] [4, * array] # = & gt; [4, 3, 2, 1] Some_meth (array) # = & gt; Some_method ([3, 2, 1]) some_method (* array) # = & gt; Some_method (3, 2, 1) def other_math (* array); Array; End other_math (1, 2, 3) # = & gt; [1, 2, 3]  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -