marcoroth / phlexing Goto Github PK
View Code? Open in Web Editor NEWSimple ERB to Phlex converter
Home Page: https://phlexing.fun
Simple ERB to Phlex converter
Home Page: https://phlexing.fun
ERB Input:
<% if some_condition? %>
<%= "Text" %>
<% end %>
Output:
class Component < Phlex::HTML
def template
if some_condition?
text "Text"
end
end
end
Expected output:
class Component < Phlex::HTML
def template
if some_condition?
text "Text"
end
end
private
# TODO: Implement the `some_condition?` method
def some_condition?
super
end
end
ERB Input:
<style type="text/css">
<%= Rouge::Themes::Monokai.render(scope: 'pre') %>
</style>
Output:
style(type: "text/css") do
"<erb loud> Rouge::Themes::Monokai.render(scope: 'pre') </erb>"
end
Expected output:
style(type: "text/css") do
Rouge::Themes::Monokai.render(scope: 'pre')
end
ERB Input:
<%= something do %>
Content
<% end %>
Output:
plain something do
plain " Content"
end
Expected output:
something do
plain " Content"
end
@adamlogic experienced this in the GoRails Phlex episode: https://youtu.be/l4bQSfqZZfQ&t=1267
I'm not sure if this is actually solvable. Since the ERB is using <%=
we expect to output the return value of the method call. If the ERB is just <%
the output works as expected. Like:
<% something do %>
Content
<% end %>
Outputs:
something { plain " Content" }
ERB Input:
<div style="<%= "background: red;" %><%= "display: block;" %>"></div>
Output:
div(style: ("background: red;" %><%=), block: %(), erb: %(), display:: %()) { %(">)
}
Expected output:
div(style: %(#{"background: red;"}#{"display: block"}))
Sorry, you don't have a bug template, so I'm freestyling.
Issue
When clicking the click->converter#copy
button, what is copied to the clipboard is output from a previous ERB conversion, not the one that is currently in the box. This happens 100% of the time for me since it started previously.
I'm guessing it might have something to do with either not having a component name, or the text input not being valid but the output is still generated, and then it gets "stuck" or something in Turbo. Refreshing the page resolves the issue.
Potential Solution
I noticed in the source that it's copying from output-copy
, a second buffer of the text in the pre#output
field.
You surely know better than me since you made it, but that seems like it might be unnecessary. The innerText
of the pre
element returns the literal text that is visible to the user, which would prevent any mismatch between what is seen and what is copied.
- await navigator.clipboard.writeText(document.getElementById("output-copy").value)
+ await navigator.clipboard.writeText(document.getElementById("output").innerText)
ERB Input:
<div style="background: <%= blue? ? "blue" : "red" %>"></div>
Output:
div(style: %(FIXME: background: <%= blue? ? "blue" : "red"))
Expected output:
div(style: %(background: #{blue? ? "blue" : "red"}))
ERB Input:
<header>Hello</header>
Output:
Expected output:
header { "Hello" }
Probably related to this regex:
phlexing/gem/lib/phlexing/parser.rb
Line 16 in a6d076f
In #52 while moving the test suite over to the gem the test in comments_test.rb
started to fail. I don't fully understand why, but html_press
does remove the HTML comments while minifing in the gem context, it didn't do that in the context of the Rails app.
Since the html_press
is deprecated we should anyway be looking for an alternative.
ERB Input:
<HTML>
<HEAD></HEAD>
<BODY></BODY>
</HTML>
Output:
body
Expected output:
html do
head
body
end
Using Phlexing::Converter.idents
we should be able to figure out if the HTML is depending on any Rails helper within the template. If that's the case we could automatically include the right module, delegate the call to the helpers
object Phlex provides or we could rewrite the Ruby code that we output
https://phlexing.fun doesn't appear to be up right now
Is your feature request related to a problem? Please describe.
It would be cool to have a proper editor on the left side to edit the ERB.
Describe the solution you'd like
An integration of the Monaco editor (VSCode) would be neat.
https://microsoft.github.io/monaco-editor/
Additional context
-
ERB Input:
<div><%= tag.div('Hello world!') %>Text</div>
Output:
div do
text(tag.div("Hello world!"))
text "Text"
end
Expected output:
div do
tag.div("Hello world!")
text "Text"
end
or ideally:
div do
div { "Hello world!" }
text "Text"
end
ERB Input:
<%= form_with(model: @test) do |form| %>
<h3>Test</h3>
<% end %>
Output:
class Test < Phlex::HTML
def view_template
end
end
Expected output:
class Test < Phlex::HTML
def view_template
form_with(model: @test) do |form|
h3 { "Test" }
end
end
It seems the form_wth
line is the issue.
I'm converting some Tailwind UI components with Phlexing and noticed that I get a lot of unnecessary whitespace
rows in my code. I've narrowed it down a bit and it seems to happen only for certain tags. Here's an example:
<div>
<label>Test</label>
</div>
converts into:
div do
whitespace
label { "Test" }
end
If you change label
to div
or h1
it does not add the whitespace
.
I've narrowed the cause down to the call to HtmlPress
here, which leaves a space between the tags:
phlexing/gem/lib/phlexing/converter.rb
Line 171 in 60df23a
irb(main):001:0" html = <<~HTML.strip
irb(main):002:0" <div>
irb(main):003:0" <label>Test</label>
irb(main):004:0" </div>
irb(main):005:0> HTML
=> "<div>\n <label>Test</label>\n</div>"
irb(main):006:0> HtmlPress.press(html)
=> "<div> <label>Test</label></div>"
^ extra space here
irb(main):007:0" html = <<~HTML.strip
irb(main):008:0" <div>
irb(main):009:0" <h1>Test</h1>
irb(main):010:0" </div>
irb(main):011:0> HTML
=> "<div>\n <h1>Test</h1>\n</div>"
irb(main):012:0> HtmlPress.press(html)
=> "<div><h1>Test</h1></div>"
^ no space here
It seems that HtmlPress is unmaintained and marked deprecated by the owner (stereobooster/html_press#21), so I don't really expect them to fix this or accept a fix.
Not sure what a good approach would be here, what are your thoughts? Happy to work on some kind of fix 👍
ERB Input:
<%= tag.p do %>
<div>Text</div>
<% end %>
Output:
text tag.p do
div { "Text" }
end
Expected output:
tag.p do
div { "Text" }
end
or ideally:
p do
div { "Text" }
end
ERB Input:
<head></head>
<body></body>
Output:
html { body }
Expected output:
html do
head
body
end
Input
<%= render SomeView.new %>
Hello
Output
text render SomeView.new
text "Hello"
Expected output
render SomeView.new
text "Hello"
Input:
<div class="<%= classes_helper %>" id="<%= many? ? "posts" : "post" %>"></div>
Output:
div(class: (classes_helper), id: (many? ? "posts" : "post"))
Expected output:
div(class: classes_helper, id: (many? ? "posts" : "post"))
ERB Input:
<div style="<%= one %><%= two %>"></div>
Output:
div(style: (one %><%= two))
}
Expected output:
div(style: %(#{one}#{two}))
ERB Input:
<div class="<%= @hello %>">Hello</div>
Output:
class Component < Phlex::HTML
def template
div(class: (@hello)) { "Hello" }
end
end
Expected output:
class Component < Phlex::HTML
def initialize(hello:)
@hello = hello
end
def template
div(class: (@hello)) { "Hello" }
end
end
ERB Input:
<head></head>
Output:
None, converter crashes with 500
Expected output:
head
ERB Input:
<button @click.prevent="something">Button</button>
Output:
class ButtonComponent < Phlex::HTML
def template
button(click.: true, prevent: %(something)) { %(Button)
}
end
end
Expected output:
class ButtonComponent < Phlex::HTML
def template
button("@click.prevent": %(something)) { %(Button) }
end
end
@adamlogic experienced this in the GoRails Phlex episode: https://youtu.be/l4bQSfqZZfQ&t=975
Is your feature request related to a problem? Please describe.
Phlexing can't tell when to leave out the text
method call on methods that render
Expected:
render SomeComponent.new do |c|
c.section { "hello world" }
end
Not sure what the solution might be. Phlexing obviously doesn't know if c.section
is rendering, capturing for Phlex::DeferredRender
or returning a String
.
ERB Input:
<html>
<head></head>
<body></body>
</html>
Output:
html do
head { meta(http_equiv: "Content-Type", content: "text/html; charset=UTF-8") }
body
end
Expected output:
html do
head
body
end
Even while it's encouraged to use Kebab-case for HTML-tags there are some use-cases where we have case-sensitive HTML elements, like in SVG (see #139). This would also allow to cleanup the special-handling we have for handling SVG elements in:
Since we are using Nokogiri::HTML4::DocumentFragment
for parsing the source it automatically casts all tag names to lowercase. While Nokogiri::HTML5::DocumentFragment
would support case-sensitive tags, it doesn't support other things like properly detecting boolean attributes (see #119 and #120).
ERB Input:
<customElement>
<anotherElement/>
</customElement>
Output:
class CustomelementComponent < Phlex::HTML
register_element :anotherelement
register_element :customelement
def template
customelement { anotherelement }
end
end
Expected output:
class CustomelementComponent < Phlex::HTML
register_element :anotherElement
register_element :customElement
def template
customElement { anotherElement }
end
end
ERB Input:
<%= t(".some.string") %> Text
Output:
text t(".some.string")
text " Text"
Expected output:
t(".some.string")
text " Text"
ERB Input:
<%= translate(".some.string") %> Text
Output:
text translate(".some.string")
text " Text"
Expected output:
translate(".some.string")
text " Text"
ERB Input:
<div class="<%= classes_helper %>">Text</div>
Output:
div(class: "<erb interpolated=", true: "") { ' classes_helper ">Text' }
Expected output:
div(class: classes_helper) { "Text" }
Let's say we have this:
<%= tag.div class: something? ? "class-1" : "class-2" do %>
<%= content_tag :span, something.text %>
<% end %>
Which would generate this through phlexing today:
class SomethingComponent < Phlex::HTML
include Phlex::Rails::Helpers::ContentTag
include Phlex::Rails::Helpers::Tag
attr_accessor :something
def initialize(something:)
@something = something
end
def template
tag.div class: something? ? "class-1" : "class-2" do
content_tag :span, something.text
end
end
private
def something?(*args, **kwargs)
# TODO: Implement me
end
end
But since this is a regular Ruby class now we could write any RuboCop rule (or maybe even a SyntaxTree mutation visitor) which statically analyses the code and auto-fixes it (we could even make the rules toggleable from the UI)
For example some rules could be:
tag.div
as div { ... }
content_tag :span
to span { ... }
something
inside def template
to @something
and remove the attr_accessor
for itclass: something? ? "class-1" : "class-2"
to **classes(something?: { then: "class-1", else: "class-2" })
We could even release these rules standalone as rubocop-phlex
which people could use in their project independent of phlexing
ERB Input:
<input required>
Output:
input(required: "")
Expected output:
input(required: true)
ERB Input:
<style>
body { background: black }
</style>
Output:
style { "body{background:#000}" }
Expected output:
style { "body { background: #000 }" }
or
style { "body { background: black }" }
ERB Input:
<svg>
<feSpecularLighting>
<fePointLight/>
</feSpecularLighting>
</svg>
Output:
svg { fespecularlighting { fepointlight } }
Expected output:
# `el` is just a random name
svg { |el| el.feSpecularLighting { el.fePointLight } }
# or
svg do |el|
el.feSpecularLighting do
el.fePointLight
end
end
There are two problems:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.