perl-lsp 0.2.4

A Perl LSP server built on tree-sitter-perl and tower-lsp
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
//! POD → Markdown converter using tree-sitter-pod.
//!
//! Sub-parses POD text with tree-sitter-pod to get a proper AST,
//! then walks the tree to render markdown. Handles nested lists,
//! data regions, multi-angle-bracket formatting, and proper inline nesting.

use tree_sitter::{Node, Parser};

/// Convert raw POD text to markdown. Caps output at ~2000 chars.
pub fn pod_to_markdown(pod_text: &str) -> String {
    let tree = match parse_pod(pod_text) {
        Some(t) => t,
        None => return String::new(),
    };
    let mut out = String::new();
    render_children(tree.root_node(), pod_text.as_bytes(), &mut out);

    let result = out.trim_end().to_string();
    if result.len() > 2000 {
        result[..2000].to_string()
    } else {
        result
    }
}

/// Extract a =head2 section for a given sub name from a POD block.
pub fn extract_head2_section(sub_name: &str, pod_text: &str) -> Option<String> {
    let tree = parse_pod(pod_text)?;
    let bytes = pod_text.as_bytes();
    let root = tree.root_node();

    let mut collecting = false;
    let mut section = String::new();

    for i in 0..root.named_child_count() {
        let child = root.named_child(i)?;
        if child.kind() == "command_paragraph" {
            let cmd = get_command_name(&child, bytes);
            if cmd == "=head2" {
                if collecting {
                    break; // next =head2 ends the section
                }
                let content = get_content_text(&child, bytes);
                let head_name = content.split(|c: char| c == '(' || c.is_whitespace()).next().unwrap_or("");
                if head_name == sub_name {
                    collecting = true;
                    continue; // don't include the =head2 line itself
                }
            } else if collecting && (cmd == "=head1" || cmd == "=cut") {
                break;
            }
        }
        if collecting {
            render_node(child, bytes, &mut section);
        }
    }

    let result = section.trim().to_string();
    if result.is_empty() { None } else { Some(result) }
}

/// Extract documentation for a sub from =item blocks within =over/=back.
pub fn extract_item_section(sub_name: &str, pod_text: &str) -> Option<String> {
    let tree = parse_pod(pod_text)?;
    let bytes = pod_text.as_bytes();
    let root = tree.root_node();

    let mut collecting = false;
    let mut section = String::new();

    for i in 0..root.named_child_count() {
        let child = root.named_child(i)?;
        if child.kind() == "command_paragraph" {
            let cmd = get_command_name(&child, bytes);
            if cmd == "=item" {
                if collecting {
                    break; // next =item ends the section
                }
                let content = get_content_text(&child, bytes);
                if let Some(name) = extract_item_method_name(&content) {
                    if name == sub_name {
                        collecting = true;
                        continue;
                    }
                }
            } else if collecting && (cmd == "=back" || cmd == "=head1" || cmd == "=head2" || cmd == "=cut") {
                break;
            }
        }
        if collecting {
            render_node(child, bytes, &mut section);
        }
    }

    let result = section.trim().to_string();
    if result.is_empty() { None } else { Some(result) }
}

// ---- Parser ----

fn parse_pod(pod_text: &str) -> Option<tree_sitter::Tree> {
    let mut parser = Parser::new();
    parser.set_language(&ts_parser_pod::LANGUAGE.into()).ok()?;
    parser.parse(pod_text, None)
}

// ---- Rendering ----

