Oct 16,
2019

Markdown On The Command Line

Greetings people from the Interwebs!

The other day I was in need of creating a brief documentation file for internal use and instead of using a simple text file I thought — "Why not using Markdown and having a kinda readable text file plus a nicely styled HTML document?" —.

Outside this blog I've never really used Markdown or even Org mode to style documents, so I only remember the tags used for the headings, bold and italics, for everything else I need to check the documentation. Nonetheless with minimum effort, basically sprinkling the text with a few '#' and '*' symbols, maximum results with no word processors involved.

I should also add that being an Emacs user I'm probably in the minority not using Org.

Anywho, on my desktop I had already installed the cmark-gfm package (Github Flavored Markdown) to use it with Emacs for rendering Markdown to HTML — or CommonMark in this specific case1 — but I wasn't able to find mentioned anywhere an utility that was able to add a specific style sheet file to the produced HTML.

It's not a complicated task, it's an header and a footer if we want to be really fancy, with an HTTP link tag that points to the chosen CSS file, it could be done in an editor or with a shell one liner, a plain cat header output.html footer > final-document.html would do the job, but it's clunky and I wanted something completed with a single command.

Maximum Effort

Well, as I should have expected, the Emacs's Markdown mode has hooks that can be used to personalize the output and there is already an example available on the Emacs WiKi where I only needed to add a few touches, so this is what I put in my ever expanding configuration:

(defalias 'markdown-add-xhtml-header-and-footer 
  'as/markdown-add-xhtml-header-and-footer)

(defun as/markdown-add-xhtml-header-and-footer (title)
  "Wrap XHTML header and footer with given TITLE around current buffer."
  (goto-char (point-min))
  (insert "<!DOCTYPE html5>\n"
    "<html>\n"
    "<head>\n<title>")
  (insert title)
  (insert "</title>\n")
  (insert "<meta charset=\"utf-8\" />\n")
  (when (> (length markdown-css-paths) 0)
    (insert (concat
             "<style type=\"text/css\">\n"
             (with-temp-buffer
               (insert-file-contents (car markdown-css-paths))
               (buffer-string))
             "\n.markdown-body {\n"
       "  box-sizing: border-box;\n"
       "  min-width: 200px;\n"
       "  max-width: 980px;\n"
       "  margin: 0 auto;\n"
       "  padding: 45px;\n"
       "}\n\n"
       "@media (max-width: 767px) {\n"
       "  .markdown-body {\n"
       "    padding: 15px;\n"
       "  }\n"
       "}\n"
             "</style>\n")))
  (insert "</head>\n"
    "<body class=\"markdown-body\">\n")
  (goto-char (point-max))
  (insert "</body>\n"
    "</html>\n")))

I prefer to have a copy available locally instead to depend to a file downloaded from a CDN so the code above embed the CSS in the header plus an incantation to add some padding to the page created. By the way the canonical home-page where to find the official CSS used on GitHub is primer.style, but there are many style-sheets to choose from, like for example:

I'm quite sure a query in the reader's favorite search engine would find others.

To complete the function above it requires a couple of tweaks for cmark-gfm's invocation to render tables and the path to the style-sheet file to embed, in this case hidden in my Emacs configuration folder.

