Project

General

Profile

Plugin that replaces words via regular expression?

Added by FLX _ over 13 years ago

I'm 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


Replies (12)

RE: Plugin that replaces words via regular expression? - Added by FLX _ over 13 years ago

The regular expression part itself is easy, I'm more interested in how to retrieve/iterate the wiki content and return it after replacing.

RE: Plugin that replaces words via regular expression? - Added by FLX _ about 13 years ago

Sorry for bumping but I really like to get this fixed :)

RE: Plugin that replaces words via regular expression? - Added by FLX _ about 13 years ago

I got to the point where I have the following code:

require 'redmine'
require "/opt/redmine/app/models/wiki_content" 

Redmine::Plugin.register :redmine_wiki_confluence do
    name 'Redmine Wiki Confluence plugin'
    author 'Author name'
    description 'This is a plugin for Redmine'
    version '0.0.1'
    url 'http://example.com/path/to/plugin'
    author_url 'http://example.com/about'

    WikiContent.all.each do |post|
      post.text = post.text.gsub!(/important_data/, "nonsense")
      post.save
    end
end

It's supposed to replace important_data with "nonsense" but it doesn't work. What am I doing wrong?

Thanks,
Dennis

RE: Plugin that replaces words via regular expression? - Added by Jean-Baptiste Barth about 13 years ago

You should use a plugin if you want a permanent behaviour. What you did is only triggered once, when redmine is started.

If you want a one shot thing :
  • either you can backup your DB, replace the word in the dump, then restore it
  • either you can open a ruby script/console and do something like you did, but replace the gsub! with gsub

If you want a permanent thing, then it's a bit more complicated, you should first read the plugin doc in the wiki, then you might be able to use Rails callbacks to achieve your goal in a plugin...

RE: Plugin that replaces words via regular expression? - Added by FLX _ about 13 years ago

Hi Jean-Baptiste,

What I want to achieve is that it basically replaces certain patterns. The idea is to make a plugin that supports the Confluence wiki markup, as that is what I'm using 90% of the time at work. How would you tackle this?

Thanks,
Dennis

RE: Plugin that replaces words via regular expression? - Added by Etienne Massip about 13 years ago

Wiki syntax formatting has been made extendable by plugin since of #2025, you will find an example plugin here : https://github.com/yugui/redmine_rd_formatter

As for the reprocessing of preexisting wiki text, you should follow Jean-Baptiste advice.

RE: Plugin that replaces words via regular expression? - Added by FLX _ about 13 years ago

Hi Etienne,

Thank you for that example. However, it seems overly complicated for what I try to achieve. I'd like to transform the regular external link to accept link macros in the form of [text|http://url.com]. Any suggestions on a simple way to do this?

Apologies for being such a n00b,
Dennis

RE: Plugin that replaces words via regular expression? - Added by Etienne Massip about 13 years ago

You need to find a way to insert your own parsing method after :parse_wiki_links the [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_headings] array in ApplicationHelper#textilizable (source:app/helpers/application_helper.rb#L450).

RE: Plugin that replaces words via regular expression? - Added by FLX _ about 13 years ago

I'm giving up; application_helper.rb doesn't hold the code for external links so it's difficult for me to edit the external link format. It's taking me too much time and effort to get this done.
Instead, I'd like to offer someone money via PayPal to create a plugin that:
  • Allows links to be made in the form of [text|http://url.com]
  • Allows page links to made in the form of [Page] and [Page|Title]. The difference is that it doesn't use double square brackets.

If you're interested in doing this, let me know via email at (junkmail redirect service). Please be sure to include your PayPal address as well.

Thanks,
Dennis

RE: Plugin that replaces words via regular expression? - Added by Diana Savvatina almost 12 years ago

So what was the result?
Is there anything reusable?

Thanks,
Diana

    (1-12/12)