Skip to content

Commit e7345c9

Browse files
authored
Merge pull request #1595 from ehuss/warning-div
Switch warning blocks to use admonitions
2 parents 9e35d2d + 4b0cb6c commit e7345c9

11 files changed

+73
-117
lines changed

mdbook-spec/src/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static RULE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?m)^r\[([^]]+)]$").unwr
1919
/// The Regex for the syntax for blockquotes that have a specific CSS class,
2020
/// like `> [!WARNING]`.
2121
static ADMONITION_RE: Lazy<Regex> = Lazy::new(|| {
22-
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *> .*\n)+)").unwrap()
22+
Regex::new(r"(?m)^ *> \[!(?<admon>[^]]+)\]\n(?<blockquote>(?: *>.*\n)+)").unwrap()
2323
});
2424

2525
pub fn handle_preprocessing(pre: &dyn Preprocessor) -> Result<(), Error> {
@@ -131,15 +131,30 @@ impl Spec {
131131
ADMONITION_RE
132132
.replace_all(&chapter.content, |caps: &Captures<'_>| {
133133
let lower = caps["admon"].to_lowercase();
134+
let term = to_initial_case(&caps["admon"]);
135+
let blockquote = &caps["blockquote"];
136+
let initial_spaces = blockquote.chars().position(|ch| ch != ' ').unwrap_or(0);
137+
let space = &blockquote[..initial_spaces];
134138
format!(
135-
"<div class=\"{lower}\">\n\n{}\n\n</div>\n",
136-
&caps["blockquote"]
139+
"{space}<div class=\"{lower}\">\n\
140+
\n\
141+
{space}> ***{term}:***\n\
142+
{blockquote}\n\
143+
\n\
144+
{space}</div>\n",
137145
)
138146
})
139147
.to_string()
140148
}
141149
}
142150

