#!/usr/bin/perl use strict; use warnings; use v5.22; # Disable experimental warnings. no warnings 'experimental'; use File::Path qw(make_path remove_tree); use Path::Class; use Getopt::Std; # Root in directory. my $indir; my $outdir; my $VERSION = "v0.1"; my @exlude_files = ( qr/log\/\w+\/*/ ); my $with_sidebar = 1; sub help { print< give you the whole file at once. local $/=undef; open FILE, "styles.css" or die "Cannot open file $!"; my $styles = ; close FILE; return $styles; } sub markdown { my ($file) = @_; my $filename = "$file"; $filename =~ s/\.html$/.md/g; return `markdown "$filename"`; } sub generate_page { my $file = $_[0]; my $sidebar = ""; if($with_sidebar) { $sidebar = transverse($indir, $file); } my $styles = styles; my $body = markdown $file; my $page = < Deprecated

Deprecated Coding for fun

$body
EOF ; return $page; } sub generate_link { my ($file, $options) = @_; $options->{hl} = 0 if !exists($options->{hl}); # Exclude links given($file) { when(@exlude_files) { say "excluding $file"; return ""; } default { my $path = $file->stringify; my $str = $file->basename; if($path =~ /index\.md$/) { return ""; } # remove the input directory. $path =~ s/^$indir//g; if($file->is_dir()) { $str = "$str/" if $file->is_dir(); } else { $path =~ s/\.md$/.html/g; $str =~ s/\.md$//g; } # Higlight? my $hl = ""; if($options->{hl} eq 1) { $hl = "class='hl'"; } return "
  • $str
  • "; } } } sub transverse { my ($dirname, $file_needed) = @_; my $retval = ""; my $directory = dir($dirname); $retval = "
      "; while(my $file = $directory->next) { my $filename = $file->stringify; # Some conditions next if $filename =~ /(\.|\.\.)$/; next if $filename eq $directory; if($file_needed eq $file or ( $file_needed->dir eq $filename and $filename eq "index.md")) { $retval = "$retval" . generate_link($file, { hl => 1 }); } else { $retval = "$retval" . generate_link($file); } if($file->is_dir()) { my $d = $file_needed->dir->stringify; if($file =~ /$d/ and !($d eq $directory)) { $retval = "$retval" . transverse($file, $file_needed); } } } $retval = "$retval
    "; return $retval; } sub run_on { my $dirname = $_[0] or die "Cannot stat input directory"; my $mddir = dir($dirname); while(my $file = $mddir->next) { my $outfile = $file->stringify; # Some conditions: # 1) next if file is . or .. # 2) next if outfile = directory with markdown # 3) die if the output file is in the input directory. next if $outfile eq $mddir; next if $outfile =~ m/(\.|\.\.)($|\/)/; $outfile =~ s/^$indir/$outdir\//g; $outfile !~ m/^$indir/ or die "trying to write in the same directory $outfile"; if($file->is_dir()) { # make a directory make_path $outfile; run_on($file); next; } $outfile =~ s/md$/html/g; # This file it's just for pretty print print STDERR "* writing: " . file($outfile) . "\n"; my $contents = generate_page $file; open FD, ">$outfile" or die " [ERROR] cannot open > $outfile: $!";; print FD $contents or die " [ERROR] Cannot write $outfile: $!"; close FD or die " [ERROR] Cannot close $outfile: $!"; } } my %opts = ( 'v' => 0, 'h' => 0, 'o' => "site.static/", 's' => 1, ); getopts('svho:', \%opts); $indir = shift @ARGV; if($opts{h}) { help; exit 0; } elsif($opts{v}) { version; exit 0; } if(-d $opts{o}) { print STDERR "removing $opts{o}\n"; remove_tree($opts{o}); } if($opts{s}) { $with_sidebar = $opts{s}; } mkdir $opts{o}; $outdir = $opts{o}; run_on $indir;