DougBeney / jekyll-pug

Jekyll Plugin That Allows You To Use Pug
MIT License
37 stars 2 forks source link

Whitespace control for liquid tags #9

Closed ibatullin closed 6 years ago

ibatullin commented 6 years ago

Liquid tags produce errors after d7fafa1e5cc43f46159a13220e3ae818b8e8a3ed:

{% assign a='b' %}
  > 1| {% assign a='b' %}
-------^
    2|
    3| h1 Welcome to the source test.
    4|

unexpected text "{% as"
    at makeError (/usr/local/lib/node_modules/pug-cli/node_modules/pug-error/index.js:32:13)
    at Lexer.error (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:58:15)
    at Lexer.fail (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1304:10)
    at Lexer.advance (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1364:15)
    at Lexer.callLexerFunction (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1319:23)
    at Lexer.getTokens (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:1375:12)
    at lex (/usr/local/lib/node_modules/pug-cli/node_modules/pug-lexer/index.js:12:42)
    at Object.lex (/usr/local/lib/node_modules/pug-cli/node_modules/pug/lib/index.js:99:27)
    at Function.loadString [as string] (/usr/local/lib/node_modules/pug-cli/node_modules/pug-load/index.js:44:24)
    at compileBody (/usr/local/lib/node_modules/pug-cli/node_modules/pug/lib/index.js:86:18)

The tag doesn't have indentation. It works in version 0.0.1, but needs whitespace control in v1.0.1:

| {% assign a='b' %}

I like syntax in v0.0.1:

.example
  {% for i in (1..6) %}
    h{{ i }} Header {{ i }}
  {% endfor %}

How to rewrite the snippet to v1.0.1?

DougBeney commented 6 years ago

Your example could be fixed as the following:

.example
  | {% for i in (1..6) %}
  | <h{{ i }}> Header {{ i }}</h1>
  | {% endfor %}

or...

.example.
  {% for i in (1..6) %}
  <h{{ i }}> Header {{ i }}</h1>
  {% endfor %}

In 1.0.1, Jekyll-Pug overwrites the parse method of Jekyll's LiquidRenderer. This allows us to create a more efficient compiling process, but the order of compiling has changed from Pug first then Liquid.

Here's the parse function:

def parse(content)        
  measure_time do
    if @filename =~ /\.pug$/
        userSource = $JEKYLLPUG_PROJECT_SOURCE
        content = Pug.compile(content, {"filename"=>userSource})
    end
    @template = Liquid::Template.parse(content, :line_numbers => true)
  end
end

Essentially, we need that Liquid::Template.parse object to be returned, which makes it difficult to first compile the file as Liquid and then compile the file as Pug.

DougBeney commented 6 years ago

Hello, @ibatullin. Were you able to resolve the issue?

ibatullin commented 6 years ago

Thanks for your help and quick answer! I will keep in mind the order of compiling.

I also found Pug-only solution:

- var headers = [1, 2, 3, 4, 5, 6]
.example
  each i in headers
    #{"h" + i} Header #{i}
DougBeney commented 6 years ago

Nice work!