Tag Archives: org-mode

Leveraging the Org Mode Structure with CSS

Org-mode is an incredible tool for editing text. May it be for HTML, Latex, doc, or even plain text format.

I’ve already told that I use it to edit this blog, but I also use it for almost every text file I have to do. For example, all my readme on Github are in .org. (Even if they don’t support the org format very well. Screw Markown, org-mode is superior!)

Without any surprises, I also use it if I have to write long texts for my commercial solution BACnet Help. I can write thousands of words without having to think once about any HTML tags (DIV, SPAN, BR…). This is all handled automatically when exporting to HTML.

This is all very unsurprising for any Org-mode user. However while inspecting a page in the Chromium browser, I noticed how an Org-mode document is so well structured when exported to HTML.

Let ‘s take the Table of Contents as an example. You get it for free, with all the links and anchors. But the more important part is how the TOC is made: with successive stages of <UL> and <LI>. The structure itself is giving information. With it, we can easily determine what is a subset of a section, and what is a subset of that subset.

Here is how the plain TOC looks: https://frozenlock.files.wordpress.com/2012/10/wpid-toc.png

Now let’s leverage the <UL> and <LI> structure with a pinch of CSS.

 #text-table-of-contents ul > li > ul {
    margin-left: 0.5em;
    border-left: 1px dotted #779898;
    border-bottom-left-radius: 1em;
    padding: 0.5em;
 }

If you don’t read CSS, just know this: We select the element identified by “text-table-of-contents”, find any children that’s an unlisted list (<UL>), inside a list item (<LI>), inside another unlisted list. Then we apply some simple decorations to those.

https://frozenlock.files.wordpress.com/2012/10/wpid-toc2.png

Such a simple change, and yet a tremendous impact!


Attach source to PDF when exporting in Org-Mode

I am a strong advocate in keeping the source of everything.

However, a source can easily be lost if it doesn’t follow the document. In LaTeX, there’s a package to attach a file to a PDF (like when you attach a file to an email). By doing so, the source will follow the PDF even if the common reader have no clue what it’s for, or even how to use it.

Here is how I attach my org source to every document I export to PDF:

;; Include the source file for every exported PDF (org-mode)
(eval-after-load "org-exp" 
  '(defadvice org-export-as-latex (around org-export-add-source-pdf activate)
     "Add the source (org file) to the resulting pdf file"
     (let ((filename (buffer-name)))
       ad-do-it ;do the function
       (let ((latex-buffer ad-return-value))
         (set-buffer latex-buffer)
         (while (re-search-forward "\\\\usepackage{.+}" nil t)); go to the end of packages
         (insert "\n\\usepackage{attachfile2}"); the package needed to attach files
         (when (re-search-forward "\\\\end{document}" nil t)
           (forward-line -1)
           (insert 
            (concat 
             "\\vfill\n"
             "\\footnotesize\n"
             "The source of this document is an Org-Mode file attached here:"
             "\n\\attachfile"
             "{" filename "}")))
         (save-buffer)))))

Simply add this code to your .emacs and you’re good to go! Please note that is simply a quick hack. Hopefully an org-mode developper will make a more integrated approach.


Syntax Highlighting with Org-Mode and Org2blog

Officemacro recently asked why I wasn’t using the WordPress sourcecode. To be honest, I didn’t even know there was such an option given to WP users. (Kudos WordPress!)

I did try it, but the resulting code wasn’t fitting with my cute CSS. Still, the idea of having a nicely highlighted code was a little lingering desire of mine.

I knew there was a way to do it with the Org-Mode export, but somehow the solution eluded me.

The first problem was directly in my org file:

#+BEGIN_SRC Powershell :exports code
$visio = [Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")
#+END_SRC

Seems about right? NO! The language mode must not begin by a capitalized letter! This is how it should be:

#+BEGIN_SRC powershell :exports code
$visio = [Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")
#+END_SRC

“But Frozenlock, your elisp code wasn’t pretty either!” Urhm… no it wasn’t, which brings me to the second problem. A careful attention to the *Messages* buffer shown that Babel was talking about a certain htmlize.el file. Guess what? This file didn’t come by default in my Win32 Emacs, nor in my org-mode package. The usual download and following code in my .emacs solved the problem:

(add-to-list 'load-path (concat dropbox-directory "htmlize"))
; Where dropbox-directory is the path to my dropbox.

Solved the elisp problem!

Unfortunately, Powershell was still a challenge. Again, the *Messages* buffer gave me the necessary hint:

(wrong-type-argument stringp t)

A quick google search brought me to Xah Lee’s website (which is like Rome, but for Emacs… you always end up there).

Note: as of version 0.5 of powershell-mode, it has a bug that generats the error 「(wrong-type-argument stringp t)」. This error is quite harmless but you can get rid of it by commenting out the devar of powershell-imenu-expressions (line 167-170), and comment out the defun of powershell-setup-imenu (line 171-179), and comment out line 206 of 「(powershell-setup-imenu)」.

Finally, after all this, you can now enjoy pretty syntax highlighting directly from my org files! Phew!