Local HTML / XHTML Markup Validation from within Coda (sort of)

Having installed the Validator S.A.C. from habilis.net (a ‘stand-alone, easy to install, version of the W3C’s HTML / XHTML Markup Validator for Mac OS X’), you can access it as a web service (see the Advanced Topics section for a setup how-to). What I wanted was a way of utilising that functionality to validate a file from within Panic’s Coda.

What I ended up hacking together is an Applescript which opens a new browser tab (or window, depending on your browser settings) and running the current document (in the current active tab) through the validator web service, assuming you have installed the web service as noted above.

It’s not the greatest solution, partly due to my lack of Applescript skill and (I believe) partly due to the limited Applescript library in the current version of Coda (1.6.5 at the time of writing) but it works – with certain caveats as listed below.

Note that this all assumes that you are working on files which are under the web-server root and are being served up through the webserver – this doesn’t pass the file to the Validator but rather points the Validator to the local url of the file in question.

The steps needed to get this working are:

  • Download & install the Validator S.A.C. & run the web service setup
  • Turn on the script menu in Coda, if it isn’t already on (see controlling coda with applescript)
  • Customise the Applescript (below) & install into the Coda Script Menu
  • [optional step] Make a keyboard shortcut to trigger it

The Applescript

-- script settings
on CodaScriptSettings()
    return {displayName:"Validate with local Validator S.A.C.", inContextMenu:"yes"}
end CodaScriptSettings

-- actual script
tell application "Coda"

    try
        tell current split of front document
            set filePath to file path

            set validateTargetURL to replaceText("PATH_TO_SITES_FOLDER", "URL_FOR_ACCESING_SITES_FOLDER", filePath) of me

            set validatorURI to "http://localhost/w3c-validator/check?uri="
            set completeURI to validatorURI & validateTargetURL

        end tell

    on error error_message number error_number
        display alert ("YIKES! Something's wrong!") ¬
            message error_message ¬
            & (" Error number ") & error_number & "."
    end try
end tell

tell application "System Events"

    try
        if completeURI is not equal to "" then
            open location completeURI
        end if
    on error error_message number error_number
        display alert ("YIKES! Something's wrong!") ¬
            message error_message ¬
            & (" Error number ") & error_number & "."
    end try

end tell

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

You need to copy the above Applescript into a new script in the Script Editor application, edit the two paths (‘PATHTOSITESFOLDER’ which should be replaced with the path from the root of the drive to the sites folder – something like ‘/Volumes/DRIVENAME/Users/USERNAME/Sites/’ and ‘URLFORACCESINGSITESFOLDER’ which needs to be replaced with the URL you would use to access the same sites folder locally – something like ‘http://localhost/~USERNAME/’) and save the script into the Coda scripts folder (Home > Library > Scripts > Applications > Coda).

Using the script

Now switch to Coda, and in the Scripts menu, select ‘Reload Scripts’. Now when you go to the script menu again our script should show up as ‘Validate with local Validator S.A.C.’ – selecting the script should open a new browser window and run validation on the file which was active in Coda.

Add a shortcut key (optional)

In System Preferences > Keyboard & Mouse > Keyboard Shortcuts, click the plus button to add a new shortcut, then select ‘Coda’ from the ‘Application’ menu and put the exact name of the script in the script menu (‘Validate with local Validator S.A.C.’) into the ‘Menu Title’ box, then put your cursor in the ‘Keyboard Shortcut’ box and hit the key combination you want to use (I’m using cmmd+alt+v).

Caveats

This is only going to work on files which can be served up locally as mentioned above, but it does mean that it will validate the output of pages generated with server-side technologies (such as PHP). Of course if you try to validate an ‘incomplete’ file in terms of well-formed HTML (for example a PHP file which is included into other files, such as a common navigation section) you will get validation errors if it doesn’t have a complete HTML structure (which it most likely wouldn’t).

Some improvements which could be made (and there’s plenty…)

Ideally the script would get the complete URL to the file from the Coda ‘Site’ (assuming the current document is part of a ‘site’) rather than the hard-coded paths in the current script – as far as I could see there isn’t a way to do that with Applescript in the version of Coda I was scripting.

It would also be nice if instead of opening a new browser window in your default browser to display the result, the script opened a new ‘split’ directly in Coda, set it to ‘Preview’ mode and then opened the validation result within that – meaning that you could get a validation report without leaving Coda. Again, I don’t think that can be done with the current version but I would love for someone to prove me wrong!

Written by , Tuesday 20th October 2009

Comments

Gravatar
Chuck Houpt

October 22, 2009

Nice script. Another option is to launch the Validator application directly with something like:

tell application “Validator-SAC” open location validateTargetURL end tell

This could be useful if you don’t want to setup the Validator as a web service (for example, due to configuration conflicts with the site your developing, etc).

Gravatar

November 4, 2009

Oooh good call – and thanks for developing the Validator S.A.C. package in the first place Chuck!

Posting comments after three months has been disabled.