
Tengo un archivo html que contiene la siguiente estructura:
<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.
Me gustaría agregar un número antes de los títulos de los capítulos como este:
<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.
Intenté insertar números de capítulo a través de CSS usando contador-reinicio, contraincremento para encabezados, pero esto solo funciona en ciertos contextos.
¿Existe algún script (python, perl, ???) que pueda buscar la clase = "sección" y luego insertar números secuencialmente antes de los títulos de los capítulos?
Aquí hay una muestra del archivo real:
<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
Respuesta1
Editar
Ahora que he visto su archivo, el problema es que no tiene finales de línea regulares. De hecho, parece que todo el archivo es una línea larga, ¿es correcto?
Mi secuencia de comandos depende de analizar su archivo línea por línea. En el formato real de su archivo, las líneas parecen estar divididas aleatoriamente, por lo que será muy difícil de analizar. Por supuesto, como se ha expresado de manera bastante elocuente aunque un poco dementeaquí, nunca debes analizar HTML con expresiones regulares.
Dicho esto, el siguiente script funciona en el archivo que has publicado.
#!/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
}
Respuesta2
Probablemente puedas usar<ol>
con<li>
?
No estoy seguro de qué quieres hacer con esas <a>
etiquetas, pero tus capítulos podrían verse así:
<ol>
<li class="chapter">Chapter title</li>
<li class="chapter">Chapter title</li>
<li class="chapter">Chapter title</li>
</ol>
Y cada nuevo conjunto de <ol>
elementos restablecerá la numeración por usted.
Respuesta3
CSS también puede ayudarlo a numerarse automáticamente:
a { counter-reset: section; }
h2:before {
counter-increment: section;
content: counter(section) ". ";
display: inline;
}