Sunday 23 December 2007

Moonshine


pa loved his ol' moonshine.

I've been working on a project to integrate Lua with the Firefox web browser, in the form of an extension which adds support for the language to write background services or on-demand applications to run directly in the browser.

I have yet to write more code to get the integration working. I will enter this project into the Mozilla Lab's extend Firefox Contest. So hopefully, I'll have enough implemented by the deadline.

For more information, please see the Google code project page.


Image courtesy of ladyphoenixx_1999 (CC-BY-NC-ND)

Digg This Story

Monday 26 November 2007

XML parser demonstration




Video on YouTube.

I managed to get the parser to handle attributes and tags with attributes.
xml.stag is no longer necessary and has been replaced with the more vesatile xml.tag .
Above is an approximately 8 minute video demonstrating use of the parser within the Lua interpreter. I apologise for the poor quality.

The next step for the parser now is to implement handling a specific occurrence of tag for the specified functions.

This video is licenced under same licence as the rest of this blog contents, except third party stuff of course (such as featured images).

Digg This Story

Tuesday 20 November 2007

Lua XML Parser - so far, so good.


California XML licence plate.

As mentioned in my last posting, I have been developing a simple XML parser for use with the Lua programming languge. It will most likely be a very basic parser, that is not one on terms with more advanced parsers.

So far I have implemented 2 basic functions for my parser:


  1. Loading XML documents

  2. Parsing short tags (e.g. Something like <tag>value</tag>)



To include the functionality of the parser (which is a Lua module) into a new Lua program, I include the line:


xml = require "xmlparser"



This gives the program access to the two functions (I have so far) provided by my parser.

These are:


xml.load(file as str)
xml.stag(xml as str, tag as str [, occur as int])


I have written this simple program to test my parser as it stands so far:


--------------------------------------------------
-- Example program using XML Parser
-- Copyright (c) 2007 Sam Saint-Pettersen
-- Released under the GNU General Public License
-- http://creativecommons.org/licenses/GPL/2.0/
--------------------------------------------------

-- include xmlparser module
xml = require "xmlparser"

-- load xml file
file = xml.load("employees.xml")

-- display name
print("Name: " .. xml.stag(file, "last") .. ", " ..
xml.stag(file, "first")) -- < this should be on above line
-- display position
print("Position: " .. xml.stag(file, "position"))
-- display e-mail addresss
print("E-mail: " .. xml.stag(file, "email"))
-- display home page
print("Home page: " .. xml.stag(file, "homepage"))



Below is my test XML file:

<employees>
<employee>
<first>Phoenix</first>
<last>Lightfoot</last>
<position>Lead Programmer</position>
<email>p.lightfoot@pathetecsys.com</email>
<homepage>http://www.pathetecsys.com/~lightfoot</homepage>
<telephone type="office">001 222 222</telephone>
<telephone type="mobile">777 222 333</telephone>
</employee>
<employee>
<first>Hugo</first>
<last>Jacob-Jones</last>
<position>Chief Executive Officer</position>
<email>h.jacobjones@pathetecsys.com</email>
<homepage>http://www.pathetecsys.com/~jacobjones</homepage>
<telephone type="office">001 222 200</telephone>
<telephone type="mobile">577 242 321</telephone>
</employee>
</employees>


Here is the console output from running my test program under the Lua interpreter:


Name: Lightfoot, Phoenix
Position: Lead Programmer
E-mail: p.lightfoot@pathetecsys.com
Home page: http://www.pathetecsys.com/~lightfoot


So far, so good. :)

I have yet to add the capability to specify an occurrence of a tag, based on its index and functions for parsing tags which contain attributes such as
<tag attribute="value">anothervalue</tag>


I should have that done soon enough.




Featured image courtesy of lambdageek (CC-BY-NC)



Digg This Story

Thursday 15 November 2007

An XML + Lua Project


Ah... XML. Nicely structured data.

There's been a few too many political posts lately. Not wanting to disappoint my (most probable) only reader, Tim, I've decided to get back on topic.


I will be using Lua to work on an upcoming project.
The project in question is a simple XML parser, which I will need later.
(Well, not need exactly. But it would be very helpful to have).



