Skip to content

Commit

Permalink
Escape HTML characters in docs
Browse files Browse the repository at this point in the history
This commit escapes HTML characters in docs (<, >, &) so that descriptions display correctly.
  • Loading branch information
russcam authored and swallez committed Aug 21, 2024
1 parent 144f586 commit e36add6
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api_generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ array_tool = "1"
dialoguer = "0.11"
flate2 = "1"
globset = "0.4"
html-escape = "0.2"
indicatif = "0.17"
Inflector = "0.11"
itertools = "0.13"
Expand Down
4 changes: 4 additions & 0 deletions api_generator/src/generator/code_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ fn doc<I: Into<String>>(comment: I) -> syn::Attribute {
}
}

fn doc_escaped<S: ?Sized + AsRef<str>>(comment: &S) -> syn::Attribute {
doc(html_escape::encode_text(comment))
}

fn stability_doc(stability: Stability) -> Option<syn::Attribute> {
match stability {
Stability::Experimental => Some(doc(r#"&nbsp;
Expand Down
3 changes: 1 addition & 2 deletions api_generator/src/generator/code_gen/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ fn generate_param(tokens: &mut Tokens, e: &ApiEnum) {
})
.unzip();

let doc = e.description.as_ref().map(code_gen::doc);

let doc = e.description.as_ref().map(code_gen::doc_escaped);
let cfg_attr = e.stability.outer_cfg_attr();
let cfg_doc = stability_doc(e.stability);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl<'a> RequestBuilder<'a> {
let impl_ident = ident(&name);
let field_ident = ident(&name);
let doc_attr = match &f.1.description {
Some(docs) => vec![doc(docs)],
Some(docs) => vec![doc_escaped(docs)],
_ => vec![],
};

Expand Down
2 changes: 1 addition & 1 deletion api_generator/src/generator/code_gen/url/enum_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ mod tests {
methods: vec![HttpMethod::Get, HttpMethod::Post],
parts: {
let mut map = BTreeMap::new();
map.insert("index".to_string(), Type {
map.insert("index".to_string(), Type {i
ty: TypeKind::List,
description: Some("A comma-separated list of index names to search".to_string()),
options: vec![],
Expand Down
2 changes: 1 addition & 1 deletion elasticsearch/src/async_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ where
self.size = Some(size);
self
}
#[doc = "A comma-separated list of <field>:<direction> pairs"]
#[doc = "A comma-separated list of &lt;field&gt;:&lt;direction&gt; pairs"]
pub fn sort(mut self, sort: &'b [&'b str]) -> Self {
self.sort = Some(sort);
self
Expand Down
6 changes: 3 additions & 3 deletions elasticsearch/src/root/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ where
self.slices = Some(slices);
self
}
#[doc = "A comma-separated list of <field>:<direction> pairs"]
#[doc = "A comma-separated list of &lt;field&gt;:&lt;direction&gt; pairs"]
pub fn sort(mut self, sort: &'b [&'b str]) -> Self {
self.sort = Some(sort);
self
Expand Down Expand Up @@ -7275,7 +7275,7 @@ where
self.size = Some(size);
self
}
#[doc = "A comma-separated list of <field>:<direction> pairs"]
#[doc = "A comma-separated list of &lt;field&gt;:&lt;direction&gt; pairs"]
pub fn sort(mut self, sort: &'b [&'b str]) -> Self {
self.sort = Some(sort);
self
Expand Down Expand Up @@ -9217,7 +9217,7 @@ where
self.slices = Some(slices);
self
}
#[doc = "A comma-separated list of <field>:<direction> pairs"]
#[doc = "A comma-separated list of &lt;field&gt;:&lt;direction&gt; pairs"]
pub fn sort(mut self, sort: &'b [&'b str]) -> Self {
self.sort = Some(sort);
self
Expand Down

0 comments on commit e36add6

Please sign in to comment.