com php-langspec: Refactor toc.php for reusability: tools/toc.php tools/util.php

[email protected] (Nikita Popov) Fri, 11 Nov 2016 21:00:47 +0000
Newsgroups php.standards.cvs
Message-ID <[email protected]>
Commit:    e07c954abd8f04847eed6884defdf8bd2219e8cc=0AAuthor:    Nikita Pop=
ov <[email protected]>         Fri, 11 Nov 2016 22:00:47 +0100=0AParents:   598=
034c9f9e6d9f36689e5f8fcd17d5abc3b9e2b=0ABranches:  master=0A=0ALink:       =
http://git.php.net/?p=3Dphp-langspec.git;a=3Dcommitdiff;h=3De07c954abd8f048=
47eed6884defdf8bd2219e8cc=0A=0ALog:=0ARefactor toc.php for reusability=0A=
=0AChanged paths:=0A  M  tools/toc.php=0A  A  tools/util.php=0A=0A=0ADiff:=
=0Adiff --git a/tools/toc.php b/tools/toc.php=0Aindex 17b51ce..276769b 1006=
44=0A--- a/tools/toc.php=0A+++ b/tools/toc.php=0A@@ -1,5 +1,7 @@=0A <?php e=
rror_reporting(E_ALL);=0A =0A+require __DIR__ . '/util.php';=0A+=0A $dir =
=3D __DIR__ . '/../spec/';=0A $tocFile =3D $dir . '00-specification-for-php=
.md';=0A $prefix =3D <<<EOS=0A@@ -17,40 +19,14 @@ is distributed without an=
y warranty.=0A **Table of Contents**=0A EOS;=0A =0A-$files =3D scandir($dir=
);=0A $output =3D "";=0A-=0A-foreach ($files as $file) {=0A-    if(pathinfo=
($file, PATHINFO_EXTENSION) !=3D 'md') {=0A-        continue;=0A-    }=0A- =
   if ($file =3D=3D '00-specification-for-php.md' || $file =3D=3D 'php-spec=
-draft.md') {=0A-        continue;=0A-    }=0A-=0A-    $anchors =3D [];=0A-=
=0A-    $lines =3D file($dir . $file);=0A-    foreach ($lines as $line) {=
=0A-        if (!preg_match('/^(#+)\s*(.+)/', $line, $matches)) {=0A-      =
      continue;=0A-        }=0A-=0A-        list(, $hashes, $title) =3D $ma=
tches;=0A-        $level =3D strlen($hashes) - 1;=0A-        $indent =3D st=
r_repeat('  ', $level);=0A-=0A-        $anchor =3D strtr(strtolower($title)=
, ' ', '-');=0A-        $anchor =3D preg_replace('/[^\w-]/', '', $anchor);=
=0A-=0A-        if (isset($anchors[$anchor])) {=0A-            $anchors[$an=
chor]++;=0A-            $anchor .=3D '-' . $anchors[$anchor];=0A-        } =
else {=0A-            $anchors[$anchor] =3D 0;=0A-        }=0A-=0A-        =
$output .=3D "$indent- [$title]($file#$anchor)\n";=0A+foreach (spec_files()=
 as $fileName =3D> $path) {=0A+    $contents =3D file_get_contents($path);=
=0A+    foreach (heading_info($contents) as $info) {=0A+        $title =3D =
$info['title'];=0A+        $anchor =3D $info['anchor'];=0A+        $indent =
=3D str_repeat('  ', $info['level']);=0A+        $output .=3D "$indent- [$t=
itle]($fileName#$anchor)\n";=0A     }=0A }=0A =0Adiff --git a/tools/util.ph=
p b/tools/util.php=0Anew file mode 100644=0Aindex 0000000..e6d21e5=0A--- /d=
ev/null=0A+++ b/tools/util.php=0A@@ -0,0 +1,46 @@=0A+<?php=0A+=0A+/* Iterat=
or of spec files, using $fileName =3D> $path. */=0A+function spec_files() {=
=0A+    $dir =3D __DIR__ . '/../spec/';=0A+    $files =3D scandir($dir);=0A=
+=0A+    foreach ($files as $file) {=0A+        if (pathinfo($file, PATHINF=
O_EXTENSION) !=3D 'md') {=0A+            continue;=0A+        }=0A+        =
if ($file =3D=3D '00-specification-for-php.md' || $file =3D=3D 'php-spec-dr=
aft.md') {=0A+            continue;=0A+        }=0A+=0A+        yield $file=
 =3D> $dir . $file;=0A+    }=0A+}=0A+=0A+function heading_info($code) {=0A+=
    $anchors =3D [];=0A+    $lines =3D explode("\n", $code);=0A+    foreach=
 ($lines as $line) {=0A+        if (!preg_match('/^(#+)\s*(.+)/', $line, $m=
atches)) {=0A+            continue;=0A+        }=0A+=0A+        list(, $has=
hes, $title) =3D $matches;=0A+=0A+        $anchor =3D strtr(strtolower($tit=
le), ' ', '-');=0A+        $anchor =3D preg_replace('/[^\w-]/', '', $anchor=
);=0A+=0A+        if (isset($anchors[$anchor])) {=0A+            $anchors[$=
anchor]++;=0A+            $anchor .=3D '-' . $anchors[$anchor];=0A+        =
} else {=0A+            $anchors[$anchor] =3D 0;=0A+        }=0A+=0A+      =
  yield [=0A+            'title' =3D> $title,=0A+            'anchor' =3D> =
$anchor,=0A+            'level' =3D> strlen($hashes) - 1=0A+        ];=0A+ =
   }=0A+}=0A