File indexing completed on 2024-05-12 04:02:13

0001 #!/usr/bin/perl
0002 
0003 # This perl script read stdin and write on stdout. It shall be an XML language file.
0004 #
0005 # * If the name of the language is 'HTML', then it creates the language 'PHP (HTML)'
0006 #   which shall be used for PHP hl.
0007 #
0008 # * If the name of the language is something else (say '*'), it creates the language '*/PHP'.
0009 #   This new language is the same as the old one, but is able to detect PHP everywhere.
0010 #
0011 # This script will correctly set extensions & mimetype, and will replace
0012 # <IncludeRules context="##*"> by <IncludeRules context="##*/PHP">
0013 #
0014 # Generated languages need a language named 'PHP/PHP', which shall take care of PHP hl itself
0015 # and which will be called every time something like <?php is encountred.
0016 #
0017 # This script also supports Twig and does the same as for PHP.
0018 #
0019 # SPDX-FileCopyrightText: Jan Villat <jan.villat@net2000.ch>
0020 # License: LGPL
0021 
0022 my $file = "";
0023 
0024 open(my $input, '<:encoding(UTF-8)', $ARGV[0])
0025   or die "Could not open file '$ARGV[0]': $!";
0026 
0027 open(my $output, '>:encoding(UTF-8)', $ARGV[1])
0028   or die "Could not open file '$ARGV[1]': $!";
0029 
0030 my $language = $ARGV[1];
0031 if ($language =~ /-php\.xml$/)
0032 {
0033     $language = "PHP";
0034 }
0035 else
0036 {
0037     $language = "Twig";
0038 }
0039 
0040 while (<$input>)
0041 {
0042   $file .= $_;
0043 }
0044 
0045 $warning = "\n\n<!-- ***** THIS FILE WAS GENERATED BY A SCRIPT - DO NOT EDIT ***** -->\n";
0046 
0047 $file =~ s/(?=<language)/$warning\n\n\n/;
0048 
0049 $file =~ /<language.*?name="([^"]+)"/;
0050 my $syntaxName = $1;
0051 
0052 if ($syntaxName eq "HTML")
0053 {
0054   $root = 1;
0055 }
0056 
0057 if ($language eq "Twig")
0058 {
0059   $file =~ s/<language([^>]+)priority="[^"]*"/<language$1/s;
0060 }
0061 
0062 if ($root == 1)
0063 {
0064   $file =~ s/<language([^>]+)name="[^"]*"/<language$1name="$language (HTML)"/s;
0065   $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Scripts"/s;
0066   if ($language eq "PHP")
0067   {
0068     $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.php;*.php3;*.wml;*.phtml;*.phtm;*.inc;*.ctp"/s;
0069     $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-php4-src;text\/x-php3-src;text\/vnd.wap.wml;application\/x-php"/s;
0070     $file =~ s/<language([^>]+)*/<language$1 indenter="cstyle"/s;
0071   }
0072   # Twig
0073   else
0074   {
0075     $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.twig;*.html.twig;*.htm.twig"/s;
0076     $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-twig"/s;
0077   }
0078 }
0079 else
0080 {
0081   $file =~ s/<language([^>]+)hidden="[^"]*"/<language$1/s;
0082   $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Other"/s;
0083   my $extra = " hidden=\"true\"";
0084   my $mimetype = "";
0085   my $extensions = "";
0086   if ($language eq "Twig")
0087   {
0088     $mimetype = "text/x-twig";
0089     if ($syntaxName eq "JavaScript")
0090     {
0091       $extra = " priority=\"1\"";
0092       $extensions = "*.js.twig;*.mjs.twig;*.cjs.twig";
0093     }
0094     elsif ($syntaxName eq "TypeScript")
0095     {
0096       $extra = " priority=\"1\"";
0097       $extensions = "*.ts.twig;*.mts.twig;*.cts.twig";
0098     }
0099   }
0100   $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="$mimetype"/s;
0101   $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/$language"$extra/s;
0102   $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="$extensions"/s;
0103 }
0104 
0105 # replace list with a include
0106 $file =~ s/<list name="([^"]+)">.*?<\/list>/<list name="$1"><include>$1##$syntaxName<\/include><\/list>/gs;
0107 
0108 $file =~ s/<language([^>]+)kateversion="[^"]*"/<language$1kateversion="5.79"/s;
0109 $file =~ s/ fallthrough="(true|1)"//gs;
0110 
0111 if ($language eq "Twig")
0112 {
0113   # remove Mustache syntax
0114   if ($root == 1)
0115   {
0116     $file =~ s/<context name="MustacheJS.*?<\/context>//gs;
0117     $file =~ s/<StringDetect attribute="Value" context="#pop#pop!MustacheJS" String="[^"]+[^\/]+\/>//gs;
0118   }
0119 }
0120 elsif ($root == 1 || $ARGV[0] =~ /mustache.xml$/)
0121 {
0122   $file =~ s/<(?:RegExpr (attribute="Processing Instruction" context="PI"|context="PI" attribute="Processing Instruction")|itemData name="Processing Instruction")[^\/]+\/>|<context name="PI".*?<\/context>//gs;
0123 }
0124 
0125 my $find_language = "##$language/$language";
0126 my $language_suffix = "/$language";
0127 if ($language eq "PHP")
0128 {
0129   $find_language = "FindPHP";
0130 }
0131 
0132 $file =~ s/<IncludeRules\s([^>]*)context="([^"#]*)##(?!Alerts|Comments|Doxygen|Modelines)([^"]+)"/<IncludeRules $1context="$2##$3$language_suffix"/g;
0133 $file =~ s/(<context\s[^>]*[^>\/]>)/$1\n<IncludeRules context="$find_language" \/>/g;
0134 $file =~ s/(<context\s[^>]*[^>\/])\s*\/>/$1>\n<IncludeRules context="$find_language" \/>\n<\/context>/g;
0135 
0136 if ($language eq "PHP")
0137 {
0138   $findphp = "<context name=\"FindPHP\" attribute=\"Normal Text\" lineEndContext=\"#stay\">\n<Detect2Chars context=\"##PHP/PHP\" char=\"&lt;\" char1=\"?\" lookAhead=\"true\" />\n</context>\n";
0139   $file =~ s/(?=<\/contexts\s*>)/$findphp/;
0140 }
0141 
0142 print $output $file;
0143 print $output $warning;