Debian Codegraph @

data/generators/generate-php.pl

raw
1 #!/usr/bin/perl
2
3 # This perl script read stdin and write on stdout. It shall be an XML language file.
4 #
5 # * If the name of the language is 'HTML', then it creates the language 'PHP (HTML)'
6 # which shall be used for PHP hl.
7 #
8 # * If the name of the language is something else (say '*'), it creates the language '*/PHP'.
9 # This new language is the same as the old one, but is able to detect PHP everywhere.
10 #
11 # This script will correctly set extensions & mimetype, and will replace
12 # <IncludeRules context="##*"> by <IncludeRules context="##*/PHP">
13 #
14 # Generated languages need a language named 'PHP/PHP', which shall take care of PHP hl itself
15 # and which will be called every time something like <?php is encountred.
16 #
17 # SPDX-FileCopyrightText: Jan Villat <[email protected]>
18 # License: LGPL
19
20 my $file = "";
21
22 open(my $input, '<:encoding(UTF-8)', $ARGV[0])
23 or die "Could not open file '$ARGV[0]': $!";
24
25 open(my $output, '>:encoding(UTF-8)', $ARGV[1])
26 or die "Could not open file '$ARGV[1]': $!";
27
28 while (<$input>)
29 {
30 $file .= $_;
31 }
32
33 $warning = "\n\n<!-- ***** THIS FILE WAS GENERATED BY A SCRIPT - DO NOT EDIT ***** -->\n";
34
35 $file =~ s/(?=<language)/$warning\n\n\n/;
36
37 if ($file =~ /<language[^>]+name="HTML"/)
38 {
39 $root = 1;
40 }
41
42 if ($root == 1)
43 {
44 $file =~ s/<language([^>]+)name="[^"]*"/<language$1name="PHP (HTML)"/s;
45 $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Scripts"/s;
46 $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.php;*.php3;*.wml;*.phtml;*.phtm;*.inc;*.ctp"/s;
47 $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-php4-src;text\/x-php3-src;text\/vnd.wap.wml;application\/x-php"/s;
48 $file =~ s/<language([^>]+)*/<language$1 indenter="cstyle"/s;
49 }
50 else
51 {
52 if ($file =~ /<language[^>]+hidden="[^"]*"/) {
53 $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/PHP"/s;
54 $file =~ s/<language([^>]+)hidden="[^"]*"/<language$1hidden="true"/s;
55 }
56 else
57 {
58 $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/PHP" hidden="true"/s;
59 }
60 $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Other"/s;
61 $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions=""/s;
62 $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype=""/s;
63 }
64
65 if ($root == 1 || $ARGV[0] =~ /mustache.xml$/)
66 {
67 $file =~ s/<(?:RegExpr (attribute="Processing Instruction" context="PI"|context="PI" attribute="Processing Instruction")|itemData name="Processing Instruction")[^\/]+\/>|<context name="PI".*?<\/context>//gs;
68 }
69
70 $findphp = "<context name=\"FindPHP\" attribute=\"Normal Text\" lineEndContext=\"#stay\">\n<Detect2Chars context=\"##PHP/PHP\" char=\"&lt;\" char1=\"?\" lookAhead=\"true\" />\n</context>\n";
71
72 $file =~ s/<IncludeRules\s([^>]*)context="([^"#]*)##(?!Alerts|Comments|Doxygen|Modelines)([^"]+)"/<IncludeRules $1context="$2##$3\/PHP"/g;
73 $file =~ s/(<context\s[^>]*[^>\/]>)/$1\n<IncludeRules context="FindPHP" \/>/g;
74 $file =~ s/(<context\s[^>]*[^>\/])\s*\/>/$1>\n<IncludeRules context="FindPHP" \/>\n<\/context>/g;
75 $file =~ s/(?=<\/contexts\s*>)/$findphp/;
76
77 print $output $file;
78 print $output $warning;