1

I'm very new to Redmine/Ruby trying to achieve a simple plugin that takes the current wiki page content and matches/replaces everytime a word occurs via regular expression. How can I do this?

Thanks!

Dennis

FLX
  • 4,634
  • 14
  • 47
  • 60

1 Answers1

1

The word replacement can de done by using gsub() with \b to match a word boundary:

irb(main):001:0> 'foo bar baz foo bar'.gsub /\bfoo\b/, 'replaced'
=> "replaced bar baz replaced bar"

Here is a more complete solution with a dictionary of words to replace:

repl = {'foo'=>'apple', 'baz'=>'banana'}
s = 'foo bar baz foo bar'
for from, to in repl:
     s = s.gsub /\b#{from}\b/, to
end

Result: apple bar banana apple bar

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Nice marcog, thanks for that! However, the regexp part is easy, how can I hook this as a Redmine plugin to replace all occurrences in the wiki content? Thanks! – FLX Dec 26 '10 at 15:54
  • @FLX Unfortunately I've never used readmine nor have I written any plugins for it. Does it expose raw page content and let you modify pages easily? – moinudin Dec 26 '10 at 15:57