fn render_children(node: Node, source: &[u8], out: &mut String) {
    let count = node.named_child_count();
    let mut i = 0;
    while i < count {
        if let Some(child) = node.named_child(i) {
            // Merge consecutive verbatim paragraphs into one code block
            if child.kind() == "verbatim_paragraph" {
                out.push_str("```perl\n");
                render_verbatim_content(child, source, out);
                i += 1;
                while i < count {
                    if let Some(next) = node.named_child(i) {
                        if next.kind() == "verbatim_paragraph" {
                            out.push('\n');
                            render_verbatim_content(next, source, out);
                            i += 1;
                        } else {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                out.push_str("```\n\n");
                continue;
            }
            render_node(child, source, out);
            if out.len() > 2000 { return; }
        }
        i += 1;
    }
}

fn render_node(node: Node, source: &[u8], out: &mut String) {
    match node.kind() {
        "command_paragraph" => render_command(node, source, out),
        "plain_paragraph" => render_plain(node, source, out),
        "verbatim_paragraph" => render_verbatim(node, source, out),
        "begin_paragraph" => render_begin(node, source, out),
        "for_paragraph" => render_for(node, source, out),
        "cut_paragraph" => {} // skip
        _ => {}
    }
}

fn render_command(node: Node, source: &[u8], out: &mut String) {
    let cmd = get_command_name(&node, source);
    let content = get_content_text(&node, source);

    match cmd.as_str() {
        "=head1" => {
            out.push_str(&format!("### {}\n\n", render_inline_content(&node, source)));
        }
        "=head2" => {
            out.push_str(&format!("#### {}\n\n", render_inline_content(&node, source)));
        }
        "=head3" | "=head4" => {
            out.push_str(&format!("##### {}\n\n", render_inline_content(&node, source)));
        }
        "=over" => {} // list start — no output needed
        "=back" => {
            out.push('\n');
        }
        "=item" => {
            let rendered = render_inline_content(&node, source);
            if rendered.is_empty() || rendered == "*" {
                out.push_str("- ");
            } else if rendered.starts_with("* ") {
                out.push_str(&format!("- {}\n", &rendered[2..]));
            } else if let Some(rest) = strip_ordered_prefix(&rendered) {
                // Ordered list: =item 1. text → 1. text
                out.push_str(&format!("{}\n", rest));
            } else {
                out.push_str(&format!("- **{}**\n", rendered));
            }
        }
        "=pod" | "=encoding" => {} // skip
        _ => {} // unknown commands
    }
    let _ = content; // used via render_inline_content
}

fn render_plain(node: Node, source: &[u8], out: &mut String) {
    let text = render_inline_content(&node, source);
    if !text.is_empty() {
        out.push_str(&text);
        out.push('\n');
    }
    if !out.ends_with("\n\n") {
        out.push('\n');
    }
}

fn render_verbatim(node: Node, source: &[u8], out: &mut String) {
    out.push_str("```perl\n");
    render_verbatim_content(node, source, out);
    out.push_str("```\n\n");
}

/// Render verbatim paragraph content, stripping one level of indent.
fn render_verbatim_content(node: Node, source: &[u8], out: &mut String) {
    if let Ok(text) = node.utf8_text(source) {
        for line in text.lines() {
            if line.starts_with("    ") {
                out.push_str(&line[4..]);
            } else if line.starts_with('\t') {
                out.push_str(&line[1..]);
            } else {
                out.push_str(line);
            }
            out.push('\n');
        }
    }
}

fn render_begin(node: Node, source: &[u8], out: &mut String) {
    let format_name = node.child_by_field_name("format")
        .and_then(|n| n.utf8_text(source).ok())
        .unwrap_or("text");
    // Render data as fenced code block with format name
    for i in 0..node.named_child_count() {
        if let Some(child) = node.named_child(i) {
            if child.kind() == "data" {
                if let Ok(text) = child.utf8_text(source) {
                    let text = text.trim();
                    if !text.is_empty() {
                        out.push_str(&format!("```{}\n{}\n```\n\n", format_name, text));
                    }
                }
            }
        }
    }
}

fn render_for(node: Node, source: &[u8], out: &mut String) {
    let format_name = node.child_by_field_name("format")
        .and_then(|n| n.utf8_text(source).ok())
        .unwrap_or("text");
    let content = get_content_text(&node, source);
    if !content.is_empty() {
        out.push_str(&format!("```{}\n{}\n```\n\n", format_name, content));
    }
}

// ---- Inline content rendering ----

/// Render the content of a node, handling interior_sequence (B<>, C<>, etc.) recursively.
fn render_inline_content(node: &Node, source: &[u8]) -> String {
    // Find the content child node
    for i in 0..node.named_child_count() {
        if let Some(child) = node.named_child(i) {
            if child.kind() == "content" {
                return render_content_node(child, source);
            }
        }
    }
    String::new()
}

fn render_content_node(node: Node, source: &[u8]) -> String {
    // If the content node has no named children, it's plain text
    if node.named_child_count() == 0 {
        return node.utf8_text(source).unwrap_or("").to_string();
    }

    // Walk children, interleaving literal text and interior_sequence nodes
    let mut result = String::new();
    let content_start = node.start_byte();
    let content_end = node.end_byte();
    let mut pos = content_start;

    for i in 0..node.named_child_count() {
        if let Some(child) = node.named_child(i) {
            // Literal text before this child
            if child.start_byte() > pos {
                if let Ok(text) = std::str::from_utf8(&source[pos..child.start_byte()]) {
                    result.push_str(text);
                }
            }
            if child.kind() == "interior_sequence" {
                result.push_str(&render_interior_sequence(child, source));
            }
            pos = child.end_byte();
        }
    }
    // Trailing literal text
    if pos < content_end {
        if let Ok(text) = std::str::from_utf8(&source[pos..content_end]) {
            result.push_str(text);
        }
    }

    result
}

fn render_interior_sequence(node: Node, source: &[u8]) -> String {
    let letter = node.child_by_field_name("letter")
        .and_then(|n| n.utf8_text(source).ok())
        .unwrap_or("");

    // Get the content — may contain nested interior_sequences
    // Trim for multi-angle-bracket variants (C<< ... >> has extra spaces per POD spec)
    let content = node.named_children(&mut node.walk())
        .find(|c| c.kind() == "content")
        .map(|c| render_content_node(c, source).trim().to_string())
        .unwrap_or_default();

    match letter {
        "C" => format!("`{}`", content),
        "B" => format!("**{}**", content),
        "I" => format!("*{}*", content),
        "F" => format!("`{}`", content),
        "L" => {
            if let Some(idx) = content.find('|') {
                let text = &content[..idx];
                let url = &content[idx + 1..];
                if url.starts_with("http://") || url.starts_with("https://") {
                    format!("[{}]({})", text, url)
                } else {
                    // L<text|Module> or L<text|Module/section> — just show text
                    text.to_string()
                }
            } else if content.contains("://") {
                // Bare URL: L<http://example.com>
                format!("[{}]({})", content, content)
            } else if content.contains('/') {
                // L<Module/section> → Module (section)
                let parts: Vec<&str> = content.splitn(2, '/').collect();
                format!("{} ({})", parts[0], parts[1].trim_matches('"'))
            } else {
                // L<Module::Name> → link to metacpan
                format!("[{}](https://metacpan.org/pod/{})", content, content)
            }
        }
        "X" => String::new(), // index entry — invisible
        "Z" => String::new(), // zero-width — invisible
        "S" => content.replace(' ', "\u{00a0}"), // non-breaking spaces
        "E" => match content.as_str() {
            "lt" => "<".to_string(),
            "gt" => ">".to_string(),
            "sol" => "/".to_string(),
            "verbar" => "|".to_string(),
            _ => content,
        },
        _ => content,
    }
}

// ---- Helpers ----

fn get_command_name(node: &Node, source: &[u8]) -> String {
    node.child_by_field_name("command")
        .and_then(|n| n.utf8_text(source).ok())
        .map(|s| s.to_string())
        .unwrap_or_default()
}

fn get_content_text(node: &Node, source: &[u8]) -> String {
    for i in 0..node.named_child_count() {
        if let Some(child) = node.named_child(i) {
            if child.kind() == "content" {
                return child.utf8_text(source).unwrap_or("").to_string();
            }
        }
    }
    String::new()
}

/// Check if text starts with an ordered list prefix like "1." or "2. ".
/// Returns the full "N. rest" string for markdown rendering.
fn strip_ordered_prefix(text: &str) -> Option<String> {
    let trimmed = text.trim_start();
    let dot_pos = trimmed.find('.')?;
    let prefix = &trimmed[..dot_pos];
    if prefix.chars().all(|c| c.is_ascii_digit()) && !prefix.is_empty() {
        Some(trimmed.to_string())
    } else {
        None
    }
}

/// Extract method name from an =item content string.
/// Handles: `$obj->method(...)`, `Class->method(...)`, `C<method>`, `method(...)`, `method`
fn extract_item_method_name(item_rest: &str) -> Option<&str> {
    let s = item_rest.trim();
    // Strip C<...> wrapper
    let s = if s.starts_with("C<") {
        let inner = s.strip_prefix("C<")?.strip_suffix('>')?;
        inner.trim()
    } else if s.starts_with("C<<") {
        let inner = s.strip_prefix("C<<")?.strip_suffix(">>")?;
        inner.trim()
    } else {
        s
    };
    // Strip $obj-> or Class::Name-> prefix
    let s = if let Some(idx) = s.find("->") {
        &s[idx + 2..]
    } else {
        s
    };
    // Strip trailing (...) and whitespace
    let s = if let Some(idx) = s.find('(') {
        s[..idx].trim()
    } else {
        s.trim()
    };
    if s.is_empty() { None } else { Some(s) }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_headings() {
        let pod = "=head1 NAME\n\nFoo - a foo module\n\n=head2 bar\n\nDoes bar things.\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("### NAME"), "got: {}", md);
        assert!(md.contains("#### bar"), "got: {}", md);
        assert!(md.contains("Does bar things."), "got: {}", md);
    }

    #[test]
    fn test_inline_formatting() {
        let pod = "=head2 test\n\nUse C<foo> for B<bold> and I<italic>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("`foo`"), "got: {}", md);
        assert!(md.contains("**bold**"), "got: {}", md);
        assert!(md.contains("*italic*"), "got: {}", md);
    }

    #[test]
    fn test_double_bracket() {
        let pod = "=head2 test\n\nC<< $hash->{key} >>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("`$hash->{key}`"), "got: {}", md);
    }

    #[test]
    fn test_link() {
        let pod = "=head2 test\n\nSee L<Foo::Bar>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("[Foo::Bar](https://metacpan.org/pod/Foo::Bar)"), "got: {}", md);
    }

    #[test]
    fn test_link_with_text() {
        let pod = "=head2 test\n\nSee L<the docs|http://example.com>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("[the docs](http://example.com)"), "got: {}", md);
    }

    #[test]
    fn test_link_section() {
        let pod = "=head2 test\n\nSee L<Module/method>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("Module (method)"), "got: {}", md);
    }

    #[test]
    fn test_escape() {
        let pod = "=head2 test\n\nE<lt>tag E<gt>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("<tag >"), "got: {}", md);
    }

    #[test]
    fn test_verbatim_block() {
        let pod = "=head2 example\n\nSome text:\n\n    my $x = 1;\n    my $y = 2;\n\nMore text.\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("```perl\nmy $x = 1;\nmy $y = 2;\n```"), "got: {}", md);
    }

    #[test]
    fn test_item_list() {
        let pod = "=over\n\n=item * first\n\n=item * second\n\n=back\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("- first"), "got: {}", md);
        assert!(md.contains("- second"), "got: {}", md);
    }

    #[test]
    fn test_item_label() {
        let pod = "=over\n\n=item ensure\n\npresent or absent\n\n=back\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("- **ensure**"), "got: {}", md);
    }

    #[test]
    fn test_truncation() {
        let long_pod = format!("=head2 foo\n\n{}\n\n=cut\n", "x".repeat(3000));
        let md = pod_to_markdown(&long_pod);
        assert!(md.len() <= 2000);
    }

    #[test]
    fn test_head2_section_extraction() {
        let pod = "=head1 METHODS\n\n=head2 path($file)\n\nCreate a path.\n\nReturns a path object.\n\n=head2 other\n\nOther stuff.\n\n=cut\n";
        let section = extract_head2_section("path", pod).unwrap();
        assert!(section.contains("Create a path."), "got: {}", section);
        assert!(section.contains("Returns a path object."), "got: {}", section);
        assert!(!section.contains("Other stuff."), "got: {}", section);
    }

    #[test]
    fn test_file_formatting() {
        let pod = "=head2 test\n\nSee F</etc/config>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("`/etc/config`"), "got: {}", md);
    }

    #[test]
    fn test_item_arrow_method() {
        let pod = "=over\n\n=item $mech->get($url)\n\nPerforms a GET request.\n\n=item $mech->post($url)\n\nPerforms a POST.\n\n=back\n";
        let section = extract_item_section("get", pod).unwrap();
        assert!(section.contains("Performs a GET request."), "got: {}", section);
        assert!(!section.contains("Performs a POST."), "got: {}", section);
    }

    #[test]
    fn test_item_bare_name() {
        let pod = "=over\n\n=item connect\n\nConnects to server.\n\n=item disconnect\n\nDisconnects.\n\n=back\n";
        let section = extract_item_section("connect", pod).unwrap();
        assert!(section.contains("Connects to server."), "got: {}", section);
    }

    #[test]
    fn test_item_class_method() {
        let pod = "=over\n\n=item DBI->connect($dsn)\n\nCreates a connection.\n\n=back\n";
        let section = extract_item_section("connect", pod).unwrap();
        assert!(section.contains("Creates a connection."), "got: {}", section);
    }

    #[test]
    fn test_item_formatted_name() {
        let pod = "=over\n\n=item C<new>\n\nConstructor.\n\n=back\n";
        let section = extract_item_section("new", pod).unwrap();
        assert!(section.contains("Constructor."), "got: {}", section);
    }

    #[test]
    fn test_item_no_match() {
        let pod = "=over\n\n=item $obj->foo()\n\nDoes foo.\n\n=back\n";
        assert!(extract_item_section("bar", pod).is_none());
    }

    #[test]
    fn test_head2_preferred_over_item() {
        let pod = "=head2 path\n\nHead2 doc.\n\n=over\n\n=item $obj->path()\n\nItem doc.\n\n=back\n=cut\n";
        let section = extract_head2_section("path", pod).unwrap();
        assert!(section.contains("Head2 doc."), "got: {}", section);
    }

    // ---- New edge case tests ----

    #[test]
    fn test_nested_lists() {
        let pod = "=over\n\n=item * Outer\n\n=over\n\n=item * Inner\n\n=back\n\n=back\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("- Outer"), "got: {}", md);
        assert!(md.contains("- Inner"), "got: {}", md);
    }

    #[test]
    fn test_begin_end_data_region() {
        let pod = "=begin html\n\n<table><tr><td>content</td></tr></table>\n\n=end html\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("```html"), "should have fenced block, got: {}", md);
        assert!(md.contains("<table>"), "got: {}", md);
    }

    #[test]
    fn test_bold_italic_nesting() {
        let pod = "=head2 test\n\nB<I<important note>>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("***important note***"), "got: {}", md);
    }

    #[test]
    fn test_triple_angle_bracket() {
        let pod = "=head2 test\n\nC<<<  $hash->{key}  >>>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("`$hash->{key}`"), "got: {}", md);
    }

    #[test]
    fn test_for_shorthand() {
        let pod = "=for html <img src=\"logo.png\">\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("```html"), "should have fenced block, got: {}", md);
    }

    #[test]
    fn test_deep_nested_formatting() {
        let pod = "=head2 test\n\nB<I<C<foo>>>\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("***`foo`***"), "got: {}", md);
    }

    #[test]
    fn test_x_index_invisible() {
        let pod = "=head2 test\n\nSee X<index>this\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("See this"), "X<> should be invisible, got: {}", md);
        assert!(!md.contains("index"), "got: {}", md);
    }

    #[test]
    fn test_z_zero_width() {
        let pod = "=head2 test\n\naZ<>b\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("ab"), "Z<> should produce nothing, got: {}", md);
    }

    #[test]
    fn test_ordered_list() {
        let pod = "=over\n\n=item 1. First\n\n=item 2. Second\n\n=back\n\n=cut\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("1. First"), "got: {}", md);
        assert!(md.contains("2. Second"), "got: {}", md);
        assert!(!md.contains("- **1."), "should not bold-wrap ordered items, got: {}", md);
    }

    #[test]
    fn test_consecutive_verbatim_merged() {
        let pod = "=head2 test\n\n    block one\n    line two\n\n    block two\n    line three\n\n=cut\n";
        let md = pod_to_markdown(pod);
        // Should be one code block, not two
        let fence_count = md.matches("```").count();
        assert_eq!(fence_count, 2, "should have exactly one code block (2 fences), got: {}", md);
        assert!(md.contains("block one"), "got: {}", md);
        assert!(md.contains("block two"), "got: {}", md);
    }

    #[test]
    fn test_eof_without_cut() {
        let pod = "=head1 NAME\n\nModule - description\n";
        let md = pod_to_markdown(pod);
        assert!(md.contains("### NAME"), "got: {}", md);
        assert!(md.contains("Module - description"), "got: {}", md);
    }
}