-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2c0432
commit 6f6ef25
Showing
31 changed files
with
728 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module Fixtures | ||
module Compilation | ||
module MethodAddBlock | ||
class WithBareString < Phlex::View | ||
def template | ||
h1 { "Hello" } | ||
end | ||
end | ||
|
||
class WithSymbol < Phlex::View | ||
def template | ||
h1 { :hello } | ||
end | ||
end | ||
|
||
class WithFloat < Phlex::View | ||
def template | ||
h1 { 1.2 } | ||
end | ||
end | ||
|
||
class WithInteger < Phlex::View | ||
def template | ||
h1 { 1 } | ||
end | ||
end | ||
|
||
class WithVariable < Phlex::View | ||
def template | ||
greeting = "Hello" | ||
h1 { greeting } | ||
end | ||
end | ||
|
||
class WithInstanceVariable < Phlex::View | ||
def template | ||
h1 { @hello } | ||
end | ||
end | ||
|
||
class WithNestedTags < Phlex::View | ||
def template | ||
article { | ||
h1 { "Inside" } | ||
} | ||
end | ||
end | ||
|
||
class WithArguments < Phlex::View | ||
def template | ||
h1(class: "hello") { "Hello" } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module Phlex::Compiler::Elements | ||
module VCall | ||
def format(formatter) | ||
Phlex::Compiler::Generators::Element.new( | ||
Phlex::Compiler::Nodes::VCall.new(self), | ||
formatter: formatter | ||
).call | ||
end | ||
end | ||
|
||
module FVall | ||
def format(formatter) | ||
Phlex::Compiler::Generators::Element.new( | ||
Phlex::Compiler::Nodes::FVall.new(self), | ||
formatter: formatter | ||
).call | ||
end | ||
end | ||
|
||
module Command | ||
def format(formatter) | ||
Phlex::Compiler::Generators::Element.new( | ||
Phlex::Compiler::Nodes::Command.new(self), | ||
formatter: formatter | ||
).call | ||
end | ||
end | ||
|
||
module MutatingMethodAddBlock | ||
def format(formatter) | ||
Phlex::Compiler::Generators::Element.new( | ||
Phlex::Compiler::Nodes::MethodAddBlock.new(self), | ||
formatter: formatter, | ||
mutating: true | ||
).call | ||
end | ||
end | ||
|
||
module MethodAddBlock | ||
def format(formatter) | ||
Phlex::Compiler::Generators::Element.new( | ||
Phlex::Compiler::Nodes::MethodAddBlock.new(self), | ||
formatter: formatter | ||
).call | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# frozen_string_literal: true | ||
|
||
module Phlex | ||
class Compiler | ||
module Generators | ||
class Content | ||
def initialize(formatter, content:, mutating: false) | ||
@formatter = formatter | ||
@content = content | ||
@mutating = mutating | ||
end | ||
|
||
def call | ||
return if nil_value? | ||
return bare_string_value if bare_string_value? | ||
return symbol_value if symbol_value? | ||
return numeric_value if numeric_value? | ||
return variable_value if variable_value? | ||
|
||
unknown_value | ||
end | ||
|
||
private | ||
|
||
def nil_value? | ||
@content in SyntaxTree::VarRef[value: SyntaxTree::Kw[value: "nil"]] | ||
end | ||
|
||
def bare_string_value? | ||
@content in SyntaxTree::StringLiteral[ | ||
parts: [ | ||
SyntaxTree::TStringContent | ||
] | ||
] | ||
end | ||
|
||
def symbol_value? | ||
@content in SyntaxTree::SymbolLiteral | ||
end | ||
|
||
def numeric_value? | ||
@content in SyntaxTree::Int | SyntaxTree::FloatLiteral | ||
end | ||
|
||
def variable_value? | ||
@content in SyntaxTree::VarRef | ||
end | ||
|
||
def bare_string_value | ||
@formatter.append do |f| | ||
f.text CGI.escape_html( | ||
@content.parts.first.value | ||
) | ||
end | ||
end | ||
|
||
def symbol_value | ||
@formatter.append do |f| | ||
f.text CGI.escape_html( | ||
@content.value.value | ||
) | ||
end | ||
end | ||
|
||
def numeric_value | ||
@formatter.append do |f| | ||
f.text CGI.escape_html( | ||
@content.value | ||
) | ||
end | ||
end | ||
|
||
def variable_value | ||
@formatter.chain_append do |f| | ||
f.text "CGI.escape_html(" | ||
@content.format(f) | ||
f.text ")" | ||
end | ||
end | ||
|
||
def unknown_value | ||
@formatter.breakable(force: true) | ||
if @mutating | ||
@content.format(@formatter) | ||
else | ||
@formatter.text "yield_content {" | ||
@formatter.breakable(force: true) | ||
@content.format(@formatter) | ||
@formatter.breakable(force: true) | ||
@formatter.text "}" | ||
end | ||
@formatter.breakable(force: true) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module Phlex | ||
class Compiler | ||
module Generators | ||
class Element | ||
def initialize(node, formatter:, mutating: false) | ||
@node = node | ||
@formatter = formatter | ||
@mutating = mutating | ||
end | ||
|
||
def call | ||
@formatter.append do |f| | ||
f.text "<" | ||
f.text tag | ||
end | ||
|
||
if @node.arguments&.parts&.any? | ||
@formatter.chain_append do |f| | ||
f.text "fetch_attrs(" | ||
@node.arguments.format(@formatter) | ||
f.text ")" | ||
end | ||
end | ||
|
||
@formatter.append do |f| | ||
f.text ">" | ||
end | ||
|
||
return if void? | ||
|
||
if @node.content | ||
if @node.content in SyntaxTree::Statements[body: [c]] | ||
Content.new(@formatter, content: c, mutating: @mutating).call | ||
else | ||
@node.content.format(@formatter) | ||
end | ||
end | ||
|
||
@formatter.append do |f| | ||
f.text "</" | ||
f.text tag | ||
f.text ">" | ||
end | ||
end | ||
|
||
private | ||
|
||
def tag | ||
HTML::STANDARD_ELEMENTS[@node.name] || HTML::VOID_ELEMENTS[@node.name] | ||
end | ||
|
||
def void? | ||
HTML::VOID_ELEMENTS.key?(@node.name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.