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