Here is a small snippet of code for the purpose of getting used to pattern matching (No, I'm not a World of Warcracker; but that is a very good reference).


function main()
local string = "tagvalue"
local match = string.gsub(string, "</?%a+>", "")
print(match) -- prints tagvalue
end
main()


Very simple stuff - here's the deal:


  1. Look in string for matches for start and end tags.

  2. Replace these tags with nothing.

  3. Print out the value contained within the pair of tags.


That will pretty much handle (with some modification of course) getting a value from within a specified pair of tags. But of course, I have to consider more advanced tags such as those with attributes.


But I will be working on this stuff shortly.




Featured image courtesy of hub19 (CC-BY-NC-SA)




Digg This Story

Monday 29 October 2007

Digg intergration

Digg

I've now added Digg submission capability to this blog. To add a post you like to Digg, just click the Digg This Story button. ;)


Digg This Story

Wednesday 17 October 2007

Click icon for Feedburner feed

FeedBurner

I have created a FeedBurner feed for this blog. However there seems to be an issue somewhere that if I choose to add an external feed provider as Blogger allows users to, it creates some sort of error in the blog mark-up, which in turns means I get errors when I try to publish to this blog via ScribeFire via the Blogger API.

Anyhow to fix the issue, to subscribe to the feed provided by FeedBurner, just click the Blog feed icon icon under the About Netting.NET section. Thanks.



Digg This Story

Monday 15 October 2007

Digg API on AIR

Digg API on AIR

Larger screenshot on Flickr.

Hey, everyone. I do have a programming related post today.

I have been coding in JavaScript, XML/HTML and CSS to create the above simple Digg API AIR application, which retrieves the top (e.g. newest) story for each category on Digg.

I have as good as finished it and will be releasing the AIR installation package (*.air) and source code (*.zip) for anyone who is interested in the AIR platform to look at.

Although I could have done an XmlHttp request without any additional framework, I did use the Prototype JavaScript framework for this project.


Here is a quick rundown of the files used in my application and their purpose:


DiggAPI.xml - This is the file which specifies the name, copyright information and file to use for content, etc. An XML file such as this is a standard requirement in any AIR application.


DiggAPI.html - The application content. This contains all of the HTML code for my application and calls to the neccessary JavaScript functions in my external *.js files.


sampleCert.pfx - The certificate used to sign the AIR application. All AIR applications must be signed to be built, even if not by a recognised body such as VeriSign.


script/AIRAliases.js - JavaScript file containing code used by the AIR runtime. Referencing this file provides convenient aliases for use in your code. Included with AIR SDK.


script/prototype.js - JavaScript file containing code for the Prototype framework. I used the cross-browser new Ajax.request provided by Prototype in my application.


script/DiggAPI.js - JavaScript file containing my code used by the application.


style/DiggAPI.css - Style sheet used to format my links and text colours, etc in my application.


style/digg.png - Digg guy logo image used in my application.


Check back soon for included AIR package and source code. Happy coding. :)



Digg This Story

Tuesday 9 October 2007

Syntax highlighter script

I have been trying to write a simple script in JavaScript to highlight source code in a element such as the code.

Here is the code:

/*
Syntax highlighter for source code
Copyright (c) 2007 Samuel Saint-Pettersen
This script is licensed under the GPL
http://www.gnu.org/copyleft/gpl.html
*/

function syntaxHighlight()
{
var element = "code"; // element for source code
var attrib = "class"; // attibute for language

// count all code elements
var codes = document.getElementsByTagName(element);

// get contents and class value for each code element
for(var a = 0; a &lt; codes.length; a++)
{
var code = codes[a];
var contents = code.innerHTML;
var language = code.className;

if(language == "boo")
{
// define first keywords
var keyw1 = new Array();
keyw1[0] = "def";
keyw1[1] = "print";
keyw1[2] = "if";

// colour first keywords
for(var b = 0; b &lt; keyw1.length; b++)
{
var regex = new RegExp(keyw1[i], "ig");
var matched = contents.match(regex);
var keyword = matched.toString();
var repl = keyword.fontcolor("blue");
codes[i].innerHTML = contents.replace(regex, repl);
}
}
}
}

Problem I have with this code is that after the first keyword is coloured, the loop does not seem to continue. Hmmm... If anyone knows what I'm doing wrong, please leave a comment. Thanks.

Digg This Story

Monday 8 October 2007

Yoda Origami: Destroy the scissors, we must.

yoda in origami
This is one of the most interesting things I've found on Digg. Check it out. Yes, I know it isn't programming related. But I am working on a coding project and I will be sure to post about it here shortly.

Digg This Story

Sunday 7 October 2007

Fnordography, a 3D Realmer web comic

fnordography by phait Okay. Not a programming related post tonight.
But if you are a 3D Realms forum user (as I am - Samji);
you might want to check out this web comic being produced by my pal Justin Self or as he is better known on the Realms, Phait.

The comics are a lighthearted jab at some of the users we
have over there. Phait describes it as such himself...

