I’m writing a blog archive converter in ruby. In order to convert wordpress post content to html format, I must implement wordpress’s wpautop() function.
Original wpautop() function : http://pastebin.com/BzV8bXxQ
My ruby implement: https://github.com/chloerei/blog_converter/blob/master/lib/blog_converter/adaptor/wordpress.rb , see WordPress#wpautop_filter
It work fine in ruby1.9.2, bu in 1.8.7, it throw an error
blog_converter/lib/blog_converter/adaptor/wordpress.rb:147: undefined (?...) sequence: /(?<!<br />)s*n/
The sources
// In php
$pee = preg_replace('|(?<!<br />)s*n|', "<br />n", $pee); // optionally make line breaks
# In ruby
string.gsub!(%r|(?<!<br />)s*n|, "<br />n") # optionally make line breaks
After some search, I found that ruby1.8.7 doesn’t have the new regexp engine ‘Oniguruma’, it doesn’t support new regexp syntax.
So I think I have two way:
- Add dependent ‘oniguruma’ when using ruby < 1.9.0
- Rewrite
/(?<!<br />)s*n/
in old syntax
Which way is better? And how to rewrite this regexp?
If not works look-ahead assertions you can like this(attention!- not tested-i haven’t 1.8):
Try this
.gsub!(%r|(<br />)?s*n|, "<br />n")