Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output content format is strange #2

Open
sillykelvin opened this issue May 20, 2013 · 6 comments
Open

Output content format is strange #2

sillykelvin opened this issue May 20, 2013 · 6 comments

Comments

@sillykelvin
Copy link

Hi,

When I am using mustache.el to render templates, I find something strange, let's take the code snippets below as example:

(mustache-render
 "<{{a}} />
  {{#b}}
  <{{b}} />
  {{/b}}
  {{#c}}
  <{{c}} />
  {{/c}}"
 (ht ("a" "AA")
     ("b" "BB")
     ("c" "CC")))

the output is:

<AA />
    <BB />
      <CC /> (Note: two more spaces are added than original format)
  (Note: there are two spaces at the beginning of this line)

what I expect is that the output should be consistent with the original format:

<AA />
  <BB />
  <CC />

no more extra spaces before <CC /> and the last line.

I don't know if this is an issue, or there is something, maybe a switch, can change the behavior.

Thanks,
Kelvin

@sillykelvin
Copy link
Author

And another issue:

(mustache-render
 "{{a}}
  {{b}}"
 (ht ("a" "AA")
     ("b" "BB")))

will produce:

AA  BB

but NOT:

AA
  BB

As you see, the newline character is missing, I think it should keep the original format.

@sillykelvin
Copy link
Author

I read your source code, and find the root cause for the second problem, it is because a cond branch in function mst--render-section:

...
(t
 (s-chop-prefix
  "\n"
  (second parsed-lexeme)))

the prefix newline character is dropped, I don't quite understand your design intention about this, could you please do some explanation?

Thanks,
Kelvin

@Wilfred
Copy link
Owner

Wilfred commented May 20, 2013

The mustache standard has a lot to say on handling newlines: https://github.com/mustache/spec/blob/v1.0.2/specs/sections.yml#L135

So, a fully compliant mustache implementation will change whitespace in some circumstances. Also, mustache.el isn't perfectly compliant with the specification yet. However, removing the s-chop-prefix call makes the following unit test fail:

(ert-deftest mustache-test-standalone-lines ()
  (should
   (equal "| This Is
|
| A Line
"
    (mustache-render "| This Is
{{#boolean}}
|
{{/boolean}}
| A Line
" (ht ("boolean" t))))))

This example is from the specification: https://github.com/mustache/spec/blob/v1.0.2/specs/sections.yml#L155

That being said, mustache.el is clearly doing the wrong thing here. I tested python-mustache (a fully compliant implementation), and it renders your second example to "AA\n BB" as expected.

I'll try to fix this when I can. I need to read the specification again to understand how we should treat whitespace.

@sillykelvin
Copy link
Author

Thanks for your reply, waiting for your great solution solving this~~ :-D

@Wilfred
Copy link
Owner

Wilfred commented May 25, 2013

OK, this should now all work according to the spec. Your examples now give the output expected. Let me know if you find any other issues.

@sillykelvin
Copy link
Author

Yeah, thanks for your great work, it works well now, except a little defect, let's take the following example:

(mustache-render
 "{{a}}
{{#b}}
{{b}}
{{/b}}
{{#c}}
{{c}}
    {{/c}}    "  ;; Note: there are four spaces both at the beginning and the end
 (ht ("a" "AA")
     ("b" "BB")
     ("c" "CC")))

the output is:

AA
BB
CC
        (Note: the extra 8 spaces are included)

As you see, the last line only contains a condition validation close tag, according to this spec(https://github.com/mustache/spec/blob/v1.0.2/specs/sections.yml#L155), this line should be removed also.

However, this does not affect much, but I am a perfactionist, so if this issue is smooth to fix, please help fix it, thanks very much~~ :-D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants