mdhabr – a commandline tool to convert markdown to habrahabr markup

November 21st, 2015 | Permalink

I have written a small toy to prepare articles for habrahabr using markdown.

I guess it will be buggy like hell since it is the first version, yet still…

To use it it you have to have NodeJS and installed as well as PATH variable set correctly (node installer does this all for you, just in case)

npm install -g markdown-habr

And it should be available now in your system. You should be able to just do something like

mdhabr my_article.md

Mongoose – a tiny, one-file http server. Absolute love.

July 29th, 2015 | Permalink

The story is this: I needed a tiny web server to provide with some front-end only website. The website used json loaded via AJAX calls, so was intended to be running on a server. One could not just open the index.html file in your browser.

But I needed the end user to be able to run my website anyway. Since I was pretty sure they will be running it on Windows I needed something like this:

  • It should be a single executable file. To not litter the project directory with tons of files
  • It should be portable. No installation. No complex, env-dependent configuration.
  • It should support command prompt
  • I should be able to specify the root (document) folder which files will be served. I didn’t need any CGI. Just files seved over HTTP correctly.
  • All of the files should be served with correct headers
  • I had not security concern since it was intended for local use only.

Now right, I know one can run server via node, ruby and even php. But again that was not an option, as well as assembling some portable nginx/apache etc.

After several unsuccessful tries I found Mongoose. It’s free (well it’s non-CGI edition is) and it gave me all I wanted and even more – the ability to run the browser on start. And it is pretty easily configurable via command line parameters. It even supports some rewrites!

..and then I gave the end user a bat file to run it:

taskkill -im mongoose.exe
start "Mongoose" "%~dp0\server\mongoose.exe" -listening_port 8085 -document_root "%~dp0\www"

And now sometimes I still use it as a way to quickly test my front-end stuff.

Quick guide to WordPress AJAX.

June 23rd, 2015 | Permalink

This article in russian on habrahabr

Ok, so there is documentation page on AJAX in WordPress which is pretty definitive. This post is for the lazy. I will be as brief as I can.

So here’s how it works: (1) you define the hook function, and (2) there is a special URL, something like wp-admin/admin-ajax.php, that recieves an action request variable, and if that variable matches hook the function is called. As easy as that.

So here’s how you use WordPress’ AJAX “WordPress way”:

(1) On back-end (in your functions.php file maybe) – define a callback for the ajax action. If your AJAX is used for site frontend, (not on admin panel), you have to define two callbacks every time (one for logged-in users and one for non-logged-in. Obviousely, if ajax is called from admin panel it is called by logged in user):

// The name of ajax action in this case is do_something

add_action( 'wp_ajax_do_something',        'do_something_callback' ); // For logged in users
add_action( 'wp_ajax_nopriv_do_something', 'do_something_callback' ); // For anonymous users

function do_something_callback(){
    echo(json_encode( array('status'=>'ok','request_vars'=>$_REQUEST) )); // Here you can echo your JSON or whatever
    wp_die();                                   // AJAX callbacks should always end with wp_die
}

(2) On back-end (in your functions.php file, too) – get the url for AJAX and pass it to frontend via script tag in head. You can use the hard-coded value, of course, but it’s not recommended. To get the url you can use admin_url('admin-ajax.php') which will return an url that can be passed to front-end then. I usually simply inject a script tag into wp_head:

function js_variables(){
    $variables = array (
        'ajaxurl' => admin_url('admin-ajax.php')
    );
    echo('<script type="text/javascript">window.wp_data = '.json_encode($variables).';</script>');
}
add_action('wp_head','wgfm_head');

Such injection will be inserted after all the assets in head so you will be able to use it in your script on ready event or, maybe, even call something yourself from the injection.

(3) On front-end So now it’s relatively easy to use it in your javascript on frontend. Assuming, I am using jQuery:

jQuery(function($){
    $.ajax({
        type: "GET",
        url: window.wp_data.ajax_url,
        data: {
            action : 'do_something'
        },
        success: function (response) {
            console.log('AJAX response : ',response);
        }
    });
});

And.. that’s all. Now front-end will call the url retrieved from admin_url('admin-ajax.php'), calling it’s action do_something and recieve answer echoed by function added to wp_ajax_do_something callback. As easy as that.

Of course, there are other ways. You can include wp_load file (which is considered really bad practice). You can use localize_script to inject your ajax url into one of your scripts, but that’s just wacky IMO.

Uh, also check the protocol. If your site has wrong SSL settings but is set to work with HTTPS – the admin_url returned will be to https, but the website itself will redirect to http. Firefox will not like it, thinking you are doing cross-origin request.

jQuery’s closest() in pure JS

December 19th, 2014 | Permalink

Imagine you have an element and you need to find it’s first parent that matches some selector.

Say, I had a plenty of elements having ‘div.audio’ element inside and i needed to find those who do. Of course, I could iterate through all of them and check if they contain such elements, but that would mean too much redundant querySelectorAll calls. So i decided to replace that with too much redundant .matches calls.

To the code!

Assembling portable Node.js (Manually installing nodeJS into folder under Windows)

December 2nd, 2014 | Permalink

For those who (like me) want to use nodeJS (or, like me, soft written on it, like LESS or bower) in a folder without installing it. And those, who want to have it somewhere on their USB or i don’t know. For example, for LESS there is a whole plenty of third-party compilers. But original is always better, isn’t it?

This approach may be useful for numerous occasions, one of which is having several different versions of node on your PC. Also it may be handy to set everything up once and then use it on different machines, without interfering with those machines’ software.

(more…)