A simple table of contents script

My website is a pure content based website, where information is present on different pages. Though I use wordpress, I have modified it to be a static website and not a blog. In my pages, I use only h2 tags for headings (h1 for the main title). I have decided to add a “Table of contents” at the top of every page so that users will find it easy to understand what information is present on the page.
The problem is that I don’t want to manually add the table of contents on each page because I know PHP can easily do it for me. But, I don’t know PHP that well so I am dependent on other people’s work. I recently installed a plugin called “table of contents plus” to my wordpress. It was nice and had a huge set of options and it did the job but the problem is that I don’t want that much complexity. I don’t want to run scripts that I am never going to use. So I have decided to make one myself. I hope you people will help me make my own script.

What I need the script to do is:

  1. Read all the h2 tags on the page and store them as array
  2. Just above each h2 tag, add an anchor to which users can come when they click the respective link in the table of conents at the top of the page.
  3. Create a div with a specific class (so that it can be styled) and add the text from the h2 tags as separali lines and convert them to links that point to respective locations.

Thats’ it. I don’t need any fancy javascript, or other PHP additions. I want it as simple as possible.