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.
This commit is contained in:
Eric Huss 2025-09-19 18:01:33 -07:00
parent 9fae3c88eb
commit 09c3e542da
3 changed files with 11 additions and 14 deletions

View file

@ -6,16 +6,11 @@ use tracing::error;
pub mod fs; pub mod fs;
mod html; mod html;
mod string;
mod toml_ext; mod toml_ext;
pub(crate) use self::toml_ext::TomlExt; pub(crate) use self::toml_ext::TomlExt;
pub use self::html::{escape_html, escape_html_attribute}; 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`]. /// Defines a `static` with a [`regex::Regex`].
#[macro_export] #[macro_export]

View file

@ -1,10 +1,10 @@
use anyhow::{Context, Result}; use self::take_lines::{
use mdbook_core::book::{Book, BookItem};
use mdbook_core::static_regex;
use mdbook_core::utils::{
take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines, take_anchored_lines, take_lines, take_rustdoc_include_anchored_lines,
take_rustdoc_include_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 mdbook_preprocessor::{Preprocessor, PreprocessorContext};
use regex::{CaptureMatches, Captures}; use regex::{CaptureMatches, Captures};
use std::fs; use std::fs;
@ -12,6 +12,8 @@ use std::ops::{Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeTo};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use tracing::{error, warn}; use tracing::{error, warn};
mod take_lines;
const ESCAPE_CHAR: char = '\\'; const ESCAPE_CHAR: char = '\\';
const MAX_LINK_NESTED_DEPTH: usize = 10; const MAX_LINK_NESTED_DEPTH: usize = 10;

View file

@ -1,9 +1,9 @@
use crate::static_regex; use mdbook_core::static_regex;
use std::ops::Bound::{Excluded, Included, Unbounded}; use std::ops::Bound::{Excluded, Included, Unbounded};
use std::ops::RangeBounds; use std::ops::RangeBounds;
/// Take a range of lines from a string. /// Take a range of lines from a string.
pub fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String { pub(super) fn take_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
let start = match range.start_bound() { let start = match range.start_bound() {
Excluded(&n) => n + 1, Excluded(&n) => n + 1,
Included(&n) => n, Included(&n) => n,
@ -28,7 +28,7 @@ static_regex!(ANCHOR_END, r"ANCHOR_END:\s*(?P<anchor_name>[\w_-]+)");
/// Take anchored lines from a string. /// Take anchored lines from a string.
/// Lines containing anchor are ignored. /// 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 retained = Vec::<&str>::new();
let mut anchor_found = false; 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 /// 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 /// lines from initial display but include them when expanding the code snippet or testing with
/// rustdoc. /// rustdoc.
pub fn take_rustdoc_include_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String { pub(super) fn take_rustdoc_include_lines<R: RangeBounds<usize>>(s: &str, range: R) -> String {
let mut output = String::with_capacity(s.len()); let mut output = String::with_capacity(s.len());
for (index, line) in s.lines().enumerate() { for (index, line) in s.lines().enumerate() {
@ -78,7 +78,7 @@ pub fn take_rustdoc_include_lines<R: RangeBounds<usize>>(s: &str, range: R) -> S
/// For any lines not between the anchors, include them but use `#` at the beginning. This will /// 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 /// hide the lines from initial display but include them when expanding the code snippet or testing
/// with rustdoc. /// 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 output = String::with_capacity(s.len());
let mut within_anchored_section = false; let mut within_anchored_section = false;