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 committed Nov 19, 2022
1 parent e39a27e commit 7dc8014
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
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.0.3"
dialoguer = "0.3.0"
flate2 = "~1"
globset = "~0.4"
html-escape = "0.2.11"
Inflector = "0.11.4"
indicatif = "0.12.0"
itertools = "0.10.0"
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
9 changes: 4 additions & 5 deletions api_generator/src/generator/code_gen/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ fn generate_param(tokens: &mut Tokens, e: &ApiEnum) {
})
.unzip();

let doc = match &e.description {
Some(description) => Some(code_gen::doc(description)),
None => None,
};

let doc = e
.description
.as_ref()
.map(|description| code_gen::doc_escaped(description));
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 elasticsearch/src/async_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,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 @@ -1574,7 +1574,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 @@ -7088,7 +7088,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 @@ -9028,7 +9028,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 7dc8014

Please sign in to comment.