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

Fix pre issues #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 47 additions & 8 deletions src/compress.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,63 @@
{% if _pres.size != 0 %}
{% if site.compress_html.blanklines %}
{% assign _lines = _pres.last | split: _LINE_FEED %}
{% assign _lastchar = _pres.last | split: "" | last %}
{% assign _outerloop = forloop %}

{% comment %}
This fancy section here is the only way to slice a string in liquid 2.
(At least that I know of)
I use this to determine whether the last line of this _pres.last is
leading up to the start of a pre (IE indentation) and it seems to be the
only way to reliably handle all the potential edge cases.
{% endcomment %}
{% assign _lastline = _lines.last.size %}
{% assign _lastline = _pres.last.size | minus: _lastline %}
{% assign _lastline = _pres.last | split: "" | shift: _lastline | join: "" %}
{% if _lastline == _lines.last %}
{% assign _lastline = true %}
{% else %}
{% assign _lastline = false %}
{% endif %}

{% capture _pres_after %}
{% assign _firstchar = _pres.last | split: "" | first %}
{% if _firstchar == _LINE_FEED %}
{% assign _trimmed = _pres.last | split: " " | join: " " %}
{% if _trimmed == empty and forloop.last %}
{% assign _trimmed = true %}
{% else %}
{% assign _trimmed = false %}
{% endif %}
{% unless forloop.first or _trimmed %}
{{ _LINE_FEED }}
{% endunless %}
{% endif %}

{% assign _outerloop = forloop %}
{% assign _firstline = true %}
{% for _line in _lines %}
{% assign _trimmed = _line | split: " " | join: " " %}
{% if forloop.last and _lastchar == _LINE_FEED %}
{% assign _section_before_pre = false %}
{% if _lastline and forloop.last %}
{% unless _outerloop.last %}
{{ _LINE_FEED }}
{% assign _section_before_pre = true %}
{% endunless %}
{% continue %}
{% endif %}
{% if _trimmed != empty or forloop.last %}
{% unless forloop.first %}
{% assign _trimmed = _line | split: " " | join: " " %}
{% if _trimmed != empty or _section_before_pre %}
{% unless _firstline %}
{{ _LINE_FEED }}
{% endunless %}
{% assign _firstline = false %}
{{ _line }}
{% endif %}
{% endfor %}

{% assign _lastchar = _pres.last | split: "" | last %}
{% if _lastchar == _LINE_FEED %}
{% assign _trimmed = _pres.last | split: " " | join: " " %}
{% unless forloop.last or _trimmed == empty %}
{{ _LINE_FEED }}
{% endunless %}
{% endif %}
{% endcapture %}
{% else %}
{% assign _pres_after = _pres.last | split: " " | join: " " %}
Expand Down
1 change: 1 addition & 0 deletions test/expected/blanklines/blanklines.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</pre><pre>

</pre>
<span>Text</span>
<pre>


Expand Down
5 changes: 5 additions & 0 deletions test/expected/blanklines/prepre.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
TEXT
<pre></pre>
<pre></pre>
</div>
3 changes: 3 additions & 0 deletions test/source/blanklines/blanklines.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

</pre>



<span>Text</span>



Expand Down
12 changes: 12 additions & 0 deletions test/source/blanklines/prepre.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
layout: compress
---

<div>

TEXT
<pre></pre>

<pre></pre>

</div>