diff options
Diffstat (limited to '.ikiwiki/IkiWiki/Plugin')
-rwxr-xr-x | .ikiwiki/IkiWiki/Plugin/pandoc.pm | 244 |
1 files changed, 243 insertions, 1 deletions
diff --git a/.ikiwiki/IkiWiki/Plugin/pandoc.pm b/.ikiwiki/IkiWiki/Plugin/pandoc.pm index 34bdd89..aa5bcd0 100755 --- a/.ikiwiki/IkiWiki/Plugin/pandoc.pm +++ b/.ikiwiki/IkiWiki/Plugin/pandoc.pm @@ -1,5 +1,9 @@ #!/usr/bin/env perl +# DEBUG +$DB::single = 1; +# END + package IkiWiki::Plugin::pandoc; use warnings; @@ -10,6 +14,20 @@ use IPC::Open2; use File::Path qw/make_path/; use JSON; +# DEBUG +use Data::Dumper; +$Data::Dumper::Indent = 3; + +my $fh = undef; +my $dh = undef; +my $fnm = undef; +my $f = undef; +my $next = 0; +my $base = '/tmp/pandoc'; + +mkdir($base); +# END + # activate with 'generate_$format' in meta; turn on all with 'generate_all_formats'. my %extra_formats = ( pdf => { ext=>'pdf', label=>'PDF', format=>'latex', extra=>[], order=>1 }, @@ -38,7 +56,51 @@ my @list_hash_meta_keys = qw/ references /; + +# DEBUG setup filename function +sub debug_get_filename_prefix { + + my $place = shift; + my $page = shift; + my $fnm_pfx = undef; + + opendir($dh, $base) || die "Can't open $base: $!"; + foreach $f (grep(/^[0-9][0-9]*/, readdir($dh))) { + $f =~ /^([0-9][0-9]*)\.(.*)$/; + $next = $1 + 1 if ($1 >= $next); + } + close($dh); + if ($page) { + $fnm_pfx = sprintf "%s/%.3d.%s.%s", $base, $next, $place, + $page =~ s/\//-/rg; + } else { + $fnm_pfx = sprintf "%s/%.3d.%s", $base, $next, $place; + } + return $fnm_pfx; +} +# END + +# DEBUG Print +print "D: pandoc: First file read.\n"; +$fnm = debug_get_filename_prefix ("verify"); +open($fh, '>', $fnm) or die "Could not"; +print $fh "Hej du glade på fil\n"; +print $fh "Detta är filen i '/home/gustav/git/fripost/wiki/.ikiwiki/IkiWiki/Plugin'\n"; +close $fh; +# END + + sub import { + + # DEBUG Print + print "D: pandoc:import: Run.\n"; + my $fnm_pfx = debug_get_filename_prefix ("import",""); + my $fnm = $fnm_pfx; + open($fh, '>', $fnm) or die "Could not open $fnm ."; + print $fh "Import.\n"; + close $fh; + # END + my $markdown_ext = $config{pandoc_markdown_ext} || "mdwn"; # May be both a string with a single value, a string containing commas or an arrayref @@ -87,6 +149,11 @@ sub import { sub getsetup () { + + # DEBUG + print "D: pandoc:getsetup: Run.\n"; + # END + return plugin => { safe => 1, @@ -362,6 +429,7 @@ sub getsetup () { sub htmlize ($@) { + my $format = shift; my %params = @_; my $page = $params{page}; @@ -370,6 +438,24 @@ sub htmlize ($@) { local(*PANDOC_IN, *JSON_IN, *JSON_OUT, *PANDOC_OUT); my @args = (); + # DEBUG Print params + print "D: pandoc:htmlize: Run for ", $page, "\n"; + my $fnm_pfx = debug_get_filename_prefix ("htmlize", $page); + my $m = 0; + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "params"; + open($fh, '>', $fnm) or die "Could not"; + $Data::Dumper::Indent = 2; + print $fh Data::Dumper->Dump([\%params], ["params"]), "\n"; + close $fh; + # END + + # DEBUG Print half way pagestate pagestate + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "early-pagestate"; + open($fh, '>>', $fnm); + print $fh Data::Dumper->Dump([$pagestate{$page}], ['pagestate']), "\n"; + close $fh; + # END + # The default assumes pandoc is in PATH my $command = $config{pandoc_command} || "pandoc"; @@ -469,6 +555,20 @@ sub htmlize ($@) { warn "WARNING: Unexpected format for meta block. Incompatible version of Pandoc?\n"; } + # DEBUG Half way content which is plenty data + print "D: pandoc:htmlize: Pandoc command: ", " ", $command, " ", '-f', " ", + $markdown_fmt, " ", '-t', " ", 'json', " ", @args, + "\n"; + + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "early-content"; + open($fh, '>>', $fnm); + print $fh Data::Dumper->Dump([$content], [qw(content)]), "\n"; + print $fh Data::Dumper->Dump([$json_content], [qw(json_content)]), "\n"; + print $fh Data::Dumper->Dump([$decoded_json], [qw(decoded_json)]), "\n"; + print $fh Data::Dumper->Dump([$meta], [qw(meta)]), "\n"; + close $fh; + # END + # Get some selected meta attributes, more specifically: # (title date bibliography csl subtitle abstract summary description # version lang locale references author [+ num_authors primary_author]), @@ -476,11 +576,13 @@ sub htmlize ($@) { my @format_keys = grep { $_ ne 'pdf' } keys %extra_formats; my %scalar_meta = map { ($_=>undef) } @scalar_meta_keys; + $scalar_meta{$_.'_template'} = undef for @format_keys; my %bool_meta = map { ("generate_$_"=>0) } keys %extra_formats; my %list_meta = map { ($_=>[]) } ( @list_meta_keys, @list_hash_meta_keys, @hash_meta_keys); $list_meta{$_.'_extra_options'} = [] for @format_keys; + my $have_bibl = 0; foreach my $k (keys %scalar_meta) { next unless $meta->{$k}; @@ -510,6 +612,23 @@ sub htmlize ($@) { $have_bibl = 1 if $k eq 'references'; $pagestate{$page}{meta}{$k} = $list_meta{$k}; $pagestate{$page}{meta}{"pandoc_$k"} = $list_meta{$k}; + + # DEBUG + # if (ref $list_meta{$k} eq 'ARRAY') {print "D: $k is ARRAY.", "\n";} + # else {print "D: $k is not array.", "\n";} + # if ($k eq 'references') { + # print "D: Found, ", ref $meta->{$k}, ", ", ref $list_meta{$k}, ", ", + # keys $meta->{$k}->%*, ".\n"; + # my @tmp_references; + # foreach my $item ($list_meta{$k}->@*) { + # print "D: Found $item", ".\n"; + # push @tmp_references, map { $_ => $l{$_}; + # } + # $pagestate{$page}{meta}{"pandoc_$k"} = @tmp_references; + # print "D: Sent as pandoc_$k, ", @tmp_references, ".\n"; + # } + # END + } # Try to add other keys as scalars, with pandoc_ prefix only. foreach my $k (keys %$meta) { @@ -527,6 +646,10 @@ sub htmlize ($@) { $pagestate{$page}{meta}{pandoc_primary_author} = $scalar_meta{primary_author} } + # DEBUG + $pagestate{$page}{meta}{fripost_debug_outer} = 'added in plugin, forced'; + # END + # The bibliography may be set in a Meta block in the page or in the .setup file. # If both are present, the Meta block has precedence. for my $bibl ($scalar_meta{bibliography}, $config{pandoc_bibliography}) { @@ -602,16 +725,50 @@ sub htmlize ($@) { close JSON_IN; my @html = <PANDOC_IN>; + close PANDOC_IN; waitpid $to_html_pid, 0; + # DEBUG + print "D: pandoc:htmlize: Pandoc command: ", " ", $command, " ", '-f', " ", + 'json', " ", '-t', $htmlformat, " ", @html_args, "\n"; + # END + $content = Encode::decode_utf8(join('', @html)); # Reinstate placeholders for inline plugin: $content =~ s{::INLINE::PLACEHOLDER::(\d+)::} {<div class="inline" id="$1"></div>}g; + # DEBUG Print meta + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "meta"; + open($fh, '>>', $fnm) or die "Could not"; + print $fh "\n"; + print $fh Data::Dumper->Dump([\%scalar_meta], [qw(scalar_meta)]), "\n"; + print $fh Data::Dumper->Dump([\%bool_meta], [qw(bool_meta)]), "\n"; + print $fh Data::Dumper->Dump([\%list_meta], [qw(list_meta)]), "\n"; + close $fh; + # END + + # DEBUG Print pagestate again + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "return-pagestate"; + open($fh, '>>', $fnm); + print $fh Data::Dumper->Dump([$pagestate{$page}], ['pagestate']), "\n"; + close $fh; + # END + + # DEBUG Print return content + $fnm = sprintf "%s.%.1d.%s", $fnm_pfx, $m++, "return-content"; + open($fh, '>>', $fnm); + print $fh Dumper($content), "\n"; + close $fh; + # END + + # DEBUG + print "D: pandoc:htmlize: Return.\n"; + # END + return $content; } @@ -619,7 +776,50 @@ sub htmlize ($@) { sub pagetemplate (@) { my %params = @_; my $page = $params{page}; + my $template = $params{template}; + + # DEBUG + my $filename = $template->{'options'}{'filename'}; + $filename =~ /^.*\/([a-z]*).tmpl$/; + my $filenamebase = $1; + print "D: pandoc:pagetemplate: Run for $page and $filenamebase \n"; + # END + + # DEBUG compare with https://metacpan.org/pod/HTML::Template#TMPL_LOOP + if ($page eq "test") { + $template->param( + EMPLOYEE_INFO => [ + {name => 'Sam', job => 'programmer'}, + {name => 'Steve', job => 'soda jerk'}]); + + # a couple of arrays of data to put in a loop: + my @words = qw(I Am Cool); + my @numbers = qw(1 2 3); + my @loop_data = (); # initialize an array to hold your loop + + while (@words and @numbers) { + my %row_data; # get a fresh hash for the row data + + # fill in this row + $row_data{WORD} = shift @words; + $row_data{NUMBER} = shift @numbers; + + # the crucial step - push a reference to this row into the loop! + push(@loop_data, \%row_data); + } + # HERE! + $template->param(THIS_LOOP => \@loop_data); + if ($pagestate{$page}{meta}{'pandoc_references'}) { + my @more_ref = $pagestate{$page}{meta}{'pandoc_references'}->@*; + my $ref = $pagestate{$page}{meta}{'pandoc_references'}; + my $k = 'pandoc_references'; + $template->param('more_pandoc_references' => \@more_ref); + #$template->param($k => $pagestate{$page}{meta}{$k}); + } + } + # END + foreach my $k (keys %{$pagestate{$page}{meta}}) { next unless (grep {/^$k$/} ( @@ -628,12 +828,37 @@ sub pagetemplate (@) { ($k =~ /^(pandoc_)/); $template->param($k => $pagestate{$page}{meta}{$k}); } + + # DEBUG Print pagetemplate data + my $fnm_pfx = debug_get_filename_prefix ("pagetemplate", $page); + my $m = 0; + my $fnm = sprintf "%s.%s.%.1d.%s", $fnm_pfx, $filenamebase, $m++, "template"; + open($fh, '>', $fnm) or die "Could not"; + print $fh Data::Dumper->Dump([$template->{'mtime'}], ["mtime"]), "\n"; + print $fh Data::Dumper->Dump([$template->{'options'}], ['options']), "\n"; + print $fh Data::Dumper->Dump( + [$template->{'param_map'}], ['param_map']), "\n"; + print $fh Data::Dumper->Dump( + [$template->{'parse_stack'}], ['parse_stack']),"\n"; + close $fh; + + $fnm = sprintf "%s.%s.%.1d.%s", $fnm_pfx, $filenamebase, $m++, "output"; + open($fh, '>', $fnm) or die "Could not"; + print $fh $template->output(); + close $fh; + # END + return $template; } sub pageactions { my %args = @_; my $page = $args{page}; + + # DEBUG + print "D: pandoc:pageactions: Run for ", $page, "\n"; + # END + my @links = (); return unless $pagestate{$page}{pandoc_extra_formats}; my @exts = sort { @@ -655,6 +880,11 @@ sub pageactions { sub export_file { my ($page, $ext, $json_content, $command, @args) = @_; + + # DEBUG + print "D: pandoc:export_file: Run for $page.\n"; + # END + my ($export_path, $export_url) = _export_file_path_and_url($page, $ext); my $subdir = $1 if $export_path =~ /(.*)\//; my @extra_args = @{ $extra_formats{$ext}->{extra} }; @@ -706,6 +936,13 @@ sub export_file { @args, @extra_args) or die "Could not open pipe for $ext: $!"; print EXPORT $json_content; close EXPORT or die "Could not close pipe for $ext: $!"; + + # DEBUG + print "D: pandoc:export_file: Pandoc command: ", " ", $command, " ", '-f' => + 'json', " ", '-t' => $to_format, " ", '-o' => + $tmp_export_path, " ", @args, " ", @extra_args; + # DEBUG + if ($indirect_pdf && $tmp_export_path ne $export_path) { my @latexmk_args = qw(-quiet -silent); if (grep { /xelatex/ } @extra_args) { @@ -745,6 +982,12 @@ sub remove_exported_file { if ($@) { warn "WARNING: remove_exported_file; page=$page, ext=$ext: $@\n"; } + + # DEBUG + print "D: pandoc:remove_exported_file: ", + "file removed for $page and $ext: $export_path.\n"; + # END + } } @@ -763,7 +1006,6 @@ sub _export_file_path_and_url { return ($export_path, $export_url); } - ## compile_string and unwrap_c are used to make the meta data structures ## easier to work with for perl. |