151+
fn to_initial_case(s: &str) -> String {
152+
let mut chars = s.chars();
153+
let first = chars.next().expect("not empty").to_uppercase();
154+
let rest = chars.as_str().to_lowercase();
155+
format!("{first}{rest}")
156+
}
157+
143158
impl Preprocessor for Spec {
144159
fn name(&self) -> &str {
145160
"spec"

src/behavior-considered-undefined.md

+4-13
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,10 @@ behaviors. `unsafe` code that satisfies this property for any safe client is
1212
called *sound*; if `unsafe` code can be misused by safe code to exhibit
1313
undefined behavior, it is *unsound*.
1414

15-
<div class="warning">
16-
17-
***Warning:*** The following list is not exhaustive; it may grow or shrink.
18-
There is no formal model of Rust's semantics for what is and is not allowed in
19-
unsafe code, so there may be more behavior considered unsafe. We also reserve
20-
the right to make some of the behavior in that list defined in the future. In
21-
other words, this list does not say that anything will *definitely* always be
22-
undefined in all future Rust version (but we might make such commitments for
23-
some list items in the future).
24-
25-
Please read the [Rustonomicon] before writing unsafe code.
26-
27-
</div>
15+
> [!WARNING]
16+
> The following list is not exhaustive; it may grow or shrink. There is no formal model of Rust's semantics for what is and is not allowed in unsafe code, so there may be more behavior considered unsafe. We also reserve the right to make some of the behavior in that list defined in the future. In other words, this list does not say that anything will *definitely* always be undefined in all future Rust version (but we might make such commitments for some list items in the future).
17+
>
18+
> Please read the [Rustonomicon] before writing unsafe code.
2819
2920
* Data races.
3021
* Accessing (loading from or storing to) a place that is [dangling] or [based on

src/conditional-compilation.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ configuration option from within the source code of the crate being compiled.
5555
> by [Cargo][cargo-feature] for specifying compile-time options and optional
5656
> dependencies.
5757
58-
<div class="warning">
59-
60-
Warning: Arbitrarily-set configuration options can clash with compiler-set configuration options. For example, it is possible to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and have both `unix` and `windows` configuration options set at the same time. Doing this would be unwise.
61-
62-
</div>
58+
> [!WARNING]
59+
> Arbitrarily-set configuration options can clash with compiler-set configuration options. For example, it is possible to do `rustc --cfg "unix" program.rs` while compiling to a Windows target, and have both `unix` and `windows` configuration options set at the same time. Doing this would be unwise.
6360
6461
### `target_arch`
6562

src/expressions/method-call-expr.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,11 @@ These cases require a [disambiguating function call syntax] for method and funct
7373
> This special case may be removed in the future.
7474
7575
76-
<div class="warning">
77-
78-
***Warning:*** For [trait objects], if there is an inherent method of the same name as a trait method, it will give a compiler error when trying to call the method in a method call expression.
79-
Instead, you can call the method using [disambiguating function call syntax], in which case it calls the trait method, not the inherent method.
80-
There is no way to call the inherent method.
81-
Just don't define inherent methods on trait objects with the same name as a trait method and you'll be fine.
82-
83-
</div>
76+
> [!WARNING]
77+
> For [trait objects], if there is an inherent method of the same name as a trait method, it will give a compiler error when trying to call the method in a method call expression.
78+
> Instead, you can call the method using [disambiguating function call syntax], in which case it calls the trait method, not the inherent method.
79+
> There is no way to call the inherent method.
80+
> Just don't define inherent methods on trait objects with the same name as a trait method and you'll be fine.
8481
8582
[_CallParams_]: call-expr.md
8683
[_Expression_]: ../expressions.md

src/expressions/operator-expr.md

+9-16
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,10 @@ If the integer type is smaller than the pointer type, the address may be truncat
455455

456456
Casting from an integer to a raw pointer interprets the integer as a memory address and produces a pointer referencing that memory.
457457

458-
<div class="warning">
459-
460-
Warning:
461-
This interacts with the Rust memory model, which is still under development.
462-
A pointer obtained from this cast may suffer additional restrictions even if it is bitwise equal to a valid pointer.
463-
Dereferencing such a pointer may be [undefined behavior] if aliasing rules are not followed.
464-
465-
</div>
458+
> [!WARNING]
459+
> This interacts with the Rust memory model, which is still under development.
460+
> A pointer obtained from this cast may suffer additional restrictions even if it is bitwise equal to a valid pointer.
461+
> Dereferencing such a pointer may be [undefined behavior] if aliasing rules are not followed.
466462
467463
A trivial example of sound address arithmetic:
468464

@@ -642,14 +638,11 @@ fn example() {
642638

643639
Like assignment expressions, compound assignment expressions always produce [the unit value][unit].
644640

645-
<div class="warning">
646-
647-
Warning: The evaluation order of operands swaps depending on the types of the operands:
648-
with primitive types the right-hand side will get evaluated first, while with non-primitive types the left-hand side will get evaluated first.
649-
Try not to write code that depends on the evaluation order of operands in compound assignment expressions.
650-
See [this test] for an example of using this dependency.
651-
652-
</div>
641+
> [!WARNING]
642+
> The evaluation order of operands swaps depending on the types of the operands:
643+
> with primitive types the right-hand side will get evaluated first, while with non-primitive types the left-hand side will get evaluated first.
644+
> Try not to write code that depends on the evaluation order of operands in compound assignment expressions.
645+
> See [this test] for an example of using this dependency.
653646
654647
[copies or moves]: ../expressions.md#moved-and-copied-types
655648
[dropping]: ../destructors.md

src/introduction.md

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ It provides three kinds of material:
77
- Chapters that informally describe the memory model, concurrency model, runtime services, linkage model, and debugging facilities.
88
- Appendix chapters providing rationale and references to languages that influenced the design.
99

10-
<div class="warning">
11-
12-
Warning:
13-
This book is incomplete. Documenting everything takes a while.
14-
See the [GitHub issues] for what is not documented in this book.
15-
16-
</div>
10+
> [!WARNING]
11+
> This book is incomplete. Documenting everything takes a while.
12+
> See the [GitHub issues] for what is not documented in this book.
1713
1814
## Rust releases
1915

@@ -92,11 +88,8 @@ These conventions are documented here.
9288
9389
* Warnings that show unsound behavior in the language or possibly confusing interactions of language features are in a special warning box.
9490

95-
<div class="warning">
96-
97-
Warning: This is an example warning.
98-
99-
</div>
91+
> [!WARNING]
92+
> This is an example warning.
10093
10194
* Code snippets inline in the text are inside `<code>` tags.
10295

src/items/external-blocks.md

+2-8
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,8 @@ to link against. An ordinal is a unique number per symbol exported by a dynamic
292292
library on Windows and can be used when the library is being loaded to find
293293
that symbol rather than having to look it up by name.
294294

295-
<div class="warning">
296-
297-
Warning: `link_ordinal` should only be used in cases where the ordinal of the
298-
symbol is known to be stable: if the ordinal of a symbol is not explicitly set
299-
when its containing binary is built then one will be automatically assigned to
300-
it, and that assigned ordinal may change between builds of the binary.
301-
302-
</div>
295+
> [!WARNING]
296+
> `link_ordinal` should only be used in cases where the ordinal of the symbol is known to be stable: if the ordinal of a symbol is not explicitly set when its containing binary is built then one will be automatically assigned to it, and that assigned ordinal may change between builds of the binary.
303297
304298
<!-- ignore: Only works on x86 Windows -->
305299
```rust,ignore

src/names/preludes.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,8 @@ The *`no_std` [attribute]* may be applied at the crate level to prevent the
9191
> library. Those capabilities are mainly dynamic memory allocation (e.g. `Box`
9292
> and `Vec`) and file and network capabilities (e.g. `std::fs` and `std::io`).
9393
94-
<div class="warning">
95-
96-
Warning: Using `no_std` does not prevent the standard library from being
97-
linked in. It is still valid to put `extern crate std;` into the crate and
98-
dependencies can also link it in.
99-
100-
</div>
94+
> [!WARNING]
95+
> Using `no_std` does not prevent the standard library from being linked in. It is still valid to put `extern crate std;` into the crate and dependencies can also link it in.
10196
10297
## Language prelude
10398

src/patterns.md

+2-7
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,8 @@ if let (a, 3) = (1, 2) { // "(a, 3)" is refutable, and will not match
142142
_Literal patterns_ match exactly the same value as what is created by the literal.
143143
Since negative numbers are not [literals], literal patterns also accept an optional minus sign before the literal, which acts like the negation operator.
144144

145-
<div class="warning">
146-
147-
C string and raw C string literals are accepted in literal patterns, but `&CStr`
148-
doesn't implement structural equality (`#[derive(Eq, PartialEq)]`) and therefore
149-
any such `match` on a `&CStr` will be rejected with a type error.
150-
151-
</div>
145+
> [!WARNING]
146+
> C string and raw C string literals are accepted in literal patterns, but `&CStr` doesn't implement structural equality (`#[derive(Eq, PartialEq)]`) and therefore any such `match` on a `&CStr` will be rejected with a type error.
152147
153148
Literal patterns are always refutable.
154149

src/type-layout.md

+4-18
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,8 @@ for field in struct.fields_in_declaration_order() {
254254
struct.size = current_offset + padding_needed_for(current_offset, struct.alignment);
255255
```
256256

257-
<div class="warning">
258-
259-
Warning: This pseudocode uses a naive algorithm that ignores overflow issues for
260-
the sake of clarity. To perform memory layout computations in actual code, use
261-
[`Layout`].
262-
263-
</div>
257+
> [!WARNING]
258+
> This pseudocode uses a naive algorithm that ignores overflow issues for the sake of clarity. To perform memory layout computations in actual code, use [`Layout`].
264259
265260
> Note: This algorithm can produce zero-sized structs. In C, an empty struct
266261
> declaration like `struct Foo { }` is illegal. However, both gcc and clang
@@ -308,17 +303,8 @@ the default `enum` size and alignment for the target platform's C ABI.
308303
> really a "best guess". In particular, this may be incorrect when the C code
309304
> of interest is compiled with certain flags.
310305
311-
<div class="warning">
312-
313-
Warning: There are crucial differences between an `enum` in the C language and
314-
Rust's [field-less enums] with this representation. An `enum` in C is
315-
mostly a `typedef` plus some named constants; in other words, an object of an
316-
`enum` type can hold any integer value. For example, this is often used for
317-
bitflags in `C`. In contrast, Rust’s [field-less enums] can only legally hold
318-
the discriminant values, everything else is [undefined behavior]. Therefore,
319-
using a field-less enum in FFI to model a C `enum` is often wrong.
320-
321-
</div>
306+
> [!WARNING]
307+
> There are crucial differences between an `enum` in the C language and Rust's [field-less enums] with this representation. An `enum` in C is mostly a `typedef` plus some named constants; in other words, an object of an `enum` type can hold any integer value. For example, this is often used for bitflags in `C`. In contrast, Rust’s [field-less enums] can only legally hold the discriminant values, everything else is [undefined behavior]. Therefore, using a field-less enum in FFI to model a C `enum` is often wrong.
322308
323309
#### `#[repr(C)]` Enums With Fields
324310

theme/reference.css

+20-20
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,46 @@ the parenthetical. So for this example, you'd use
1111
}
1212

1313
/*
14-
Warnings and notes:
14+
Warnings are defined using admonitions in blockquotes:
1515
16-
Write the <div>s on their own line. E.g.
16+
> [!WARNING]
17+
> This is bad!
1718
18-
<div class="warning">
19-
20-
Warning: This is bad!
21-
22-
</div>
2319
*/
24-
main .warning p {
25-
padding: 10px 20px;
26-
margin: 20px 0;
20+
main .warning blockquote {
21+
padding: 0px;
22+
}
23+
24+
main .warning blockquote p {
25+
padding: 0px 20px;
26+
margin: 10px 0;
2727
}
2828

29-
main .warning p::before {
29+
main .warning blockquote p:first-child::before {
3030
content: "⚠️ ";
3131
}
3232

33-
.light main .warning p,
34-
.rust main .warning p {
33+
.light main .warning blockquote,
34+
.rust main .warning blockquote {
3535
border: 2px solid red;
3636
background: #ffcece;
3737
}
3838

39-
.rust main .warning p {
39+
.rust main .warning blockquote {
4040
/* overrides previous declaration */
4141
border-color: #961717;
4242
}
4343

44-
.coal main .warning p,
45-
.navy main .warning p,
46-
.ayu main .warning p {
44+
.coal main .warning blockquote,
45+
.navy main .warning blockquote,
46+
.ayu main .warning blockquote {
4747
background: #542626;
4848
}
4949

5050
/* Make the links higher contrast on dark themes */
51-
.coal main .warning p a,
52-
.navy main .warning p a,
53-
.ayu main .warning p a {
51+
.coal main .warning blockquote p a,
52+
.navy main .warning blockquote p a,
53+
.ayu main .warning blockquote p a {
5454
color: #80d0d0;
5555
}
5656

0 commit comments

Comments
 (0)