Posts Tagged ‘blogging’

h1

Extending wplatex

January 31, 2010

It’s amazing how good snow is for your productivity. Maybe I should move to Antarctica.

Anyways, I added some rudimentary support for displaying source code using the listings package. Let me show you how this was done as an example of how to add support for whatever packages and macros you would like.

So first of all, plasTeX is pretty smart about understanding the packages that you are using in a Latex document. It will parse your preamble and do the best it can to load and parse the packages you are using as well. This is great if you have, say, some custom macros put aside that you always reference. So if you put a \usepackage{mymacros} in your preamble, plasTeX will find the mymacros.sty file the same way Latex would, parse the file, and then understand how to expand the macros contained therein. This is the default behavior.

For more complicated packages, like listings, for example, there’s a better way. Instead of letting plasTeX load and parse the source, we can choose to tell plasTeX about which commands we care about directly in python. This saves a lot of time and adds a lot of flexibility. (By the way, this has already been done for a bunch of packages that you might want to use, like amsmath, amssymb, graphicx and more.)

Now, when plasTeX finds a \usepackage command in your preamble, before it tries to load the latex source, it will look for a python module on the path which shares the same name as the package specified. This means if I put a file called listings.py somewhere in my python path, when plasTeX sees the \usepackage{listings} at the top of my file, this module will get loaded instead of the Latex source. For convenience, I have the wplpost program add the wplatex package directory to the python path, so you can just add any package code to that directory, reinstall, and everything should work automatically.

Great. So what should go in the python package definition. This is easy: you just define a class for any command that the package let’s you use. In our case, the listings package defines a new environment called lstlisting. When you put source code in this environment, the package will render it in your Latex output. Thus I have the following class definition in the file wplatex/listings.py

  from plasTeX.Base.LaTeX.Verbatim import verbatim

  # This should be a verbatim environment
  class lstlisting(verbatim):
    pass

In most cases, your new class will derive from either the plasTeX.Base.Command or plasTeX.Base.Environment classes, depending on whether you want to tell plasTeX about a new command or a new environment. In the current case, we want to use a special kind of environment, a verbatim environment, since we don’t want to do any rendering or parsing of the code inside. That’s why I’ve derived this class from verbatim.

In the future, this class could override some of its base class’ methods, particularly invoke(), and do some preprocessing like figure out what language to use and add any formatting information. It’s nice to notice, though, that just telling plasTeX about the command by creating a dummy class is enough for it to now recognize it in a Latex source file.

We’re almost done. The only thing left to do is to tell renderer what we want to see when plasTeX encounters one of these environments. The code for the renderer is contained in the file wplate/renderer.py. I added the following method to the WPRenderer class:

    def do_lstlisting(self, node):
        '''Encode Source code blocks
        
        Arguments:
        - `node`:
        '''
        return u'<sourcecode language=Python>%s</sourcecode>' % unicode(node)  

Ridiculously simple, no? All this method does is return a unicode string which we would like to see in the output. The command

  unicode(node)

renders all the children of our lstlisting node in plasTeX’s internal tree representation of our document. Since we’ve already told plasTeX that the lstlisting environment should be verbatim, this will just return all the text found inside. We then plug that in between WordPress’ tag for source code and return the result.

And that’s it! After reinstalling the wplatex package to udpate the code, the wplpost program now understands how to post source code. Not bad for two or three lines of code. In fact, in continuing with the incredibly self-referential tone of the past couple posts, I’ve used this functionality in writing the post you’re reading. Here’s a screenshot.

You can take a look at the source code to see how I’ve implemented other commands and environments. There’s still a lot to do, but I hope this post makes it clear how easy it is to add or customize the output that you send to WordPress. If you do get around to adding support for more things, I hope you’ll drop me an e-mail. And now, I’m making myself some spaghetti.

h1

Overview of wplatex

January 30, 2010

Man, we are getting a lot of snow today. I guess that means I’ll stay inside and describe how to use the wplatex package that I put together.

Okay, so like I said in the last post, you should be able to install the package using the standard way for Python packages. Unzip it into whatever directory you like, then do a

 
    sudo python setup.py install
   

Next, copy the wpblogentry.cls document class definition file into your Latex path. This is usually something like /usr/share/texmf/.... Basically do whatever you have to on your system to make latex find the definition.

