I have a blog in Rails in which I want to formalize the external links in the content part, for example to keep track of clicks, broken links etc.
I have started creating my own type of markup in which I use the following:
block_code = [[LINK_A##URL:http://www.exampleA.com##ANCHOR:A]]
block_code = [[LINK_B##URL:http://www.exampleB.com##ANCHOR:B]]
The interpretation of these strings/markups are quite easy (since I have specified them myself) in order to extract info about whether it is LINK_A
or LINK_B
and the URL-value. I do this with a function:
render_markup(block_code) #=> "<a href='http://www.exampleA.com'>A</a>"
What I need to do now is to have a function that iteratest through a text and when it finds my block_codes, replaces them with the result of render_markup()
.
example_content = "Today was a really nice day when I went to the [[LINK_A##URL:http://www.thepark.com##ANCHOR:The park]] where I had a [[LINK_B##URL:http://www.swim.com##ANCHOR:swim]] and this was very nice"
so that when I do:
scan_and_replace(example_content)
it creates:
"Today was a really nice day when I went to the <a href='http://www.thepark.com'>the park</a> where I had a <a href='http://swim.com'>swim</a> and this was very nice."
whenever it encounters my codes blocks in the text.
So, what I need help with is finding and replacing these code blocks, i.e. creating scan_and_replace() function. I do NOT need help in rendering my code block into HTML, that is already working.
If this proves too difficult or if you find this method really bad (and know a better method that solves the same problem), let me know!