Wednesday 15 January 2014

Never powders!

Once upon a time, when I was very young, I had a good piece of advice. I must have been young as, since I've got older, I've taken advice less and less seriously... either that or I must have really respected whoever it was who gave me the advice (despite forgetting his name now). The advice was dead simple really and consisted of the two words in the title. I think I must have been asking about his illicit drug use... thinking about it while I'm writing I'm pretty sure I remember the chap now and I remember the place too. I'm pretty sure that we were just chatting and that the conversation was in the context of him being a disabled guest at the hotel I was volunteering at as a rather naive 16 year old. He was disabled for some reason I can't remember - I think he was either damaged as result of his work as part of the uniformed services or was suffering from some degenerative illness - and we were talking about his youth and the drugs he'd taken... he seemed really rather wistful about it all but not in that he wished he'd taken powders but in that he wanted a spliff.

His advice was to never take anything too strong... things like cocaine or heroin. Other things were fine, pills and herbs and such, but powders led to all sorts of issues.

My professional experience pretty much mirrored the thoughts behind that advice. The odd bit of wacky-backi was no biggie but those who played with smack were generally left buggered. Cocaine use I had less experience of professionally but the experience that I did have generally was mixed. Some people seemed to be happy as pigs in shite after taking the odd bit of coke but ended up feeling like shite for a while afterwards - some were just complete monsters when they'd taken it and shite afterwards.

The advice stood though as it seemed that those who took it were always beholden afterwards. Plus it is/was an expensive old drug and definitely a rich persons drug. I know people who take it like some people take champagne: every-so-often they can afford it and take it as a treat... others take it when they can't afford it and keep on taking it at the expense of their jobs, relationships, homes... even their children - not nice and really quite cheap in the end.

Heroin chic, or even cocaine chic, is all well and good - just so long as it's in a film and not in my face. Losing everything and fixing yourself and battling heroically back is lovely and all and heart-warming but not half as good as not losing it all in the first place and not being an utter knob!

The advice stands then. It might cost me everything or it might be a treat but either way it's not something I'm going to gamble on so I'll follow the advice and never take powders.

Those that don't follow that advice then good luck!

Monday 6 January 2014

Secret Santa Poem

Someone really rather lovely wrote this poem for my Secret Santa this year:

DOM

There once was a man named Dom
Who joined when Arcus 1st hit the bomb
He liked to brew his own drink
And enjoyed the sound of clink clink
But always wrote code with aplomb

Thank you Anon!

Thursday 2 January 2014

PHP: remove first and last lines

I end up consuming a lot of JSON both by JavaScript itself and also by PHP. The move towards callbacks within scripts means that I end up getting an awful lot of gumph returned alongside the meat of the JSON - thankfully most coders maintain the lovely formatting of the JSON and simply add a reference to the callback on the first line and then add an extra ); on the last line so that the code we get back from the remote server looks a little like this:

callback(
  /*[JSON HERE]*/
);

This leaves me wanting to remove the first and last lines of a string in order to get to the the JSON. Thankfully I found this post so I'll copy the solution here for the next time I need to do it:

<?php
  /*make an array*/
  $bits = explode("\n", $str);
  /*remove the first item*/
  array_shift($bits);
  /*remove the last item*/
  array_pop($bits);
  /*rebuild it*/
  $str = implode($bits);
?>

If, however, the return is all on one line like this:

callback(/*[JSON HERE]*/)

Then you'll need to alter the function to something like this:

<?php
  $str = substr($str, 0, -1);
  $str = substr($str, 9);
?>

Wednesday 1 January 2014

Hyde (frontend)

So we've now created our Google Sheet backend for Hyde and it's published, now we need a way to display the other data we're going to put there. To do this I've stood on the shoulders of giants and used all sorts of tools and libraries. Of course I've used jQuery but the layout is nigh on pure BootStrap (I guess I should use BootStrap 3 but I've not gotten around to using it just yet). Along with those fine libraries I'm also really rather keen on jsRender so I'll sling that in there as well.

Apart from those we'll use showdown.js and highlight.js and various other things as we see fit. I've packaged all these files up in a git repo so you're welcome to download them and for testing you can place them in a folder in your Google Drive, in Dropbox folder using site44 or set up your GitHub pages.

Once you've downloaded the repo all you need to do is make a couple of changes to the index.html file. On line 24 replace [LOCATION OF YOUR BLOG] with the actual URI for your site and rename it if you like. On line 43 replace [ID OF YOUR GOOGLE SHEET] with the id of your Google Sheet. Save and you should end up with something like the image above.

On my version I've implemented a comments system using a Google Form and various bits of jiggery-pokery, by all means have a squint and implement my version if you'd like but be aware that I'll be actively developing on there on-and-off so things are likely to change.

Hyde (backend)

I recently introduced my response to Jekyll on GitHub pages (which I've called Hyde) and promised that I'd go into more detail about it's usage, here goes...

In order to get started with Hyde let us start with the backend first. All you need is a Google Sheet to start with so head over to Drive and create a new Sheet - I've called mine mdblog but it really doesn't matter what it's called. We'll need 4 columns which I've called id, date, title and content. I'm guessing that we could also end up adding further columns like tags but for now that will do us I reckon.

Once the Sheet is made we need to make sure that we hit Publish to the web... on the File menu (don't forget to tick the Automatically republish when changes are made tickbox, and especially don't forget to click the button Start publishing) I've also named it but left it as that as I think that that will do in terms of sharing permissions. Now we need to look at the URI, it will be in the form of something like: https://docs.google.com/spreadsheet/ccc?key=0AiRgQIhodQXfdGVOMEZTcHFJbm95R1l0V1ljRWlBTHc#gid=0. That there long string of seemingly random numbers and letters after key= and before #gid=0 is what we're interested in (so: 0AiRgQIhodQXfdGVOMEZTcHFJbm95R1l0V1ljRWlBTHc). Copy that string and keep it somewhere safe. In order to check that we're cooking we'll need to populate at least one row so why not add some data? Keep things simple and add 1 to the id, a date in the format DD/MM/YYYY HH:MM:SS to the date, Hello World to the title and some markdown formatted text to the content.

Now with some data in our backend let's see if we can access it. Go back to your copied long string and copy it again onto another line then put https://spreadsheets.google.com/feeds/list/ in front and /od6/public/values?alt=json behind (you should end up with something like this : https://spreadsheets.google.com/feeds/list/0AiRgQIhodQXfdElYTEg2SmtBZTBrNjVlcC1ST3c1VEE/od6/public/values?alt=json). You should end up seeing a long string of json formatted text (Sometimes the ordering/naming of the pages within a spreadsheet can get confusing - if you're not seeing any json then take the same string and put https://spreadsheets.google.com/feeds/worksheets/ in front and /private/full behind (https://spreadsheets.google.com/feeds/worksheets/0AiRgQIhodQXfdElYTEg2SmtBZTBrNjVlcC1ST3c1VEE/private/full), this should give you an idea of the id of the specific page (in our case it is od6)).

That's it for your backend for now - that'll give you enough structure and data in order to get things working.