Skip to content

Commit 5c170ad

Browse files
committed
basic, unformatted css
1 parent f526886 commit 5c170ad

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

html-node-macro/src/lib.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,32 @@
77

88
mod node_handlers;
99

10-
use std::collections::{HashMap, HashSet};
10+
use std::{
11+
collections::{HashMap, HashSet},
12+
os::linux::raw,
13+
};
1114

1215
use node_handlers::{
1316
handle_block, handle_comment, handle_doctype, handle_element, handle_fragment, handle_raw_text,
1417
handle_text,
1518
};
1619
use proc_macro::TokenStream;
17-
use proc_macro2::{Ident, TokenStream as TokenStream2};
20+
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
1821
use proc_macro2_diagnostics::Diagnostic;
19-
use quote::quote;
22+
use quote::{quote, quote_spanned};
2023
use rstml::{node::Node, Parser, ParserConfig};
21-
use syn::Type;
24+
use syn::{spanned::Spanned, Type};
2225

2326
#[proc_macro]
2427
pub fn html(tokens: TokenStream) -> TokenStream {
2528
html_inner(tokens.into(), None)
2629
}
2730

31+
#[proc_macro]
32+
pub fn style(tokens: TokenStream) -> TokenStream {
33+
style_inner(tokens.into())
34+
}
35+
2836
#[cfg(feature = "typed")]
2937
#[proc_macro]
3038
pub fn typed_html(tokens: TokenStream) -> TokenStream {
@@ -169,3 +177,26 @@ fn tokenize_nodes(
169177

170178
(token_streams, diagnostics)
171179
}
180+
181+
fn style_inner(tokens: TokenStream2) -> TokenStream {
182+
let span = tokens.span();
183+
let raw_css = tokens
184+
.into_iter()
185+
.map(|t| t.to_string().split_whitespace().collect::<String>())
186+
.collect::<String>();
187+
188+
quote_spanned! { span=>
189+
::html_node::Node::Element(
190+
::html_node::Element {
191+
name: ::std::convert::Into::<::std::string::String>::into("style"),
192+
attributes: ::std::vec::Vec::new(),
193+
children: ::std::option::Option::Some(
194+
::std::vec![
195+
::html_node::Node::UnsafeText(#raw_css.into())
196+
]
197+
)
198+
}
199+
)
200+
}
201+
.into()
202+
}

html-node-macro/src/node_handlers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::{HashMap, HashSet};
55

66
use proc_macro2::{Ident, Literal, TokenStream as TokenStream2};
77
use proc_macro2_diagnostics::{Diagnostic, SpanDiagnosticExt};
8-
use quote::{quote, ToTokens};
8+
use quote::{quote, quote_spanned, ToTokens};
99
use rstml::node::{
1010
KeyedAttribute, NodeAttribute, NodeBlock, NodeComment, NodeDoctype, NodeElement, NodeFragment,
1111
NodeName, NodeText, RawText,

html-node/examples/axum.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use axum::{extract::Query, routing::get, Router, Server};
7-
use html_node::{html, text, Node};
7+
use html_node::{html, style, text, Node};
88
use html_node_core::pretty::Pretty;
99

1010
#[tokio::main]
@@ -100,3 +100,18 @@ async fn pretty() -> Pretty {
100100
</div>
101101
}))
102102
}
103+
104+
async fn css() -> Pretty {
105+
Pretty(layout(html! {
106+
{ style! {
107+
html {
108+
margin: 0;
109+
padding: 0;
110+
height: 100vh;
111+
}
112+
} }
113+
<div>
114+
<h1>Pretty</h1>
115+
</div>
116+
}))
117+
}

html-node/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,7 @@ pub use html_node_core::{Comment, Doctype, Element, Fragment, Node, Text, Unsafe
125125
///
126126
/// See the [crate-level documentation](crate) for more information.
127127
pub use html_node_macro::html;
128+
/// CSS to [`Node`] macro.
129+
pub use html_node_macro::style;
128130

129131
pub use self::macros::*;

0 commit comments

Comments
 (0)