Palagpat Coding

Fun with JavaScript, game theory, and the occasional outbreak of seriousness

Sunday, April 25, 2010

How to move to Blogspot and keep your blog on your own domain

Recently faced with the problem of how to have my blogs remain in their pre-existing locations while complying with Blogger's mandatory move away from FTP publishing, I think I've found a pretty easy solution: if you're on a LAMP server (which I suspect most of you are; it's nearly ubiquitous), it's as easy as removing all other index.* files and creating a new index.php file in the appropriate folder, and dropping in these 5 lines of PHP code, changing the URL in the first line as appropriate:

$session = curl_init("http://your.web.address");
curl_setopt($session, CURLOPT_HEADER, 0);
$content = curl_exec($session);
curl_close($session);
echo $content;

cURL is the secret sauce in there, and I could go into a lot more depth, but if you don't need to worry about authentication or anything more complex than a simple page-fetch, there's no need. I allow the post-comments and labels pages to link directly to Blogspot, and I use Feedburner to host my RSS feeds, so this simple solution gets me 95% of the way there. The rest is just resource-management and wise template design.

I love it when something that looks like it'll be difficult turns out to be so easy! But please, if I'm doing anything that opens me up to possible security problems (I don't think I am, but...), please let me know in the comments.

Labels: , , ,

Saturday, February 13, 2010

MUGEN plugin for SyntaxHighlighter

Earlier today, I uploaded my first blog post that needed to include MUGEN source code. Between this site and my MUGEN-centric blog, I suspect it won't be my last. So, I took the time to put together a plugin for Alex Gorbatchev's excellent SyntaxHighlighter, and now I can post blocks of MUGEN code that get auto-magically highlighted, like this:

[State 1000, fireball]
type = Projectile
trigger1 = AnimElem = 1
ProjID = 1010
projanim = 1010
projhitanim = 1020
projremanim = 1020
projcancelanim = 1020
velocity = 6,0
offset = 0,-80
; HitDef
attr = S,SP
hitflag = MAF
guardflag = MA
animtype = medium
priority = 4,Hit
damage = 40,5
pausetime = 4,4
sparkno = S9992
guard.sparkno = 0
hitsound = S2,2
guardsound = S3,0
ground.type = Low
ground.slidetime = 8
ground.hittime  = 8
ground.velocity = -6,0
air.velocity = -2,-2

Anyway, I thought this was worth sharing, so for anyone interested, you can download a copy of the MUGEN brush for SyntaxHighter right here.

Labels: , ,

Friday, November 06, 2009

Javascript Snippet: Flip Text Upside-down

This week I was playing with my Google Analytics account to see what was happening with my blogs; it turns out that my MUGEN blog is about a bajillion times more popular than this one. Guess I'm learning, as Arthur Conan Doyle did, that you don't get to pick what you're known for (sigh). That said, the most popular post here on the ol' Coding blog was this one, where I talked about my home-grown tag cloud generator. So, although Canvassa isn't going to be dropped, I'm going to pepper in more of these "snippets" types of articles from time to time... like today!

Back in April of this year, YouTube did an April Fool's joke where they turned their site upside-down by means of a sneaky little code snippet that swapped out all alphanumeric characters with characters that looked like upside-down versions of themselves (for example, 6 became 9, M became W, etc). Later, Paul Irish adapted the code into a jQuery plugin. Still later, I followed his lead and adapted the code as a Dojo plugin, and posted it on Twitter. But I never blogged about it here.

In fact, it's even been in one of my Github repositories for months now, completely undocumented save its internal code comments. Surprise!

Click here to flip all the posts on this page, re-arranging them from top to bottom, or click here to flip their text in place without moving it around.

I wanted to go back re-implement the code as a bookmarklet, but it's currently dependent on (and namespaced in) Dojo, so that didn't get done. Maybe for the next snippet.

Oh, also of note: I've re-arranged my site template a bit, moving navigation from the sidebar to the header, adding more social networking links to my profile, and pushing it further down the sidebar. This is part of my effort to harmonize all three of my blogs into a single site, and there will likely be more changes before that effort is complete.

Update, 11/30/2009: Some recent template changes to the blog have broken the dynamically-loaded code in this post; I'm working on a fix.

Labels: , ,

Monday, June 22, 2009

Simple tag cloud generator in JavaScript

After having to roll my own tag cloud generator for my blogs (I'm old-school, so I don't have access to a blog layout engine with prebuilt widgets), I thought I'd clean up the code and share it. So, it's in this new repository I just created on GitHub.

Usage is pretty simple; copy the JS (and optional CSS) to your server, and link to them from your blog code like so:

  <style type="text/css">
    @import "http://www.myblog.com/css/tagCloud.css";
  </style>
  <script type="text/javascript" src="http://www.myblog.com/js/tagCloud.js"></script>

Once you've got it loaded, there are only two steps to create a new tag cloud: first, you need to populate a JavaScript object with the tag information structured like an associative array, with the tag names as properties and the instance counts for each tag as its hash value. Ideally this will be retrieved via an AJAX/XHR request, but in a pinch you can hard-code it (but that's not terribly useful, since you'd have to constantly update it whenever you post something new):

    // hard-coded, pure JavaScript version:
    tags = {
      "JavaScript":17,
      "Conferences":2,
      ".NET":1,
      "GeoWeb":1,
      "Site news":1,
      "snippets":1,
      "Dojo":16
    };
    function init_tagCloud(parentId) {
      var parentDiv = document.getElementById(parentId);
      if (parentDiv) {
        var cloud = makeCloud(tags, "http://www.myblog.com/labels/", 0,1,4,' ');
        parentDiv.appendChild( cloud );
      }
    }
    window.onload = function() {
      init_tagCloud('target_div');
    }

    // or, Dojo-powered and XHR-populated (substitute JS lib of your choice):
    dojo.addOnLoad(function() {
      dojo.xhrGet({
        url: "getBlogCategories.php",
        handleAs: "json",
        load: function(data){
          tags = data;
          var cloud = makeCloud(tags, "http://www.myblog.com/labels/", 0,1,4,' ');
          dojo.byId('target_div').appendChild( cloud );
        }
      });
    });

The parameters on the makeCloud() method are:

  • tags: the object hash mentioned above
  • baseUrl: the URL that should serve as the base for all tag links
  • minCount: minimum number of times tag must be used to show up in the cloud (defaults to 0)
  • minSize: minimum font size for tags in the cloud (defaults to 1em)
  • max: maximum font size for tags in the cloud (defaults to 4.5em)
  • delim: delimiter(s) to insert between tags in the cloud (defaults to 1 space)
  • shuffle: boolean value that controls if the tags get shuffled in random order, or sorted (defaults to false)

That's about all there is to say about it. The basic math for determining the tag size is taken almost verbatim from the Wikipedia page on tag clouds, just slightly tweaked to allow for a minimum font size. Surely there are other customizations that could be made, and ideally this would be implemented as a Dojo/plugd-style plugin. I'll leave that for an exercise at a later date.

--Edit: I inadvertently forgot to strip out the <script> tags from the sample code blocks up above, and they were getting run! D'oh!

Labels: , ,