Merge pull request #2810 from ehuss/smart-punctuation-default

Enable smart-punctuation by default
This commit is contained in:
Eric Huss 2025-08-22 23:58:42 +00:00 committed by GitHub
commit ec436adca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 70 additions and 42 deletions

View file

@ -417,7 +417,7 @@ pub enum RustEdition {
} }
/// Configuration for the HTML renderer. /// Configuration for the HTML renderer.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)] #[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
#[non_exhaustive] #[non_exhaustive]
pub struct HtmlConfig { pub struct HtmlConfig {
@ -485,6 +485,35 @@ pub struct HtmlConfig {
pub hash_files: bool, pub hash_files: bool,
} }
impl Default for HtmlConfig {
fn default() -> HtmlConfig {
HtmlConfig {
theme: None,
default_theme: None,
preferred_dark_theme: None,
smart_punctuation: true,
mathjax_support: false,
additional_css: Vec::new(),
additional_js: Vec::new(),
fold: Fold::default(),
playground: Playground::default(),
code: Code::default(),
print: Print::default(),
no_section_label: false,
search: None,
git_repository_url: None,
git_repository_icon: None,
input_404: None,
site_url: None,
cname: None,
edit_url_template: None,
live_reload_endpoint: None,
redirect: HashMap::new(),
hash_files: false,
}
}
}
impl HtmlConfig { impl HtmlConfig {
/// Returns the directory of theme from the provided root directory. If the /// Returns the directory of theme from the provided root directory. If the
/// directory is not present it will append the default directory of "theme" /// directory is not present it will append the default directory of "theme"

View file

@ -23,16 +23,25 @@ pub use pulldown_cmark;
mod tests; mod tests;
/// Options for parsing markdown. /// Options for parsing markdown.
#[derive(Default)]
#[non_exhaustive] #[non_exhaustive]
pub struct MarkdownOptions { pub struct MarkdownOptions {
/// Enables smart punctuation. /// Enables smart punctuation.
/// ///
/// Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and /// Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and
/// `---` to em-dash. /// `---` to em-dash.
///
/// This is `true` by default.
pub smart_punctuation: bool, pub smart_punctuation: bool,
} }
impl Default for MarkdownOptions {
fn default() -> MarkdownOptions {
MarkdownOptions {
smart_punctuation: true,
}
}
}
/// Options for converting markdown to HTML. /// Options for converting markdown to HTML.
#[non_exhaustive] #[non_exhaustive]
pub struct HtmlRenderOptions<'a> { pub struct HtmlRenderOptions<'a> {

View file

@ -61,7 +61,8 @@ fn it_can_wrap_tables() {
#[test] #[test]
fn it_can_keep_quotes_straight() { fn it_can_keep_quotes_straight() {
let options = HtmlRenderOptions::new(&Path::new("")); let mut options = HtmlRenderOptions::new(&Path::new(""));
options.markdown_options.smart_punctuation = false;
assert_eq!(render_markdown("'one'", &options), "<p>'one'</p>\n"); assert_eq!(render_markdown("'one'", &options), "<p>'one'</p>\n");
} }
@ -78,8 +79,7 @@ fn it_can_make_quotes_curly_except_when_they_are_in_code() {
</code></pre> </code></pre>
<p><code>'three'</code> four</p> <p><code>'three'</code> four</p>
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
} }
@ -102,9 +102,7 @@ more text with spaces
</code></pre> </code></pre>
<p>more text with spaces</p> <p>more text with spaces</p>
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
} }
@ -117,9 +115,7 @@ fn rust_code_block_properties_are_passed_as_space_delimited_class() {
let expected = r#"<pre><code class="language-rust,no_run,should_panic,property_3"></code></pre> let expected = r#"<pre><code class="language-rust,no_run,should_panic,property_3"></code></pre>
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
} }
@ -132,9 +128,7 @@ fn rust_code_block_properties_with_whitespace_are_passed_as_space_delimited_clas
let expected = r#"<pre><code class="language-rust,,,,,no_run,,,should_panic,,,,property_3"></code></pre> let expected = r#"<pre><code class="language-rust,,,,,no_run,,,should_panic,,,,property_3"></code></pre>
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
} }
@ -147,17 +141,13 @@ fn rust_code_block_without_properties_has_proper_html_class() {
let expected = r#"<pre><code class="language-rust"></code></pre> let expected = r#"<pre><code class="language-rust"></code></pre>
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
let input = r#" let input = r#"
```rust ```rust
``` ```
"#; "#;
let mut options = HtmlRenderOptions::new(&Path::new("")); let options = HtmlRenderOptions::new(&Path::new(""));
assert_eq!(render_markdown(input, &options), expected);
options.markdown_options.smart_punctuation = true;
assert_eq!(render_markdown(input, &options), expected); assert_eq!(render_markdown(input, &options), expected);
} }

