Hi guys,

I'm having some trouble figuring out how to do some regular expression  
foo and thought maybe someone on here could help,

Basically i am trying to parse out special tags from html returned  
from TinyMCE, and replace them with content for doing html to pdf  
generation.

I've written a little gist page demonstrating what i need it to do: 
http://gist.github.com/53383

i have got it working so far with 1 paragraph inside the if statement  
(the first example), but when you have two content paragraphs, it  
fails to match.

i have written some rspec tests for it, if anyone fancies having a go,  
i've included my current regular expression, if you want to build on  
that :)

Cheers,
Adam


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en
-~----------~----~----~----~------~----~------~--~---

describe "Regular expression" do
  before(:each) do
    @regular_expression = /(<p>.*\{\{if[\s\w_\.><=\-\+\*\/&|]*?\}\}.*<\/p>\r?\n?<p>.*<\/p>\r?\n?<p>.*\{\{end\}\}.*<\/p>)\r?\n?/
  end

  it 'should match a single paragraph block statement' do
    string = "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>{{end}}</p>"
    result = string.scan(@regular_expression)
    result.size.should == 1
    result.first.first.should == "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>{{end}}</p>"
  end
  
  it 'should match a single paragraph block statement with a custom matcher in the content' do
    string = "<p>{{if something > something_else}}</p>\n<p>Show some content, and a {{custom_matcher}}</p>\n<p>{{end}}</p>"
    result = string.scan(@regular_expression)
    result.size.should == 1
    result.first.first.should == "<p>{{if something > something_else}}</p>\n<p>Show some content, and a {{custom_matcher}}</p>\n<p>{{end}}</p>"
  end
  
  it 'should match a single paragraph block statement with multiple nested html elements' do
    string = "<p><span><span><span>{{if something > something_else}}</span></span></span></p>\n<p><span><span><span>Show some content</span></span></span></p>\n<p><span><span><span>{{end}}</span></span></span></p>"
    result = string.scan(@regular_expression)
    result.size.should == 1
    result.first.first.should == "<p><span><span><span>{{if something > something_else}}</span></span></span></p>\n<p><span><span><span>Show some content</span></span></span></p>\n<p><span><span><span>{{end}}</span></span></span></p>"
  end

  it 'should match a double paragraph block statement' do
    string = "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>Show some more content</p>\n<p>{{end}}</p>"
    result = string.scan(@regular_expression)
    result.size.should == 1
    result.first.first.should == "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>Show some more content</p>\n<p>{{end}}</p>"
  end
  
  it 'should match a double paragraph block statement with extra html elements' do
    string = "<p><span>{{if something > something_else}}</span></p>\n<p><strong>Show some content</strong></p>\n<p><strong>Show some more content</strong></p>\n<p><span>{{end}}</span></p>"
    result = string.scan(@regular_expression)
    result.size.should == 1
    result.first.first.should == "<p><span>{{if something > something_else}}</span></p>\n<p><strong>Show some content</strong></p>\n<p><strong>Show some more content</strong></p>\n<p><span>{{end}}</span></p>"
  end

  it 'should match two double paragraph block staatements' do
    string = "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>Show some more content</p>\n<p>{{end}}</p>\n<p>{{if something_else > something_other}}</p>\n<p>Show some other content</p>\n<p>Show some other other content</p>\n<p>{{end}}</p>"
    result = string.scan(@regular_expression)
    result.size.should == 2
    result[0].first.should == "<p>{{if something > something_else}}</p>\n<p>Show some content</p>\n<p>Show some more content</p>\n<p>{{end}}</p>"
    result[1].first.should == "<p>{{if something_else > something_other}}</p>\n<p>Show some other content</p>\n<p>Show some other other content</p>\n<p>{{end}}</p>"
  end
end


Reply via email to