From 09c3e542da29d70cd47d6891781511ef36ac9114 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 19 Sep 2025 18:01:33 -0700 Subject: [PATCH] Move take_lines functions to mdbook-driver and make private These functions are only used by the links preprocessor. I'm moving these functions to put them closer to the code that they are associated with, and to reduce the public API surface. --- crates/mdbook-core/src/utils/mod.rs | 5 ----- .../mdbook-driver/src/builtin_preprocessors/links.rs | 10 ++++++---- .../src/builtin_preprocessors/links/take_lines.rs} | 10 +++++----- 3 files changed, 11 insertions(+), 14 deletions(-) rename crates/{mdbook-core/src/utils/string.rs => mdbook-driver/src/builtin_preprocessors/links/take_lines.rs} (96%) diff --git a/crates/mdbook-core/src/utils/mod.rs b/crates/mdbook-core/src/utils/mod.rs index 5e78d5e3..01bd2a6d 100644 --- a/crates/mdbook-core/src/utils/mod.rs +++ b/crates/mdbook-core/src/utils/mod.rs @@ -6,16 +6,11 @@ use tracing::error; pub mod fs; mod html; -mod string; mod toml_ext; pub(crate) use self::toml_ext::TomlExt; pub use self::html::{escape_html, escape_html_attribute}; -pub use self::string::{ - take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines, - take_rustdoc_include_lines, -}; /// Defines a `static` with a [`regex::Regex`]. #[macro_export] diff --git a/crates/mdbook-driver/src/builtin_preprocessors/links.rs b/crates/mdbook-driver/src/builtin_preprocessors/links.rs index a15fea8b..787b7745 100644 --- a/crates/mdbook-driver/src/builtin_preprocessors/links.rs +++ b/crates/mdbook-driver/src/builtin_preprocessors/links.rs @@ -1,10 +1,10 @@ -use anyhow::{Context, Result}; -use mdbook_core::book::{Book, BookItem}; -use mdbook_core::static_regex; -use mdbook_core::utils::{ +use self::take_lines::{ take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines, take_rustdoc_include_lines, }; +use anyhow::{Context, Result}; +use mdbook_core::book::{Book, BookItem}; +use mdbook_core::static_regex; use mdbook_preprocessor::{Preprocessor, PreprocessorContext}; use regex::{CaptureMatches, Captures}; use std::fs; @@ -12,6 +12,8 @@ use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo}; use std::path::{Path, PathBuf}; use tracing::{error, warn}; +mod take_lines; + const ESCAPE_CHAR: char = '\\'; const MAX_LINK_NESTED_DEPTH: usize = 10; diff --git a/crates/mdbook-core/src/utils/string.rs b/crates/mdbook-driver/src/builtin_preprocessors/links/take_lines.rs similarity index 96% rename from crates/mdbook-core/src/utils/string.rs rename to crates/mdbook-driver/src/builtin_preprocessors/links/take_lines.rs index 9f75dfaa..e884a994 100644 --- a/crates/mdbook-core/src/utils/string.rs +++ b/crates/mdbook-driver/src/builtin_preprocessors/links/take_lines.rs @@ -1,9 +1,9 @@ -use crate::static_regex; +use mdbook_core::static_regex; use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::RangeBounds; /// Take a range of lines from a string. -pub fn take_lines>(s: &str, range: R) -> String { +pub(super) fn take_lines>(s: &str, range: R) -> String { let start = match range.start_bound() { Excluded(&n) => n + 1, Included(&n) => n, @@ -28,7 +28,7 @@ static_regex!(ANCHOR_END, r"ANCHOR_END:\s*(?P[\w_-]+)"); /// Take anchored lines from a string. /// Lines containing anchor are ignored. -pub fn take_anchored_lines(s: &str, anchor: &str) -> String { +pub(super) fn take_anchored_lines(s: &str, anchor: &str) -> String { let mut retained = Vec::<&str>::new(); let mut anchor_found = false; @@ -60,7 +60,7 @@ pub fn take_anchored_lines(s: &str, anchor: &str) -> String { /// For any lines not in the range, include them but use `#` at the beginning. This will hide the /// lines from initial display but include them when expanding the code snippet or testing with /// rustdoc. -pub fn take_rustdoc_include_lines>(s: &str, range: R) -> String { +pub(super) fn take_rustdoc_include_lines>(s: &str, range: R) -> String { let mut output = String::with_capacity(s.len()); for (index, line) in s.lines().enumerate() { @@ -78,7 +78,7 @@ pub fn take_rustdoc_include_lines>(s: &str, range: R) -> S /// For any lines not between the anchors, include them but use `#` at the beginning. This will /// hide the lines from initial display but include them when expanding the code snippet or testing /// with rustdoc. -pub fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { +pub(super) fn take_rustdoc_include_anchored_lines(s: &str, anchor: &str) -> String { let mut output = String::with_capacity(s.len()); let mut within_anchored_section = false;