The newLISP on Rockets blog

 RSS Feed for this blog

Converting other newLISP code to Rockets


Post #: 85
Post type: Blog post
Date: 2013-02-21 18:54:17.000
Author: Rocket Man

Today I relaunched my personal website at http://www.jeremyreimer.com. It had previously been running on Dragonfly, a web development framework that inspired me to write Rockets.

Converting the code was really easy. All it took was the following:

1. Replace all instances of (print) with (display) and (println) with (displayln)
2. Remove Dragonfly-specific functions like (web-root) that are no longer necessary.
3. Make sure to start each page with:


#!/usr/bin/env newlisp

(load "newlisp-rockets.lisp") ; this is where the magic happens!


and end each page with


(display-footer "Jeremy Reimer")
(display-page)


And that's pretty much it! So if you want to convert any Dragonfly code to Rockets, that's how you do it.

Comments (2)

Views: 9526


Refactoring Rockets


Post #: 84
Post type: Blog post
Date: 2013-02-15 11:59:24.000
Author: Rocket Man

I've made some changes to the code base that will make it easier to start new custom sites based on Rockets. This is for my benefit as well as everyone else's-- I'm currently developing a bunch of new sites with this technology!

Now, when you run setup-rockets.lisp, the script asks you for a long and short name for your blog, an owner name, and the database name. These are automatically saved in a new configuration file called Rockets-config.lisp. I went through every page in the Rockets blog and changed it so that it reads various custom data about the blog from the config file.

The config file looks like this:


