9
Root
1y

So at work, there is this class/model thing that's for storing translated strings. It also supports n-level nested macros, cascading lookup (e->d->c->b->a->blank), and I've added transforms too. The code is a bloody mess and very inefficient (legendary dev's code), but it's useful.

You call methods with a symbol representing one of the strings, and it does... whatever you ask, like return text, booleans, expand macros and submacros, pass in data to interpolate, etc.

But I just learned something today.
Its `.html` method... doesn't support html. In fact, calling it strips out all html, takes whatever is left, and attempts to convert that back into html. Because that makes so much sense. So, if you have an html string? Don't call html on it.

Also, macros use the same <angle brackets> as html tags, and macro expansion eats unknown macros, so... you can't mix html and macros, meaning you cannot inject values into your markup. That's a freaking joy to work around. (You end up writing a parser every time.)

So no, if you have an html string, you need to get the raw data out and handle it yourself. Don't reach for that shiny .html method; it'll just ruin your day.

It's the little things that make my day so terribly long.

Comments
  • 0
    perhaps it was meant to be called `unhtml()`? Like unwrap the contents from html and assume its content is wrapped HTML, so turn it into an unwrapped html.

    But then again, it's not really un-html-ing, it's... html'ing, so the method name `html()` seems about right :D

    Perhaps `unwrapHTML(html: string)`?
  • 1
    html as in "I'm gonna put the result into HTML, please protect me against markup injection"?
  • 1
    Btw is this still Ruby? With the metaprogramming features Ruby has, why do you need a DSL? I had to implement my own templated i18n solution with macros once, dynamically loaded script files seemed to do everything you could ever possibly want, and they're relatively fast too.
  • 0
    @netikras It converts arrays into <ul><li>’s. For strings, it strips out all <>’s (tags, macros) then replaces any \n’s with <br>‘s. It doesn’t do anything else, and doesn’t support any other tags. It’s great.
  • 0
    @lorentz There are definitely several other ways this could have been built. This approach makes the cascading lookup easier / more flexible, and allows editing values in production, among a few other features.

    It’s a decent system. I have to say, though, it isn’t written very well.

    And the inability to mix markup with macros is just… argh! Really!?
  • 1
    @Root pretty sure you can modify classes at runtime in Ruby. I see the benefit in avoiding variable depth metaprogramming. If I were somehow coerced into writing Ruby, I think I couldn't resist dynamically generating dynamic macro generators (I have a generalization problem and I'm working on it) so I'm surprised your coworkers didn't.
  • 0
    @lorentz They often accuse one another of being brilliant, but truth be told, talent is pretty rare among them.
  • 1
    @Root Exactly the sort of person I would expect to dynamically generate everything but still use a duck typed language. Achieving maximal generality in C++, Rust or C# requires actual skill
Add Comment