[Code snippets for layout layer] - [Parse * tags / make "Add as User Icon" buttons]

Apr 14, 2005 16:35

Two snippets. First, a function to parse strings for * and < comm=*> tags. Also includes a recursive function to find the earliest occurrence of a substring in a larger string using a binary search. These functions are useful for parsing picture or gallery descriptions.

function find_earliest_instance (string input, string subs) : int { #Finds the earliest instance of substring subs in in that occurs after character start if (not $input->contains($subs)) { return -1; } var int sub_length = $subs->length(); var int input_length = $input->length(); if ($input_length == $sub_length) { return 0; } var int mid2 = ($input_length / 2) + ($sub_length / 2); var int mid1 = $mid2 - $sub_length + 1; var int val; $val = find_earliest_instance($input->substr(0, $mid2), $subs); if ($val != -1) { return $val; } $val = find_earliest_instance($input->substr($mid1, $input_length), $subs); return $mid1 + $val; } function parse_lj_user (string input) : string { # FUNCTION BLOCK var string user_string = "<lj user="; var string comm_string = "<lj comm="; var string rep_base_1 = "<" +"a href='http://www.livejournal.com/userinfo.bml?user="; var string rep_commbase_2 = "'>

"; var string rep_base_4 = ""; var int beginning; var int end; var string string_to_rep; var string rep_string; var int rep_length; var string username; var string linktype; var string part1; var string part2; var string output; $beginning = find_earliest_instance($input, $user_string); if ($beginning != -1) { $linktype = "user"; } else { $beginning = find_earliest_instance($input, $comm_string); if ($beginning != -1) { $linktype = "comm"; } else { return $input; } } $end = find_earliest_instance($input, ">"); if ($end == -1) { return $input;} # REPLACE THIS STRING $rep_length = $end - $beginning + 4; $string_to_rep = $input->substr($beginning, $rep_length); $username = $string_to_rep->substr(12, $rep_length - 16); var int userlength = $username->length(); var string userbegin = $username->substr(0,6); var string userend = $username->substr($userlength - 6, $userlength); if ($userbegin->contains(""")) { if (not $userend->contains(""")) { return $input; } $username = $username->substr(6, $userlength - 12); } elseif ($userbegin->contains("'")) { if (not $userend->contains("'")) { return $input; } $username = $username->substr(5, $userlength - 10); } if ($linktype->contains("comm")) { $rep_string = $rep_base_1 + $username + $rep_commbase_2 + $username + $rep_base_3 + $username + $rep_base_4; } else { $rep_string = $rep_base_1 + $username + $rep_userbase_2 + $username + $rep_base_3 + $username + $rep_base_4; } $part1 = $input->substr(0, $beginning); $part2 = $input->substr($end + 4, $input->length()); $output = $part1 + $rep_string + $part2; if ($output->contains($input)) { return $output; } else { return parse_lj_user($output); } } #END FUNCTION BLOCK
The second snippet is HTML (encased in strings) for a form button that lets any user add an image's thumbnail as their own LJ user icon, assuming they're logged in. The URL, Keywords, and Comments values assume you're in Gallery View on the Super Simple style (having run $this->load_pict_descriptions()) but it's pretty simple to adapt to any view, just change $i.url, $p.title, and $p.des to the correct values for the image's thumbnail, title, and description respectively.


"""\n\n""";
I recommend setting properties in your layout for the use of these snippets. You can see an example in my modified Super Simple style, number 14200. This gallery (and each of its subgalleries) is using the style right now, if you want to see an example.
Previous post Next post
Up