View file

@ -123,7 +123,7 @@ The following configuration options are available:
CSS media query. Defaults to `navy`. CSS media query. Defaults to `navy`.
- **smart-punctuation:** Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and `---` to em-dash. - **smart-punctuation:** Converts quotes to curly quotes, `...` to `…`, `--` to en-dash, and `---` to em-dash.
See [Smart Punctuation](../markdown.md#smart-punctuation). See [Smart Punctuation](../markdown.md#smart-punctuation).
Defaults to `false`. Defaults to `true`.
- **mathjax-support:** Adds support for [MathJax](../mathjax.md). Defaults to - **mathjax-support:** Adds support for [MathJax](../mathjax.md). Defaults to
`false`. `false`.
- **additional-css:** If you need to slightly change the appearance of your book - **additional-css:** If you need to slightly change the appearance of your book

View file

@ -220,8 +220,8 @@ characters:
So, no need to manually enter those Unicode characters! So, no need to manually enter those Unicode characters!
This feature is disabled by default. This feature is enabled by default.
To enable it, see the [`output.html.smart-punctuation`] config option. To disable it, see the [`output.html.smart-punctuation`] config option.
[strikethrough]: https://github.github.com/gfm/#strikethrough-extension- [strikethrough]: https://github.github.com/gfm/#strikethrough-extension-
[tables]: https://github.github.com/gfm/#tables-extension- [tables]: https://github.github.com/gfm/#tables-extension-

View file

@ -90,23 +90,7 @@ Carrots</li>
#[test] #[test]
fn smart_punctuation() { fn smart_punctuation() {
BookTest::from_dir("markdown/smart_punctuation") BookTest::from_dir("markdown/smart_punctuation")
// Default is off. // Default is on.
.check_main_file(
"book/smart_punctuation.html",
str![[r##"
<h1 id="smart-punctuation"><a class="header" href="#smart-punctuation">Smart Punctuation</a></h1>
<ul>
<li>En dash: --</li>
<li>Em dash: ---</li>
<li>Ellipsis: ...</li>
<li>Double quote: "quote"</li>
<li>Single quote: 'quote'</li>
</ul>
"##]],
)
.run("build", |cmd| {
cmd.env("MDBOOK_OUTPUT__HTML__SMART_PUNCTUATION", "true");
})
.check_main_file( .check_main_file(
"book/smart_punctuation.html", "book/smart_punctuation.html",
str![[r##" str![[r##"
@ -118,6 +102,22 @@ fn smart_punctuation() {
<li>Double quote: quote</li> <li>Double quote: quote</li>
<li>Single quote: quote</li> <li>Single quote: quote</li>
</ul> </ul>
"##]],
)
.run("build", |cmd| {
cmd.env("MDBOOK_OUTPUT__HTML__SMART_PUNCTUATION", "false");
})
.check_main_file(
"book/smart_punctuation.html",
str![[r##"
<h1 id="smart-punctuation"><a class="header" href="#smart-punctuation">Smart Punctuation</a></h1>
<ul>
<li>En dash: --</li>
<li>Em dash: ---</li>
<li>Ellipsis: ...</li>
<li>Double quote: "quote"</li>
<li>Single quote: 'quote'</li>
</ul>
"##]], "##]],
); );
} }

View file

@ -63,7 +63,7 @@ fn reasonable_search_index() {
// html. // html.
assert_eq!( assert_eq!(
docs[&sneaky]["body"], docs[&sneaky]["body"],
"I put &lt;HTML&gt; in here! Sneaky inline event alert(\"inline\");. But regular inline is indexed." "I put &lt;HTML&gt; in here! Sneaky inline event alert(“inline”);. But regular inline is indexed."
); );
assert_eq!( assert_eq!(
docs[&no_headers]["breadcrumbs"], docs[&no_headers]["breadcrumbs"],

File diff suppressed because one or more lines are too long