84 lines
2.4 KiB
Dart
84 lines
2.4 KiB
Dart
/// Text cleaning utilities for documentation and comments
|
|
// ignore_for_file: use_raw_strings
|
|
|
|
library;
|
|
|
|
/// Text cleaning utilities
|
|
class TextCleaner {
|
|
/// Clean description text for use in documentation
|
|
static String cleanDescription(String text) {
|
|
if (text.isEmpty) return text;
|
|
|
|
// Remove leading/trailing whitespace
|
|
var result = text.trim();
|
|
|
|
// Replace multiple spaces with single space
|
|
result = result.replaceAll(RegExp(r'\s+'), ' ');
|
|
|
|
// Remove HTML tags
|
|
result = result.replaceAll(RegExp('<[^>]*>'), '');
|
|
|
|
// Escape special characters for Dart comments
|
|
result = result.replaceAll('*/', '* /');
|
|
|
|
// Remove newlines within text (for single-line comments)
|
|
result = result.replaceAll('\n', ' ').replaceAll('\r', ' ');
|
|
|
|
return result;
|
|
}
|
|
|
|
/// Clean text for use in code (identifiers, etc.)
|
|
static String cleanForCode(String text) {
|
|
if (text.isEmpty) return text;
|
|
|
|
// Remove special characters
|
|
var result = text.replaceAll(RegExp('[^a-zA-Z0-9_]'), '_');
|
|
|
|
// Remove leading/trailing underscores
|
|
result = result.replaceAll(RegExp(r'^_+|_+$'), '');
|
|
|
|
// Replace multiple underscores with single underscore
|
|
result = result.replaceAll(RegExp('_+'), '_');
|
|
|
|
return result;
|
|
}
|
|
|
|
/// Escape string for use in Dart string literals
|
|
static String escapeString(String text) {
|
|
return text
|
|
.replaceAll('\\', '\\\\')
|
|
.replaceAll("'", "\\'")
|
|
.replaceAll('"', '\\"')
|
|
.replaceAll('\n', '\\n')
|
|
.replaceAll('\r', '\\r')
|
|
.replaceAll('\t', '\\t');
|
|
}
|
|
|
|
/// Unescape string from Dart string literals
|
|
static String unescapeString(String text) {
|
|
return text
|
|
.replaceAll('\\n', '\n')
|
|
.replaceAll('\\r', '\r')
|
|
.replaceAll('\\t', '\t')
|
|
.replaceAll('\\"', '"')
|
|
.replaceAll("\\'", "'")
|
|
.replaceAll('\\\\', '\\');
|
|
}
|
|
|
|
/// Truncate text to specified length
|
|
static String truncate(String text, int maxLength, {String suffix = '...'}) {
|
|
if (text.length <= maxLength) return text;
|
|
return text.substring(0, maxLength - suffix.length) + suffix;
|
|
}
|
|
|
|
/// Remove extra whitespace and normalize line endings
|
|
static String normalize(String text) {
|
|
return text
|
|
.replaceAll('\r\n', '\n')
|
|
.replaceAll('\r', '\n')
|
|
.replaceAll(RegExp(r'[ \t]+'), ' ')
|
|
.replaceAll(RegExp(r'\n{3,}'), '\n\n')
|
|
.trim();
|
|
}
|
|
}
|