Skip to content

Commit 8055b07

Browse files
authored
Improve/json errors (#68)
* restore warnings in postprocessor * more json errors
1 parent 8780040 commit 8055b07

5 files changed

Lines changed: 122 additions & 208 deletions

File tree

crates/qmd-syntax-helper/src/main_old.rs

Lines changed: 0 additions & 201 deletions
This file was deleted.

crates/quarto-markdown-pandoc/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,17 @@ fn main() {
8989
}
9090

9191
if !input.ends_with("\n") {
92-
eprintln!("(Warning) Adding missing newline to end of input.");
93-
//
92+
if args.json_errors {
93+
// Output as JSON to stderr
94+
let warning_json = serde_json::json!({
95+
"title": "Warning",
96+
"message": "Adding missing newline to end of input"
97+
});
98+
eprintln!("{}", warning_json);
99+
} else {
100+
// Output as plain text to stderr
101+
eprintln!("(Warning) Adding missing newline to end of input.");
102+
}
94103
input.push('\n'); // ensure the input ends with a newline
95104
}
96105

crates/quarto-markdown-pandoc/src/readers/qmd.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::pandoc::{self, Block, Meta};
1616
use crate::pandoc::{MetaValue, rawblock_to_meta};
1717
use crate::readers::qmd_error_messages::{produce_error_message, produce_error_message_json};
1818
use crate::traversals;
19-
use crate::utils::error_collector::{JsonErrorCollector, TextErrorCollector};
19+
use crate::utils::error_collector::{ErrorCollector, JsonErrorCollector, TextErrorCollector};
2020
use std::io::Write;
2121
use tree_sitter::LogType;
2222
use tree_sitter_qmd::MarkdownParser;
@@ -140,26 +140,43 @@ where
140140
let context = ASTContext::with_filename(filename.to_string());
141141

142142
// Create appropriate error collector based on whether JSON errors are requested
143+
// and collect warnings after conversion
143144
let mut result = if error_formatter.is_some() {
144145
// JSON error format requested
145146
let mut error_collector = JsonErrorCollector::new();
146-
pandoc::treesitter_to_pandoc(
147+
let pandoc_result = pandoc::treesitter_to_pandoc(
147148
&mut output_stream,
148149
&tree,
149150
&input_bytes,
150151
&context,
151152
&mut error_collector,
152-
)?
153+
)?;
154+
155+
// Output warnings to stderr as JSON
156+
let warnings = error_collector.messages();
157+
for warning in warnings {
158+
eprintln!("{}", warning);
159+
}
160+
161+
pandoc_result
153162
} else {
154163
// Text error format (default)
155164
let mut error_collector = TextErrorCollector::new();
156-
pandoc::treesitter_to_pandoc(
165+
let pandoc_result = pandoc::treesitter_to_pandoc(
157166
&mut output_stream,
158167
&tree,
159168
&input_bytes,
160169
&context,
161170
&mut error_collector,
162-
)?
171+
)?;
172+
173+
// Output warnings to stderr as formatted text
174+
let warnings = error_collector.messages();
175+
for warning in warnings {
176+
eprintln!("{}", warning);
177+
}
178+
179+
pandoc_result
163180
};
164181
let mut meta_from_parses = Meta::default();
165182

crates/quarto-markdown-pandoc/tests/test_json_errors.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,24 @@ fn test_label_range_note_type() {
158158
"Should find at least one label-range note in the error"
159159
);
160160
}
161+
162+
#[test]
163+
fn test_missing_newline_warning_json_format() {
164+
// This test verifies that the missing newline warning is formatted as JSON
165+
// when --json-errors is used. Currently this test will fail because the
166+
// warning is always output as plain text.
167+
168+
// Create input without trailing newline
169+
let input = "# Hello World";
170+
171+
// We can't easily test the binary's stderr output from here, but we can
172+
// document the expected behavior: when --json-errors is used, the warning
173+
// should be output as:
174+
// {"title":"Warning","message":"Adding missing newline to end of input"}
175+
//
176+
// Currently it outputs:
177+
// (Warning) Adding missing newline to end of input.
178+
179+
// This test just documents the issue. The actual fix will be in main.rs
180+
// where the warning is emitted.
181+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use quarto_markdown_pandoc::readers;
2+
use quarto_markdown_pandoc::utils;
3+
4+
#[test]
5+
fn test_caption_without_table_warning() {
6+
// Create input with a caption after a div (not a table)
7+
// This should parse successfully but emit a warning
8+
let input = r#"::: {.my-div}
9+
Some content
10+
:::
11+
12+
: This caption has no table
13+
"#;
14+
15+
// Parse the document
16+
let result = readers::qmd::read(
17+
input.as_bytes(),
18+
false,
19+
"test.md",
20+
&mut std::io::sink(),
21+
None::<
22+
fn(&[u8], &utils::tree_sitter_log_observer::TreeSitterLogObserver, &str) -> Vec<String>,
23+
>,
24+
);
25+
26+
// Parsing should succeed (warnings are not errors)
27+
assert!(result.is_ok(), "Document should parse successfully despite warning");
28+
29+
// TODO: Once the fix is implemented, we need to verify that the warning
30+
// "Caption found without a preceding table" was actually output.
31+
// For now, this test just verifies that parsing succeeds.
32+
// After the fix, we'll need to capture stderr or modify the API
33+
// to return warnings alongside the successful parse result.
34+
}
35+
36+
#[test]
37+
fn test_caption_with_table_no_warning() {
38+
// Create input with a proper table caption
39+
// This should parse successfully with no warnings
40+
let input = r#"| A | B |
41+
|---|---|
42+
| 1 | 2 |
43+
44+
: Table caption
45+
"#;
46+
47+
// Parse the document
48+
let result = readers::qmd::read(
49+
input.as_bytes(),
50+
false,
51+
"test.md",
52+
&mut std::io::sink(),
53+
None::<
54+
fn(&[u8], &utils::tree_sitter_log_observer::TreeSitterLogObserver, &str) -> Vec<String>,
55+
>,
56+
);
57+
58+
// Parsing should succeed and no warnings should be emitted
59+
assert!(result.is_ok(), "Document with valid table caption should parse successfully");
60+
61+
let (pandoc, _context) = result.unwrap();
62+
63+
// Verify we have a table in the output
64+
assert!(
65+
pandoc.blocks.iter().any(|b| matches!(b, quarto_markdown_pandoc::pandoc::Block::Table(_))),
66+
"Should have a table in the output"
67+
);
68+
}

0 commit comments

Comments
 (0)