Skip to content

Commit

Permalink
Merge pull request #6 from Maaarcocr/main
Browse files Browse the repository at this point in the history
Use `Renderable` trait to print to html output
  • Loading branch information
Pitasi authored Oct 1, 2023
2 parents b0780e5 + b1822e2 commit eb954ed
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 11 deletions.
1 change: 0 additions & 1 deletion benches/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ fn horrorshow_many_dyn_attrs() -> String {
}

fn rscx_many_dyn_attrs() -> String {
use rscx::EscapeAttribute;
let value: i32 = 42;
rscx::html! {
<h1 attr1=value attr2=value attr3=value attr4=value attr5=value attr6=value attr7=value attr8=value attr9=value attr10=value attr11=value attr12=value attr13=value attr14=value attr15=value attr16=value attr17=value attr18=value attr19=value attr20=value attr21=value attr22=value attr23=value attr24=value attr25=value attr26=value attr27=value attr28=value attr29=value attr30=value attr31=value attr32=value attr33=value attr34=value attr35=value attr36=value attr37=value attr38=value attr39=value attr40=value attr41=value attr42=value attr43=value attr44=value attr45=value attr46=value attr47=value attr48=value attr49=value attr50=value attr51=value attr52=value attr53=value attr54=value attr55=value attr56=value attr57=value attr58=value attr59=value attr60=value attr61=value attr62=value attr63=value attr64=value attr65=value attr66=value attr67=value attr68=value attr69=value attr70=value attr71=value attr72=value attr73=value attr74=value attr75=value attr76=value attr77=value attr78=value attr79=value attr80=value attr81=value attr82=value attr83=value attr84=value attr85=value attr86=value attr87=value attr88=value attr89=value attr90=value attr91=value attr92=value attr93=value attr94=value attr95=value attr96=value attr97=value attr98=value attr99=value attr100=value attr101=value attr102=value attr103=value attr104=value attr105=value attr106=value attr107=value attr108=value attr109=value attr110=value attr111=value attr112=value attr113=value attr114=value attr115=value attr116=value attr117=value attr118=value attr119=value attr120=value attr121=value attr122=value attr123=value attr124=value attr125=value attr126=value attr127=value attr128=value attr129=value attr130=value attr131=value attr132=value attr133=value attr134=value attr135=value attr136=value attr137=value attr138=value attr139=value attr140=value attr141=value attr142=value attr143=value attr144=value attr145=value attr146=value attr147=value attr148=value attr149=value attr150=value attr151=value attr152=value attr153=value attr154=value attr155=value attr156=value attr157=value attr158=value attr159=value attr160=value attr161=value attr162=value attr163=value attr164=value attr165=value attr166=value attr167=value attr168=value attr169=value attr170=value attr171=value attr172=value attr173=value attr174=value attr175=value attr176=value attr177=value attr178=value attr179=value attr180=value attr181=value attr182=value attr183=value attr184=value attr185=value attr186=value attr187=value attr188=value attr189=value attr190=value attr191=value attr192=value attr193=value attr194=value attr195=value attr196=value attr197=value attr198=value attr199=value attr200=value attr201=value attr202=value attr203=value attr204=value attr205=value attr206=value attr207=value attr208=value attr209=value attr210=value attr211=value attr212=value attr213=value attr214=value attr215=value attr216=value attr217=value attr218=value attr219=value attr220=value attr221=value attr222=value attr223=value attr224=value attr225=value attr226=value attr227=value attr228=value attr229=value attr230=value attr231=value attr232=value attr233=value attr234=value attr235=value attr236=value attr237=value attr238=value attr239=value attr240=value attr241=value attr242=value attr243=value attr244=value attr245=value attr246=value attr247=value attr248=value attr249=value attr250=value attr251=value attr252=value attr253=value attr254=value attr255=value attr256=value attr257=value attr258=value attr259=value attr260=value attr261=value attr262=value attr263=value attr264=value attr265=value attr266=value attr267=value attr268=value attr269=value attr270=value attr271=value attr272=value attr273=value attr274=value attr275=value attr276=value attr277=value attr278=value attr279=value attr280=value attr281=value attr282=value attr283=value attr284=value attr285=value attr286=value attr287=value attr288=value attr289=value attr290=value attr291=value attr292=value attr293=value attr294=value attr295=value attr296=value attr297=value attr298=value attr299=value attr300=value attr301=value attr302=value attr303=value attr304=value attr305=value attr306=value attr307=value attr308=value attr309=value attr310=value attr311=value attr312=value attr313=value attr314=value attr315=value attr316=value attr317=value attr318=value attr319=value attr320=value attr321=value attr322=value attr323=value attr324=value attr325=value attr326=value attr327=value attr328=value attr329=value attr330=value attr331=value attr332=value attr333=value attr334=value attr335=value attr336=value attr337=value attr338=value attr339=value attr340=value attr341=value attr342=value attr343=value attr344=value attr345=value attr346=value attr347=value attr348=value attr349=value attr350=value attr351=value attr352=value attr353=value attr354=value attr355=value attr356=value attr357=value attr358=value attr359=value attr360=value attr361=value attr362=value attr363=value attr364=value attr365=value attr366=value attr367=value attr368=value attr369=value attr370=value attr371=value attr372=value attr373=value attr374=value attr375=value attr376=value attr377=value attr378=value attr379=value attr380=value attr381=value attr382=value attr383=value attr384=value attr385=value attr386=value attr387=value attr388=value attr389=value attr390=value attr391=value attr392=value attr393=value attr394=value attr395=value attr396=value attr397=value attr398=value attr399=value attr400=value attr401=value attr402=value attr403=value attr404=value attr405=value attr406=value attr407=value attr408=value attr409=value attr410=value attr411=value attr412=value attr413=value attr414=value attr415=value attr416=value attr417=value attr418=value attr419=value attr420=value attr421=value attr422=value attr423=value attr424=value attr425=value attr426=value attr427=value attr428=value attr429=value attr430=value attr431=value attr432=value attr433=value attr434=value attr435=value attr436=value attr437=value attr438=value attr439=value attr440=value attr441=value attr442=value attr443=value attr444=value attr445=value attr446=value attr447=value attr448=value attr449=value attr450=value attr451=value attr452=value attr453=value attr454=value attr455=value attr456=value attr457=value attr458=value attr459=value attr460=value attr461=value attr462=value attr463=value attr464=value attr465=value attr466=value attr467=value attr468=value attr469=value attr470=value attr471=value attr472=value attr473=value attr474=value attr475=value attr476=value attr477=value attr478=value attr479=value attr480=value attr481=value attr482=value attr483=value attr484=value attr485=value attr486=value attr487=value attr488=value attr489=value attr490=value attr491=value attr492=value attr493=value attr494=value attr495=value attr496=value attr497=value attr498=value attr499=value attr500=value attr501=value attr502=value attr503=value attr504=value attr505=value attr506=value attr507=value attr508=value attr509=value attr510=value attr511=value attr512=value attr513=value attr514=value attr515=value attr516=value attr517=value attr518=value attr519=value attr520=value attr521=value attr522=value attr523=value attr524=value attr525=value attr526=value attr527=value attr528=value attr529=value attr530=value attr531=value attr532=value attr533=value attr534=value attr535=value attr536=value attr537=value attr538=value attr539=value attr540=value attr541=value attr542=value attr543=value attr544=value attr545=value attr546=value attr547=value attr548=value attr549=value attr550=value attr551=value attr552=value attr553=value attr554=value attr555=value attr556=value attr557=value attr558=value attr559=value attr560=value attr561=value attr562=value attr563=value attr564=value attr565=value attr566=value attr567=value attr568=value attr569=value attr570=value attr571=value attr572=value attr573=value attr574=value attr575=value attr576=value attr577=value attr578=value attr579=value attr580=value attr581=value attr582=value attr583=value attr584=value attr585=value attr586=value attr587=value attr588=value attr589=value attr590=value attr591=value attr592=value attr593=value attr594=value attr595=value attr596=value attr597=value attr598=value attr599=value attr600=value attr601=value attr602=value attr603=value attr604=value attr605=value attr606=value attr607=value attr608=value attr609=value attr610=value attr611=value attr612=value attr613=value attr614=value attr615=value attr616=value attr617=value attr618=value attr619=value attr620=value attr621=value attr622=value attr623=value attr624=value attr625=value attr626=value attr627=value attr628=value attr629=value attr630=value attr631=value attr632=value attr633=value attr634=value attr635=value attr636=value attr637=value attr638=value attr639=value attr640=value attr641=value attr642=value attr643=value attr644=value attr645=value attr646=value attr647=value attr648=value attr649=value attr650=value attr651=value attr652=value attr653=value attr654=value attr655=value attr656=value attr657=value attr658=value attr659=value attr660=value attr661=value attr662=value attr663=value attr664=value attr665=value attr666=value attr667=value attr668=value attr669=value attr670=value attr671=value attr672=value attr673=value attr674=value attr675=value attr676=value attr677=value attr678=value attr679=value attr680=value attr681=value attr682=value attr683=value attr684=value attr685=value attr686=value attr687=value attr688=value attr689=value attr690=value attr691=value attr692=value attr693=value attr694=value attr695=value attr696=value attr697=value attr698=value attr699=value attr700=value attr701=value attr702=value attr703=value attr704=value attr705=value attr706=value attr707=value attr708=value attr709=value attr710=value attr711=value attr712=value attr713=value attr714=value attr715=value attr716=value attr717=value attr718=value attr719=value attr720=value attr721=value attr722=value attr723=value attr724=value attr725=value attr726=value attr727=value attr728=value attr729=value attr730=value attr731=value attr732=value attr733=value attr734=value attr735=value attr736=value attr737=value attr738=value attr739=value attr740=value attr741=value attr742=value attr743=value attr744=value attr745=value attr746=value attr747=value attr748=value attr749=value attr750=value attr751=value attr752=value attr753=value attr754=value attr755=value attr756=value attr757=value attr758=value attr759=value attr760=value attr761=value attr762=value attr763=value attr764=value attr765=value attr766=value attr767=value attr768=value attr769=value attr770=value attr771=value attr772=value attr773=value attr774=value attr775=value attr776=value attr777=value attr778=value attr779=value attr780=value attr781=value attr782=value attr783=value attr784=value attr785=value attr786=value attr787=value attr788=value attr789=value attr790=value attr791=value attr792=value attr793=value attr794=value attr795=value attr796=value attr797=value attr798=value attr799=value attr800=value attr801=value attr802=value attr803=value attr804=value attr805=value attr806=value attr807=value attr808=value attr809=value attr810=value attr811=value attr812=value attr813=value attr814=value attr815=value attr816=value attr817=value attr818=value attr819=value attr820=value attr821=value attr822=value attr823=value attr824=value attr825=value attr826=value attr827=value attr828=value attr829=value attr830=value attr831=value attr832=value attr833=value attr834=value attr835=value attr836=value attr837=value attr838=value attr839=value attr840=value attr841=value attr842=value attr843=value attr844=value attr845=value attr846=value attr847=value attr848=value attr849=value attr850=value attr851=value attr852=value attr853=value attr854=value attr855=value attr856=value attr857=value attr858=value attr859=value attr860=value attr861=value attr862=value attr863=value attr864=value attr865=value attr866=value attr867=value attr868=value attr869=value attr870=value attr871=value attr872=value attr873=value attr874=value attr875=value attr876=value attr877=value attr878=value attr879=value attr880=value attr881=value attr882=value attr883=value attr884=value attr885=value attr886=value attr887=value attr888=value attr889=value attr890=value attr891=value attr892=value attr893=value attr894=value attr895=value attr896=value attr897=value attr898=value attr899=value attr900=value attr901=value attr902=value attr903=value attr904=value attr905=value attr906=value attr907=value attr908=value attr909=value attr910=value attr911=value attr912=value attr913=value attr914=value attr915=value attr916=value attr917=value attr918=value attr919=value attr920=value attr921=value attr922=value attr923=value attr924=value attr925=value attr926=value attr927=value attr928=value attr929=value attr930=value attr931=value attr932=value attr933=value attr934=value attr935=value attr936=value attr937=value attr938=value attr939=value attr940=value attr941=value attr942=value attr943=value attr944=value attr945=value attr946=value attr947=value attr948=value attr949=value attr950=value attr951=value attr952=value attr953=value attr954=value attr955=value attr956=value attr957=value attr958=value attr959=value attr960=value attr961=value attr962=value attr963=value attr964=value attr965=value attr966=value attr967=value attr968=value attr969=value attr970=value attr971=value attr972=value attr973=value attr974=value attr975=value attr976=value attr977=value attr978=value attr979=value attr980=value attr981=value attr982=value attr983=value attr984=value attr985=value attr986=value attr987=value attr988=value attr989=value attr990=value attr991=value attr992=value attr993=value attr994=value attr995=value attr996=value attr997=value attr998=value attr999=value attr1000=value>
Expand Down
16 changes: 7 additions & 9 deletions rscx-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashSet;

use proc_macro::TokenStream;
use proc_macro2::{Literal, TokenTree};
use proc_macro2_diagnostics::Diagnostic;
use quote::{quote, quote_spanned, ToTokens};
use rstml::{
Expand Down Expand Up @@ -75,7 +74,7 @@ fn process_nodes<'n>(
#(#errors;)*
// Make sure that "enum x{};" and "let _x = crate::element;" can be used in this context
#(#docs;)*
format!(#html_string, #(#values),*)
format!(#html_string, #(rscx::FormatWrapper::new(#values)),*)
}
}
}
Expand Down Expand Up @@ -212,11 +211,7 @@ fn walk_nodes<'a>(nodes: &'a Vec<Node>) -> WalkNodesOutput<'a> {
out.static_format.push_str(&text.value_string());
}
Node::RawText(text) => {
out.static_format.push_str("{}");
let tokens = text.to_string_best();
let literal = Literal::string(&tokens);

out.values.push(TokenTree::from(literal).into());
out.static_format.push_str(&text.to_string_best());
}
Node::Fragment(fragment) => {
let other_output = walk_nodes(&fragment.children);
Expand All @@ -227,8 +222,10 @@ fn walk_nodes<'a>(nodes: &'a Vec<Node>) -> WalkNodesOutput<'a> {
out.values.push(comment.value.to_token_stream());
}
Node::Block(block) => {
let block = block.try_block().unwrap();
let stmts = &block.stmts;
out.static_format.push_str("{}");
out.values.push(block.to_token_stream());
out.values.push(quote!(#(#stmts)*));
}
}
}
Expand Down Expand Up @@ -277,7 +274,8 @@ fn walk_attribute(attribute: &KeyedAttribute) -> (String, Option<proc_macro2::To
static_format.push_str(r#"="{}""#);
format_value = Some(
quote! {{
(#value).escape_attribute()
// (#value).escape_attribute()
::rscx::EscapeAttribute::escape_attribute(&#value)
}}
.into_token_stream(),
);
Expand Down
27 changes: 27 additions & 0 deletions rscx/examples/with_option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use rscx::html;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = app().await;
println!("{}", app);
Ok(())
}

// simple function returning a String
// it will call the Items() function
async fn app() -> String {
let s = Some("ul { color: red; }");
let none: Option<&str> = None;
html! {
<!DOCTYPE html>
<html>
<head>
<style>{ s }</style>
</head>
<body>
{ none }
// call a component with no props
</body>
</html>
}
}
20 changes: 20 additions & 0 deletions rscx/src/format_wrapper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::fmt::{self, Display};

use crate::render::Render;

pub struct FormatWrapper<T> {
inner: T,
}

impl<T> FormatWrapper<T> {
pub fn new(inner: T) -> Self {
Self { inner }
}
}

impl<T: Render> Display for FormatWrapper<T> {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.render(f)
}
}
3 changes: 3 additions & 0 deletions rscx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub use attributes::*;
#[cfg(feature = "axum")]
pub mod axum;
pub mod context;
pub mod format_wrapper;
pub mod render;

pub extern crate rscx_macros;
use std::future::Future;
Expand All @@ -14,6 +16,7 @@ pub use rscx_macros::*;
pub extern crate typed_builder;

pub extern crate html_escape;
pub use format_wrapper::FormatWrapper;

use async_trait::async_trait;
use futures::future::join_all;
Expand Down
Loading

0 comments on commit eb954ed

Please sign in to comment.