html 標題編號

html 標題編號

我有一個 html 文件,其中包含以下結構:

<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.

我想在章節標題前面加上一個數字,如下所示:

<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.

我嘗試使用標題的計數器重設、計數器增量透過 CSS 插入章節編號,但這僅在某些情況下有效。

是否有一個腳本(python,perl,???)可以搜尋class =“section”,然後在章節標題之前按順序插入數字?

這是實際文件的範例:

<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

答案1

編輯

現在我已經看到了您的文件,問題是您沒有常規的行結尾。事實上,看起來你的整份文件都是一長行,對嗎?

我的腳本取決於逐行解析您的文件。在文件的實際格式中,行似乎是隨機斷開的,因此很難解析。當然,正如已經相當雄辯地表達的那樣,儘管有點瘋狂這裡,你不應該用正規表示式解析 HTML。

也就是說,下面的腳本適用於您發布的文件。


#!/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
}

答案2

你也許可以使用<ol><li>

我不確定你想用這些<a>標籤做什麼,但你的章節可能看起來像這樣:

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

每組新<ol>元素都會為您重設編號。

答案3

CSS 也可以幫助它自動編號:

a { counter-reset: section; }
h2:before {
    counter-increment: section;
    content: counter(section) ". ";
    display: inline;
}

相關內容