(setq markdown-command "cmark-gfm -e table")
(setq markdown-css-paths '("~/.emacs.d/github-markdown.css"))

Colophon

The astute reader would have surely noted that the variable name above is markdown-css-pathsi.e. plural. I haven't deeply investigated how Markdown mode for Emacs works and if there's a way to choose a specific style-sheet from a list, nonetheless my code always picks the first path, if available, from the list.

Or I could have installed Pandoc from the beginning with the bonus of PDF creation...


  1. Markdown is very minimal so it was natural to imagine extensions would appear to "fix it", so we have now few specifications like CommonMark, RMarkdown or Markdown Extra and at least 20 implementations ↩︎

 
 

Sep 06,
2019

Mason Build System

Greetings folks,

this is a quick personal remind note on how to specify an installation directory with the Mason build system, as the regular reader would recall I use Stow to install applications compiled from source to avoid tangling the path under /usr/local.

So, the incantation to use at configuration time is --prefix=/installation/path and that should be enough to specify the destination path of binaries and other assets at installation time.

The contest, for the curious reader, I was in need of an utility to control my Android phone using a PC's keyboard — and mouse — so the scrcpy's description fitted the bill. I'm pretty sure this was the first time that I stumbled on this build system.

So to make a long story short, it uses Mason and when compiling from sources the default installation is under /usr/local, but I wanted to install it under /usr/local/stow/scrcpy.

That's all.

 
 

Jul 13,
2019

Weekly Yak Shaving

Ahoy Emacs aficionados.

I don't know if it's a common experience to other Emacs' users, but often in my daily usage of the programmable editor, I became aware of a missing feature, I take a mental note that I should look into it later and usually I end up forgetting about it.

Rinse and repeat, but this time with a slightly sense of deja-vu.

Anywho, a few days ago, after a lot of postponement, I recalled that I wanted to expose the X's primary selection via a keyboard shortcuts on Emacs. For the non initiated, Xorg1 traditionally uses two distinct buffers for copy/paste text operations: the "primary selection" and the "clipboard".

In Emacs, from what I know, the only way to paste the primary selection is clicking the middle mouse button. Well, hidden in the documentation there's the gui-get-primary-selection function, but strangely it doesn't work when invoked directly with a shortcut. It's not a big deal, but that's probably the reason why I wasn't bored enough to fix it yet.

So nothing earth shattering, it's only a wrapper and it's not going to increase my productivity tenfold, but it was a pet peeve of mine. For the interested reader:

;; paste X primary selection
(defun yank-primary-selection ()
  (interactive)
  (insert
   (gui-get-primary-selection)))

(global-set-key (kbd "C-c C-y") 'yank-primary-selection)

Unfortunately, outside Emacs there isn't a common easy way to access the primary selection without applying brittle hacks like, for example, simulating a middle mouse button click sending the corresponding X11 event and this ignoring Wayland2 completely.

A little digression

I suppose for consistency with other desktops/operating systems, Shift-Insert is configured to paste the copy buffer and this is honored, I think, in all the applications I use, except for Gnome Terminal. In Gnome Terminal Shift-Insert paste the primary selection.

I tried in the past to add Shift-Insert to the default Control-Shift-v in the Gnome Terminal as a workaround, in other words having two shortcuts for the same action, but I doesn't seem to work. I tried to define an array with gsettings with the two bindings but only the first element of the array seems to be evaluated.

Bummer.

Another little digression

My default keyboard3 has a spare key in the top left corner, under the Esc key, so long time ago I mapped the "XF86Copy" and "XF86Paste" symbols to it. Unfortunately they aren't supported consistently on all applications as they work with Firefox for example, but I don't recall any Gnome or GTK based application where they do.

In Emacs I've added a couple of bindings, but I used to the traditional yank and kill4 shortcuts:

;; adding support for XF86Copy and XF86Paste
(global-set-key (kbd "<XF6Copy>") 'clipboard-kill-ring-save)
(global-set-key (kbd "<XF6Paste>") 'x-clipboard-yank)

To be honest, the configuration is there but I seldom use it.

The Shell

Now, for something slightly related, when using the shell and the result of a command is a single or at most a couple of lines, I can use it as an argument for another command using the following Zsh function instead to reach for the mouse to do a copy&paste:

# quote the last command to reuse output
_insert-last-command-output() {
    LBUFFER+="$(LS_COLORS= TERM=vt220 eval $history[$((HISTCMD-1))])"
}
zle -N _insert-last-command-output
bindkey '^[`' _insert-last-command-output

It's bound to Alt-` in my .zshrc and basically it relaunch the last executed command present in the shell history, with a couple of incantations used to remove colours from the output (a.k.a. ANSI escape sequences), and it injects it the Zsh input buffer. I don't recall where I "borrowed" the idea for the above function, but it has been handy plenty of times.

Finally, the Zsh copy-earlier-word is another pretty unknown little function that I've bound it to Alt-, that I want to throw to the interested reader. It treats the input buffer like a stack and pops out the current inserted words and it also works in the history parameters substitutions.

We should chalk it in the things Bash sorely misses.


  1. the free software implementation of the display server for the X Window System, in other words the thing that draws graphics on your Linux distribution ↩︎

  2. Wayland is a communication protocol that aims to replace the X Window System ↩︎

  3. three according to the ICCCM documentation, even if I never found how to use the secondary selection ↩︎

  4. What has two thumbs and an Ergodox ↩︎

  5. the Emacs user knows what I'm talking about, for the others not yet converted yank is paste and kill is cut ↩︎