Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have an html file which contains the following structure:

<h1 class="section">First title</h1>
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.
<h1 class="section">Second title</h1>
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">Chapter title</h2>
     Chapter text here.

I would like to add a number before the chapter titles like this:

<h1 class="section">First title</h1>
  <div><h2 class="chapter">1. Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">2. Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">3. Chapter title</h2>
     Chapter text here.
<h1 class="section">Second title</h1>
  <div><h2 class="chapter">1. Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">2. Chapter title</h2>
     Chapter text here.
  <div><h2 class="chapter">3. Chapter title</h2>
     Chapter text here.

I tried inserting chapter numbers through CSS using counter-reset, counter-increment for headers but this works only in certain contexts.

Is there a script (python, perl, ???) that could search for the class="section", then sequentially insert numbers before the chapter titles?

Here's a sample of the actual file:

<body><div class='root'><h1 class="section">Génesis</h1><div><h2
class="chapter">Dios ordena el universo</h2><div>01 En el principio,
cuando Dios creó los cielos y la tierra, </div><div>02 todo era
confusión y no había nada en la tierra. Las tinieblas cubrían los
abismos mientras el espíritu de Dios aleteaba sobre la superficie de
las ag [many lines here] </div><div><h2 class="chapter">Descanso del
séptimo día</h2><div>01 Así estuvieron [many lines here] <div
class='root'><h1 class="section">Éxodo</h1><div><h2 class="chapter">Los
hebreos se multiplican en Egipto</h2><div>01 Estos son los nombres de
los hijos de Israel que llegaron con Jacob a Egipto, cada uno con su
familia:</div><div>02 Rubén, Simeón, Leví, Judá,</div><div>03 Isacar,
[many lines here] etc, etc
share|improve this question

3 Answers 3

up vote 1 down vote accepted

Edit

Now that I have seen your file, the problem is that you don't have regular line endings. In fact, it looks like your entire file is one long line, is that correct?

My script depends on parsing your file line by line. In the actual format of your file, the lines seem to be broken randomly, so it will be very hard to parse. Of course, as has been expressed rather eloquently if a little dementedly here, you should never parse HTML with regular expressions.

That said, the script below works on the file you have posted.


#!/usr/bin/perl 

my $file=<>; ## Load the file into memory
my $a=1;     ## Set up a counter

## Split the file on each occurence of
## 'class="chapter"' and save into the array @b
my @b=split(/class=.chapter.>/,$file);

## Print the beginning of the file
## and remove it from the array.
print shift(@b);

## Now, go through the array, adding the counter ($a)
## to each chapter heading.
foreach (@b) {
    ## Print 'class="chapter"', the counter and 
    ## the rest of the text until the next chapter heading
    print "class=\"chapter\">$a. $_"; 

    $a++;   ## Increment the counter
    $a=1 if /class="section"/; ## reset the counter
}
share|improve this answer
    
I tried it but it didn't work. I don't know perl but if I understand a little bit this script looks for "Heading" in title. The "Heading 1" and "Heading 2" above are examples. The common bit to all these <a> tags is the class="section" and not the title. I modified my example above of this might help. –  To Do Feb 7 '13 at 22:03
    
@ToDo, I modified the script, should work now –  terdon Feb 7 '13 at 23:46
    
Still not working. No error messages. Maybe if you could explain what the different lines mean, I could try to debug, if it is not much trouble. –  To Do Feb 8 '13 at 9:59
    
@ToDo, comments added. The script as posted works on the example in your question. Is it an excerpt of your actual file? If not please post the file, any small change in format will break the script, I could make it more general if I could see the actual file. –  terdon Feb 8 '13 at 11:40
    
I added a sample of the actual file. I modified the file structure as well replacing <a> with <h1> –  To Do Feb 8 '13 at 21:17

You can probably use <ol> with <li>?

I'm not sure what you want to do with those <a> tags, but your chapters could look something like this:

<ol>
  <li class="chapter">Chapter title</li>
  <li class="chapter">Chapter title</li>
  <li class="chapter">Chapter title</li>
</ol>

And every new set of <ol> elements will reset the numbering for you.

share|improve this answer

CSS can also help it number automatically:

a { counter-reset: section; }
h2:before {
    counter-increment: section;
    content: counter(section) ". ";
    display: inline;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.