Anki-Import
view release on metacpan or search on metacpan
lib/Anki/Import.pm view on Meta::CPAN
package Anki::Import ;
$Anki::Import::VERSION = '0.030';
use strict;
use warnings;
use Cwd;
use Getopt::Args;
use Log::Log4perl::Shortcuts 0.021 qw(:all);
use Exporter qw(import);
our @EXPORT = qw(anki_import);
# change log config to test for development for fine-tuned control over log output
set_log_config('anki-import.cfg');
#set_log_config('test.cfg', __PACKAGE__);
# set up variables
my @lines; # lines from source file
my $line_count = 0; # count processed lines to give more helpful error msg
my $cline = ''; # current line getting processed
my $lline = ''; # last (previous) line processed
my $ntype = 'Basic'; # default note type
my @notes = (); # array for storing notes
my @autotags = (); # for storing automated tags
# argument processing
arg file => (
isa => 'Str',
required => 1,
comment => 'the name of the source file'
);
arg parent_dir => (
isa => 'Str',
default => cwd,
comment => 'optional directory to save output files, defaults to current directory',
);
opt quiet => (
isa => 'Bool',
alias => 'q',
default => 1,
comment => 'On by default. Use --quiet to override this setting to suppress'
. ' the success message after a successful execution of the command.'
);
opt verbose => (
isa => 'Bool',
alias => 'v',
comment => 'provide details on progress of Anki::Import'
);
opt vverbose => (
isa => 'Bool',
alias => 'V',
comment => 'verbose information plus debug info'
);
# start here
sub anki_import {
my $args = optargs( @_ );
my $file = $args->{file};
if (!$file) {
logf('Aborting: No file passed to Anki::Import.');
}
# set parent directory
my $pd = $args->{parent_dir};
# set log level as appropriate
if ($args->{verbose}) {
set_log_level('info');
} elsif ($args->{vverbose}) {
set_log_level('debug');
} else {
set_log_level('error');
}
logi('Log level set');
# get and load the source file
logi('Loading file');
my $path = File::Spec->catfile($file); logd($path);
if (! -e $path) {
logf("Aborting: Source file named '$path' does not exist.");
};
open (my $handle, "<:encoding(UTF-8)", $path) or logf("Could not open $path");;
chomp(@lines = <$handle>);
close $handle;
logi('Source file loaded.');
# pad data with a blank line to make it easier to process
push @lines, '';
# do the stuff we came here for
validate_src_file(); logd(\@notes);
generate_importable_files($pd);
# print a success message
unless ($args->{'quiet'}) {
set_log_level('info');
logi("Success! Your import files are in the $pd"
. '/anki_import_files directory');
}
# fin
}
# functions for first pass parsing of source data
sub validate_src_file {
logi('Validating source file');
# throw error if file is empty
logf('Source data file is empty.') if !$lines[0];
# outer loop for parsing notes
my %fields; # keeps track of number of fields for each type of note
while (next_line()) {
# ignore blank lines
next if ($cline =~ /^$|^\s+$/);
if ($cline =~ /^#\s*(\S+)/) {
$ntype = $1;
logi("Found note type");
logd($ntype);
next;
}
logi('Processing new note');
# get the note
my $note = slurp_note();
logd($note);
logi('Checking number of note fields');
( run in 2.355 seconds using v1.01-cache-2.11-cpan-2398b32b56e )