(context 'RocketsConfig)
(set 'Database "ROCKETS-BLOG")
(set 'Name "The newLISP on Rockets Blog")
(set 'ShortName "newLISP on Rockets")
(set 'Owner "Rocket Man")
(context MAIN)


This should make it a lot easier to set up new sites with different configurations.

Comments (2)

Views: 7227


Adding custom Javascript or jQuery to your page in Rockets


Post #: 83
Post type: Blog post
Date: 2013-02-06 17:53:38.000
Author: Rocket Man

newLISP on Rockets comes with and works with jQuery, which lets you do all sorts of wild and wonderful things on your page. But where should you put your Javascript code?

It doesn't really matter where it goes as long as there are <script> tags around it, but sometimes you want to defer execution to the end of the page. This leads to the best performance, because the user isn't waiting for the Javascript to load before seeing the page elements.

I've modified (display-footer) to let you add a string of Javascript or jQuery code to your page and insert it right at the end. You assign the code to a string variable and then just call (display-footer "Your name" name-of-string-variable) and it will execute the Javascript or jQuery code at the end of the page.

Comments (2)

Views: 6074


Converting JSON data into a list


Post #: 81
Post type: Blog post
Date: 2013-01-30 16:28:47.000
Author: Rocket Man

JSON stands for the JavaScript Object Notation, and it's used by lots of applications because it's much more compact than the bulky XML format. It's also suspiciously similar to LISP lists, which isn't that surprising considering that JavaScript was originally written as a LISP interpreter.

The basic format is something like this:


{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}


I've written a function in Rockets that converts this notation into a native newLISP list. Basically all I've done is replace { with ( and [ with (, then removed colons, commas, and other extraneous syntax. I ran into a small problem with very large text strings, because newLISP's internal list evaluator doesn't like them and returns a "string token too long" error. To fix this, I put [text] and [/text] in place of everything that had quotes, which tells newLISP's internal evaluator to treat it as a long string of text. Using (read-eval) I convert this long string of long strings back into a list, and all the [text] and [/text] goes away and you have a normal list!

The function is called (convert-json-to-list) and it's included in Rockets. I'm already finding it useful for my new project.

Comments (5)

Views: 6694


How to extract data from an RSS feed in Rockets


Post #: 80
Post type: Blog post
Date: 2013-01-25 14:12:04.000
Author: Rocket Man

RSS feeds are useful and great, but the XML structure they come in is rather unwieldy to read through and parse. newLISP has a great function called (parse-xml) that converts an XML tree into a list, but now you just have an unwieldy list. It's helpful to be able to extract only the data we want and put it in a simple nested list.

I've written a new function in Rockets that lets you do just that.

First, get the data from the RSS feed URL that you want:


(set 'feed-url " http://penny-arcade.com/feed";))
(set 'rss-result (xml-parse (get-url feed-url)))


Now, we want to extract the fields "title", "author", and "link" from that feed.


(set 'field-result (get-fields-from-rss rss-result '("title" "author" "link")))


(field-result) will return the following:


(("News Post: The Book Of Divine Wisdom" "tycho@penny-arcade.com (Tycho)" " http://penny-arcade.com/2013/01/25/the-book-of-divine-wisdom1";)
("News Post: So many games!" "gabe@penny-arcade.com (Gabe)" " http://penny-arcade.com/2013/01/25/so-many-games";)
...etc ... )


If a field isn't present in the RSS data, (field-result) will populate that part of the list with the "nil" value instead.

NOTE: Remove the spurious ";" characters in the example code here before running it

Happy RSSing!



Views: 5906


Copying Apache log files to a database


Post #: 79
Post type: Blog post
Date: 2013-02-15 10:57:40.000
Author: Rocket Man

Apache log files typically just sit around filling up your hard drive space, a vast array of text too unwieldy to search through.

A useful thing to do is to parse these files and copy them to a database. I've written a script that will do this using Rockets. It's not a web page-- it's a script that you should run from the command line (use sudo newlisp log-to-database.lsp) It will create a new database from scratch called SERVER-LOGS.db that you can then query and search using SQLite.

The script can be found here: https://github.com/newlisponrockets/newLISP-on-Rockets/blob/master/log-to-database.lsp

Note: I may be updating this later, as the database files seems quite large (only slightly smaller than the logs themselves)

Comments (1)

Views: 6092


How to implement New/Unread Posts in a forum


Post #: 78
Post type: Blog post
Date: 2013-01-16 14:14:11.000
Author: Rocket Man

This is a trickier issue than it appears at first.

The obvious solution is to just check the date of the last posted message and compare it with, say, the last time the user logged in. But in practice this doesn't work, because you want to view a post and have it removed from your "New" list right away, not at some later date. You actually have to bite the bullet and store a list of posts that you have read or unread. I chose "read" because by default users don't have anything in that list, which is easier to implement.

The list is stored as a string of post Id numbers, delimited by hyphens. When displaying the list of forum posts, it checks each one to see if the post Id is in your list of read posts, just by doing a (find) search on the string.

That part is easy. But what about when someone adds a new comment to a thread you've read? Ideally you want it to be bumped back to "Unread" status.

The solution I came up with is to get a list of every user's Read list whenever someone posts a comment. Then you loop through it and if you find the Post Id in there, you just snip it out using (replace).

Oh, I also added a "Mark All as Read" button, for people who just want all those pesky "New" icons to go away, RIGHT NOW. It gets a list of every post and adds them all to your Read list.

Is it as efficient as it could be? Will it bog down when you have a million users? I have no idea. I'll cross that bridge when I come to it. For now, it seems to work pretty well.

Comments (1)

Views: 6036


View counts!


Post #: 77
Post type: Blog post
Date: 2013-01-15 17:19:09.000
Author: Rocket Man

I've added a small feature that shows the number of views each forum thread or blog post has gained. Each time you view a thread in the forum view or view a complete blog post by clicking on the link, the number is bumped.

Because this is a new feature, all the old views aren't counted. :( So they are starting at zero.

This did require a change to the database, as I didn't already have the column "PostViews" in the Posts table. This can present a problem when people are upgrading from an earlier version of Rockets that doesn't have that column. I've updated setup-rockets.lisp to create this field when making a new database, but people's existing databases won't have it. I'll probably have to add a "update database to latest version" script that will just add the missing columns for people with older versions. One nice thing is that the database doesn't need any values in there. It just needs to be altered to add this missing field.

Comments (4)

Views: 5390


Happy New Year and a server upgrade!


Post #: 76
Post type: Blog post
Date: 2013-01-07 14:36:31.000
Author: Rocket Man

Welcome to 2013! It's fun living in the future.

I've upgraded the server from Ubuntu 10.04 to Ubuntu 12.04 LTS, so please let me know if there are any hiccups or strange behaviors on the site.

I'll be posting more soon!

Comments (34)

Views: 12343


Towards the creation of newLISP on Rockets installation scripts...


Post #: 73
Post type: Blog post
Date: 2012-12-12 15:07:52.000
Author: Rocket Man

I'm now starting to use Rockets for projects at work, so I need a way to create the user database from scratch. Anyone wanting to start using the newLISP on Rockets Blog source code will need to do the same thing. So our desires align!

I've created a simple installation script called setup-rockets.lisp. It needs to be run from the command line:

sudo newlisp setup-rockets.lisp

This will ask you a few questions (database, name, password, etc) and set up a new database for you with the right tables and create an admin user. Obviously you never want this to be run by someone else over the web, so make sure that it can't be viewed over your web server (either by permissions, or by using my Apache .htaccess directive that excludes all files ending in .lisp from being viewed (.lsp files are fine though!)


# Prevent framework source from being accessed
<Files ~ "\.lisp$">
Order allow,deny
Deny from all
</Files>

# Prevent database files from being accessed
<Files ~ "\.db$">
Order allow,deny
Deny from all
</Files>


Once the database is created (it will append the ".db" extension as well... you notice I've excluded those files so that you can't download the database from the web or via wget) you need to make sure it has the permissions to read and write by the owner:


chown www-data:www-data DATABASENAME.db
chmod 755 DATABASENAME.db


and you should be ready to go!

Comments (20)

Views: 6319