A
Alexandru Popescu
Hi!
I have the following hypothesis: a piece of text containing anywhere a line with the following content:
with any number of tagnames on that line.
Finally I want to modify the above text where every tagname is replaced by [[tagname => tagname]]
This is the code I have done, but I feel it is not the rubyiest:
Note: the above code is able also to avoid the case where a tagname was already transformed into
[[tagname => tagname]]
many thanks for any ruby ideas,
:alex |.::the_mindstorm::.|
I have the following hypothesis: a piece of text containing anywhere a line with the following content:
Code:
tags: tagone tagtwo
with any number of tagnames on that line.
Finally I want to modify the above text where every tagname is replaced by [[tagname => tagname]]
This is the code I have done, but I feel it is not the rubyiest:
Code:
def prepare_content(new_content)
updated_content = ""
new_content.each_line do |line|
if line !~ /^tags: /i
updated_content += line
next
end
tokens = line.split(%r{\s+})
depth = 0
1.upto(tokens.length - 1) do |index|
if tokens[index][0, 2] == "[["
depth += 1
next
end
if tokens[index][-2, 2] == "]]"
depth -= 1
next
end
if depth == 0
page.tags << tokens[index]
regexp = Regexp.new( '\s+(' + tokens[index] + ')\s*')
line.sub!(regexp, ' [[\1 => \1]] ')
end
end
end
new_content += line
end
Note: the above code is able also to avoid the case where a tagname was already transformed into
[[tagname => tagname]]
many thanks for any ruby ideas,
:alex |.::the_mindstorm::.|