@@ -398,6 +398,89 @@ fn test_json_writer() {
398398 ) ;
399399}
400400
401+ /// Normalize HTML for comparison by removing extra whitespace
402+ fn normalize_html ( html : & str ) -> String {
403+ // First, join all lines with spaces to handle attributes split across lines
404+ let single_line = html
405+ . lines ( )
406+ . map ( |line| line. trim ( ) )
407+ . collect :: < Vec < _ > > ( )
408+ . join ( " " ) ;
409+
410+ // Then split by > to preserve tag boundaries
411+ single_line
412+ . split ( '>' )
413+ . map ( |s| s. trim ( ) )
414+ . filter ( |s| !s. is_empty ( ) )
415+ . collect :: < Vec < _ > > ( )
416+ . join ( ">\n " )
417+ }
418+
419+ #[ test]
420+ fn test_html_writer ( ) {
421+ assert ! (
422+ has_good_pandoc_version( ) ,
423+ "Pandoc version is not suitable for testing"
424+ ) ;
425+ let mut file_count = 0 ;
426+ for entry in glob ( "tests/writers/html/*.md" ) . expect ( "Failed to read glob pattern" ) {
427+ match entry {
428+ Ok ( path) => {
429+ eprintln ! ( "Opening file: {}" , path. display( ) ) ;
430+ let markdown = std:: fs:: read_to_string ( & path) . expect ( "Failed to read file" ) ;
431+
432+ // Parse with our parser
433+ let mut parser = MarkdownParser :: default ( ) ;
434+ let input_bytes = markdown. as_bytes ( ) ;
435+ let tree = parser
436+ . parse ( input_bytes, None )
437+ . expect ( "Failed to parse input" ) ;
438+ let pandoc = treesitter_to_pandoc (
439+ & mut std:: io:: sink ( ) ,
440+ & tree,
441+ input_bytes,
442+ & ASTContext :: anonymous ( ) ,
443+ )
444+ . unwrap ( ) ;
445+ let mut buf = Vec :: new ( ) ;
446+ writers:: html:: write ( & pandoc, & mut buf) . unwrap ( ) ;
447+ let our_html = String :: from_utf8 ( buf) . expect ( "Invalid UTF-8 in our HTML output" ) ;
448+
449+ // Get Pandoc's output
450+ let output = Command :: new ( "pandoc" )
451+ . arg ( "-t" )
452+ . arg ( "html" )
453+ . arg ( "-f" )
454+ . arg ( "markdown" )
455+ . arg ( & path)
456+ . output ( )
457+ . expect ( "Failed to execute pandoc" ) ;
458+
459+ let pandoc_html = String :: from_utf8 ( output. stdout ) . expect ( "Invalid UTF-8" ) ;
460+
461+ // Normalize both HTML outputs for comparison
462+ let our_normalized = normalize_html ( & our_html) ;
463+ let pandoc_normalized = normalize_html ( & pandoc_html) ;
464+
465+ assert_eq ! (
466+ our_normalized,
467+ pandoc_normalized,
468+ "HTML outputs don't match for file {}.\n \n Ours:\n {}\n \n Pandoc's:\n {}" ,
469+ path. display( ) ,
470+ our_html,
471+ pandoc_html
472+ ) ;
473+ file_count += 1 ;
474+ }
475+ Err ( e) => panic ! ( "Error reading glob entry: {}" , e) ,
476+ }
477+ }
478+ assert ! (
479+ file_count > 0 ,
480+ "No files found in tests/writers/html directory"
481+ ) ;
482+ }
483+
401484fn ensure_file_does_not_parse ( path : & std:: path:: Path ) {
402485 let markdown = std:: fs:: read_to_string ( path) . expect ( "Failed to read file" ) ;
403486 let mut parser = MarkdownParser :: default ( ) ;
0 commit comments