diff options
author | Matias Linares <matiaslina@openmailbox.org> | 2016-02-21 22:52:43 -0300 |
---|---|---|
committer | Matias Linares <matiaslina@openmailbox.org> | 2016-02-21 22:52:43 -0300 |
commit | 9c337f164b076d664da75aba9361d0dfff94732c (patch) | |
tree | 44f0e9dff13d2dd329204a80f075ade2fadb8a84 /ssg | |
parent | 3f394b49b87654c50165768246d7321fbb0d5d3e (diff) | |
download | ssg-9c337f164b076d664da75aba9361d0dfff94732c.tar.gz |
Rename swn to SSG
Diffstat (limited to 'ssg')
-rwxr-xr-x | ssg | 199 |
1 files changed, 199 insertions, 0 deletions
@@ -0,0 +1,199 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use File::Path qw(make_path remove_tree); +use Path::Class; +use Getopt::Std; + +# Root in directory. +my $dir; +my $VERSION = "v0.1"; + +sub help { + print<<EOF +$0 [switches] + -v print version and exits + -h print this help and exits + -d set the input directory (by default in/) +EOF +} + +sub version { + print <<EOF +$0 $VERSION +EOF +} + +sub styles { + # Unset $/, the Input Record Separator, to make <> give you the whole file at once. + local $/=undef; + open FILE, "styles.css" or die "Cannot open file $!"; + my $styles = <FILE>; + 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 = transverse($dir, $file); + my $styles = styles; + my $body = markdown $file; + my $page = <<EOF +<!doctype html> +<html> +<head> +<title>Deprecated</title> +<link rel="icon" href="/favicon.png" type="image/png"/> +<meta charset="UTF-8"> +<style> +$styles +</style> +</head> +<body> +<div class="header"> +<h1 class="headerTitle"> +<a href="/">Deprecated</a> <span class="headerSubtitle">Just coding for fun</span> +</h1> +</div> + +<div id="side-bar"> +$sidebar +</div> + +<div id="main"> +$body +</div> + + +<div id="footer"> +<div class="right">Powered by <a href="https://github.com/jroimartin/sw">swn</a></div> +</div> +</body> +</html> +EOF +; + return $page; +} + +sub generate_link { + my ($file, $options) = @_; + $options->{hl} = 0 if !exists($options->{hl}); + + # Get the path. + my $path = $file->stringify; + $path =~ s/^$dir//g; + $path =~ s/\.md$/.html/g; + + # String to show + my $str = $file->basename; + $str =~ s/\.md$//g; + + # Higlight? + my $hl = ""; + if($options->{hl} eq 1) { + $hl = "class='hl'"; + } + + return "<li><a $hl href='$path'>$str</a></li>"; +} + +sub transverse { + my ($directory, $file_needed) = @_; + my $retval = ""; + + $retval = "<ul>"; + while(my $file = $directory->next) { + my $filename = $file->stringify; + + # Some conditions + next if $filename =~ /(\.|\.\.)$/; + next if $filename eq $directory; + + if($file_needed eq $file) { + $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</ul>"; + + return $retval; +} + +sub run_on { + my $mddir = $_[0] || dir($dir); + remove_tree $mddir; + + while(my $file = $mddir->next) { + my $outfile = $file->stringify; + + # Some conditions + next if $outfile =~ /(\.|\.\.)$/; + next if $outfile eq $mddir; + + $outfile =~ s/$dir\//out\//g; + !($outfile =~ m/^$dir/) or die "$outfile =~ m/$dir/"; + + if($file->is_dir()) { + # make a directory + make_path $outfile; + run_on($file); + next; + } + + $outfile =~ s/md$/html/g; + print STDERR "* writing: $outfile\n"; + my $contents = generate_page $file; + + open FD, ">$outfile"; + + print FD $contents; + + close FD; + } +} + +sub main { + my %opts = ( + 'v' => 0, + 'h' => 0, + 'd' => "in" + ); + getopts('vhd:', \%opts); + + if($opts{h}) + { + help; + exit 0; + } + elsif($opts{v}) + { + version; + exit 0; + } + elsif($opts{d}) + { + $dir = dir $opts{d}; + } + + run_on dir($dir); +} + +main @ARGV; + |