2008-05-16

webindent.c

Sometimes I want to post code to a website, but they have utterly broken filters (I'm looking at you Yahoo! Answers). So that when I paste nicely indented code it goes from this:

def double(x):
    return x+x

to this:

def double(x):
return x+x

It is annoying in C or Lisp, but it is wrong in python where the syntax relies on proper indentation. When websites do this it is broken.

So I wrote a little program to rectify the situation. It processes a source file so that it looks the same or similar on the web as in your text editor.

usage: webindent < source.c > page.html

#include <stdio.h>
#define true 1
#define false 0

/* main(): get characters from stdin, replace
 * leading spaces with '&nbsp;'
 * and replace leading tabs with 4 '&nbsp;'s
 * This code is donated to the public domain */
int main()
{
    int start = true; /* we're at the start of a line */
    char *space = "&nbsp;";
    char *fourspaces = "&nbsp;&nbsp;&nbsp;&nbsp;";
    char c; /* the current char */

    while ((c = getchar()) != EOF) {
        if (c == ' ') {
            if (start)
                printf(space);
            else
                putchar(c);
        } else if (c == '\t') {
            if (start)
                printf(fourspaces);
            else
                putchar(c);
        } else if (c == '<') {
            printf("&lt;");
        } else if (c == '>') {
            printf("&gt;");
        } else if (c == '\n') {
            printf("<br />");
            putchar(c);
            start = true;
        } else if (c == '&') {
            printf("&amp;");
        } else {
            if (start)
                start = false;
            putchar(c);
        }
    }
    return 0;
}

It does a bit more than is stated in that comment, because it has to remove '&', '<' and '>' for it to be a well-formed HTML fragment. Note that I used this program to process itself.

2008-05-12

Make a book

I had been wondering about how to create a printed book from the novel that I had written a couple of years ago and I just now got it in my head to put everything together. I'll list the steps that I took from a big ASCII file to the printed page.
  1. Convert ASCII to LaTeX: (beyond the scope of this article)
  2. Produce a postscript file from the LaTeX source:
    • latex novel.tex
    • dvips novel.dvi
  3. Alternatively, if you're starting from an existing PDF (which is what I had to do, I had lost the LaTeX source) you can run pdftops to get to this step.
  4. Run psbook -q -s 8 novel.ps > novel_bookorder.ps — this rearranges the postscript file so that pages are in the following order 8, 1, 2, 7, 6, 3, 4, and 5. The -s 8 means use a signature of size 8. This will be useful during the next step.
  5. Run psnup -q -n 2 -p letter novel_bookorder.ps > novel_book.ps this puts two pages onto each 8.5x11 output page. Print the pages back to back. We will now have 4 pages on each sheet, two on the front and two on the back. The page will be in landscape orientation, but the printing will be side-by-side portrait. Coupled with the previous command we can now fold over two pages to form an 8-page signature
  6. Combine the signatures (sewing, stapling, choose your method)
  7. clamp the sewn signatures and run hot glue along the spine of the book, this would be the time to add a cover.
This should work reasonably well. Though I have not had much luck with staples, they seem to contribute too much thickness to the spine and make clamping awkward. Sewing would likely work much better but I don't have any good suggestions for technique.

twopoint718

About Me

My photo
A sciency type, but trying to branch out into other areas. After several years out in the science jungle, I'm headed back to school to see what I can make of the other side of the brain.