1pub mod anchor_styles;
6pub mod code_block_utils;
7pub mod early_returns;
10pub mod element_cache;
11pub mod emphasis_utils;
12pub mod fix_utils;
13pub mod header_id_utils;
14pub mod jinja_utils;
15pub mod kramdown_utils;
16pub mod line_ending;
17pub mod markdown_elements;
18pub mod mkdocs_abbreviations;
19pub mod mkdocs_admonitions;
20pub mod mkdocs_attr_list;
21pub mod mkdocs_common;
22pub mod mkdocs_critic;
23pub mod mkdocs_definition_lists;
24pub mod mkdocs_extensions;
25pub mod mkdocs_footnotes;
26pub mod mkdocs_icons;
27pub mod mkdocs_patterns;
28pub mod mkdocs_snippets;
29pub mod mkdocs_tabs;
30pub mod mkdocs_test_utils;
31pub mod mkdocstrings_refs;
32pub mod quarto_divs;
33pub mod range_utils;
34pub mod regex_cache;
35pub mod skip_context;
36pub mod string_interner;
37pub mod table_utils;
38pub mod text_reflow;
39pub mod utf8_offsets;
40
41pub use code_block_utils::CodeBlockUtils;
42pub use line_ending::{
44 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
45 normalize_line_ending,
46};
47pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
48pub use range_utils::LineIndex;
49
50pub fn is_definition_list_item(line: &str) -> bool {
60 let trimmed = line.trim_start();
61 trimmed.starts_with(": ")
62 || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(|c| c.is_whitespace()))
63}
64
65pub trait StrExt {
67 fn replace_trailing_spaces(&self, replacement: &str) -> String;
69
70 fn has_trailing_spaces(&self) -> bool;
72
73 fn trailing_spaces(&self) -> usize;
75}
76
77impl StrExt for str {
78 fn replace_trailing_spaces(&self, replacement: &str) -> String {
79 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
83 (stripped, true)
84 } else {
85 (self, false)
86 };
87
88 let mut non_space_len = content.len();
90 for c in content.chars().rev() {
91 if c == ' ' {
92 non_space_len -= 1;
93 } else {
94 break;
95 }
96 }
97
98 let mut result =
100 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
101 result.push_str(&content[..non_space_len]);
102 result.push_str(replacement);
103 if ends_with_newline {
104 result.push('\n');
105 }
106
107 result
108 }
109
110 fn has_trailing_spaces(&self) -> bool {
111 self.trailing_spaces() > 0
112 }
113
114 fn trailing_spaces(&self) -> usize {
115 let content = self.strip_suffix('\n').unwrap_or(self);
119
120 let mut space_count = 0;
122 for c in content.chars().rev() {
123 if c == ' ' {
124 space_count += 1;
125 } else {
126 break;
127 }
128 }
129
130 space_count
131 }
132}
133
134use std::collections::hash_map::DefaultHasher;
135use std::hash::{Hash, Hasher};
136
137pub fn fast_hash(content: &str) -> u64 {
150 let mut hasher = DefaultHasher::new();
151 content.hash(&mut hasher);
152 hasher.finish()
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn test_detect_line_ending_pure_lf() {
161 let content = "First line\nSecond line\nThird line\n";
163 assert_eq!(detect_line_ending(content), "\n");
164 }
165
166 #[test]
167 fn test_detect_line_ending_pure_crlf() {
168 let content = "First line\r\nSecond line\r\nThird line\r\n";
170 assert_eq!(detect_line_ending(content), "\r\n");
171 }
172
173 #[test]
174 fn test_detect_line_ending_mixed_more_lf() {
175 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
177 assert_eq!(detect_line_ending(content), "\n");
178 }
179
180 #[test]
181 fn test_detect_line_ending_mixed_more_crlf() {
182 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
184 assert_eq!(detect_line_ending(content), "\r\n");
185 }
186
187 #[test]
188 fn test_detect_line_ending_empty_string() {
189 let content = "";
191 assert_eq!(detect_line_ending(content), "\n");
192 }
193
194 #[test]
195 fn test_detect_line_ending_single_line_no_ending() {
196 let content = "This is a single line with no line ending";
198 assert_eq!(detect_line_ending(content), "\n");
199 }
200
201 #[test]
202 fn test_detect_line_ending_equal_lf_and_crlf() {
203 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
207 assert_eq!(detect_line_ending(content), "\n");
208 }
209
210 #[test]
211 fn test_detect_line_ending_single_lf() {
212 let content = "Line 1\n";
214 assert_eq!(detect_line_ending(content), "\n");
215 }
216
217 #[test]
218 fn test_detect_line_ending_single_crlf() {
219 let content = "Line 1\r\n";
221 assert_eq!(detect_line_ending(content), "\r\n");
222 }
223
224 #[test]
225 fn test_detect_line_ending_embedded_cr() {
226 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
229 assert_eq!(detect_line_ending(content), "\n");
231 }
232}