Parser new with a path, not a IO::Path

This commit is contained in:
Aoran Zeng 2025-07-16 02:14:16 +08:00
parent cefa25a883
commit c86b75987e
No known key found for this signature in database
GPG Key ID: 8F8BA8488E10ED98
2 changed files with 20 additions and 6 deletions

View File

@ -157,12 +157,26 @@ my class Section {
- ... - ...
) )
class Parser { class Parser {
has IO::Path $.input-file is rw; has Str $.input-file is rw;
#| sections #| sections
has Section @!sections; has Section @!sections;
method new($input-file) { #| $path
self.bless(:$input-file); method new($path) {
my $markdown-file;
if $path.IO.d {
$markdown-file = $path.IO.add("rawstr4c.md");
unless $markdown-file.e {
die "Error: No 'rawstr4c.md' file found in directory '$path'\n";
}
}
elsif $path.IO.f {
$markdown-file = $path.IO;
} else {
die "Error: '$path' is neither a file nor a directory\n";
}
self.bless(:input-file($markdown-file.Str));
} }
# 获取根sectionlevel 0 # 获取根sectionlevel 0
@ -184,7 +198,7 @@ class Parser {
method parse() { method parse() {
my $content = $.input-file.slurp; my $content = $.input-file.IO.slurp;
my @lines = $content.lines; my @lines = $content.lines;
my $current-section; my $current-section;

View File

@ -11,8 +11,8 @@
use lib '../lib'; use lib '../lib';
use Parser; use Parser;
my $test-file1 = './fixture/test-hierarchy-with-root.md'.IO; my $test-file1 = './fixture/test-hierarchy-with-root.md';
my $test-file2 = './fixture/test-hierarchy-without-root.md'.IO; my $test-file2 = './fixture/test-hierarchy-without-root.md';
my $parser1 = Parser::Parser.new($test-file1); my $parser1 = Parser::Parser.new($test-file1);
my $parser2 = Parser::Parser.new($test-file2); my $parser2 = Parser::Parser.new($test-file2);