At this point, you should be able to make a Latex blog entry by doing something like this:

 
    \documentclass{wpblogentry}

    \title{A Latex Blog Entry}
    \tags{tag1, tag2}
    \category{latex}

    \begin{document}

      Your blog entry

    \end{document}
   

The package will install a script called wplpost on your system, so when you have got your blog entry looking like you want it, you’ll want to run this script to render it and upload it to wordpress. The script will prompt you for a username and password, but you can also specify them on the command line using the -u and -p options respectively. You also need to specify where to upload the post to. You can do this two different ways, using either the -b or -x options. If you do -b blogname, then the script will guess that it can access your blog’s xmlrpc.php file at

 

http://blogname.wordpress.com/xmlrpc.php

   

If this is not the correct location, you can specify the url directly using the -x option. As an example, I would upload this blog entry by doing

 
    wplpost -u ericfinster -b curiousreasoning wplatex.tex
   

Note that this form will automatically publish the post as well. If you would like to upload the post as a draft only, you can specify -d and you should then be able to go in and preview it on WordPress before you actually publish the result.

I think this much should be functional right now. I’m going to try to get the listings package working so that I can write source code in my entries. When I’m done, I’ll write a post about how to extend wplatex’s renderer so that you can add customizations if you like. Cheers everyone.

h1

Update: wplatex

January 29, 2010

Okay, I built a python package, called wplatex, which contains the code for everything I described below. This is still in a very early state, but if anyone wants to play with it, feel free. You can download the package here. Once you’ve unzipped it, install it using the setup.py script as usual. (i.e. (sudo) python setup.py install)

There’s a custom Latex class definition file called wpblogentry.cls which you should put in your Latex path. Then use this for the documentclass in each of your entries. I’ll maybe say more about what else you can do tomorrow, but I think I’ll quit for tonight.

h1

More on Blogging in Latex

January 28, 2010

Okay, so the irony of making a couple of posts about how great my blogging setup was, and then not writing anything else, is not lost on me. Before you completely write me off, I beg you, hear me out.

See, I was thinking about starting a series of posts on just some basic set theory. I figure, if this is going to be a place where I can describe some of my work, I better start at the beginning. Besides, even though set theory is pretty simple and cool and easy to explain, you can get into some interesting and deep ideas pretty quickly. I love the theory of ordinal numbers, for example, so I thought I would write about normal forms and the like

But the problem is, set theory is much easier to explain with pictures, so I needed to be able to draw pictures and post them along with the mathematics. And it occured to me, that in the spirit of automating my Latex posts, I ought to be able to extract any images I include and have them uploaded and linked in automatically. Unfortunately, the script latex2wp.py which I described before doesn’t really do anything like this.

And there were a couple other things I wanted: the script should automatically know the title of my post. (I did hack up a quick kludge to do this, but it wasn’t pretty.) And I should be able to make a new Latex macro to record my tags and categories.

In other words, I am a perfectionist. Even though I had things pretty well automated already, it was driving me crazy that there were a couple of loose ends, and I was compelled by my twisted brain to look into them.

My first conclusion was that the latex2wp.py script was nice, but that it was going to be difficult to make it really do all the things I wanted. The script was clearly meant to be called from the command line, to perform its basic transformations and quit. There was no support for parsing and understanding what your Latex file said, storing the information, and customizing a post based on the content. And when I thought about it, this is what I was really after.

Enter plasTeX. This was what I was really envisioning in my head all along. It parses your Latex document into a something like a custom DOM so that you can perform arbitrary transformations and rendering operations. Moreover, it’s extensible, object-oriented, Unicode aware, and written in Python. Combined with wordpresslib, which I mentioned before, this seemed like the best way of getting real integration between the Latex I was writing on my computer, and the posts that I sent out to the web.

So I’ve been working on this idea for the past couple of days, and finally came up with this. It basically does everything I wanted, and in the future could do even more, since it’s extremely easy to add new features. Automatically making use of WordPress’ source code feature comes to mind, along with more flexible rendering of arrays, aligns, and diagrams. You get the idea.

Unfortunately, there’s a bit more to getting this to work now than just running the script linked to above (you need a couple auxillary files and some paths to be set correctly) but I’ll try to tar up what you need and post some instructions in the next day or so.

I guess that’s all for now. I’m hungry.

Follow

Get every new post delivered to your Inbox.