Skip to main content

rumdl_lib/utils/
mod.rs

1//!
2//! Shared utilities for rumdl, including document structure analysis, code block handling, regex helpers, and string extensions.
3//! Provides reusable traits and functions for rule implementations and core linter logic.
4
5pub mod anchor_styles;
6pub mod atomic_write;
7pub mod blockquote;
8pub mod code_block_utils;
9pub mod emphasis_utils;
10pub mod fix_utils;
11pub mod frontmatter_values;
12pub mod header_id_utils;
13pub mod html_block;
14pub mod html_elements;
15pub mod jinja_utils;
16pub mod kramdown_utils;
17pub mod line_ending;
18pub mod mdg;
19pub mod mkdocs_admonitions;
20pub mod mkdocs_attr_list;
21pub mod mkdocs_common;
22pub mod mkdocs_config;
23pub mod mkdocs_critic;
24pub mod mkdocs_definition_lists;
25pub mod mkdocs_extensions;
26pub mod mkdocs_footnotes;
27pub mod mkdocs_html_markdown;
28pub mod mkdocs_icons;
29pub mod mkdocs_patterns;
30pub mod mkdocs_snippets;
31pub mod mkdocs_tabs;
32pub mod mkdocstrings_refs;
33pub mod obsidian_config;
34pub mod pandoc;
35pub mod parser_options;
36pub mod project_root;
37pub mod pymdown_blocks;
38pub mod quarto_chunks;
39pub mod range_utils;
40pub mod regex_cache;
41pub mod sentence_utils;
42pub mod skip_context;
43pub mod table_utils;
44pub mod text_reflow;
45pub mod thematic_break;
46pub mod unicode;
47pub mod upward_walk;
48pub mod utf8_offsets;
49
50pub use code_block_utils::CodeBlockUtils;
51pub use line_ending::{
52    LineEnding, NormalizedLineEndingMap, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings,
53    get_line_ending_str, normalize_line_ending,
54};
55pub use parser_options::rumdl_parser_options;
56pub use range_utils::LineIndex;
57
58/// Calculate the visual indentation width of a string, expanding tabs to spaces.
59///
60/// Per CommonMark, tabs expand to the next tab stop (columns 4, 8, 12, ...).
61pub fn calculate_indentation_width(indent_str: &str, tab_width: usize) -> usize {
62    let mut width = 0;
63    for ch in indent_str.chars() {
64        if ch == '\t' {
65            width = ((width / tab_width) + 1) * tab_width;
66        } else if ch == ' ' {
67            width += 1;
68        } else {
69            break;
70        }
71    }
72    width
73}
74
75/// Calculate the visual indentation width using default tab width of 4
76pub fn calculate_indentation_width_default(indent_str: &str) -> usize {
77    calculate_indentation_width(indent_str, 4)
78}
79
80/// Check if a line is a definition list item (Extended Markdown)
81///
82/// Definition lists use the pattern:
83/// ```text
84/// Term
85/// : Definition
86/// ```
87///
88/// Supported by: PHP Markdown Extra, Kramdown, Pandoc, Hugo, and others
89pub fn is_definition_list_item(line: &str) -> bool {
90    let trimmed = line.trim_start();
91    trimmed.starts_with(": ")
92        || (trimmed.starts_with(':') && trimmed.len() > 1 && trimmed.chars().nth(1).is_some_and(char::is_whitespace))
93}
94
95/// Check if a line consists only of a template directive with no surrounding text.
96///
97/// Detects template syntax used in static site generators:
98/// - Handlebars/mdBook/Mustache: `{{...}}`
99/// - Jinja2/Liquid/Jekyll: `{%...%}`
100/// - Hugo shortcodes: `{{<...>}}` or `{{%...%}}`
101///
102/// Template directives are preprocessor instructions that should not be merged
103/// into surrounding paragraphs during reflow.
104pub fn is_template_directive_only(line: &str) -> bool {
105    let trimmed = line.trim();
106    if trimmed.is_empty() {
107        return false;
108    }
109    (trimmed.starts_with("{{") && trimmed.ends_with("}}")) || (trimmed.starts_with("{%") && trimmed.ends_with("%}"))
110}
111
112/// Trait for string-related extensions
113pub trait StrExt {
114    /// Replace trailing spaces with a specified replacement string
115    fn replace_trailing_spaces(&self, replacement: &str) -> String;
116
117    /// Check if the string has trailing whitespace
118    fn has_trailing_spaces(&self) -> bool;
119
120    /// Count the number of trailing spaces in the string
121    fn trailing_spaces(&self) -> usize;
122}
123
124impl StrExt for str {
125    fn replace_trailing_spaces(&self, replacement: &str) -> String {
126        // Custom implementation to handle both newlines and tabs specially
127
128        // Check if string ends with newline
129        let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
130            (stripped, true)
131        } else {
132            (self, false)
133        };
134
135        // Find where the trailing spaces begin
136        let mut non_space_len = content.len();
137        for c in content.chars().rev() {
138            if c == ' ' {
139                non_space_len -= 1;
140            } else {
141                break;
142            }
143        }
144
145        // Build the final string
146        let mut result = String::with_capacity(non_space_len + replacement.len() + usize::from(ends_with_newline));
147        result.push_str(&content[..non_space_len]);
148        result.push_str(replacement);
149        if ends_with_newline {
150            result.push('\n');
151        }
152
153        result
154    }
155
156    fn has_trailing_spaces(&self) -> bool {
157        self.trailing_spaces() > 0
158    }
159
160    fn trailing_spaces(&self) -> usize {
161        // Custom implementation to handle both newlines and tabs specially
162
163        // Prepare the string without newline if it ends with one
164        let content = self.strip_suffix('\n').unwrap_or(self);
165
166        // Count only trailing spaces at the end, not tabs
167        let mut space_count = 0;
168        for c in content.chars().rev() {
169            if c == ' ' {
170                space_count += 1;
171            } else {
172                break;
173            }
174        }
175
176        space_count
177    }
178}
179
180use std::collections::hash_map::DefaultHasher;
181use std::hash::{Hash, Hasher};
182
183/// Fast hash function for string content
184///
185/// This utility function provides a quick way to generate a hash from string content
186/// for use in caching mechanisms. It uses Rust's built-in DefaultHasher.
187///
188/// # Arguments
189///
190/// * `content` - The string content to hash
191///
192/// # Returns
193///
194/// A 64-bit hash value derived from the content
195pub fn fast_hash(content: &str) -> u64 {
196    let mut hasher = DefaultHasher::new();
197    content.hash(&mut hasher);
198    hasher.finish()
199}
200
201#[cfg(test)]
202mod tests {
203    use super::*;
204
205    #[test]
206    fn test_detect_line_ending_pure_lf() {
207        // Test content with only LF line endings
208        let content = "First line\nSecond line\nThird line\n";
209        assert_eq!(detect_line_ending(content), "\n");
210    }
211
212    #[test]
213    fn test_detect_line_ending_pure_crlf() {
214        // Test content with only CRLF line endings
215        let content = "First line\r\nSecond line\r\nThird line\r\n";
216        assert_eq!(detect_line_ending(content), "\r\n");
217    }
218
219    #[test]
220    fn test_detect_line_ending_mixed_more_lf() {
221        // Test content with mixed line endings where LF is more common
222        let content = "First line\nSecond line\r\nThird line\nFourth line\n";
223        assert_eq!(detect_line_ending(content), "\n");
224    }
225
226    #[test]
227    fn test_detect_line_ending_mixed_more_crlf() {
228        // Test content with mixed line endings where CRLF is more common
229        let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
230        assert_eq!(detect_line_ending(content), "\r\n");
231    }
232
233    #[test]
234    fn test_detect_line_ending_empty_string() {
235        // Test empty string - should default to LF
236        let content = "";
237        assert_eq!(detect_line_ending(content), "\n");
238    }
239
240    #[test]
241    fn test_detect_line_ending_single_line_no_ending() {
242        // Test single line without any line endings - should default to LF
243        let content = "This is a single line with no line ending";
244        assert_eq!(detect_line_ending(content), "\n");
245    }
246
247    #[test]
248    fn test_detect_line_ending_equal_lf_and_crlf() {
249        // Test edge case with equal number of CRLF and LF
250        // Since LF count is calculated as total '\n' minus CRLF count,
251        // and the algorithm uses > (not >=), it should default to LF
252        let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
253        assert_eq!(detect_line_ending(content), "\n");
254    }
255
256    #[test]
257    fn test_detect_line_ending_single_lf() {
258        // Test with just a single LF
259        let content = "Line 1\n";
260        assert_eq!(detect_line_ending(content), "\n");
261    }
262
263    #[test]
264    fn test_detect_line_ending_single_crlf() {
265        // Test with just a single CRLF
266        let content = "Line 1\r\n";
267        assert_eq!(detect_line_ending(content), "\r\n");
268    }
269
270    #[test]
271    fn test_detect_line_ending_embedded_cr() {
272        // Test with CR characters that are not part of CRLF
273        // These should not affect the count
274        let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
275        // This has 1 CRLF and 2 LF (after subtracting the CRLF)
276        assert_eq!(detect_line_ending(content), "\n");
277    }
278}