Emptied of expectation. Relax. (tinyjo) wrote in lj_nifty,
Emptied of expectation. Relax.
tinyjo
lj_nifty

Url rewriter

My workplace recently started blocking community.lj urls (I suspect they just block anything starting with community, to be honest). I could still read posts by editing the URL but that got tired fast so I decided to let my style do the work and wrote an S2 function to rewrite URLs from community.livejournal.com/commname format to commname.livejournal.com format. Stick it at the top of your layout layer to be able to call it anywhere in your style. It's not possible to use this with a theme layer so you do need to have your own custom S2 style if you want to do this. See this comment for info on how to do this in a theme layer - thanks!

The function is as follows:

# community URL rewriter
function urlRewriter (string url) : string {
	var string retUrl;

	if (not $url->starts_with("http://community.livejournal.com/")) {
		return $url;
	}

	# first drop the http://community.livejournal.com/
	var int len = $url->length();
	$retUrl = $url->substr(33, $len);

	# now scan the string for the first slash to
	# get the username part
	var string comm;
	var string entry;

	var int[] possLocations;
	$possLocations = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
	$len = $retUrl->length();
	var int j;
	$j = 0;
	foreach var int i ($possLocations) {
		if ($j == 0) {
			$comm = $comm + $retUrl->substr($i, 1);
			$entry = $retUrl->substr($i + 1, $len);

			# replace _ with - as we go
			if ($comm->ends_with("_")) {
				$comm = $comm->substr(0, $i);
				$comm = $comm + "-";
			}

			if ($entry->starts_with("/")) {
				$j = $i;
			}
		}
	}

	return "http://" + $comm + ".livejournal.com" + $entry;
}
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 24 comments