Rather than having to think up my own characters and backgrounds, I thought the online personas of several members
themselves each exuded a type of character that was ripe for parody or playful exploitation - not excluding myself
(Phait). As this strip is mostly intended for the community, some of it's attempted humor might only be effective
for the members themselves. Though, I have tried to keep it fair to members and non-members alike.

So don't cry at his quips :).
Unless he starts putting you in situations involving 'net shock memes you would rather not be part of. Goatse or Tubgirl, anyone?

Anyway, be sure to check out Fnordography!

Digg This Story

Tuesday 2 October 2007

Adobe AIR

helloworld application for AIR
Today I decided to check out Adobe AIR.
AIR (Adobe Integrated Runtime) is a runtime platform / environment which allows the creation of desktop applications which use existing web technologies such as HTML and AJAX.
Since it is Adobe, unsurprisingly, it also has support for Flash.

To develop an AIR application, all you need is the AIR SDK (which is freely available from Adobe Labs) and your exisiting development tools. E.g. your text editor for your code.

Following the tutorial for developing for AIR with HTML and AJAX, I coded the customary Hello World application.

The tutorial, you follow for its output is complicated. But I think this is due to the demonstration of how a more complex AIR application should be coded. E.g. You use an XmlHttpRequest to display the message to the user.

Development of the AIR application was quite straightforward, so maybe I'll develop something which uses the Digg or YouTube APIs in it.

Digg This Story

Digg API and Greasemonkey


I wrote this straightforward Greasemonkey user script in JavaScript using Greasemonkey's implementation of an XmlHttpRequest:

// Digg API script
// version 1.0
// Copyright (c) 2007 Samuel Saint-Pettersen
// Released under the GPL licence
// http://www.gnu.org/copyleft/gpl.html
// ---------------------------------------------------------------------
// This is a Greasemonkey user script.
// To install it, you need Greasemonkey:
// http://greasemonkey.mozdev.org
// ---------------------------------------------------------------------
// ==UserScript==
// @name Digg API script
// @version 1.0
// @author Samuel Saint-Pettersen
// @description Utilise the Digg API
// @include *
// ==/UserScript==
(function() {
GM_xmlhttpRequest({
method: 'GET',
url: "http://services.digg.com/stories/topic/gadgets?count=1&
appkey=http://apidoc.digg.com&type=xml",

onload: function(responseDetails) {
var response = responseDetails.status;
var returned = responseDetails.responseText;
if(response != "200")
{
// if the request fails, display error message
alert("Error retrieving Digg Data");
}
else
{
// display Digg data in message box
alert(returned);
}
}});
})();

The Greasemonkey script request returned fine.
I wonder what it is with my .NET application which meant it didn't work?

Digg This Story

Monday 1 October 2007

Digg API and .NET


I have being trying to develop a simple console application, which utilises Digg API in the Boo programming language.

This is the source code I have for my application:

/*
Simple application to access the Digg API
Version: 1.0
Language: Boo
Environment: SharpDevelop
Author: Samuel Saint-Pettersen
---------------------------------------------------------------------
Prerequisites:
* Boo libraries (included with)
* Microsoft .NET 2.0 Framework
---------------------------------------------------------------------
*/
namespace DiggAPI

import System
import System.Collections
import System.IO
import System.Net
import System.Text
import System.Xml
import System.Web

print "Digg API application\n"

// base request url
burl = "http://services.digg.com/stories/topic/gadgets?count=3&appkey="

// encode appkey
appkey = HttpUtility.UrlEncode("http://apidoc.digg.com")

// full request url
address = burl + appkey + "&type=xml"

// create request
request = cast(HttpWebRequest, WebRequest.Create(address))

// method
request.Method = "GET"

// get reponse
response = cast(HttpWebResponse, request.GetResponse())

// get response stream into reader
reader = StreamReader(response.GetResponseStream(), Encoding.UTF8)

// print response
print reader.ReadToEnd()
print "\n"

// clean up
reader.Close()

// wait for key press
print "Press any key to continue...\n"
Console.ReadKey(true)


For some reason, when I run this application I always get a Forbidden (403) error returned.

Weird? I'm sure I've got the the request URL correct. I get this error whether or not I encoded the appkey.

I'm sure this is some sort of odd behaviour on .NET's part, so I decided to try using the Digg API with something else.

If you know what the the problem is with the .NET application, please leave me a comment or e-mail


EDIT 10/03/2007:

Timothy Parez helped me out. The solution was to specify the
UserAgent. So I used:


request.UserAgent = appkey;


I used my encoded appkey for the the user agent string. However, in
the Digg API's current state it does not seem to matter whether or not
the appkey is encoded. But I might as well encode it in case they
change this.

Thanks, Timothy.

Digg This Story