TobyInkster.co.uk

editor-wrapper

The situation: you are a web author, surfing some of your own pages and spot a mistake. You hit the "View Source" button in your browser, fix the mistake and go on surfing. Later on you come back to that page with the mistake and the mistake is still there! Why? Because you were editing the file in your browser's cache, not the original file!

This situation annoyed me so much that I decided to do something about it!

Stage One

Firstly, you assign the source-code viewer for your browser to be, instead of your regular text editor, to be the following Perl script:

#!/usr/bin/perl

# options
$editor = $ENV{'XEDITOR'}||'/usr/bin/nedit';
$settings = $ENV{'HOME'}.'/.editor-wrapper';
die "Should be called with a web page as an argument\n"
	unless ($file = shift @ARGV);

# search for special line
open(FILE,$file);
READLINE: while (<FILE>)
{
	chomp;
	last READLINE if /^<!-- editor-wrapper:.* -->$/;
}
close(FILE);

# split into components
s/^.....editor-wrapper.//;
s/....$//;
($path,$servername) = split(/\@/);

# No special line found. Give up and do things as normal.
system("$editor \"$file\"") unless ($servername);

# special line found. do special stuff.
if ($servername)
{

	# OK -- we've found a specially formatted file
	print STDERR "editor-wrapper: FILE IS $path AT $servername\n";
	
	# Read our settings
	open(SETTINGS,$settings);
	SETTING: while (<SETTINGS>)
	{
		chomp;
		last SETTING if /^${servername}:/;
	}
	close(SETTINGS);
	
	# Split settings into original path and translated path
	s/^${servername}://;
	($orig,$trans) = split(/=/);
	
	# not found in settings file :-(
	system("$editor \"$file\"") unless ($trans);

	if ($trans)
	{
		# status message
		print STDERR
			"editor-wrapper: NEED TO TRANSLATE $orig TO $trans\n";
		
		# do translation
		$path =~ s/${orig}/${trans}/; #
		
		# got it!
		print STDERR
			"editor-wrapper: OPENING $path INSTEAD OF $file\n";
		$path =~ s/\"/\\\"/g; # for safety -- needs work!
		system("$editor \"$path\"");
	}
}

That is simply a wrapper around your editor. It's hard-coded to use Nedit as a text-editor, but you can change that on line 4.

Stage Two

Somewhere in each of the files in our website, we include a line like this:

<!-- editor-wrapper:/filename.html@MySiteName -->

Stage Three

Now when we open the file by pressing "View Source" in our browser, the script will kick in, and it will figure out that the original file name was "filename.html", but it won't know where to look for this file locally. This is why we have a settings file (~/.editor-wrapper). Add this line to it:

MySiteName:/=/home/tai/websites/mysitename.com/

This line says that for files at MySiteName the script should replace the / with /home/tai/websites/mysitename.com/.

Bringing it all Together

So now, when you open the file, by pressing "View Source" in our browser, the script will grab the file from the browser's cache directory, see the comment and combine that information with the settings file to find out the path to the original file, which it will open in your favourite text editor. Fantastic!

If the HTML file doesn't contain one of the special comments, or the site name isn't found in the settings file, the script will simply open the file in the editor from the cache as normal.

Further Tips

At work, I have the /var/www/html/ directory on the web server mounted via SAMBA as /mnt/html/ on my workstation. On a website in a PHP include file, that is included at the top of every page on the site I use:

<!-- editor-wrapper:<?=$_SERVER['SCRIPT_FILENAME']?>@SiteName -->

Then in my settings file:

SiteName:/var/www/html/=/mnt/html/

Now, when I hit "View Source" when browsing the site, it pops up the original PHP, rather than the generated, cached HTML.

Google Search

Article Details

Author:Toby Inkster
Licence:All rights reserved
Created:Mon, 16 May 2005
Status:Archived
Permalink:Permalink

Tags

Blogroll

Here are some other sites. Some might be good; some might be rubbish. You decide.