Skip to content

Commit 2fce307

Browse files
authored
Merge pull request #10 from wechat-miniprogram/feat-better-style-properties-docs
feat: better style properties docs
2 parents b1fdcc4 + a5cd4ed commit 2fce307

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

float-pigment-css-macro/src/property_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ impl ToTokens for PropertiesDefinition {
532532
cur_index += 1;
533533
let invalid_ident =
534534
Ident::new(&format!("Invalid{:X}", cur_index), enum_name.span());
535-
enum_fields.push(quote!(#[serde(rename = "_")] #invalid_ident));
535+
enum_fields.push(quote!(#[doc(hidden)] #[serde(rename = "_")] #invalid_ident));
536536
}
537537
cur_index += 1;
538538
enum_fields.push(quote! {

float-pigment-css-macro/src/style_syntax.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use proc_macro2::TokenStream;
22
use quote::*;
33
use rustc_hash::FxHashMap;
4+
use std::fmt::Write;
45
use std::ops::RangeInclusive;
56
use syn::parse::*;
67
use syn::punctuated::Punctuated;
@@ -737,10 +738,14 @@ impl Parse for StyleSyntaxDefinition {
737738
impl ToTokens for StyleSyntaxDefinition {
738739
fn to_tokens(&self, tokens: &mut TokenStream) {
739740
let Self { trait_name, items } = self;
741+
742+
// the parser functions
740743
let item_fn_list: Vec<_> = items.iter().map(|x| quote! { #x }).collect();
741744
tokens.append_all(quote! {
742745
#(#item_fn_list)*
743746
});
747+
748+
// the value parser mapping
744749
let map: Vec<_> = items
745750
.iter()
746751
.map(|item| {
@@ -846,6 +851,60 @@ impl ToTokens for StyleSyntaxDefinition {
846851
Ok(false)
847852
}
848853
});
854+
855+
// the supported property list
856+
let mut supported_properties: Vec<_> = items.iter().filter(|x| x.name.is_some()).collect();
857+
supported_properties.sort_by(|a, b| {
858+
let a = a.name.as_ref().unwrap();
859+
let b = b.name.as_ref().unwrap();
860+
a.cmp(b)
861+
});
862+
let supported_property_count = supported_properties.len();
863+
let supported_property_names = supported_properties.iter().map(|x| x.name.as_ref().unwrap());
864+
let mut style_doc = String::new();
865+
writeln!(&mut style_doc, "The supported CSS property names.\n").unwrap();
866+
writeln!(&mut style_doc, "This list is sorted, so it is safe to do binary search on it.\n").unwrap();
867+
writeln!(&mut style_doc, "Note that this is just a simple list of basic parsing rules.\n").unwrap();
868+
writeln!(&mut style_doc, "* Some properties in this list are shorthand properties that cannot be found in the [Property] enum.").unwrap();
869+
writeln!(&mut style_doc, "* Parsing rules of some properties are slightly different from the web standard.").unwrap();
870+
writeln!(&mut style_doc, "\nSee the table below for more information about all supported properties.\n").unwrap();
871+
writeln!(&mut style_doc, "| Property Name | Related Property Variant | Major Value Options |").unwrap();
872+
writeln!(&mut style_doc, "| ---- | ---- | ---- |").unwrap();
873+
let table_list_a = supported_properties
874+
.iter()
875+
.filter(|x| !x.name.as_ref().unwrap().starts_with("-"));
876+
let table_list_b = supported_properties
877+
.iter()
878+
.filter(|x| x.name.as_ref().unwrap().starts_with("-"));
879+
for x in table_list_a.chain(table_list_b) {
880+
let name = x.name.as_ref().unwrap();
881+
let non_standard = name.starts_with("-");
882+
let name_col = if non_standard { format!("*`{}`*", name) } else { format!("`{}`", name) };
883+
let mut doc_col = String::new();
884+
let mut options_col = vec![];
885+
if let StyleSyntaxValueItem::Assign(variant, v) = &x.value {
886+
doc_col = format!("[Property::{}]", variant);
887+
if let StyleSyntaxValueItem::Branch(branches) = &**v {
888+
for item in branches {
889+
if let StyleSyntaxValueItem::Convert(v, _) = item {
890+
if let StyleSyntaxValueItem::MatchIdent(s) = &**v {
891+
options_col.push(format!("`{}`", s));
892+
}
893+
}
894+
}
895+
}
896+
}
897+
options_col.sort();
898+
writeln!(&mut style_doc, "| {} | {} | {} |", name_col, doc_col, options_col.join("<br>")).unwrap();
899+
}
900+
tokens.append_all(quote! {
901+
#[doc = #style_doc]
902+
pub const SUPPORTED_CSS_PROPERTY_NAMES: [&'static str; #supported_property_count] = [
903+
#(
904+
#supported_property_names,
905+
)*
906+
];
907+
});
849908
}
850909
}
851910

float-pigment-css-macro/src/value_type.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ impl Parse for PropertyValueType {
3939
Unset,
4040
Var(Box<StrRef>),
4141
VarInShorthand(Box<StrRef>, Box<StrRef>),
42-
Invalid0,
42+
#[doc(hidden)] Invalid0,
4343
}
4444
"#;
4545
let mut new_variants = parse_str::<ItemEnum>(s)?.variants;
4646
for i in new_variants.len()..PRESERVE_GLOBAL_VALUE_RANGE {
4747
let mut empty_slot = new_variants.last().unwrap().clone();
4848
empty_slot.ident = Ident::new(&format!("Invalid{:X}", i), empty_slot.ident.span());
49+
empty_slot.attrs.push(parse_quote!(#[doc(hidden)]));
4950
new_variants.push(empty_slot);
5051
}
5152
new_variants

float-pigment-css/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
//! 1. Merge the `MatchedRuleList` into `NodeProperties` with `MatchedRuleList::merge_node_properties`.
1515
//!
1616
//! The result `NodeProperties` contains all supported CSS properties.
17+
//!
18+
//! ### Supported CSS Features
19+
//!
20+
//! The supported selectors can be found in [StyleQuery] docs.
21+
//!
22+
//! The supported media features can be found in [MediaQueryStatus] docs.
23+
//!
24+
//! The supported style properties can be found in [SUPPORTED_CSS_PROPERTY_NAMES](crate::property::SUPPORTED_CSS_PROPERTY_NAMES) docs.
1725
//!
1826
//! ### The Binary Format
1927
//!

0 commit comments

Comments
 (0)