Sunday, August 23, 2015

Vim: Convert Markdown to HTML

This is pretty obvious, but I just started blogging, and I decided to dust off my old blogger account and use it for my posts, because, why not?

However, what has been bothering me is blogspots WYSIWYG editor, and while I can edit my posts in HTML, I am a bit too lazy to fight the editor. My process was to save rough drafts in Apache OpenOffice's native ODT format, then copy and paste the draft into blogspot's editor and 'polish' off the blog, switching between the Compose and HTML screens.

Then a thought hit me, why not blog in Markdown and convert to HTML, and post the HTML instead. Great idea, but how?

For almost a month now, I had made the switch from Emacs to Vim as my default editor and "IDE" of choice, so I did a search for available plugins and came across this awesome article, Vim Essential Plugin: Markdown to HTML

So I downloaded Markdown 1.0.1, unzipped it and placed it in my /usr/local/binand tested it out...

/usr/local/bin/Markdown.pl: not found

Whuh? Not found? Oh yes, I needed to ensure the file could find my perl, so I edited the first line in the file to reflect it's location.

$ which perl
/usr/local/bin/perl

I edited the first line of Markdown.pl so it looks like this #!/usr/local/bin/perl and I tested it from my terminal this time.

Markdown.pl --html4tags README.md

It's output was splendid, clean HTML. So I assigned my vim shortcut key, like so:

" Markdown to HTML
nnoremap <leader>m2h :%!/usr/local/bin/Markdown.pl --html4tags <CR>

I also downloaded the html2text.py script from Aaron Swartz html2text page.

That way I can now convert between the two formats within vim. Just follow the same steps as we did for Markdown.

As a sign off note, this is my first blog written entirely in Markdown, converted to HTML and published to Blogger!

Saturday, August 22, 2015

UZBL LastPass Bookmarklet

In my attempt to be as minimalist as possible, I came across a wonderful browser I never knew existed before a few days ago, UZBL. From their site:

Uzbl follows the UNIX philosophy - "Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface."

Uzbl comes with uzbl-core, uzbl-browser and uzbl-tabbed. For everyday web browsing, I use uzbl-tabbed, which in my dmenu setup, I created a script so that it shows up as just 'uzbl'. Less typing. Uzbl has modes, and keyboard bindings similar to Vi/Vim, so if you are already familiar with that editor, you are almost there.

Now in order to replace Opera, which was my default browser, and was very lightwieght, I needed a few things in place:

  1. Could it render the pages that I visited often and were important to me. CHECK[1]
  2. Does it support LastPass...not in the usual way other browsers do, via extensions.

After much searching on google, I came across an email thread where someone else had accomplished this. Okay, so it can be done, but how? Breaking down the email, I needed to understand two things, how to get the javascript code from LastPass and how to create a bookmarklet for uzbl.


A bookmarklet as it turns out is just javascript, well, that's a simplified explanation, but for this article, that's all that is relevant, and LastPass already has bookmarklets, all I had to do was log into LastPass and I was greeted with this message:

**You are using a browser that currently does not have a LastPass plug-in available.
This may result in reduced functionality. We recommend you use Firefox so you can use the plugin.
Install the bookmarklet if that's not an option.
Clicking the bookmarklet link will show you three options and their explanations. I chose to grab Fill and Fill Forms. Once downloaded, the next step is an adventure, how to actually create an uzbl bookmarklet, luckily someone already created a wiki page on how to do it.

Unfortunately, I could not understand it at first, and missed a few things, so it took me a bit longer to figure out. This is what I did to get it working for me:
  1. Saved LastPass Fill Forms as lastpass_bookmarklet to my home directory.
  2. Run the command from the wiki:

    $ cat lastpass_bookmarklet | sed 's/javascript://' \
    | python -c 'import urllib, sys; print urllib.unquote(sys.stdin.read())' \
    > lastpass_autofill.js

  3. $ mv lastpass_autofill.js ~/.local/share/uzbl/scripts/
  4. $ chmod +x ~/.local/share/uzbl/scripts/lastpass_autofill.js
  5. Edit ~/.config/uzbl/config and add the following line
    @cbind !lpaf = script .local/share/uzbl/scripts/lastpass_autofill.js

That's it, so whenever you need to autofill your login details, just go to Cmd mode and type the keybind you had set in step 5.

1 - As a bonus, I was also able to view sites that opera 12.16 blocked due to certification errors.