File: generate-html.pl

package info (click to toggle)
kf6-syntax-highlighting 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 47,568 kB
  • sloc: xml: 197,750; cpp: 12,850; python: 3,023; sh: 955; perl: 546; ruby: 488; pascal: 393; javascript: 161; php: 150; jsp: 132; lisp: 131; haskell: 124; ada: 119; ansic: 107; makefile: 96; f90: 94; ml: 85; cobol: 81; yacc: 71; csh: 62; erlang: 54; sql: 51; java: 47; objc: 37; awk: 31; asm: 30; tcl: 29; fortran: 18; cs: 10
file content (144 lines) | stat: -rwxr-xr-x 4,808 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl

# This perl script read stdin and write on stdout. It shall be an XML language file.
#
# * If the name of the language is 'HTML', then it creates the language 'PHP (HTML)'
#   which shall be used for PHP hl.
#
# * If the name of the language is something else (say '*'), it creates the language '*/PHP'.
#   This new language is the same as the old one, but is able to detect PHP everywhere.
#
# This script will correctly set extensions & mimetype, and will replace
# <IncludeRules context="##*"> by <IncludeRules context="##*/PHP">
#
# Generated languages need a language named 'PHP/PHP', which shall take care of PHP hl itself
# and which will be called every time something like <?php is encountred.
#
# This script also supports Twig and does the same as for PHP.
#
# SPDX-FileCopyrightText: Jan Villat <jan.villat@net2000.ch>
# License: LGPL

my $file = "";

open(my $input, '<:encoding(UTF-8)', $ARGV[0])
  or die "Could not open file '$ARGV[0]': $!";

open(my $output, '>:encoding(UTF-8)', $ARGV[1])
  or die "Could not open file '$ARGV[1]': $!";

my $language = $ARGV[1];
if ($language =~ /-php\.xml$/)
{
    $language = "PHP";
}
else
{
    $language = "Twig";
}

while (<$input>)
{
  $file .= $_;
}

$warning = "\n\n<!-- ***** THIS FILE WAS GENERATED BY A SCRIPT - DO NOT EDIT ***** -->\n";

$file =~ s/(?=<language)/$warning\n\n\n/;

$file =~ /<language.*?name="([^"]+)"/;
my $syntaxName = $1;

if ($syntaxName eq "HTML")
{
  $root = 1;
}

if ($language eq "Twig")
{
  $file =~ s/<language([^>]+)priority="[^"]*"/<language$1/s;
}

if ($root == 1)
{
  $file =~ s/<language([^>]+)name="[^"]*"/<language$1name="$language (HTML)"/s;
  $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Scripts"/s;
  if ($language eq "PHP")
  {
    $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.php;*.php3;*.wml;*.phtml;*.phtm;*.inc;*.ctp"/s;
    $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-php4-src;text\/x-php3-src;text\/vnd.wap.wml;application\/x-php"/s;
    $file =~ s/<language([^>]+)*/<language$1 indenter="cstyle"/s;
  }
  # Twig
  else
  {
    $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.twig;*.html.twig;*.htm.twig"/s;
    $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-twig"/s;
  }
}
else
{
  $file =~ s/<language([^>]+)hidden="[^"]*"/<language$1/s;
  $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Other"/s;
  my $extra = " hidden=\"true\"";
  my $mimetype = "";
  my $extensions = "";
  if ($language eq "Twig")
  {
    $mimetype = "text/x-twig";
    if ($syntaxName eq "JavaScript")
    {
      $extra = " priority=\"1\"";
      $extensions = "*.js.twig;*.mjs.twig;*.cjs.twig";
    }
    elsif ($syntaxName eq "TypeScript")
    {
      $extra = " priority=\"1\"";
      $extensions = "*.ts.twig;*.mts.twig;*.cts.twig";
    }
  }
  $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="$mimetype"/s;
  $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/$language"$extra/s;
  $file =~ s/<language([^>]+)alternativeNames="([^"]*)"/<language$1alternativeNames="$2\/$language"/s;
  $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="$extensions"/s;
}

# replace list with a include
$file =~ s/<list name="([^"]+)">.*?<\/list>/<list name="$1"><include>$1##$syntaxName<\/include><\/list>/gs;

$file =~ s/<language([^>]+)kateversion="[^"]*"/<language$1kateversion="5.79"/s;
$file =~ s/ fallthrough="(true|1)"//gs;

if ($language eq "Twig")
{
  # remove Mustache syntax
  if ($root == 1)
  {
    $file =~ s/<context name="MustacheJS.*?<\/context>//gs;
    $file =~ s/<StringDetect attribute="Value" context="#pop#pop!MustacheJS" String="[^"]+[^\/]+\/>//gs;
  }
}
elsif ($root == 1 || $ARGV[0] =~ /mustache.xml$/)
{
  $file =~ s/<(?:RegExpr (attribute="Processing Instruction" context="PI"|context="PI" attribute="Processing Instruction")|itemData name="Processing Instruction")[^\/]+\/>|<context name="PI".*?<\/context>//gs;
}

my $find_language = "##$language/$language";
my $language_suffix = "/$language";
if ($language eq "PHP")
{
  $find_language = "FindPHP";
}

$file =~ s/<IncludeRules\s([^>]*)context="([^"#]*)##(?!Alerts|Comments|Doxygen|Modelines)([^"]+)"/<IncludeRules $1context="$2##$3$language_suffix"/g;
$file =~ s/(<context\s[^>]*[^>\/]>)/$1\n<IncludeRules context="$find_language" \/>/g;
$file =~ s/(<context\s[^>]*[^>\/])\s*\/>/$1>\n<IncludeRules context="$find_language" \/>\n<\/context>/g;

if ($language eq "PHP")
{
  $findphp = "<context name=\"FindPHP\" attribute=\"Normal Text\" lineEndContext=\"#stay\">\n<Detect2Chars context=\"##PHP/PHP\" char=\"&lt;\" char1=\"?\" lookAhead=\"true\" />\n</context>\n";
  $file =~ s/(?=<\/contexts\s*>)/$findphp/;
}

print $output $file;
print $output $warning;