Beacon Hill, Inc. Technology Solutions

Home > Knowledge Base > Emacs > Converting TOC to Table

Converting a book table of contents to an org-mode table

Lately, I've been using org-mode to organize my tasks. One thing I've also been doing is tracking my process in reading a number of books in PDF format. I have been copying the table of contents from the PDF into my org-mode todo file and then manually chopping it up to convert it into a table. Once this is done I've been happy with the results. What I usually end up with looks like the following example.


|-----------+------+------------|
| Chapter   | Page |  Completed |
|-----------+------+------------|
| Chapter 1 |    1 | 11-12-2009 |
| Chapter 2 |   15 |            |
| Chapter 3 |      |            |
| etc...    |      |            |
|-----------+------+------------|

This simple table makes it easy to mark progress in the completed column.

Having done this a couple times I now figured it was best to create some sort of Emacs macro to make the task easier.

Initial Table of Contents

I started with the following by cutting and pasting from a PDF file.


 Introduction .................................................................1
 Part I: Laying the Groundwork .......................................7
 Chapter 1: Tackling the Chess Basics ..........................................................................9
 Chapter 2: Greeting the Pieces and Their Powers ........................................................23
 Chapter 3: Getting to Know the Elements of Chess .....................................................41
 Chapter 4: Looking Out for the King: Check, Stalemate, and Checkmate .................63
 Part II: Gaining Chess Know-How ................................79
 Chapter 5: Tactics and Combinations in Hand-to-Hand Combat ................................81
 Chapter 6: Sacrifices: When It's Better to Give than to Receive ...............................109
 Chapter 7: Mastering Mating Patterns .......................................................................121
 Chapter 8: Building Pattern Recognition .....................................................................137
 Chapter 9: Recognizing Pawn Formations ...................................................................155
 Chapter 10: Making Special Moves ...........................................................................169
 Part III: Game Time: Putting Your Chess Foot Forward ...177
 Chapter 11: Selecting Your Strategy: The Principles of Play .....................................179
 Chapter 12: Coming on Strong in the Opening ...........................................................193
 Chapter 13: Making Headway during the Middlegame ..............................................215
 Chapter 14: Exiting with Style in the Endgame ...........................................................225
 Part IV: Getting Into Advanced Action .......................243
 Chapter 15: Competition Play and Necessary Etiquette ...........................................245
 Chapter 16: Hitting the Net with Computer Chess .....................................................257
 Chapter 17: Got Notation? Reading and Writing about Chess ..................................263
 Part V: The Part of Tens ............................................275
 Chapter 18: The Ten Most Famous Chess Games ......................................................277
 Chapter 19: The Ten Best Players of All Time .............................................................305
 Part VI: Appendixes ..................................................313
 Appendix A: A Glossary of Chess .............................................................................315
 Appendix B: Other Chess Resources ...........................................................................341
 Index........................................................................345

The first think I did is remove the lines that I'm not going to track like the Introduction, Parts, Appendices and Index.


 Chapter 1: Tackling the Chess Basics ..........................................................................9
 Chapter 2: Greeting the Pieces and Their Powers ........................................................23
 Chapter 3: Getting to Know the Elements of Chess .....................................................41
 Chapter 4: Looking Out for the King: Check, Stalemate, and Checkmate .................63
 Chapter 5: Tactics and Combinations in Hand-to-Hand Combat ................................81
 Chapter 6: Sacrifices: When It's Better to Give than to Receive ...............................109
 Chapter 7: Mastering Mating Patterns .......................................................................121
 Chapter 8: Building Pattern Recognition .....................................................................137
 Chapter 9: Recognizing Pawn Formations ...................................................................155
 Chapter 10: Making Special Moves ...........................................................................169
 Chapter 11: Selecting Your Strategy: The Principles of Play .....................................179
 Chapter 12: Coming on Strong in the Opening ...........................................................193
 Chapter 13: Making Headway during the Middlegame ..............................................215
 Chapter 14: Exiting with Style in the Endgame ...........................................................225
 Chapter 15: Competition Play and Necessary Etiquette ...........................................245
 Chapter 16: Hitting the Net with Computer Chess .....................................................257
 Chapter 17: Got Notation? Reading and Writing about Chess ..................................263
 Chapter 18: The Ten Most Famous Chess Games ......................................................277
 Chapter 19: The Ten Best Players of All Time .............................................................305

Regular Expressions

Looking at the text I'll need the following three steps to convert to a table. A vertical bar at the beginning, two trailing the page number and one to replace the series of periods.

Each of these steps I could do by hand. The beginning and end are the simplest.

(replace-regexp "^" "|")

takes care or the beginning and

(replace-regexp "$" " | |")

takes care of the end of line.

Series of Periods

The series of periods are a bit tricker. The . character in a regular expression matches on any character. To match on an actual period you need to escape it with a \. Also, you want to match at least one or more to grab the series. This means you need to use the + postfix operator.

