tailwind_ui 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f24853abac6cb65ef0f5d0146b614635a06f6c167df3df2691a37ff1fd53660
4
- data.tar.gz: ac9844710363c5c19bff6de70379a3005e82bf7c5b4cc94e066df583feaa2e31
3
+ metadata.gz: 1e4997c80b6251a4c702f08d220cdc8fa147c3455c06c9cd57ddabb2974a56f9
4
+ data.tar.gz: e029cb0d11008291b4e66c4665930d25cdf7284821924acb72fd76a5e0232f5e
5
5
  SHA512:
6
- metadata.gz: 842ffb6c6704387e25c25376d9ad9e1bf433e5a041604a14fe26a9d9ecf454f8b2c5ebd2d08695a619e1bd607743a9ccd4985819116e3a2c881d1bbe68ae97a7
7
- data.tar.gz: f34251b2529576443ddafc6471d32534cf410c4676d35fc35b70548353644a205609c5c65e9877f04ac140f89132a88f39cc8ebe50651e90e55f2f32263fd82b
6
+ metadata.gz: 53f7a781875243af0ea6103ad94819a9c930fc4d4e0cfa5e3ccb8afe3c905871ef8c6a0eae3fa55ebf6488b573161dcdd3cd4a062af95b94f1e0a99d1688204a
7
+ data.tar.gz: 026e78bdc383918a1a08ef231fd79a32552eb23d68983eb26733660401a86ce720a5800dee419ffbc827445b7bf123116ac5b2eac001edcbeffa331f655826eb
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.4.1
@@ -0,0 +1,6 @@
1
+ {
2
+ "rubyLsp.linters": [
3
+ "standard"
4
+ ],
5
+ "rubyLsp.formatter": "standard"
6
+ }
data/README.md CHANGED
@@ -1,24 +1,26 @@
1
- # TailwindUi
1
+ # tailwind_ui
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
4
-
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tailwind_ui`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This gem is for working with [Tailwind UI](https://tailwindui.com).
6
4
 
7
5
  ## Installation
8
6
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
-
11
7
  Install the gem and add to the application's Gemfile by executing:
12
8
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
9
+ $ bundle add tailwind_ui
14
10
 
15
11
  If bundler is not being used to manage dependencies, install the gem by executing:
16
12
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
13
+ $ gem install tailwind_ui
18
14
 
19
15
  ## Usage
20
16
 
21
- TODO: Write usage instructions here
17
+ First, copy the React (JSX) code from [Tailwind UI](https://tailwindui.com) to a local file.
18
+
19
+ Then use the `jsx_to_erb` command to convert it to ERB, for example:
20
+
21
+ `bundle exec jsx_to_erb simple.jsx > app/views/_simple.html.erb`
22
+
23
+ Only some basic components are currently supported. The plan is to gradually increase the coverage.
22
24
 
23
25
  ## Development
24
26
 
@@ -28,7 +30,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
28
30
 
29
31
  ## Contributing
30
32
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tailwind_ui.
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andyw8/tailwind_ui.
32
34
 
33
35
  ## License
34
36
 
data/Rakefile CHANGED
@@ -5,8 +5,6 @@ require "minitest/test_task"
5
5
 
6
6
  Minitest::TestTask.create
7
7
 
8
- require "rubocop/rake_task"
8
+ require "standard/rake"
9
9
 
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[test rubocop]
10
+ task default: %i[test standard]
data/exe/jsx_to_erb ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler"
4
+ Bundler.setup
5
+
6
+ require "tailwind_ui/jsx_to_erb"
7
+
8
+ puts TailwindUi::JsxToErb.from_path(ARGV[0]).full
@@ -0,0 +1,124 @@
1
+ require "erb"
2
+ require "nokogiri"
3
+
4
+ module TailwindUi
5
+ class Error < StandardError
6
+ end
7
+
8
+ class NotYetSupported < TailwindUi::Error
9
+ end
10
+
11
+ class UnconvertedBraces < TailwindUi::Error
12
+ end
13
+
14
+ class ClipPathNotYetSupported < TailwindUi::Error
15
+ end
16
+
17
+ class Special < TailwindUi::Error
18
+ end
19
+
20
+ class JsxToErb
21
+ def self.from_path(path)
22
+ file_contents = File.read(path)
23
+ new(file_contents)
24
+ end
25
+
26
+ def initialize(file_contents)
27
+ if file_contents.include?("This example requires updating your template")
28
+ raise Special
29
+ end
30
+
31
+ unless file_contents.lines.first.start_with?("export default function")
32
+ raise NotYetSupported
33
+ end
34
+
35
+ if file_contents.include?("clipPath")
36
+ raise ClipPathNotYetSupported
37
+ end
38
+
39
+ @file_contents = file_contents
40
+ end
41
+
42
+ def full
43
+ tags = @file_contents.match(/(?<tags> *<.*>)/m)[:tags]
44
+ result = without_indentation(tags)
45
+ result = handle_style_attributes(result)
46
+ result = with_jsx_keywords_updated(result)
47
+ result = convert_camelcase_attributes(result)
48
+ check_for_missed_camel_case_tags!(result)
49
+ result = handle_jsx_comments(result)
50
+ result = handle_brace_attributes(result)
51
+ result = handle_inner_braces(result)
52
+ raise UnconvertedBraces if result.include?("{") || result.include?("}")
53
+
54
+ # Parse the ERB to ensure it's valid
55
+ ERB.new(result).result
56
+
57
+ result
58
+ end
59
+
60
+ private
61
+
62
+ def convert_camelcase_attributes(markup)
63
+ result = markup
64
+ {
65
+ autoComplete: "autocomplete",
66
+ dateTime: "datetime",
67
+ defaultValue: "value",
68
+ fillOpacity: "fill-opacity",
69
+ gradientUnits: "gradient-units",
70
+ preserveAspectRatio: "preserve-aspect-ratio",
71
+ stopColor: "stop-color",
72
+ viewBox: "view-box"
73
+ }.each do |(camel_case, normal)|
74
+ result = result.gsub(camel_case.to_s, normal)
75
+ end
76
+ result
77
+ end
78
+
79
+ CAMEL_CASE = /^[a-zA-Z]+([A-Z][a-z]+)+$/
80
+
81
+ def check_for_missed_camel_case_tags!(str)
82
+ # Need to specify XML here to avoid Nokgiri downcasing automatically
83
+ doc = Nokogiri::XML.fragment(str)
84
+
85
+ doc.traverse do |node|
86
+ node.attributes.each do |name, value|
87
+ raise TailwindUi::Error, "found camelcase: #{name}" if name.match?(CAMEL_CASE)
88
+ end
89
+ end
90
+ end
91
+
92
+ def handle_jsx_comments(markup)
93
+ markup
94
+ .gsub("{/*", "<%#")
95
+ .gsub("*/}", "%>")
96
+ end
97
+
98
+ def handle_brace_attributes(markup)
99
+ markup
100
+ .gsub(/(\w+)=\{(\d+)}/m, '\1="\2"')
101
+ end
102
+
103
+ def handle_style_attributes(markup)
104
+ markup.gsub(/style={{ (.*) }}/, 'style="\1"')
105
+ end
106
+
107
+ def handle_inner_braces(markup)
108
+ markup.gsub("{", "<%= ").gsub("}", " %>")
109
+ end
110
+
111
+ def with_jsx_keywords_updated(markup)
112
+ markup
113
+ .gsub(/className="([^"]+)"/, 'class="\1"')
114
+ .gsub(/htmlFor="([^"]+)"/, 'for="\1"')
115
+ end
116
+
117
+ def without_indentation(tags)
118
+ indentation_level = tags.index("<")
119
+ tags.lines.map do |line|
120
+ line[indentation_level..]
121
+ end.join + "\n"
122
+ end
123
+ end
124
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TailwindUi
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tailwind_ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Waite
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-12-23 00:00:00.000000000 Z
10
+ date: 2024-12-29 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: nokogiri
@@ -24,19 +23,22 @@ dependencies:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
25
  version: '1.17'
27
- description:
28
26
  email:
29
27
  - 13400+andyw8@users.noreply.github.com
30
- executables: []
28
+ executables:
29
+ - jsx_to_erb
31
30
  extensions: []
32
31
  extra_rdoc_files: []
33
32
  files:
34
- - ".rubocop.yml"
33
+ - ".ruby-version"
34
+ - ".vscode/settings.json"
35
35
  - CHANGELOG.md
36
36
  - LICENSE.txt
37
37
  - README.md
38
38
  - Rakefile
39
+ - exe/jsx_to_erb
39
40
  - lib/tailwind_ui.rb
41
+ - lib/tailwind_ui/jsx_to_erb.rb
40
42
  - lib/tailwind_ui/version.rb
41
43
  - sig/tailwind_ui.rbs
42
44
  homepage: https://github.com/andyw8/tailwind_ui
@@ -46,7 +48,6 @@ metadata:
46
48
  allowed_push_host: https://rubygems.org
47
49
  homepage_uri: https://github.com/andyw8/tailwind_ui
48
50
  source_code_uri: https://github.com/andyw8/tailwind_ui
49
- post_install_message:
50
51
  rdoc_options: []
51
52
  require_paths:
52
53
  - lib
@@ -61,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  - !ruby/object:Gem::Version
62
63
  version: '0'
63
64
  requirements: []
64
- rubygems_version: 3.5.3
65
- signing_key:
65
+ rubygems_version: 3.6.2
66
66
  specification_version: 4
67
67
  summary: Tools for working with Tailwind UI
68
68
  test_files: []
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- TargetRubyVersion: 3.0
3
-
4
- Style/StringLiterals:
5
- EnforcedStyle: double_quotes
6
-
7
- Style/StringLiteralsInInterpolation:
8
- EnforcedStyle: double_quotes