Now I have two problems
I'm converting one of my websites from Ruby on Rails to Clojure in my spare time. I stupidly put a bunch of RoR-style links inline into certain bits of plaintext content, so in my DB there are a bunch of text fields with <%= link_to ... %> in the middle.
It was easy to fix with a regex though:
(defn clean [txt]
(re-gsub #"<%=\s*link_to\s+(\"[^\"]+\"|'[^']+')\s*(?:,\s*'([^']+)'\s*)?(?:,\s*image_path\(['\"]([^'\"]+)['\"]\)\s*)?(?:,\s*:controller\s*=>\s*(?::(\S+)|['\"]([^\"']+)['\"])\s*)?(?:,\s*:action\s*=>\s*(?::(\S+)|['\"]([^\"']+)['\"])\s*)?(?:,\s*:id\s*=>\s*(?:(\d+)|:(\S+)|['\"]([^\"']+)['\"])\s*)?\s*%>"
(fn [[_ s & parts]] (let [href (str-join "/" (filter identity parts))]
(str "<a href=\"/" href "\">" (re-gsub #"^[\"']|[\"']$" "" s) "</a>")))
txt))
And by easy, I mean not easy.
Note to self, try something other than a regex next time.
Note to self, don't bury some framework's funky-syntax DSL in the middle of plaintext content. Next time use HTML or do the conversion from DSL to HTML early rather than late.
Silly how two years ago I thought I'd be using Ruby for that site forever.

3 Comments
This entry breaks the RSS feed on my Livejournal :/
lol, 'bout to say, "that's easy?" Think you'll be dreaming of bracket and carets for awhile.
@Baloki Glad I could be of service.
@Gen2ly I honestly think I have had a nightmare about source code before. :(
Speak your Mind
Preview