Regexp'n bbcode

M

Mikko Heiskanen

I'm scratching my head over the goal of converting bbcode to markdown.
The gist of the problem boils down to handling regexp matches inside
regexp matches.

Example: bbcode:
[quote:4d59322f12="foo"]
bar said:
diu which I'd like to convert to:
foo:
diu[/QUOTE]

What I've got so far is something like this:

START = '\[quote'
IDENT = '(?::\w+)?'
USER = '(?:="([\w -_]+)")?'
STOP = "\\[\\/quote#{IDENT}\\]"
QUOTE = "#{START+IDENT+USER}\\]"
INSIDEQUOTE = /#{QUOTE}(.+?)(?!#{START})#{STOP}/m

def bbcode2markdown(txt)
@quotedusers = {}
txt.scan(/#{START}/).each_with_index { |match,index|
txt.gsub!(INSIDEQUOTE) { |str| $2.gsub(/^(.)/, '>\1').gsub(/(.)$/,
"\n\\1\n") }
depth = '>' * (index + 1)
if $1
@quotedusers[depth] = $1
end
}

@quotedusers.each { |depth,user|
txt.sub!(/^#{depth}(\w)/, "#{depth}**#{user}**: \\1")
}
end

But I am missing some cases for some reason, and my brain is melting
merely at looking at that code after a couple of months after writing
it.
I'm sure there must be a straight-forward way to get this done, but my
melted brain just can't figure out how.

(re-posted after a mistake, sorry about that rforum)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top