This means you need a regular expression of "\.+" to match the series of dots.

Putting it in the expression gives you.

(replace-regexp "\\.+" " | ")

Notice, the extract \ to escape the \ inside a string. If you use the interactive form of replace-regexp you don't need that.

Putting it together

To put these three in a macro you need some other functions. First since you'll be calling this macro yourself you'll need to call interactive at the beginning.

Since, the text we want to work on is one section of a document it makes sense to require that the caller of the macro mark the region to convert before calling the macro.

This means we'll pass the 'r' for region as an argument to interative.

(interactive "r")

With this our macro can have parameters start and end which we can use in our replace-regexp calls.

At first, I thought we'ed be using save-excursion and jumping back and forth to the beginning of the text block for each replace-regexp call. At the time I was thinking along the lines of automating what I had been doing manually. This isn't the case if you think about regions and let the replace-regexp call take the start and end parameters.

Given these thoughts the following should do the trick.

convert-toc-to-org-table macro


(defun convert-toc-to-org-table (start end)
  "Convert a text table of contents from a PDF to a org-mode table.
Do three things:
1. Replace the beginning of each line with a |
2. Replace the end of the line with two |'s
3. Replace the series of ... periods with a |
"
  (interactive "r")
  (progn
    (replace-regexp "\\.+" " | " nil start end)
    (replace-regexp "^" "| " nil start end)
    (replace-regexp "$" " | |" nil start end)))

Trial

1. Open a new buffer and put it into org-mode

2. Cust and paste Take the following, select it then run convert-toc-to-org-table on it


 Chapter 1: Tackling the Chess Basics ..........................................................................9
 Chapter 2: Greeting the Pieces and Their Powers ........................................................23
 Chapter 3: Getting to Know the Elements of Chess .....................................................41
 Chapter 4: Looking Out for the King: Check, Stalemate, and Checkmate .................63
 Chapter 5: Tactics and Combinations in Hand-to-Hand Combat ................................81
 Chapter 6: Sacrifices: When It's Better to Give than to Receive ...............................109
 Chapter 7: Mastering Mating Patterns .......................................................................121
 Chapter 8: Building Pattern Recognition .....................................................................137
 Chapter 9: Recognizing Pawn Formations ...................................................................155
 Chapter 10: Making Special Moves ...........................................................................169
 Chapter 11: Selecting Your Strategy: The Principles of Play .....................................179
 Chapter 12: Coming on Strong in the Opening ...........................................................193
 Chapter 13: Making Headway during the Middlegame ..............................................215
 Chapter 14: Exiting with Style in the Endgame ...........................................................225
 Chapter 15: Competition Play and Necessary Etiquette ...........................................245
 Chapter 16: Hitting the Net with Computer Chess .....................................................257
 Chapter 17: Got Notation? Reading and Writing about Chess ..................................263
 Chapter 18: The Ten Most Famous Chess Games ......................................................277
 Chapter 19: The Ten Best Players of All Time .............................................................305

3. Go into any cell and select tab. Org-mode will fix things up.

4. Put the heading on

Above the fist line. Enter the following


| Chauter | Page | Completed |

Then above and below enter


| then a Tab


|----------------------------------------------------------------------+------+-----------|
| Chapter                                                              | Page | Completed |
|----------------------------------------------------------------------+------+-----------|
| Chapter 1: Tackling the Chess Basics                                 |    9 |           |
| Chapter 2: Greeting the Pieces and Their Powers                      |   23 |           |
| Chapter 3: Getting to Know the Elements of Chess                     |   41 |           |
| Chapter 4: Looking Out for the King: Check, Stalemate, and Checkmate |   63 |           |
| Chapter 5: Tactics and Combinations in Hand-to-Hand Combat           |   81 |           |
| Chapter 6: Sacrifices: When It's Better to Give than to Receive      |  109 |           |
| Chapter 7: Mastering Mating Patterns                                 |  121 |           |
| Chapter 8: Building Pattern Recognition                              |  137 |           |
| Chapter 9: Recognizing Pawn Formations                               |  155 |           |
| Chapter 10: Making Special Moves                                     |  169 |           |
| Chapter 11: Selecting Your Strategy: The Principles of Play          |  179 |           |
| Chapter 12: Coming on Strong in the Opening                          |  193 |           |
| Chapter 13: Making Headway during the Middlegame                     |  215 |           |
| Chapter 14: Exiting with Style in the Endgame                        |  225 |           |
| Chapter 15: Competition Play and Necessary Etiquette                 |  245 |           |
| Chapter 16: Hitting the Net with Computer Chess                      |  257 |           |
| Chapter 17: Got Notation? Reading and Writing about Chess            |  263 |           |
| Chapter 18: The Ten Most Famous Chess Games                          |  277 |           |
| Chapter 19: The Ten Best Players of All Time                         |  305 |           |
|----------------------------------------------------------------------+------+-----------|