Skip to content

Commit 54a7f49

Browse files
bors[bot]pellico
andauthored
Merge #711
711: Escape <> and & symbol in doc attributes. r=Emilgardis a=pellico Close #710 The fix escapes only very basic chars <> and & to be as much less intrusive. The fix could be implement without a new dependencies but more escaping may be required in the near future. Co-authored-by: pellico <[email protected]>
2 parents f1295c4 + 513f692 commit 54a7f49

File tree

8 files changed

+23
-14
lines changed

8 files changed

+23
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
- { rust: stable, vendor: Toshiba, options: all }
7979
- { rust: stable, vendor: Toshiba, options: "" }
8080
# Test MSRV
81-
- { rust: 1.64.0, vendor: Nordic, options: "" }
81+
- { rust: 1.65.0, vendor: Nordic, options: "" }
8282
# Use nightly for architectures which don't support stable
8383
- { rust: nightly, vendor: MSP430, options: "--atomics" }
8484
- { rust: nightly, vendor: MSP430, options: "" }

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
77

88
## [Unreleased]
99

10-
- Bump MSRV to 1.64
10+
- Bump MSRV to 1.65
1111
- Fix dangling implicit derives
12+
- Fix escaping <> and & characters in doc attributes
1213

1314
## [v0.28.0] - 2022-12-25
1415

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ serde = { version = "1.0", optional = true }
5656
serde_json = { version = "1.0.85", optional = true }
5757
serde_yaml = { version = "0.9.11", optional = true }
5858
regex = "1.7.0"
59+
html-escape = "0.2"
5960

6061
[dependencies.svd-parser]
6162
features = ["expand"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ This project is developed and maintained by the [Tools team][team].
1313

1414
## Minimum Supported Rust Version (MSRV)
1515

16-
The **generated code** is guaranteed to compile on stable Rust 1.64.0 and up.
16+
The **generated code** is guaranteed to compile on stable Rust 1.65.0 and up.
1717

18-
If you encounter compilation errors on any stable version newer than 1.64.0, please open an issue.
18+
If you encounter compilation errors on any stable version newer than 1.65.0, please open an issue.
1919

2020
# Testing Locally
2121

src/generate/interrupt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn render(
6363
.as_ref()
6464
.map(|s| util::respace(s))
6565
.as_ref()
66-
.map(|s| util::escape_brackets(s))
66+
.map(|s| util::escape_special_chars(s))
6767
.unwrap_or_else(|| interrupt.0.name.clone())
6868
);
6969

src/generate/peripheral.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
184184
}
185185
}
186186

187-
let description = util::escape_brackets(
187+
let description = util::escape_special_chars(
188188
util::respace(p.description.as_ref().unwrap_or(&name.as_ref().to_owned())).as_ref(),
189189
);
190190

@@ -516,7 +516,7 @@ impl FieldRegions {
516516
}
517517

518518
fn make_comment(size: u32, offset: u32, description: &str) -> String {
519-
let desc = util::escape_brackets(&util::respace(description));
519+
let desc = util::escape_special_chars(&util::respace(description));
520520
if size > 32 {
521521
let end = offset + size / 8;
522522
format!("0x{offset:02x}..0x{end:02x} - {desc}")
@@ -1306,7 +1306,7 @@ fn cluster_block(
13061306
config: &Config,
13071307
) -> Result<TokenStream> {
13081308
let description =
1309-
util::escape_brackets(&util::respace(c.description.as_ref().unwrap_or(&c.name)));
1309+
util::escape_special_chars(&util::respace(c.description.as_ref().unwrap_or(&c.name)));
13101310
let mod_name = util::replace_suffix(&c.name, "");
13111311

13121312
// name_snake_case needs to take into account array type.

src/generate/register.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn render(
3939
let span = Span::call_site();
4040
let name_constant_case = name.to_constant_case_ident(span);
4141
let name_snake_case = name.to_snake_case_ident(span);
42-
let description = util::escape_brackets(
42+
let description = util::escape_special_chars(
4343
util::respace(&register.description.clone().unwrap_or_else(|| {
4444
warn!("Missing description for register {}", register.name);
4545
Default::default()
@@ -129,7 +129,7 @@ pub fn render_register_mod(
129129
rsize.next_power_of_two()
130130
};
131131
let rty = rsize.to_ty()?;
132-
let description = util::escape_brackets(
132+
let description = util::escape_special_chars(
133133
util::respace(&register.description.clone().unwrap_or_else(|| {
134134
warn!("Missing description for register {}", register.name);
135135
Default::default()
@@ -436,7 +436,7 @@ pub fn fields(
436436
let name_snake_case = name.to_snake_case_ident(span);
437437
let name_constant_case = name.to_sanitized_constant_case();
438438
let description_raw = f.description.as_deref().unwrap_or(""); // raw description, if absent using empty string
439-
let description = util::respace(&util::escape_brackets(description_raw));
439+
let description = util::respace(&util::escape_special_chars(description_raw));
440440

441441
let can_read = can_read
442442
&& (f.access != Some(Access::WriteOnly))
@@ -833,7 +833,7 @@ pub fn fields(
833833
for v in &variants {
834834
let pc = &v.pc;
835835
let sc = &v.sc;
836-
let doc = util::escape_brackets(&util::respace(&v.doc));
836+
let doc = util::escape_special_chars(&util::respace(&v.doc));
837837
proxy_items.extend(quote! {
838838
#[doc = #doc]
839839
#inline
@@ -1105,7 +1105,7 @@ fn add_from_variants(
11051105

11061106
let mut vars = TokenStream::new();
11071107
for v in variants.iter().map(|v| {
1108-
let desc = util::escape_brackets(&util::respace(&format!("{}: {}", v.value, v.doc)));
1108+
let desc = util::escape_special_chars(&util::respace(&format!("{}: {}", v.value, v.doc)));
11091109
let pcv = &v.pc;
11101110
let pcval = &util::unsuffixed(v.value);
11111111
quote! {
@@ -1178,7 +1178,7 @@ fn description_with_bits(description: &str, offset: u64, width: u32) -> String {
11781178
};
11791179
if !description.is_empty() {
11801180
res.push_str(" - ");
1181-
res.push_str(&util::respace(&util::escape_brackets(description)));
1181+
res.push_str(&util::respace(&util::escape_special_chars(description)));
11821182
}
11831183
res
11841184
}

src/util.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22

33
use crate::svd::{Access, Device, DimElement, Field, RegisterInfo, RegisterProperties};
4+
use html_escape::encode_text_minimal;
45
use inflections::Inflect;
56
use proc_macro2::{Ident, Span, TokenStream};
67
use quote::quote;
@@ -287,6 +288,12 @@ pub fn escape_brackets(s: &str) -> String {
287288
})
288289
}
289290

291+
/// Escape basic html tags and brackets
292+
pub fn escape_special_chars(s: &str) -> String {
293+
let html_escaped = encode_text_minimal(s);
294+
escape_brackets(&html_escaped)
295+
}
296+
290297
pub fn name_of<T: FullName>(maybe_array: &MaybeArray<T>, ignore_group: bool) -> Cow<str> {
291298
match maybe_array {
292299
MaybeArray::Single(info) => info.fullname(ignore_group),

0 commit comments

Comments
 (0)