Robert Basic's blog

GitHub flavoured code fences in Hugo

by Robert Basic on March 28, 2016.

This was an undocumented feature until today, so I missed it when I was converting my site to Hugo last week. It is also possible to highlight code examples with GitHub flavoured code fences, or, in other words, with triple backticks ```.

I like this a lot because it makes highlighting code in posts easier. Typing the {{< highlight >}} shortcode is just awkward and I always end up forgetting either the angle brackets or add too much or too little currly brackets. Backticks are much nicer.

The code fences are not enabled by default, though. You need to set PygmentsCodeFences to true in Hugo’s configuration file and that’s about it. Everything else about syntax highlighting stays the same.

Change to backticks in old posts

I used these two simple sed one-liners to change all the highlighting shortcodes to the code fences:

find -name "*.md" -print0 | xargs -0 sed -i 's/{{< highlight \([a-z]*\) >}}/``` \1/g'
find -name "*.md" -print0 | xargs -0 sed -i 's/{{< \/highlight >}}/```/g'

Now I don’t even need a custom Vim function to insert the highlighting shortcode. Sweet.

Tags: highlight, hugo, syntax.
Categories: Development, Programming, Software.

Vim Hugo helper

by Robert Basic on March 25, 2016.

I think I just wrote my first Vim plugin. OK, it’s more a bunch of Vim functions slapped together than an actual plugin, but gotta start somewhere, right?

Last week I converted this blog to a static web site and I’m using Hugo as the static website engine. Writing posts is a lot easier now, plus it’s written in Go, which I started learning a few weeks ago.

Vim Hugo helper is a plugin, collection of Vim functions that help me while writing posts. It will hopefully speed up a few recurring tasks such as drafting and undrafting posts, inserting code highlight blocks, etc. I guess I’ll be adding more to it in the future, but for now it has a total of 5 functions.

Front matter reorder

Hugo posts have a front matter which is basically meta data for the content. Hugo will create the front matter out of an archetype which is a template of sorts for the front matter. One thing I don’t like is that when a new post is created, the meta data is sorted alphabetically so the HugoHelperFrontMatterReorder function reorders it in the way that I do like.

Drafting/undrafting posts

The HugoHelperDraft and HugoHelperUndraft functions simply draft and undraft posts by setting the draft meta data in the front matter to either true or false.

Code highlight blocks

The HugoHelperHighlight(‘language’) function inserts the highlight shortcode that comes built-in with Hugo and sets the language of the highlight block.

Setting the date of the post

Finally, the HugoHelperDateIsNow function sets the date meta data of the post to the current date and time.

It’s not much, definitely has room for improvements, but I used it even when writing this very post, so I guess this helper is helpful.

Tags: hugo, plugin, vim.
Categories: Development, Programming.

Find duplicate lines from a CSV

by Robert Basic on March 21, 2016.

Finding duplicate lines from a CSV file is something I have to do from time to time, yet not on a regular enough basis to remember it all. Plus, I’m trying to blog more often.

cut -d, -f1 file.csv | tr -d '"' | sort | uniq -dc

cut to split the lines at the commas and select the first field, then tr to delete any double quotes that encloses the field, then sort and finally with uniq to show only the duplicated lines and to prefix every line with the count of occurrences.

Tags: linux, shell.
Categories: Development.

Tags for PHP in Vim

by Robert Basic on March 09, 2016.

One thing I was missing for a long time in Vim is to be able to “jump to definition” in an easy and painless way.

The other thing I wanted to improve is to be able to tell easily where am I actually in the code base; to see the current class and method name of wherever the cursor was.

With a bit of googling and poking around, I finally came up with a perfect combo of 5 plugins (yep, five!) that enables me to do both, and a little bit of extra.

Tags made easy

Gutentags  is a brilliant Vim plugin that makes it so easy to have tags. Just install the plugin and boom! Tags! It will figure out things on it’s own and just generate the tags in the background. I use it daily for a fairly large code base and I never had any problems with the tags, or with Vim being unresponsive while the tags are being generated.

The only two settings I have set for it is what to exclude and where to store the tag files to not pollute the current project with them.

let g:gutentags_exclude = ['*.css', '*.html', '*.js']
let g:gutentags_cache_dir = '~/.vim/gutentags'

That’s it.

Jump to definition

Pair gutentags with CtrlP and it’s CtrlPTag method and you get jump to definition.

map <silent> <leader>jd :CtrlPTag<cr><c-\>w

Move to the method name you’re interested in, hit jd and it will jump to it’s definition. Tip: w means “insert word under cursor”.

Where the hell am I?

My second requirement for displaying the current class and method name was a bit more difficult to fulfill. It takes the tagbar, tagbar-phpctags and lightline plugins as well as the phpctags tag generator to accomplish that.

Let the tagbar plugin know where the phpctags bin is:

let g:tagbar_phpctags_bin='~/.vim/phpctags'

This will make the tagbar plugin use phpctags to generate tags for the current file. To finally display the current tag in lightline, I wrote a simple tagbar component for it:

'tagbar': '%{tagbar#currenttag("[%s]", "", "f")}'

My complete lightline settings at the time of writing this can be found here.

Tags: gutentags, lightline, php, tagbar, tags, vim.
Categories: Development, Programming, Software.

pugdebug 1.0.0.

by Robert Basic on July 01, 2015.

After 3 months since announcing that I’m working on pugdebug, and some 5 months since I actually started working on it, it is finally time to let version 1.0.0 out in the wild.

It’s been a busy 3 months: 82 pull requests got merged, 67 issues resolved, more than 350 commits pushed. A lot of changes, fixes and improvements found their way into this first version.

First of all, a big thanks goes out to Ivan Habunek and Srdjan Vranac for helping. They asked for and added new features, tested on Windows and OSX systems, helped fleshing out ideas.

One of the biggest news is that there are binaries built for Linux and Windows operating systems, using pyinstaller. These binaries include everything pugdebug needs to work so there is no need to install anything. Just download the binary for your system and run it. That’s it. It makes me incredibly happy that it’s possible to have it this simple to run and use pugdebug.

pugdebug

The user interface has improved a great deal. It is using dockable widgets for different pieces of the UI, making the layout of the application configurable by just dragging the widgets around. It’s not all great though, there’s still room for improvement, but it will be better over time.

pugdebug allows to debug multiple requests one after the other which helps debugging in a more “complicated” scenario where there are, for example, multiple AJAX calls triggered in succession. By starting to listen to incomming connections, pugdebug will listen to all incoming connections and, based on the IDE key setting, decide should the connection be queued for debugging, or ignored.

It is also now possible to create projects inside pugdebug, as a way to help switching between different PHP projects where debugging is needed. Simply name the project, set it’s settings and that’s it. pugdebug stores all the configuration files in it’s own directory, so nothing will be added to your PHP projects.

I’m especially happy and proud that pugdebug got included on Xdebug’s website on the list of remote debugging clients. Thanks to Derick Rethans!

For more information on how to use pugdebug, take a look at the read me file or the wiki and let me know if you have any issues with it!

Tags: debugging, pugdebug, pyqt, python, qt, stable, xdebug.
Categories: Development, Programming, Software.