wip: refactor out translation into its own crate
This commit is contained in:
1 parent
4aac2cdae1
commit
14bc007c15
9 files changed
+153
-2
No files matched your search
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "rsx-rosetta"
|
||||
version = "0.0.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
dioxus-autofmt = { path = "../autofmt" }
|
||||
dioxus-rsx = { path = "../rsx" }
|
||||
html_parser = "0.6.3"
|
||||
proc-macro2 = "1.0.49"
|
||||
quote = "1.0.23"
|
||||
syn = { version = "1.0.107", features = ["full"] }
|
||||
thiserror = "1.0.38"
|
||||
|
||||
# [features]
|
||||
# default = ["html"]
|
||||
|
||||
# eventually more output options
|
||||
@@ -0,0 +1,19 @@
|
||||
# Rosetta for RSX
|
||||
---
|
||||
|
||||
Dioxus sports its own templating language inspired by C#/Kotlin/RTMP, etc. It's pretty straightforward.
|
||||
|
||||
However, it's NOT HTML. This is done since HTML is verbose and you'd need a dedicated LSP or IDE integration to get a good DX in .rs files.
|
||||
|
||||
RSX is simple... It's similar enough to regular Rust code to trick most IDEs into automatically providing support for things like block selections, folding, highlighting, etc.
|
||||
|
||||
To accomodate the transition from HTML to RSX, you might need to translate some existing code.
|
||||
|
||||
This library provids a central AST that can accept a number of inputs:
|
||||
|
||||
- HTML
|
||||
- Syn (todo)
|
||||
- Akama (todo)
|
||||
- Jinja (todo)
|
||||
|
||||
From there, you can convert directly to a string or into some other AST.
|
||||
@@ -0,0 +1,13 @@
|
||||
use html_parser::Dom;
|
||||
|
||||
fn main() {
|
||||
let html = "hello world!";
|
||||
|
||||
let dom = Dom::parse(html).unwrap();
|
||||
|
||||
let body = rsx_rosetta::convert_from_html(dom);
|
||||
|
||||
let out = dioxus_autofmt::write_block_out(body).unwrap();
|
||||
|
||||
dbg!(out);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
use dioxus_rsx::{BodyNode, CallBody, IfmtInput};
|
||||
use html_parser::{Dom, Node};
|
||||
use proc_macro2::Span;
|
||||
use syn::LitStr;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum ConvertError {}
|
||||
|
||||
pub fn convert_from_html(html: Dom) -> CallBody {
|
||||
let roots = html
|
||||
.children
|
||||
.into_iter()
|
||||
.map(|f| create_body_node_from_node(f))
|
||||
.filter_map(|f| f)
|
||||
.collect();
|
||||
|
||||
CallBody { roots }
|
||||
}
|
||||
|
||||
fn create_body_node_from_node(node: Node) -> Option<BodyNode> {
|
||||
let res = match node {
|
||||
Node::Text(text) => BodyNode::Text(IfmtInput {
|
||||
source: Some(LitStr::new(text.as_str(), Span::call_site())),
|
||||
segments: vec![],
|
||||
}),
|
||||
Node::Element(_) => todo!(),
|
||||
Node::Comment(_) => return None,
|
||||
};
|
||||
|
||||
Some(res)
|
||||
}
|
||||
Reference in new issue
Block a user