-
Notifications
You must be signed in to change notification settings - Fork 57
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
Multiline comments #126
Multiline comments #126
Changes from 3 commits
83a345c
698e29a
2964e07
e83fe7a
740249a
3511749
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule Slime.Parser.TextBlock do | ||
@moduledoc " | ||
Utilities for parsing text blocks. | ||
" | ||
|
||
import Slime.Parser.Transform, only: [wrap_in_quotes: 1] | ||
|
||
@doc """ | ||
Given a text block and its declaration indentation level (see below), | ||
produces a string (or a dynamic EEx tuple) that can be inserted into the tree. | ||
|
||
nested | ||
| Text block | ||
that spans over multiple lines | ||
--- | ||
^ | ||
declaration indent | ||
""" | ||
def render(lines, decl_indent, trailing_whitespace \\ "") do | ||
[{first_line_indent, first_line, is_eex_line} | rest] = lines | ||
|
||
text_indent = if first_line == "" do | ||
[{indent, _, _} | _] = rest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about single
I understand, |
||
indent | ||
else | ||
decl_indent + first_line_indent | ||
end | ||
|
||
content = [{text_indent, first_line, is_eex_line} | rest] | ||
|
||
{text, is_eex} = Enum.reduce(content, {"", false}, | ||
fn ({line_indent, line, is_eex_line}, {text, is_eex}) -> | ||
text = if text == "", do: text, else: text <> "\n" | ||
leading_space = String.duplicate(" ", line_indent - text_indent) | ||
{text <> leading_space <> line, is_eex || is_eex_line} | ||
end) | ||
|
||
text = text <> trailing_whitespace | ||
|
||
if is_eex do | ||
{:eex, content: wrap_in_quotes(text), inline: true} | ||
else | ||
text | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,10 +8,10 @@ doctype <- space? 'doctype' space (!eol .)+ eol; | |
% Also remove dedent from eol definition and modify transforms accordingly | ||
tags_list <- tag (crlf indent? empty_lines:(space? crlf indent?)* tag:tag dedent*)*; | ||
|
||
% NOTE: We have to track leading spaces for verbatim_text, so it handled separately | ||
tag <- verbatim_text / space? tag_item / blank:(space? &eol); | ||
% NOTE: Leading whitespace is handled separately for text items | ||
tag <- text_item / space? tag_item / blank:(space? &eol); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to keep |
||
|
||
tag_item <- (embedded_engine / comment / inline_html / code / html_tag); | ||
tag_item <- (embedded_engine / inline_html / code / html_tag); | ||
|
||
inline_html <- &'<' text_content; | ||
|
||
|
@@ -83,22 +83,22 @@ braces <- '{' (braces / !'}' .)* '}'; | |
|
||
interpolation <- '#{' (string / string_with_interpolation / !'}' .)* '}'; | ||
|
||
comment <- html_comment / ie_comment / code_comment; | ||
text_item <- verbatim_text / html_comment / space? ie_comment / space? code_comment; | ||
|
||
html_comment <- '/!' space? (!eol .)*; | ||
verbatim_text <- indent:space? type:[|'] content:text_block; | ||
|
||
html_comment <- indent:space? type:'/!' content:text_block; | ||
|
||
ie_comment <- '/[' condition:(!']' .)+ ']' space? content:(!eol .)*; | ||
|
||
% TODO: support multi-line | ||
code_comment <- '/' (!eol .)*; | ||
code_comment <- '/' text_block; | ||
|
||
verbatim_text <- indent:space? type:[|'] space:space? content:verbatim_text_lines; | ||
verbatim_text_lines <- verbatim_text_line (crlf indent space:space lines:verbatim_text_nested_lines dedent)?; | ||
verbatim_text_nested_lines <- verbatim_text_line (crlf ( | ||
indent space:space lines:verbatim_text_nested_lines dedent / | ||
space:space lines:verbatim_text_nested_lines | ||
text_block <- text_block_line (crlf | ||
indent lines:text_block_nested_lines dedent)?; | ||
text_block_nested_lines <- text_block_line (crlf ( | ||
indent text_block_nested_lines dedent / text_block_nested_lines | ||
))*; | ||
verbatim_text_line <- dynamic:text_with_interpolation / simple:text / ''; | ||
text_block_line <- space? (dynamic:text_with_interpolation / simple:text); | ||
|
||
embedded_engine <- tag_name ':' ( | ||
crlf indent lines:embedded_engine_lines dedent / empty:('' &eol) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be use
declaration_indent
as variable name? An abbreviation is a bit confusing.