Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Regexp'n bbcode
morbusg
1/23/2009 8:10:00 PM
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"][quote="bar"]baz[/quote]diu[/quote]
which I'd like to convert to:
> foo:
>> bar:
>> baz
> diu
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)
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Regexp'n bbcode
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password