SwiftUI to LiveViewNative Converter

View Source

Transform SwiftUI code into LiveViewNative templates with ease.

Installation

The package can be installed by adding swiftui_2_lvn to your list of dependencies in mix.exs:

def deps do
  [
    {:swiftui_2_lvn, "~> 0.1.0"}
  ]
end

Usage

Basic Conversion

# Convert simple SwiftUI code to LiveViewNative template
Swiftui2Lvn.convert(~s|Text("Hello World")|)
# => "<Text>Hello World</Text>"

# Convert with attributes
Swiftui2Lvn.convert(~s|VStack(alignment: .leading)|)
# => <VStack alignment=".leading" />

# Convert with modifiers
Swiftui2Lvn.convert(~s|Text("Hello").font(.title).padding()|)
# => <Text style={["font(.title)", "padding()"]}>Hello</Text>

Complex Examples

# Nested elements
swift_code = ~s|
VStack {
  Text("One")
  Text("Two")
}
|

Swiftui2Lvn.convert(swift_code)
# => "<VStack>\n  <Text>One</Text>\n  \n  <Text>Two</Text>\n</VStack>\n""

# Elements with complex modifiers
swift_code = ~s|Text("Hello").font(Font.system(size: 18).weight(.medium))|
Swiftui2Lvn.convert(swift_code)
# => <Text style={["font(Font.system(size:18).weight(.medium))"]}>Hello</Text>

Working with AST

If you need to inspect or modify the AST before converting to the final template:

ast = Swiftui2Lvn.to_ast(~s|Text("Hello")|)
# Returns the AST structure that can be modified before printing

Features

  • SwiftUI Parsing: Parses SwiftUI code using the Swift AST
  • Template Generation: Converts SwiftUI elements to LiveViewNative syntax
  • Modifier Support: Handles SwiftUI modifiers like .font(), .padding(), etc.
  • Nested Elements: Supports complex nested view structures
  • Attribute Mapping: Converts SwiftUI parameters to LiveViewNative attributes

Architecture

The library is composed of several modules:

Development

Running Tests

mix test

Documentation

mix docs

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built with ex_swift_parser for Swift code parsing
  • Inspired by the need to simplify LiveViewNative development workflows