Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Another interpolation issue #55

Closed elquimista closed 8 years ago

elquimista commented 8 years ago

Here's an example:

.col.s6.right-align #[strong Sign In] / !{$view->Html->link('Sign Up', ['action' => 'add'])}

This should be interpreted as:

<div class="col s6 right-align">

  <strong>
    Sign In 
  </strong> / <?=$view->Html->link('Sign Up', ['action' => 'add'])?>
</div>

But it's misinterpreted like this:

<div class="col s6 right-align">

  <strong>
    Sign In] / <?=$view->Html->link('Sign Up', ['action' => 'add'
  </strong>)?>
</div>

I confirmed it on talejade sandbox.

I think this is caused by wrong lookup of matching brackets during interpolation processing.

TorbenKoehn commented 8 years ago

Yep, this is a known problem of Jade interpolation since it only uses a simple greedy regex and making it non-greedy would break simple variable access more easily (#[a(href=$someVar['someValue'])]).

I will think about a better way to resolve this.

elquimista commented 8 years ago

Is it a native problem of original Jade language interpreter?

TorbenKoehn commented 8 years ago

Nope, the native implementation can handle both cases just fine.

I'm already working on a solution.

elquimista commented 8 years ago

FYI, here's my suggestion /#(\[(?:[^\[\]]+|(?1))*+\])/

elquimista commented 8 years ago

Just use my suggestion for reference. I want to see your solution as well. And pls release the new version covering this issue asap. thanks.

elquimista commented 8 years ago

And can you do me a favor? This is not about your talejade repo ;). Please check https://github.com/clthck/cakephp-jade-tmbundle/blob/master/Syntaxes/Jade.tmLanguage I tried to customize Jade sublime text syntax so that it displays php syntax instead of javascript (e.g. = $this->variable), but it doesn't work well.

TorbenKoehn commented 8 years ago

Honestly, I've never taken a look at Sublime text syntax highlighting definition, but I'll look if I can find the problem.

What exactly is the problem?

elquimista commented 8 years ago
p= strtolower($s)

In the above code, strtolower($s) should be highlighted as PHP language, but currently I think it's considered plain text.

Btw, what do you think of my regex suggestion?

TorbenKoehn commented 8 years ago

Your regex suggestion would fix this problem indeed.

It would make something like p Some string #[a(href=$params['url']) #[i.fa.fa-fw.fa-person] My #{$lang['link']}, welcome #{$user->{$nameProperty}}] impossible, though.

That's why I took a different approach which doesn't use a single reg-ex (I could put that into the lexer but it would be a mess to lex and interpolation works inside text- and attribute values only)

https://github.com/Talesoft/tale-jade/blob/dev-interpolation-fix/Compiler.php#L664

I now simply count brackets.

This will allow probably any kind of syntax you might need inside interpolation, once and for all.

elquimista commented 8 years ago

Your case is nested interpolation. I think using nested interpolation is not good for the sake of code legibility and it's also rarely used. I'd rather use line break and multiple lines in that case.

TorbenKoehn commented 8 years ago

I guess nested interpolation might make sense for stuff like

article
    p.
        Some random article content #[a #{$lang['more']}] #[a #{$lang['comments']} (#{$commentCount})]

and also stuff like

article
   p.
       Some wiki article #[a Some external ref #[i.fa.fa-whatever]]

In any case, bracket counting fixes both cases, I've written the tests already, the fix will be online soon :)

TorbenKoehn commented 8 years ago

New release is out (1.3.7), can you update and test it?

elquimista commented 8 years ago

Great :+1: Works perfect !! Thank you so much for releasing v1.3.7. Btw, what is tale-config used for?

TorbenKoehn commented 8 years ago

tale-config is just a small configuration utility library that I will use in some further tale-* projects and components. Some of them are already online, just take a look at the Talesoft GitHub page. Every one except for tale-jade and tale-config is really experimental