Update
This is not a new idea - for a similar approach, using the same chomping code which appears in the new writeups nodelet, see sleeping wolf's edev: long node title in random nodes fix. A pox on my ignorance and inattention in not spotting this before I was pointed to it by the author. The only thing I can claim is that at least my version never actually makes the long words longer :) The method of getting the title and link is cleaner and better in sleeping wolf's version, and I've adapted it for use here in the preferred and bolded second mod, below.
Well, it's a minor irritation, but those long-named chemical nodes drive me nuts when they appear in random nodes nodelet, distorting the page layout, etc..

I noticed this has been fixed in the new writeups nodelet, and I've adapted the methodology from there for use in the random nodes nodelet.

At the moment the code there is short and sweet:

 
   2: my @phrase = (
   3:   'Nodes your grandma would have liked:',
   4:   'After stirring Everything, these nodes rose to the top:',
   5:   'Look at this mess the Death Borg made!',
   6:   'Just another sprinking of indeterminacy',
   7:   'The best nodes of all time:'
   8: );
   9: 
  10: my $str;
  11: $str.='<em>'.$phrase[rand(int(@phrase))]."</em><br />\n";
  12: 
  13: foreach (1..12) {
  14:   $str.= linkNode(getRandomNode)."<br />\n";
  15: }
  16: 
  17: $str;
I proposed the following (also simple) modification, which truncates all nonspace-composed words to a maximum of 23 characters, the last 3 characters being replaced by an ellipsis ("..."). Words of exactly 23 characters should be left exactly as they are. Changes start at line 13:
  10: my $str;
  11: $str.='<em>'.$phrase[rand(int(@phrase))]."</em><br />\n";
  12: 
  13: my $len = 20; # 23 - 3 (the other 3 is in the \S{4,} in the regexp)
  14: foreach (1..12) {
  15:    my $rn_str = linkNode(getRandomNode)."<br />\n";
  16:    if ($rn_str =~ m|^(<a href=[^>]+>)(.+)</a>|oi) {
  17:       my ($href, $node_title) = ($1, $2);
  18:       # having isolated the title, we chomp all long words..
  19:       $node_title =~ s/(\S{$len})\S{4,}/$1.../go;
  20:       $str .= $href . $node_title . "</a><br />\n";
  21:    } else {
  22:    # error trap here? or just ...
  23:       $str .= $rn_str;
  24:    }
  25: }
  26:
  27: $str;
This uses a global regexp to do the chomping, rather than a while loop, as in the new writeups nodelet, but it's essentially the same operation.. It also assumes that the string '</a>' won't appear in a node title (a pretty plausible assumption, I think!), and that the output of linkNode won't change too radically, when it parses that output.

I tested at home with the same routine in a script that I fed the RNN-generated HTML to on STDIN, and it works fine, chomping the words with more than 24 characters, but leaving the <a href...> part operational. As I've not tested in an Everything installation, it could probably use some more testing ;-)

That was the original idea, but I should probably use sleeping wolf's seemingly more correct way of getting the title out, which looks better, as well as shorter. The caveat about testing applies doubly here, because I don't have a working getRandomNode at home, so it's untested!


  10: my $str;
  11: $str.='<em>'.$phrase[rand(int(@phrase))]."</em><br />\n";
  12: 
  13: my $len = 20; # 23 - 3 (the other 3 is in the \S{4,} in the regexp)
  14: foreach (1..12) {
  15:    my $RN = getRandomNode;
  16:    my $node_title = $$RN{'title'};
  18:    # chomp those long words..
  19:    $node_title =~ s/(\S{$len})\S{4,}/$1.../go;
  20:    $str .= linkNode($RN, $node_title) . "<br />\n";
  21: }
  22: $str;
Comments, bugs, etc, welcome via /msg jk

(Thanks, ariels, for some improvements to my original, incorporated above, and for the suggested one-liner version which knocks out the if statement in my first attempt and accomplishes the chomping in one fell swoop:

1 while $rn_str =~
   s{^(<a href=[^>]+>)(.*)(\S{$len})\S{4,}(.*)(</a>)}
    {$1$2$3...$4$5}oi;
(the '1' at the start is not a line number, but a minimal expression, so the while can be used as a modifier, rather than as a statement, and the null block this would entail omitted.) I've kept my version in the interests of maintainability, but this would also work.)

Log in or register to write something here or to contact authors.