Jump to navigation

‘Recent Links’ from Entries

Question:

From: Don Graver

I would like to somehow create a list of recent links within my posts. Basically, copy all the href’s out and create a sidebar that shows recent things I have linked to. I found your code suggestion (at bottom) on the MT support forum, and am wondering if this will create what I am looking for… thanks. I haven’t worked with either of these two plugins, but was curious if I’m on the right track.

Brad Answers:

I think I have what you’re looking for. It will require you to install both the Macro and PerlScript plugins. There’s no canned plugin that will do quite what you’re looking for at the moment. The ‘StripTags’ plugin comes quite close, but no cigar.

Strip Tags has a method you can use to ‘collect’ tags from HTML, but it fails to grab the content they hold. In this case, the hyperlinked text. For your sidebar, I assume you would want to display some kind of title for the link instead of just the URL. So a Macro with PerlScript is the way to go today.

This is what I came up with:

<MTMacroDefine ctag="a" name="suck_links" script="PerlScript">
  my $href = $MTMacroAttr{href} || '';
  if ($href =~ m/^http:/ && !(exists $perlscript::seen{$href})) {
    push @perlscript::links, {%MTMacroAttr,
      content => $MTMacroContent};
    $perlscript::seen{$MTMacroAttr{href}} = 1;
  }
</MTMacroDefine>

<MTMacroApply macro="suck_links" trim_to="0.">
  <MTEntries lastn="10">
    <$MTEntryBody$><$MTEntryMore$>
  </MTEntries>
</MTMacroApply>

<h4>Recent Links</h4>
<ul>
<MTPerlScript>
  foreach $link (@perlscript::links) {
    print "<li><a href='$link->{href}'" .
      (exists $link->{title} ? " title='$link->{title}'" : '') .
      ">$link->{content}</a></li>\n";
  }
</MTPerlScript>
</ul>

I would put the above in a new Movable Type Index Template. You can adjust it as needed. I limited the number of entries to process to the last 10. If you want more or less, change the ‘lastn’ parameter in the MTEntries tag to the desired amount. Or remove it altogether if you wish to go by the number of days you have configured for your blog.

The MTMacroDefine tag sets up a handler for the ‘a’ container tag. The Macro and PerlScript plugins work in concert here to do some magic with the ‘a’ tag content. In this case, it makes sure that the hyperlink starts with ‘http:’ for consideration — if you use relative links for internal pages, they will be excluded from your sidebar. It also keeps track of the links that are already on the list, so you won’t get duplicates either. And the ‘trim_to’ attribute on the MTMacroDefine tag suppresses the entry content (I use ‘0.’ for the trim_to attribute since ‘0’ fails to trim at all).

Finally MTPerlScript block iterates over the list of links that were accumulated and prints each of them out using li tags.

Sorry that we have to resort to writing Perl to get the job done, but sometimes you gotta do whatcha gotta do. (Since Don is JAPH, he’s perfectly fine with that.)

[ Movable Type ] / posted @ Monday, January 27, 2003 5:40 PM EST
Copyright © 1995-2008 Brad Choate, Some Rights Reserved.