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"]
foo:
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)
The gist of the problem boils down to handling regexp matches inside
regexp matches.
Example: bbcode:
[quote:4d59322f12="foo"]
diu which I'd like to convert to:bar said:
foo:
diu[/QUOTE]bar:
baz
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)