html_generator/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
// Copyright © 2025 HTML Generator. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
#![doc = include_str!("../README.md")]
#![doc(
html_favicon_url = "https://kura.pro/html-generator/images/favicon.ico",
html_logo_url = "https://kura.pro/html-generator/images/logos/html-generator.svg",
html_root_url = "https://docs.rs/html-generator"
)]
#![crate_name = "html_generator"]
#![crate_type = "lib"]
use std::{
fmt,
fs::File,
io::{self, BufReader, BufWriter, Read, Write},
path::{Component, Path},
};
/// Maximum buffer size for reading files (16MB)
const MAX_BUFFER_SIZE: usize = 16 * 1024 * 1024;
// Re-export public modules
pub mod accessibility;
pub mod emojis;
pub mod error;
pub mod generator;
pub mod performance;
pub mod seo;
pub mod utils;
// Re-export primary types and functions for convenience
pub use crate::error::HtmlError;
pub use accessibility::{add_aria_attributes, validate_wcag};
pub use emojis::load_emoji_sequences;
pub use generator::generate_html;
pub use performance::{async_generate_html, minify_html};
pub use seo::{generate_meta_tags, generate_structured_data};
pub use utils::{extract_front_matter, format_header_with_id_class};
/// Common constants used throughout the library.
///
/// This module contains configuration values and limits that help ensure
/// secure and efficient operation of the library.
pub mod constants {
/// Maximum allowed input size (5MB) to prevent denial of service attacks
pub const DEFAULT_MAX_INPUT_SIZE: usize = 5 * 1024 * 1024;
/// Minimum required input size (1KB) for meaningful processing
pub const MIN_INPUT_SIZE: usize = 1024;
/// Default language code for HTML generation (British English)
pub const DEFAULT_LANGUAGE: &str = "en-GB";
/// Default syntax highlighting theme (github)
pub const DEFAULT_SYNTAX_THEME: &str = "github";
/// Maximum file path length
pub const MAX_PATH_LENGTH: usize = 4096;
/// Regular expression pattern for validating language codes
pub const LANGUAGE_CODE_PATTERN: &str = r"^[a-z]{2}-[A-Z]{2}$";
/// Verify invariants at compile time
const _: () = assert!(MIN_INPUT_SIZE <= DEFAULT_MAX_INPUT_SIZE);
const _: () = assert!(MAX_PATH_LENGTH > 0);
}
/// Result type alias for library operations
pub type Result<T> = std::result::Result<T, HtmlError>;
/// Configuration options for Markdown to HTML conversion.
///
/// This struct holds settings that control how Markdown content is processed
/// and converted to HTML.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MarkdownConfig {
/// The encoding to use for input/output (defaults to "utf-8")
pub encoding: String,
/// HTML generation configuration
pub html_config: HtmlConfig,
}
impl Default for MarkdownConfig {
fn default() -> Self {
Self {
encoding: String::from("utf-8"),
html_config: HtmlConfig::default(),
}
}
}
/// Errors that can occur during configuration.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
/// Error for invalid input size configuration
#[error(
"Invalid input size: {0} bytes is below minimum of {1} bytes"
)]
InvalidInputSize(usize, usize),
/// Error for invalid language code
#[error("Invalid language code: {0}")]
InvalidLanguageCode(String),
/// Error for invalid file path
#[error("Invalid file path: {0}")]
InvalidFilePath(String),
}
/// Output destination for HTML generation.
///
/// Specifies where the generated HTML content should be written.
///
/// # Examples
///
/// Writing HTML to a file:
/// ```
/// use std::fs::File;
/// use html_generator::OutputDestination;
///
/// let output = OutputDestination::File("output.html".to_string());
/// ```
///
/// Writing HTML to an in-memory buffer:
/// ```
/// use std::io::Cursor;
/// use html_generator::OutputDestination;
///
/// let buffer = Cursor::new(Vec::new());
/// let output = OutputDestination::Writer(Box::new(buffer));
/// ```
///
/// Writing HTML to standard output:
/// ```
/// use html_generator::OutputDestination;
///
/// let output = OutputDestination::Stdout;
/// ```
#[non_exhaustive]
pub enum OutputDestination {
/// Write output to a file at the specified path.
///
/// # Example
///
/// ```
/// use html_generator::OutputDestination;
///
/// let output = OutputDestination::File("output.html".to_string());
/// ```
File(String),
/// Write output using a custom writer implementation.
///
/// This can be used for in-memory buffers, network streams,
/// or other custom output destinations.
///
/// # Example
///
/// ```
/// use std::io::Cursor;
/// use html_generator::OutputDestination;
///
/// let buffer = Cursor::new(Vec::new());
/// let output = OutputDestination::Writer(Box::new(buffer));
/// ```
Writer(Box<dyn Write>),
/// Write output to standard output (default).
///
/// This is useful for command-line tools and scripts.
///
/// # Example
///
/// ```
/// use html_generator::OutputDestination;
///
/// let output = OutputDestination::Stdout;
/// ```
Stdout,
}
/// Default implementation for OutputDestination.
impl Default for OutputDestination {
fn default() -> Self {
Self::Stdout
}
}
/// Debug implementation for OutputDestination.
impl fmt::Debug for OutputDestination {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::File(path) => {
f.debug_tuple("File").field(path).finish()
}
Self::Writer(_) => write!(f, "Writer(<dyn Write>)"),
Self::Stdout => write!(f, "Stdout"),
}
}
}
/// Implements `Display` for `OutputDestination`.
impl fmt::Display for OutputDestination {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OutputDestination::File(path) => {
write!(f, "File({})", path)
}
OutputDestination::Writer(_) => {
write!(f, "Writer(<dyn Write>)")
}
OutputDestination::Stdout => write!(f, "Stdout"),
}
}
}
/// Configuration options for HTML generation.
///
/// Controls various aspects of the HTML generation process including
/// syntax highlighting, accessibility features, and output formatting.
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct HtmlConfig {
/// Enable syntax highlighting for code blocks
pub enable_syntax_highlighting: bool,
/// Theme to use for syntax highlighting
pub syntax_theme: Option<String>,
/// Minify the generated HTML output
pub minify_output: bool,
/// Automatically add ARIA attributes for accessibility
pub add_aria_attributes: bool,
/// Generate structured data (JSON-LD) based on content
pub generate_structured_data: bool,
/// Maximum size (in bytes) for input content
pub max_input_size: usize,
/// Language for generated content
pub language: String,
/// Enable table of contents generation
pub generate_toc: bool,
}
impl Default for HtmlConfig {
fn default() -> Self {
Self {
enable_syntax_highlighting: true,
syntax_theme: Some("github".to_string()),
minify_output: false,
add_aria_attributes: true,
generate_structured_data: false,
max_input_size: constants::DEFAULT_MAX_INPUT_SIZE,
language: String::from(constants::DEFAULT_LANGUAGE),
generate_toc: false,
}
}
}
impl HtmlConfig {
/// Creates a new `HtmlConfig` using the builder pattern.
///
/// # Examples
///
/// ```rust
/// use html_generator::HtmlConfig;
///
/// let config = HtmlConfig::builder()
/// .with_syntax_highlighting(true, Some("monokai".to_string()))
/// .with_language("en-GB")
/// .build()
/// .unwrap();
/// ```
pub fn builder() -> HtmlConfigBuilder {
HtmlConfigBuilder::default()
}
/// Validates the configuration settings.
///
/// Checks that all configuration values are within acceptable ranges
/// and conform to required formats.
///
/// # Returns
///
/// Returns `Ok(())` if the configuration is valid, or an appropriate
/// error if validation fails.
pub fn validate(&self) -> Result<()> {
if self.max_input_size < constants::MIN_INPUT_SIZE {
return Err(HtmlError::InvalidInput(format!(
"Input size must be at least {} bytes",
constants::MIN_INPUT_SIZE
)));
}
if !validate_language_code(&self.language) {
return Err(HtmlError::InvalidInput(format!(
"Invalid language code: {}",
self.language
)));
}
Ok(())
}
/// Validates file path safety to prevent directory traversal attacks.
///
/// # Arguments
///
/// * `path` - The file path to validate
///
/// # Returns
///
/// Returns `Ok(())` if the path is safe, or an appropriate error
/// if validation fails.
pub(crate) fn validate_file_path(
path: impl AsRef<Path>,
) -> Result<()> {
let path = path.as_ref();
if path.to_string_lossy().is_empty() {
return Err(HtmlError::InvalidInput(
"File path cannot be empty".to_string(),
));
}
if path.to_string_lossy().len() > constants::MAX_PATH_LENGTH {
return Err(HtmlError::InvalidInput(format!(
"File path exceeds maximum length of {} characters",
constants::MAX_PATH_LENGTH
)));
}
if path.components().any(|c| matches!(c, Component::ParentDir))
{
return Err(HtmlError::InvalidInput(
"Directory traversal is not allowed in file paths"
.to_string(),
));
}
#[cfg(not(test))]
if path.is_absolute() {
return Err(HtmlError::InvalidInput(
"Only relative file paths are allowed".to_string(),
));
}
if let Some(ext) = path.extension() {
if !matches!(ext.to_string_lossy().as_ref(), "md" | "html")
{
return Err(HtmlError::InvalidInput(
"Invalid file extension: only .md and .html files are allowed".to_string(),
));
}
}
Ok(())
}
}
/// Builder for constructing `HtmlConfig` instances.
///
/// Provides a fluent interface for creating and customizing HTML
/// configuration options.
#[derive(Debug, Default)]
pub struct HtmlConfigBuilder {
config: HtmlConfig,
}
impl HtmlConfigBuilder {
/// Creates a new `HtmlConfigBuilder` with default options.
pub fn new() -> Self {
Self::default()
}
/// Enables or disables syntax highlighting for code blocks.
///
/// # Arguments
///
/// * `enable` - Whether to enable syntax highlighting
/// * `theme` - Optional theme name for syntax highlighting
#[must_use]
pub fn with_syntax_highlighting(
mut self,
enable: bool,
theme: Option<String>,
) -> Self {
self.config.enable_syntax_highlighting = enable;
self.config.syntax_theme = if enable {
theme.or_else(|| Some("github".to_string()))
} else {
None
};
self
}
/// Sets the language for generated content.
///
/// # Arguments
///
/// * `language` - The language code (e.g., "en-GB")
#[must_use]
pub fn with_language(
mut self,
language: impl Into<String>,
) -> Self {
self.config.language = language.into();
self
}
/// Builds the configuration, validating all settings.
///
/// # Returns
///
/// Returns the validated configuration or an error if validation fails.
pub fn build(self) -> Result<HtmlConfig> {
self.config.validate()?;
Ok(self.config)
}
}
/// Converts Markdown content to HTML.
///
/// This function processes Unicode Markdown content and returns HTML output.
/// The input must be valid Unicode - if your input is encoded (e.g., UTF-8),
/// you must decode it before passing it to this function.
///
/// # Arguments
///
/// * `content` - The Markdown content as a Unicode string
/// * `config` - Optional configuration for the conversion
///
/// # Returns
///
/// Returns the generated HTML as a Unicode string wrapped in a `Result`
///
/// # Errors
///
/// Returns an error if:
/// * The input content is invalid Unicode
/// * HTML generation fails
/// * Input size exceeds configured maximum
///
/// # Examples
///
/// ```rust
/// use html_generator::{markdown_to_html, MarkdownConfig};
///
/// let markdown = "# Hello\n\nWorld";
/// let html = markdown_to_html(markdown, None)?;
/// assert!(html.contains("<h1>Hello</h1>"));
/// # Ok::<(), html_generator::error::HtmlError>(())
/// ```
pub fn markdown_to_html(
content: &str,
config: Option<MarkdownConfig>,
) -> Result<String> {
let config = config.unwrap_or_default();
if content.is_empty() {
return Err(HtmlError::InvalidInput(
"Input content is empty".to_string(),
));
}
if content.len() > config.html_config.max_input_size {
return Err(HtmlError::InputTooLarge(content.len()));
}
generate_html(content, &config.html_config)
}
/// Converts a Markdown file to HTML.
///
/// This function reads from a file or stdin and writes the generated HTML to
/// a specified destination. It handles encoding/decoding of content.
///
/// # Arguments
///
/// * `input` - The input source (file path or None for stdin)
/// * `output` - The output destination (defaults to stdout)
/// * `config` - Optional configuration including encoding settings
///
/// # Returns
///
/// Returns `Result<()>` indicating success or failure of the operation.
///
/// # Errors
///
/// Returns an error if:
/// * Input file is not found or cannot be read
/// * Output file cannot be written
/// * Configuration is invalid
/// * Input size exceeds configured maximum
///
/// # Examples
///
/// ```no_run
/// use html_generator::{markdown_file_to_html, OutputDestination, MarkdownConfig};
/// use std::path::{Path, PathBuf};
///
/// // Convert file to HTML and write to stdout
/// markdown_file_to_html(
/// Some(PathBuf::from("input.md")),
/// None,
/// None,
/// )?;
///
/// // Convert stdin to HTML file
/// markdown_file_to_html(
/// None::<PathBuf>, // Explicit type annotation
/// Some(OutputDestination::File("output.html".into())),
/// Some(MarkdownConfig::default()),
/// )?;
/// # Ok::<(), html_generator::error::HtmlError>(())
/// ```
#[inline]
pub fn markdown_file_to_html(
input: Option<impl AsRef<Path>>,
output: Option<OutputDestination>,
config: Option<MarkdownConfig>,
) -> Result<()> {
let config = config.unwrap_or_default();
let output = output.unwrap_or_default();
// Validate paths first
validate_paths(&input, &output)?;
// Read and process input
let content = read_input(input)?;
// Generate HTML
let html = markdown_to_html(&content, Some(config))?;
// Write output
write_output(output, html.as_bytes())
}
/// Validates input and output paths
fn validate_paths(
input: &Option<impl AsRef<Path>>,
output: &OutputDestination,
) -> Result<()> {
if let Some(path) = input.as_ref() {
HtmlConfig::validate_file_path(path)?;
}
if let OutputDestination::File(ref path) = output {
HtmlConfig::validate_file_path(path)?;
}
Ok(())
}
/// Reads content from the input source
fn read_input(input: Option<impl AsRef<Path>>) -> Result<String> {
match input {
Some(path) => {
let file = File::open(path).map_err(HtmlError::Io)?;
let mut reader =
BufReader::with_capacity(MAX_BUFFER_SIZE, file);
let mut content = String::with_capacity(MAX_BUFFER_SIZE);
let _ =
reader.read_to_string(&mut content).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to read input: {}", e),
))
})?;
Ok(content)
}
None => {
let stdin = io::stdin();
let mut reader =
BufReader::with_capacity(MAX_BUFFER_SIZE, stdin.lock());
let mut content = String::with_capacity(MAX_BUFFER_SIZE);
let _ =
reader.read_to_string(&mut content).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to read from stdin: {}", e),
))
})?;
Ok(content)
}
}
}
/// Writes content to the output destination
fn write_output(
output: OutputDestination,
content: &[u8],
) -> Result<()> {
match output {
OutputDestination::File(path) => {
let file = File::create(&path).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to create file '{}': {}", path, e),
))
})?;
let mut writer = BufWriter::new(file);
writer.write_all(content).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!(
"Failed to write to file '{}': {}",
path, e
),
))
})?;
writer.flush().map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!(
"Failed to flush output to file '{}': {}",
path, e
),
))
})?;
}
OutputDestination::Writer(mut writer) => {
let mut buffered = BufWriter::new(&mut writer);
buffered.write_all(content).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to write to output: {}", e),
))
})?;
buffered.flush().map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to flush output: {}", e),
))
})?;
}
OutputDestination::Stdout => {
let stdout = io::stdout();
let mut writer = BufWriter::new(stdout.lock());
writer.write_all(content).map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to write to stdout: {}", e),
))
})?;
writer.flush().map_err(|e| {
HtmlError::Io(io::Error::new(
e.kind(),
format!("Failed to flush stdout: {}", e),
))
})?;
}
}
Ok(())
}
/// Validates that a language code matches the BCP 47 format (e.g., "en-GB").
///
/// This function checks if a given language code follows the BCP 47 format,
/// which requires both language and region codes.
///
/// # Arguments
///
/// * `lang` - The language code to validate
///
/// # Returns
///
/// Returns true if the language code is valid (e.g., "en-GB"), false otherwise.
///
/// # Examples
///
/// ```
/// use html_generator::validate_language_code;
///
/// assert!(validate_language_code("en-GB")); // Valid
/// assert!(!validate_language_code("en")); // Invalid - missing region
/// assert!(!validate_language_code("123")); // Invalid - not a language code
/// assert!(!validate_language_code("en_GB")); // Invalid - wrong separator
/// ```
pub fn validate_language_code(lang: &str) -> bool {
use once_cell::sync::Lazy;
use regex::Regex;
// Pre-compiled regex using Lazy<Regex>
static LANG_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^[a-z]{2}(?:-[A-Z]{2})$")
.expect("Failed to compile language code regex")
});
// Match the input against the pre-compiled regex
LANG_REGEX.is_match(lang)
}
#[cfg(test)]
mod tests {
use super::*;
use regex::Regex;
use std::io::Cursor;
use tempfile::{tempdir, TempDir};
/// Creates a temporary test directory for file operations.
///
/// The directory and its contents are automatically cleaned up when
/// the returned TempDir is dropped.
fn setup_test_dir() -> TempDir {
tempdir().expect("Failed to create temporary directory")
}
/// Creates a test file with the given content.
///
/// # Arguments
///
/// * `dir` - The temporary directory to create the file in
/// * `content` - The content to write to the file
///
/// # Returns
///
/// Returns the path to the created file.
fn create_test_file(
dir: &TempDir,
content: &str,
) -> std::path::PathBuf {
let path = dir.path().join("test.md");
std::fs::write(&path, content)
.expect("Failed to write test file");
path
}
mod config_tests {
use super::*;
#[test]
fn test_config_validation() {
// Test invalid input size
let config = HtmlConfig {
max_input_size: 100, // Too small
..Default::default()
};
assert!(config.validate().is_err());
// Test invalid language code
let config = HtmlConfig {
language: "invalid".to_string(),
..Default::default()
};
assert!(config.validate().is_err());
// Test valid default configuration
let config = HtmlConfig::default();
assert!(config.validate().is_ok());
}
#[test]
fn test_config_builder() {
let result = HtmlConfigBuilder::new()
.with_syntax_highlighting(
true,
Some("monokai".to_string()),
)
.with_language("en-GB")
.build();
assert!(result.is_ok());
let config = result.unwrap();
assert!(config.enable_syntax_highlighting);
assert_eq!(
config.syntax_theme,
Some("monokai".to_string())
);
assert_eq!(config.language, "en-GB");
}
#[test]
fn test_config_builder_invalid() {
let result = HtmlConfigBuilder::new()
.with_language("invalid")
.build();
assert!(matches!(
result,
Err(HtmlError::InvalidInput(msg)) if msg.contains("Invalid language code")
));
}
#[test]
fn test_html_config_with_no_syntax_theme() {
let config = HtmlConfig {
enable_syntax_highlighting: true,
syntax_theme: None,
..Default::default()
};
assert!(config.validate().is_ok());
}
#[test]
fn test_file_conversion_with_large_output() -> Result<()> {
let temp_dir = setup_test_dir();
let input_path = create_test_file(
&temp_dir,
"# Large\n\nContent".repeat(10_000).as_str(),
);
let output_path = temp_dir.path().join("large_output.html");
let result = markdown_file_to_html(
Some(&input_path),
Some(OutputDestination::File(
output_path.to_string_lossy().into(),
)),
None,
);
assert!(result.is_ok());
let content = std::fs::read_to_string(output_path)?;
assert!(content.contains("<h1>Large</h1>"));
Ok(())
}
#[test]
fn test_markdown_with_broken_syntax() {
let markdown = "# Unmatched Header\n**Bold start";
let result = markdown_to_html(markdown, None);
assert!(result.is_ok());
let html = result.unwrap();
assert!(html.contains("<h1>Unmatched Header</h1>"));
assert!(html.contains("**Bold start</p>")); // Ensure content is preserved
}
#[test]
fn test_language_code_with_custom_regex() {
let custom_lang_regex =
Regex::new(r"^[a-z]{2}-[A-Z]{2}$").unwrap();
assert!(custom_lang_regex.is_match("en-GB"));
assert!(!custom_lang_regex.is_match("EN-gb")); // Case-sensitive check
}
#[test]
fn test_markdown_to_html_error_handling() {
let result = markdown_to_html("", None);
assert!(matches!(result, Err(HtmlError::InvalidInput(_))));
let oversized_input =
"a".repeat(constants::DEFAULT_MAX_INPUT_SIZE + 1);
let result = markdown_to_html(&oversized_input, None);
assert!(matches!(result, Err(HtmlError::InputTooLarge(_))));
}
#[test]
fn test_performance_with_nested_lists() {
let nested_list = "- Item\n".repeat(1000);
let result = markdown_to_html(&nested_list, None);
assert!(result.is_ok());
let html = result.unwrap();
assert!(html.matches("<li>").count() == 1000);
}
}
mod file_validation_tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_valid_paths() {
let valid_paths = [
PathBuf::from("test.md"),
PathBuf::from("test.html"),
PathBuf::from("subfolder/test.md"),
];
for path in valid_paths {
assert!(
HtmlConfig::validate_file_path(&path).is_ok(),
"Path should be valid: {:?}",
path
);
}
}
#[test]
fn test_invalid_paths() {
let invalid_paths = [
PathBuf::from(""), // Empty path
PathBuf::from("../test.md"), // Directory traversal
PathBuf::from("test.exe"), // Invalid extension
PathBuf::from(
"a".repeat(constants::MAX_PATH_LENGTH + 1),
), // Too long
];
for path in invalid_paths {
assert!(
HtmlConfig::validate_file_path(&path).is_err(),
"Path should be invalid: {:?}",
path
);
}
}
}
mod markdown_conversion_tests {
use super::*;
#[test]
fn test_basic_conversion() {
let markdown = "# Test\n\nHello world";
let result = markdown_to_html(markdown, None);
assert!(result.is_ok());
let html = result.unwrap();
assert!(html.contains("<h1>Test</h1>"));
assert!(html.contains("<p>Hello world</p>"));
}
#[test]
fn test_conversion_with_config() {
let markdown = "# Test\n```rust\nfn main() {}\n```";
let config = MarkdownConfig {
html_config: HtmlConfig {
enable_syntax_highlighting: true,
..Default::default()
},
..Default::default()
};
let result = markdown_to_html(markdown, Some(config));
assert!(result.is_ok());
assert!(result.unwrap().contains("language-rust"));
}
#[test]
fn test_empty_content() {
assert!(matches!(
markdown_to_html("", None),
Err(HtmlError::InvalidInput(_))
));
}
#[test]
fn test_content_too_large() {
let large_content =
"a".repeat(constants::DEFAULT_MAX_INPUT_SIZE + 1);
assert!(matches!(
markdown_to_html(&large_content, None),
Err(HtmlError::InputTooLarge(_))
));
}
}
mod file_operation_tests {
use super::*;
#[test]
fn test_file_conversion() -> Result<()> {
let temp_dir = setup_test_dir();
let input_path =
create_test_file(&temp_dir, "# Test\n\nHello world");
let output_path = temp_dir.path().join("test.html");
markdown_file_to_html(
Some(&input_path),
Some(OutputDestination::File(
output_path.to_string_lossy().into(),
)),
None::<MarkdownConfig>,
)?;
let content = std::fs::read_to_string(output_path)?;
assert!(content.contains("<h1>Test</h1>"));
Ok(())
}
#[test]
fn test_writer_output() {
let temp_dir = setup_test_dir();
let input_path =
create_test_file(&temp_dir, "# Test\nHello");
let buffer = Box::new(Cursor::new(Vec::new()));
let result = markdown_file_to_html(
Some(&input_path),
Some(OutputDestination::Writer(buffer)),
None,
);
assert!(result.is_ok());
}
#[test]
fn test_writer_output_no_input() {
let buffer = Box::new(Cursor::new(Vec::new()));
let result = markdown_file_to_html(
Some(Path::new("nonexistent.md")),
Some(OutputDestination::Writer(buffer)),
None,
);
assert!(result.is_err());
}
}
mod language_validation_tests {
use super::*;
#[test]
fn test_valid_language_codes() {
let valid_codes =
["en-GB", "fr-FR", "de-DE", "es-ES", "zh-CN"];
for code in valid_codes {
assert!(
validate_language_code(code),
"Language code '{}' should be valid",
code
);
}
}
#[test]
fn test_invalid_language_codes() {
let invalid_codes = [
"", // Empty
"en", // Missing region
"eng-GBR", // Wrong format
"en_GB", // Wrong separator
"123-45", // Invalid characters
"GB-en", // Wrong order
"en-gb", // Wrong case
];
for code in invalid_codes {
assert!(
!validate_language_code(code),
"Language code '{}' should be invalid",
code
);
}
}
}
mod integration_tests {
use super::*;
#[test]
fn test_end_to_end_conversion() -> Result<()> {
let temp_dir = setup_test_dir();
let content = r#"---
title: Test Document
---
# Hello World
This is a test document with:
- A list
- And some **bold** text
"#;
let input_path = create_test_file(&temp_dir, content);
let output_path = temp_dir.path().join("test.html");
let config = MarkdownConfig {
html_config: HtmlConfig {
enable_syntax_highlighting: true,
generate_toc: true,
..Default::default()
},
..Default::default()
};
markdown_file_to_html(
Some(&input_path),
Some(OutputDestination::File(
output_path.to_string_lossy().into(),
)),
Some(config),
)?;
let html = std::fs::read_to_string(&output_path)?;
assert!(html.contains("<h1>Hello World</h1>"));
assert!(html.contains("<strong>bold</strong>"));
assert!(html.contains("<ul>"));
Ok(())
}
#[test]
fn test_output_destination_debug() {
assert_eq!(
format!(
"{:?}",
OutputDestination::File("test.html".to_string())
),
r#"File("test.html")"#
);
assert_eq!(
format!("{:?}", OutputDestination::Stdout),
"Stdout"
);
let writer = Box::new(Cursor::new(Vec::new()));
assert_eq!(
format!("{:?}", OutputDestination::Writer(writer)),
"Writer(<dyn Write>)"
);
}
}
mod markdown_config_tests {
use super::*;
#[test]
fn test_markdown_config_custom_encoding() {
let config = MarkdownConfig {
encoding: "latin1".to_string(),
html_config: HtmlConfig::default(),
};
assert_eq!(config.encoding, "latin1");
}
#[test]
fn test_markdown_config_default() {
let config = MarkdownConfig::default();
assert_eq!(config.encoding, "utf-8");
assert_eq!(config.html_config, HtmlConfig::default());
}
#[test]
fn test_markdown_config_clone() {
let config = MarkdownConfig::default();
let cloned = config.clone();
assert_eq!(config, cloned);
}
}
mod config_error_tests {
use super::*;
#[test]
fn test_config_error_display() {
let error = ConfigError::InvalidInputSize(100, 1024);
assert!(error.to_string().contains("Invalid input size"));
let error =
ConfigError::InvalidLanguageCode("xx".to_string());
assert!(error
.to_string()
.contains("Invalid language code"));
let error =
ConfigError::InvalidFilePath("../bad/path".to_string());
assert!(error.to_string().contains("Invalid file path"));
}
}
mod output_destination_tests {
use super::*;
#[test]
fn test_output_destination_default() {
assert!(matches!(
OutputDestination::default(),
OutputDestination::Stdout
));
}
#[test]
fn test_output_destination_file() {
let dest = OutputDestination::File("test.html".to_string());
assert!(matches!(dest, OutputDestination::File(_)));
}
#[test]
fn test_output_destination_writer() {
let writer = Box::new(Cursor::new(Vec::new()));
let dest = OutputDestination::Writer(writer);
assert!(matches!(dest, OutputDestination::Writer(_)));
}
}
mod html_config_tests {
use super::*;
#[test]
fn test_html_config_builder_all_options() {
let config = HtmlConfig::builder()
.with_syntax_highlighting(
true,
Some("dracula".to_string()),
)
.with_language("en-US")
.build()
.unwrap();
assert!(config.enable_syntax_highlighting);
assert_eq!(
config.syntax_theme,
Some("dracula".to_string())
);
assert_eq!(config.language, "en-US");
}
#[test]
fn test_html_config_validation_edge_cases() {
let config = HtmlConfig {
max_input_size: constants::MIN_INPUT_SIZE,
..Default::default()
};
assert!(config.validate().is_ok());
let config = HtmlConfig {
max_input_size: constants::MIN_INPUT_SIZE - 1,
..Default::default()
};
assert!(config.validate().is_err());
}
}
mod markdown_processing_tests {
use super::*;
#[test]
fn test_markdown_to_html_with_front_matter() -> Result<()> {
let markdown = r#"---
title: Test
author: Test Author
---
# Heading
Content"#;
let html = markdown_to_html(markdown, None)?;
assert!(html.contains("<h1>Heading</h1>"));
assert!(html.contains("<p>Content</p>"));
Ok(())
}
#[test]
fn test_markdown_to_html_with_code_blocks() -> Result<()> {
let markdown = r#"```rust
fn main() {
println!("Hello");
}
```"#;
let config = MarkdownConfig {
html_config: HtmlConfig {
enable_syntax_highlighting: true,
..Default::default()
},
..Default::default()
};
let html = markdown_to_html(markdown, Some(config))?;
assert!(html.contains("language-rust"));
Ok(())
}
#[test]
fn test_markdown_to_html_with_tables() -> Result<()> {
let markdown = r#"
| Header 1 | Header 2 |
|----------|----------|
| Cell 1 | Cell 2 |
"#;
let html = markdown_to_html(markdown, None)?;
// First verify the HTML output to see what we're getting
println!("Generated HTML for table: {}", html);
// Check for common table elements - div wrapper is often used for table responsiveness
assert!(html.contains("Header 1"));
assert!(html.contains("Cell 1"));
assert!(html.contains("Cell 2"));
Ok(())
}
#[test]
fn test_invalid_encoding_handling() {
let config = MarkdownConfig {
encoding: "unsupported-encoding".to_string(),
html_config: HtmlConfig::default(),
};
// Simulate usage where encoding matters
let result = markdown_to_html("# Test", Some(config));
assert!(result.is_ok()); // Assuming encoding isn't directly validated during processing
}
#[test]
fn test_config_error_types() {
let error = ConfigError::InvalidInputSize(512, 1024);
assert_eq!(format!("{}", error), "Invalid input size: 512 bytes is below minimum of 1024 bytes");
}
}
mod file_processing_tests {
use crate::constants;
use crate::HtmlConfig;
use crate::{
markdown_file_to_html, HtmlError, OutputDestination,
};
use std::io::Cursor;
use std::path::Path;
use tempfile::NamedTempFile;
#[test]
fn test_display_file() {
let output =
OutputDestination::File("output.html".to_string());
let display = format!("{}", output);
assert_eq!(display, "File(output.html)");
}
#[test]
fn test_display_stdout() {
let output = OutputDestination::Stdout;
let display = format!("{}", output);
assert_eq!(display, "Stdout");
}
#[test]
fn test_display_writer() {
let buffer = Cursor::new(Vec::new());
let output = OutputDestination::Writer(Box::new(buffer));
let display = format!("{}", output);
assert_eq!(display, "Writer(<dyn Write>)");
}
#[test]
fn test_debug_file() {
let output =
OutputDestination::File("output.html".to_string());
let debug = format!("{:?}", output);
assert_eq!(debug, r#"File("output.html")"#);
}
#[test]
fn test_debug_stdout() {
let output = OutputDestination::Stdout;
let debug = format!("{:?}", output);
assert_eq!(debug, "Stdout");
}
#[test]
fn test_debug_writer() {
let buffer = Cursor::new(Vec::new());
let output = OutputDestination::Writer(Box::new(buffer));
let debug = format!("{:?}", output);
assert_eq!(debug, "Writer(<dyn Write>)");
}
#[test]
fn test_file_to_html_invalid_input() {
let result = markdown_file_to_html(
Some(Path::new("nonexistent.md")),
None,
None,
);
assert!(matches!(result, Err(HtmlError::Io(_))));
}
#[test]
fn test_file_to_html_with_invalid_output_path(
) -> Result<(), HtmlError> {
let input = NamedTempFile::new()?;
std::fs::write(&input, "# Test")?;
let result = markdown_file_to_html(
Some(input.path()),
Some(OutputDestination::File(
"/invalid/path/test.html".to_string(),
)),
None,
);
assert!(result.is_err());
Ok(())
}
// Test for Default implementation of OutputDestination
#[test]
fn test_output_destination_default() {
let default = OutputDestination::default();
assert!(matches!(default, OutputDestination::Stdout));
}
// Test for Debug implementation of OutputDestination
#[test]
fn test_output_destination_debug() {
let file_debug = format!(
"{:?}",
OutputDestination::File(
"path/to/file.html".to_string()
)
);
assert_eq!(file_debug, r#"File("path/to/file.html")"#);
let writer_debug = format!(
"{:?}",
OutputDestination::Writer(Box::new(Cursor::new(
Vec::new()
)))
);
assert_eq!(writer_debug, "Writer(<dyn Write>)");
let stdout_debug =
format!("{:?}", OutputDestination::Stdout);
assert_eq!(stdout_debug, "Stdout");
}
// Test for Display implementation of OutputDestination
#[test]
fn test_output_destination_display() {
let file_display = format!(
"{}",
OutputDestination::File(
"path/to/file.html".to_string()
)
);
assert_eq!(file_display, "File(path/to/file.html)");
let writer_display = format!(
"{}",
OutputDestination::Writer(Box::new(Cursor::new(
Vec::new()
)))
);
assert_eq!(writer_display, "Writer(<dyn Write>)");
let stdout_display =
format!("{}", OutputDestination::Stdout);
assert_eq!(stdout_display, "Stdout");
}
// Test for Default implementation of HtmlConfig
#[test]
fn test_html_config_default() {
let default = HtmlConfig::default();
assert!(default.enable_syntax_highlighting);
assert_eq!(
default.syntax_theme,
Some("github".to_string())
);
assert!(!default.minify_output);
assert!(default.add_aria_attributes);
assert!(!default.generate_structured_data);
assert_eq!(
default.max_input_size,
constants::DEFAULT_MAX_INPUT_SIZE
);
assert_eq!(
default.language,
constants::DEFAULT_LANGUAGE.to_string()
);
assert!(!default.generate_toc);
}
// Test for HtmlConfigBuilder
#[test]
fn test_html_config_builder() {
let builder = HtmlConfig::builder()
.with_syntax_highlighting(
true,
Some("monokai".to_string()),
)
.with_language("en-US")
.build()
.unwrap();
assert!(builder.enable_syntax_highlighting);
assert_eq!(
builder.syntax_theme,
Some("monokai".to_string())
);
assert_eq!(builder.language, "en-US");
}
// Test for long file path validation
#[test]
fn test_long_file_path_validation() {
let long_path = "a".repeat(constants::MAX_PATH_LENGTH + 1);
let result = HtmlConfig::validate_file_path(long_path);
assert!(
matches!(result, Err(HtmlError::InvalidInput(ref msg)) if msg.contains("File path exceeds maximum length"))
);
}
// Test for relative file path validation
#[test]
fn test_relative_file_path_validation() {
#[cfg(not(test))]
{
let absolute_path = "/absolute/path/to/file.md";
let result =
HtmlConfig::validate_file_path(absolute_path);
assert!(
matches!(result, Err(HtmlError::InvalidInput(ref msg)) if msg.contains("Only relative file paths are allowed"))
);
}
}
}
mod language_validation_extended_tests {
use super::*;
#[test]
fn test_language_code_edge_cases() {
// Test empty string
assert!(!validate_language_code(""));
// Test single character
assert!(!validate_language_code("a"));
// Test incorrect casing
assert!(!validate_language_code("EN-GB"));
assert!(!validate_language_code("en-gb"));
// Test invalid separators
assert!(!validate_language_code("en_GB"));
assert!(!validate_language_code("en GB"));
// Test too many segments
assert!(!validate_language_code("en-GB-extra"));
}
#[test]
fn test_language_code_special_cases() {
// Test with numbers
assert!(!validate_language_code("e1-GB"));
assert!(!validate_language_code("en-G1"));
// Test with special characters
assert!(!validate_language_code("en-GB!"));
assert!(!validate_language_code("en@GB"));
// Test with Unicode characters
assert!(!validate_language_code("あa-GB"));
assert!(!validate_language_code("en-あa"));
}
}
mod integration_extended_tests {
use super::*;
#[test]
fn test_full_conversion_pipeline() -> Result<()> {
// Create temporary files
let temp_dir = tempdir()?;
let input_path = temp_dir.path().join("test.md");
let output_path = temp_dir.path().join("test.html");
// Test content with various Markdown features
let content = r#"---
title: Test Document
author: Test Author
---
# Main Heading
## Subheading
This is a paragraph with *italic* and **bold** text.
- List item 1
- List item 2
- Nested item
- Another nested item
```rust
fn main() {
println!("Hello, world!");
}
```
| Column 1 | Column 2 |
|----------|----------|
| Cell 1 | Cell 2 |
> This is a blockquote
[Link text](https://example.com)"#;
std::fs::write(&input_path, content)?;
// Configure with all features enabled
let config = MarkdownConfig {
html_config: HtmlConfig {
enable_syntax_highlighting: true,
generate_toc: true,
add_aria_attributes: true,
generate_structured_data: true,
minify_output: true,
..Default::default()
},
..Default::default()
};
markdown_file_to_html(
Some(&input_path),
Some(OutputDestination::File(
output_path.to_string_lossy().into(),
)),
Some(config),
)?;
let html = std::fs::read_to_string(&output_path)?;
// Verify all expected elements are present
println!("Generated HTML: {}", html);
assert!(html.contains("<h1>"));
assert!(html.contains("<h2>"));
assert!(html.contains("<em>"));
assert!(html.contains("<strong>"));
assert!(html.contains("<ul>"));
assert!(html.contains("<li>"));
assert!(html.contains("language-rust"));
// Verify table content instead of specific HTML structure
assert!(html.contains("Column 1"));
assert!(html.contains("Column 2"));
assert!(html.contains("Cell 1"));
assert!(html.contains("Cell 2"));
assert!(html.contains("<blockquote>"));
assert!(html.contains("<a href="));
Ok(())
}
#[test]
fn test_missing_html_config_fallback() {
let config = MarkdownConfig {
encoding: "utf-8".to_string(),
html_config: HtmlConfig {
enable_syntax_highlighting: false,
syntax_theme: None,
..Default::default()
},
};
let result = markdown_to_html("# Test", Some(config));
assert!(result.is_ok());
}
#[test]
fn test_invalid_output_destination() {
let result = markdown_file_to_html(
Some(Path::new("test.md")),
Some(OutputDestination::File(
"/root/forbidden.html".to_string(),
)),
None,
);
assert!(result.is_err());
}
}
mod performance_tests {
use super::*;
use std::time::Instant;
#[test]
fn test_large_document_performance() -> Result<()> {
let base_content =
"# Heading\n\nParagraph\n\n- List item\n\n";
let large_content = base_content.repeat(1000);
let start = Instant::now();
let html = markdown_to_html(&large_content, None)?;
let duration = start.elapsed();
// Log performance metrics
println!("Large document conversion took: {:?}", duration);
println!("Input size: {} bytes", large_content.len());
println!("Output size: {} bytes", html.len());
// Basic validation
assert!(html.contains("<h1>"));
assert!(html.contains("<p>"));
assert!(html.contains("<ul>"));
Ok(())
}
}
}