Publishing in WordPress with emacs and org2blog

In this post, I will provide instructions about setting alias for org2blog in .emacs file, and how post in wordpress from emacs, inserting code blocks (examples with R), plots, and equations in your post.

This is my first post using emacs, org2blog, and wordpress. I gotta say that this will save a lot of time and make my posts a way more time-efficient. If by any change I become more active in this blog, it is because of this facility. I am going to explain how to use such functionality for mac users. The overall process is basically the same for non-mac users, though. I will assume that you already know how to use emacs and og-mode.

I am using Mac with Aquamacs 3.1a GNU Emacs 24.4.1, org2blog version 20141214.2105 downloaded at Nov-6-2014 (available through Melpa of here). The instructions about settings and installation of org2blog can also be found here.

So, lets start.

org2blog is a package that allows you to publish you posts directly from your emacs buffer. You can install the package using Melpa or download it manually in GitHub here. The instructions here are for the second approach. On Mac, the easiest way to download the package from GitHub is to open the terminal (from emacs of not) and to navigate to the .emacs.d folder. Once in that folder, just type

git clone http://github.com/punchagan/org2blog.git

and you will be downloading automatically the package into to correct folder.

You will have to install two more packages (dependencies). org2blog depends on them. They are xml-rpc (available here) and metaweblog.el (available here). To download xml-rpc and metaweblog.el you can do the same procedure you did to download org2blog.

In the case of the file xml-rpc, just put it in the folder you want to. I strongly recomend to use the same folder you put the org2blog files and the specially the org2blog-autoloads.el one.

An alternative way to install them all is, inside the buffer:

M-x package-install RET xml-rpc
M-x package-install RET metaweblog
M-x package-install RET org2blog

After downloading the packages you have to load the code provided by the .el file in your .emacs file. Suppose you saved org2blog-autoloads.el and xml-rpc.el in the Users/myname.emacs.d/org2blog/ folder and the metaweblog files in Users/myname.emacs.d/org2blog/metaweblog/. So add in your .emacs file:

(setq load-path (cons "/Users/myname/.emacs.d/org2blog/metaweblog/" load-path))
(setq load-path (cons "/Users/myname/.emacs.d/org2blog/" load-path))
(require 'org2blog-autoloads)
(require 'xml-rpc)

This code will load the org2blog functionalities.

Lets know list and configure your blogs so that .emacs see it. Include in the .emacs file the following:

(setq org2blog/wp-blog-alist
      '(("wordpress"
         :url "http://username.wordpress.com/xmlrpc.php"
         :username "username"
         :default-title ""
         :default-categories "my-default category"
         :tags-as-categories nil)
        ("my-blog"
         :url "http://username.server.com/xmlrpc.php"
         :username "admin")))

You are defining two blogs here. Just change the url, username, etc accordingly. The “wordpress” and “my-blog” are just labels to use identify them when you log in from emacs buffer. When you log-in from the buffer, emacs will ask you which blog you want to post.

There are a lot of costumization features that you can use. I particularly don’t use them.

Now, you are ready to go. Below there is a short list with the basic commands that are very useful (some of them essential) to post. The third column includes the alias that I created in the emacs file to make the proccess of posting even faster.

Table 1: Table with some essential and/or useful commands for org2blog
Command Shortcut Description Alias
M-x org2blog/wp-login   log-in blog-in
M-x org2blog/wp-new-entry   initiate new a post blog-new
M-x org2blog/wp-post-buffer C-c d post buffer as draft  
M-x org2blog/wp-post-buffer-as-page C-c D post buffer as page draft  
C-u M-x org2blog/wp-post-buffer C-c p publish buffer blog-post
C-u M-x org2blog/wp-post-buffer-as-page C-c P publish buffer as page blog-postp
M-x org2blog/wp-delete-entry   when used inside the buffer, it delets the entry in the website  
M-x org2blog/wp-delete-page   idem, but delets the page  
C-u post-id M-x org2blog/wp-delete-entry   it asks you to provide the postid that you want to delete  
M-x org2blog/wp-mode   It is a key-building functionality that will enable C-c d, C-c p and C-c t (complete tag name when inserting it at the top of the page)  
org2blog/wp-use-sourcecode-shortcod   turn in the wordpress sourcecode /sourcecode for code blocks  
M-x org2blog/wp-logout   log-out blog-out

