Skip to content

Instantly share code, notes, and snippets.

@jcenters
Last active August 29, 2015 14:01
Show Gist options
  • Save jcenters/3b9d599616d6b7adf1aa to your computer and use it in GitHub Desktop.
Save jcenters/3b9d599616d6b7adf1aa to your computer and use it in GitHub Desktop.
Converts standard Markdown inline links to lazy links.
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
# Converts standard Markdown inline links to lazy links.
# Serving Suggestions: Pipe text into inline-to-lazy.pl
# from the command line or install as a BBEdit Text Filter.
# 2014 Josh Centers. http://joshcenters.com/
my $text; # Our hero, the $text variable.
local $/; # Grab all the text.
$text = <>; # Assign STDIN to our hero.
$text = $text . "\n";
$text =~ s/\[(.*?)\]/\[$1\]\[\*\]/g; # replace [text] with [text][*]
# Now we're going to split our $text at the start of each new line,
# turning our $text into a list of text lines, then iterate through
# each one, so we can easily put the linked URLs at the end of each
# line.
foreach my $line (split /^/, $text) {
if ($line =~ m/\]\(/) { # Check to see if there's even an inline Markdown URL on the line. Without this, the script outputs way too many newlines.
my @url_list = ($line =~ m/(\(http.*?\))/g); # Find URLs in parentheses assign them to the array @URL_list.
$line =~ s/(\(http.*?\))//g; # Now that the URLs are safely stashed away, delete them.
foreach my $url (@url_list) { # For each URL in the URL_list…
$url = "[*]: " . $url; # …prepend [*]: to each URL
$url =~ s/\(//; # …delete (…
$url =~ s/\)//; # …and delete ).
}
print "$line\n"; #print the line, followed by a newline
print join("\n", @url_list); #print each URL in the @url_list, with a newline before the list
print "\n";
}
else {print $line;} # If there's no inline Markdown URLs, just print the line as is.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment