Skip to content

Commit de84e9a

Browse files
André L F S Baccijordikroon
authored andcommitted
XML Entities
1 parent c734dcf commit de84e9a

3 files changed

Lines changed: 570 additions & 0 deletions

File tree

scripts/dtdent-conv.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2023 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net, so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
| Description: Convert old style .ent into new style .ent XML bundle. |
16+
+----------------------------------------------------------------------+
17+
18+
See `entities.php` source for detailed rationale.
19+
20+
Use this for converting bundled entities files that use <!ENTITY> into
21+
XML version used by `entities.php`.
22+
23+
After converting, add the generated entities in an global.ent or
24+
manual.ent file, and delete the previous one.
25+
26+
After all old style .ent files are split or converted, this script can
27+
be removed. */
28+
29+
ini_set( 'display_errors' , 1 );
30+
ini_set( 'display_startup_errors' , 1 );
31+
error_reporting( E_ALL );
32+
33+
if ( count( $argv ) < 2 )
34+
die(" Syntax: php $argv[0] infile\n" );
35+
36+
$infile = $argv[1];
37+
38+
$content = file_get_contents( $infile );
39+
40+
$pos1 = 0;
41+
while ( true )
42+
{
43+
$pos1 = strpos( $content , "<!ENTITY", $pos1 );
44+
if ( $pos1 === false ) break;
45+
46+
$posS = strpos( $content , "'" , $pos1 );
47+
$posD = strpos( $content , '"' , $pos1 );
48+
49+
if ( $posS < $posD )
50+
$q = "'";
51+
else
52+
$q = '"';
53+
54+
$pos1 += 8;
55+
$pos2 = min( $posS , $posD ) + 1;
56+
$pos3 = strpos( $content , $q , $pos2 );
57+
58+
$name = substr( $content , $pos1 , $pos2 - $pos1 - 1 );
59+
$text = substr( $content , $pos2 , $pos3 - $pos2 );
60+
61+
// weird &ugly; ass, namespace corret, DOMDocumentFragment -> DOMNodeList (ampunstand intended)
62+
63+
$name = trim( $name );
64+
$text = str_replace( "&" , "&amp;" , $text );
65+
66+
$frag = "<entities xmlns='http://docbook.org/ns/docbook' xmlns:xlink='http://www.w3.org/1999/xlink'>\n";
67+
$frag .= " <entity name='$name'>$text</entity>\n";
68+
$frag .= '</entities>';
69+
70+
$dom = new DOMDocument( '1.0' , 'utf8' );
71+
$dom->recover = true;
72+
$dom->resolveExternals = false;
73+
libxml_use_internal_errors( true );
74+
75+
$dom->loadXML( $frag , LIBXML_NSCLEAN );
76+
$dom->normalizeDocument();
77+
78+
libxml_clear_errors();
79+
80+
$text = $dom->saveXML( $dom->getElementsByTagName( "entity" )[0] );
81+
$text = str_replace( "&amp;" , "&" , $text );
82+
83+
echo "$text\n";
84+
}

scripts/dtdent-split.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php /*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) 1997-2023 The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| https://www.php.net/license/3_01.txt. |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| license@php.net, so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: André L F S Bacci <ae php.net> |
14+
+----------------------------------------------------------------------+
15+
| Description: Split old style .ent file into individual files. |
16+
+----------------------------------------------------------------------+
17+
18+
See `entities.php` source for detailed rationale.
19+
20+
Use this for spliting `language-snippets-ent` or other "big" entities
21+
files into individual .xml files.
22+
23+
After spliting, add the new directory entities/ with they contents,
24+
and remove `language-snippets-ent`, in one go.
25+
26+
After all old style .ent files are split or converted, this script can
27+
be removed. */
28+
29+
ini_set( 'display_errors' , 1 );
30+
ini_set( 'display_startup_errors' , 1 );
31+
error_reporting( E_ALL );
32+
33+
if ( count( $argv ) < 4 )
34+
die(" Syntax: php $argv[0] infile outdir [hash user]\n" );
35+
36+
$infile = $argv[1];
37+
$outdir = $argv[2];
38+
$hash = $argv[3] ?? "";
39+
$user = $argv[4] ?? "_";
40+
41+
$content = file_get_contents( $infile );
42+
$entities = [];
43+
44+
// Parse
45+
46+
$pos1 = 0;
47+
while ( true )
48+
{
49+
$pos1 = strpos( $content , "<!ENTITY", $pos1 );
50+
if ( $pos1 === false ) break;
51+
52+
$posS = strpos( $content , "'" , $pos1 );
53+
$posD = strpos( $content , '"' , $pos1 );
54+
55+
if ( $posS < $posD )
56+
$q = "'";
57+
else
58+
$q = '"';
59+
60+
$pos1 += 8;
61+
$pos2 = min( $posS , $posD ) + 1;
62+
$pos3 = strpos( $content , $q , $pos2 );
63+
64+
$name = substr( $content , $pos1 , $pos2 - $pos1 - 1 );
65+
$text = substr( $content , $pos2 , $pos3 - $pos2 );
66+
67+
$name = trim( $name );
68+
69+
$entities[$name] = $text;
70+
}
71+
72+
// Check
73+
74+
foreach( $entities as $name => $text )
75+
{
76+
$file = "$outdir/$name.xml";
77+
if ( file_exists( $file ) )
78+
exit( "Name colision: $file\n" );
79+
}
80+
81+
// Write
82+
83+
foreach( $entities as $name => $text )
84+
{
85+
$file = "$outdir/$name.xml";
86+
87+
$header = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
88+
89+
if ( $hash != "" )
90+
$header .= "<!-- EN-Revision: $hash Maintainer: $user Status: ready --><!-- CREDITS: $user -->\n";
91+
92+
file_put_contents( $file , $header . $text );
93+
}
94+
95+
$total = count( $entities );
96+
print "Generated $total files.\n";

0 commit comments

Comments
 (0)