mslinn/jekyll_flexible_include_plugin

File content escaping destroys special characters in code examples

PhilLab opened this issue · 3 comments

Thanks for this helpful plugin!

However, I have problems when including C++ code examples. Problems which the original built-in include does not have.
The following markdown:

Plain:
```cpp
#include <iostream>

class Example
{
};
```

Built-in include:
```cpp
{% include example.cpp %}
```

flexible_include:
```cpp
{% flexible_include _includes/example.cpp %}
```

results in the following output, scrambling special characters like < or { to &lt; and &#123;

grafik

Reason could be the line

escaped_contents = read_file(path, context).gsub("{", "&#123;").gsub("}", "&#125;").gsub("<", "&lt;")

However the question would be: when do we want escaping and when do we not? How does the built-in include handle this?

As I am unfamiliar with Ruby and the inner workings of Jekyll I can only guess, but these seem to be some relevant lines in the original include.rb script:

https://github.com/jekyll/jekyll/blob/fdbb03b2584546a8d9a593b8eef89c4dd0303a93/lib/jekyll/tags/include.rb#L151

cached_partial[path] = unparsed_file.parse(read_file(path, context))

https://github.com/jekyll/jekyll/blob/fdbb03b2584546a8d9a593b8eef89c4dd0303a93/lib/jekyll/tags/include.rb#L90

Liquid::Template.parse(@file).render(context) if VARIABLE_SYNTAX.match?(@file)

@PhilLab The proper term is "HTML escaping", not "scrambling". Scrambling has a disorderly aspect, but this is a translation that preserves order.

Happily, I have a version of this program that allows you to suppress the HTML escape if and when desired. I'll commit it shortly.

@mslinn Awesome, thanks for taking the time to look into this.

Not to be nick-picky but if HTML escaping results in characters you are not expecting at a certain instance, I would call them "scrambled" with respect to this specific use case. Of course, in other places this might be exactly what you want, just as you said. Then, they would not be "scrambled". But so what, English is not my mother tongue so I am not well equipped to argue about rhetoric. Scrambled indeed seems to be too judgmental here - I didn't mean it to be.

As I said "when do we want escaping and when do we not?" and I am grateful that you have taken the time to add a parameter for this in a85cca5 👍