How to rewrite a regexp (?<!…) in ruby1.8. (reimplement wpautop function)

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

Read More

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:

  1. Add dependent ‘oniguruma’ when using ruby < 1.9.0
  2. Rewrite /(?<!<br />)s*n/ in old syntax

Which way is better? And how to rewrite this regexp?

Related posts

Leave a Reply

2 comments