From a203ce1f68ebd36552d34290e03a319d6860bcde Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 29 Dec 2018 16:20:05 -0200 Subject: Use Getopt::Long and Pod::Usage to build basic CLI. --- pastebin/website-pastebin | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 pastebin/website-pastebin (limited to 'pastebin/website-pastebin') diff --git a/pastebin/website-pastebin b/pastebin/website-pastebin new file mode 100755 index 0000000..6dacdfe --- /dev/null +++ b/pastebin/website-pastebin @@ -0,0 +1,90 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i perl -p gettext less perl --pure -I nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-18.09.tar.gz + +=head1 NAME + +website pastebin - Create new pastebins from the org-mode template. + +=head1 SYNOPSIS + +website pastebin [options] + + Options: + --help Show the manpage. + --title Title of the pastebin. + +=head1 OPTIONS + +=over 4 + +=item B<-h, --help> + +Prints the manual page and exits. + +=item B<-t, --title> + +The title of the pastebin. This string will be slugified and the output is used to create the pastebin file name. Special characters are simplified or discarded. + +=back + +=head1 DESCRIPTION + +B creates pastebin org-mode text files, that are later processed to produce HTML to be deployed statically. + +=cut + +use strict; +use warnings; +use Getopt::Long qw(:config no_ignore_case bundling); +use Pod::Usage qw(pod2usage); +use Unicode::Normalize; +use File::Basename; + +my $help = 0; +my $title = ''; +GetOptions( + 'help|h|?' => \$help, + 'title|t=s' => \$title +) or pod2usage(-verbose => 1, -exitval => 2); +pod2usage(-verbose => 2, -exitval => 0) if $help; + +if (!$title) { + die "Missing required --title argument."; +} + +# Taken from: +# https://stackoverflow.com/a/4009519 +sub slugify { + my $input = shift; + $input = NFKD($input); # Normalize (decompose) the Unicode string + $input =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127) + $input =~ s/[^\w\s-]//g; # Remove all characters that are not word characters (includes _), spaces, or hyphens + $input =~ s/^\s+|\s+$//g; # Trim whitespace from both ends + $input = lc($input); + $input =~ s/[-\s]+/-/g; # Replace all occurrences of spaces and hyphens with a single hyphen + return $input; +} + + +our $dirname = dirname(__FILE__); +our $in = "$dirname/skeleton.org"; +our $out; +my %ENV = (title => $title); +# Derived from both: +# https://unix.stackexchange.com/a/294836 +# https://stackoverflow.com/a/47664214 +sub envsubst { + open(IN, '<'.$in) or die $!; + open(OUT, '>'.$out) or die $!; + while() { + $_ =~ s/\$([_a-zA-Z]+)/$ENV{$1}/g; + print OUT $_; + } + close(IN); + close(OUT); +} + +my $slug = slugify($title); +$out = "$dirname/../site/pastebin/$slug.org"; + +envsubst(); -- cgit v1.2.3