Skip to main content

Ralsina.Me — Roberto Alsina's website

MinCSS is amazing

I had this is­sue open in the bug track­er for Niko­la (my stat­ic site gen­er­a­tor) for a long time: "Add minc­ss sup­port­".

Well, no, it does­n't have it yet, but I did some re­search on whether it would be worth adding. And boy, minc­ss im­pressed the heck out of me.

You see, Niko­la's themes tend to use unadul­tered boot­strap, which means they car­ry a large num­ber of things that are not used in their CSS. Be­sides, it us­es sev­er­al stylesheets from do­cu­til­s, pyg­ments, and more.

What minc­ss does is ex­am­ine your HTML and your CSS, and re­move all the un­used CSS. So, I wrote a script that ex­am­ines the Niko­la out­put and over­writes the CSS files with the min­i­mal things that are ac­tu­al­ly need­ed there.

And the re­sult?

Here is the be­fore/after for each CSS file in Niko­la's de­mo site:

bootstrap-responsive.min.css  16849  3251
bootstrap.min.css            106059 14737
code.css                       3670  2114
colorbox.css                   6457   774
rst.css                        6559  2581
theme.css                      1287  1061
                             140881 24518

But wait, Niko­la sup­ports bundling all those files in­to a sin­gle large CSS file to avoid net­work re­quests (us­ing we­bas­sets). Does it work in that case too?

Well yes:

all-nocdn.css                167457 29496

But that is not al­l. The minc­ss files are not mini­fied. Pass­ing al­l-nocd­n.c­ss through Yui-­com­pres­sor shrinks it fur­ther to 20599 bytes. Which, gzipped, is a pal­try 4801 bytes. That means the com­plete styling of the whole site is a sin­gle CSS file less than 5KB in size.

That, is im­pres­sive.

Deploying Django Into My Cheap VPS

I am pre­par­ing to open my cheap site-and-blog-host­ing ser­vice to the pub­lic at some point, so I need­ed to do some ground­work in­to de­ploy­men­t. Con­sid­er that the host that will run it will have very lim­it­ed re­sources, so I need­ed to find lean and cheap so­lu­tions when pos­si­ble, but at the same time, I want to achieve rea­son­able re­li­a­bil­i­ty and ease of de­ploy­men­t.

Since this is a test­ing server, I want it to have git mas­ter de­ployed. I don't want au­to­mat­ic de­ploy­men­t, but I want to de­ploy of­ten, mean­ing sev­er­al times dai­ly.

I pre­ferred sim­ple tools in­stead of com­plex tool­s, light­weight tools with just enough fea­tures in­stead of heav­ier, more ful­ly-fea­tured tool­s. Your choic­es on each step could and prob­a­bly should be dif­fer­ent than mine, de­pend­ing on your sit­u­a­tion, re­quire­ments and per­son­al pref­er­ences.

So, here's my notes from how it's done cur­rent­ly. This is not meant as a HOW­TO, just a de­scrip­tion of what seems to be work­ing well enough so far.

Read more…

Javascript Makes Me Cry: Turning a Date into a String

Work­ing late last night in Al­va I want­ed to do some­thing that sound­ed triv­ial:

When the page load­s, get the cur­rent date and time, and if a cer­tain in­put is emp­ty, put it there like this:

28/05/2013 23:45

So, how hard can that be, right? Well not hard, but...

Getting the current date-time is easy: now = new Date(); So, is there something like strftime in Javascript? Of course not. You can get code from the usual places and have a untested, perhaps broken, limited version of it. And I am not about to add a strftime implementation to use it once. Sure, there are a number of Date methods that convert to strings, but none of them lets you specify the output format. So, let's try to do this The Javascript Way, right?

To get the el­e­ments I want to put in the val­ue, I used ac­ces­sor meth­od­s. So, ob­vi­ous­ly, these should give me what I want for the string, right?

now.get­Day(), now.get­Mon­th(), now.getYear(), now.getH­our() now.get­Min­ute()

Well, they are, at the date men­tioned above, re­spec­tive­ly: 2, 4, 113, er­ror, er­ror

Ok, the errors are easy to fix from the docs. It's actually getHours() and getMinutes(), so now we have 2, 4, 113, 23, 45 and of those five things, the last two are what one would expect, at least. Let's go over the other three and see why they are so weird:

Date.getDay() returned 2 instead of 28

Because getDay() gives you the week day and not the day of the month. Which is absolutely idiotic. So, you have to use getDate() instead. Which means the name is a lie, becasue the logical thing for getDate() to return is the whole date.

Date.getMonth() returned 4 instead of 5

Because getMonth() returns months in the [0,11] range. Which is beyond idiotic and bordering in evil. Come on, Javascript, people have been referring to may as "5" for nearly two thousand years now! What other language does this? Anyone knows one?

Date.getYear() returned 113 instead of 2013

Because it uses offset-from-1900. Which is amazing, and I had never heard of a language doing in a standard type. Because why? So, use getFullYear() instead.

Now, armed with the right 5 num­ber­s, let's for­mat it. Does Javascript have the equiv­a­lent of sprintf or for­mat ? Of course not. In JavaScrip­t, with­out 3rd par­ty mod­ules, you cre­ate strings by ad­di­tion, like a cave­man. Again, I know I could add a for­mat method to the String pro­to­type and make this work, but I am not adding an im­ple­men­ta­tion of for­mat or sprintf just to use it on­ce!

So, this pro­duces that I wan­t:

now.getDate()+'/'+(now.getMonth()+1)+'/'+now.getFullYear()+' '+now.getHours()+':'+now.getMinutes()

Un­less... the day or month are low­er than 10, in which case it's miss­ing the left­-­padding ze­ro. Luck­i­ly, for the pur­pose I was us­ing it, it worked any­way. Be­cause OF COURSE there's no in­clud­ed func­tion to left­-­pad a string. You have to do it by ad­di­tion. Or, of course, add a 3rd par­ty func­tion that's out there, in the in­ter­net, some­where.

Contents © 2000-2020 Roberto Alsina