Note that the complete line command in the .emacs file to definde the alias must be:

(defalias 'alias #'org2blog/wp-login)

Where you substitute “alias” fot your prefered shortcut. Them type, for instance C-u M-x blog-post and you will be publishing your new entry.

In order to modify the content of a existing post, it is only necessary to publish the buffer again. No additional post will be created, but the existing one will be updated (based on the postid on the head of the file).

So, to finish this post, lets suppose we already did all the steps above. We are ready to post. The steps are:

  1. Create a new buffer
  2. initiate a new post (M-x blog-new. The command will ask you to log-in you had not alerady done that for the post)
  3. Write your beautiful toughts (it is totally on you)
  4. Publish it as buffer of page (see 1)

Lets include a plot, a code block, and an equation in our post:

To include an equation, just type

The inline equation is writen as \(\sum x^2\). And the unumbered as:
\[ f(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-(x-\mu)^2/2\sigma} \]
You can also write a numbered one (maybe html do not show the number)
\begin{equation}<< >>
f(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-(x-\mu)^2/2\sigma} 
\end{equation}

The result will be:

The inline equation is writen as \(\sum x^2\). And the unumbered as:
\[ f(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-(x-\mu)^2/2\sigma} \]
You can also write a numbered one (maybe html do not show the number)
\begin{equation}<< >>
f(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-(x-\mu)^2/2\sigma} 
\end{equation}

The code block in org-mode is simply enclosed by “”#+Begin_src” and “#+End_src”. For instance, the following code block

#+begin_src R :exports both :results output
df <- matrix(1:10, nrow=2)
df
#+End_src

produces

df <- matrix(1:10, nrow=2)
df
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

Note: I will provide more detailed discussion about code blocks and exporting features of org-mode in another post soon. You can find information here. For now, just note that the code block can be executed inside the buffer. The org-mode read the code block, either execute it or not, and can post the code, the results of both. Which way will be depends on the instructions on the heading of the code block.

Lets include a plot in our post created by the code block inside the buffer. There two ways (at least) to include the plot in the buffer. You can first save the plot locally and them link it or to do it at once (save and link).

Lets first save locally and them link the plot to upload in the post automatically.

The code block must be

#+BEGIN_SRC R :exports code :results output
png(file='../plots/plotExample.png')
plot(1:10,1:10)
dev.off()
#+end_src

[[../plots/plotExample.png]]

And the result is

png(file='../plots/plotExample.png')
plot(1:10,1:10)
dev.off()

plotExample.png

The other approach is better and strainghtforward. Lets finish with this elegant approach, with a elegant plot. The code block do not show the code in the post, only the plot.

The code block (to not show the code, just the plot) must be

#+BEGIN_SRC R :exports results :results graphics :file ../plots/plotExampleBivariateNormal.png
require(plot3D)
require(mvtnorm)

mu1	<- 1	# expected value of x
sig1	<- 0.5	# variance of x
mu2	<- 2	# expected value of y
sig2	<- 2	# variance of y
rho	<- .3	# corr(x, y)

x <- seq(-2,5,length=100)
y <- seq(-2,5,length=100)

jointpdf <- function(x,y) dmvnorm(cbind(x,y), mean=c(mu1,mu2),sigma=matrix(c(sig1, rho, rho,sig2), byrow=T,nrow=2)) 
fxy <- outer(x, y, jointpdf)

par(mfrow=c(2,2), mar=c(2,2,2,2))
persp3D(x, y,fxy, theta=10, phi=30, expand=0.8 ,shade = 0.03, colkey = FALSE)
contour2D(fxy, x=x,y=y,NAcol = "black", colkey = FALSE)
image2D(fxy, x=x,y=y,NAcol = "black", colkey = FALSE)
image2D(fxy, x=x,y=y,NAcol = "black",contour = TRUE, lphi = 0.7,  main = "volcano")
#+end_src

The result of the above code block is:

plotExampleBivariateNormal.png

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s