65 lines
1.3 KiB
Perl
65 lines
1.3 KiB
Perl
#!/usr/bin/perl
|
|
use strict;
|
|
use warnings;
|
|
use v5.26;
|
|
|
|
use Carp;
|
|
use Data::Dumper;
|
|
use File::Find;
|
|
use SQL::Abstract;
|
|
use DBI;
|
|
|
|
my $db =
|
|
DBI->connect( "DBI:SQLite:dbname=../data.db", "", "", { RaiseError => 1 } )
|
|
or confess $DBI::errstr;
|
|
|
|
sub sqlstr {
|
|
return qq
|
|
/create table if not exists @_ (
|
|
id integer primary key autoincrement,
|
|
title text,
|
|
filepath text,
|
|
content text
|
|
);/;
|
|
}
|
|
my %tablesql = (
|
|
"origin" => sqlstr("origin"),
|
|
"trans" => sqlstr("trans"),
|
|
);
|
|
my $act;
|
|
for my $table ( keys %tablesql ) {
|
|
$act = $db->prepare( $tablesql{$table} );
|
|
$act->execute() or confess $DBI::errstr;
|
|
}
|
|
|
|
my @data;
|
|
finddepth(
|
|
sub {
|
|
return unless ( $_ =~ /.md$/ );
|
|
push @data, $File::Find::name;
|
|
},
|
|
'../data/'
|
|
);
|
|
my $csql = qq
|
|
/insert into origin (title,filepath,content)
|
|
values (?,?,?);/;
|
|
for my $d (@data) {
|
|
my %info;
|
|
my $content;
|
|
open my $file, "< :encoding(UTF-8)", $d;
|
|
<$file>;
|
|
while (<$file>) {
|
|
chomp;
|
|
if (/^([^:]+):\s+(.+)/) {
|
|
$info{$1} = $2;
|
|
}
|
|
last if /---/;
|
|
}
|
|
while (<$file>) {
|
|
$content .= $_;
|
|
}
|
|
close($file);
|
|
$act = $db->prepare($csql);
|
|
$act->execute( $info{'title'}, $d, $content ) or carp $DBI::errstr;
|
|
$act->finish() or carp $DBI::errstr;
|
|
}
|