update-syntax.pl 5.03 KB
Newer Older
1 2
#!/usr/bin/perl -w

3
# needed things
4
use FindBin;
5
use Cwd;
6
use XML::Parser;
7 8 9 10 11 12

# be strict & warn
use strict;
use warnings;

# switch to update.pl location or die
13
chdir("$FindBin::Bin/static/syntax") || die "Failed to switch to static/syntax directory!\n";
14 15 16 17

# get new syntax files
system("rm -rf data") == 0 || die "Failed to remove old syntax files!\n";
system("git archive --remote=git://anongit.kde.org/syntax-highlighting master data/syntax | tar xf -") == 0 || die "Failed to retrieve new syntax files!\n";
18
system("cd data && git archive --remote=git://anongit.kde.org/syntax-highlighting master CMakeLists.txt | tar xf -") == 0 || die "Failed to retrieve CMakeLists.txt to get version info!\n";
19
system("git add data") == 0 || die "Failed to add syntax files to git!\n";
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

# try to get current frameworks version
my $currentVersion;
open (my $list, "<data/CMakeLists.txt");
for (<$list>) {
    if ((my $version) = /^set\(KF5_VERSION "[0-9]+\.([0-9]+)\.[0-9]+"\)/) {
       $currentVersion = $version;
       last;
    }
}
close $list;
if (!defined($currentVersion)) {
    die "Failed to determine current version of syntax-highlighting framework!\n"
}

# current maximal version
print "Current version of syntax-highlighting: $currentVersion\n";

38 39 40 41 42 43 44 45
# setup XML parser with handler for start element
my %languageAttributes = ();
sub start_tag
{
    # we only care for the language element, remember the attributes
    my($p, $tag, %attrs) = @_;
    if ($tag eq "language") {
        %languageAttributes = %attrs;
Christoph Cullmann's avatar
Christoph Cullmann committed
46
        $p->finish();
47 48 49 50
    }
}
my $parser = XML::Parser->new( Handlers => { Start => \&start_tag });

51 52
# read all syntax files and remember for their version infos!
my %metaInfo;
53
my %nameToFile;
54
my %hiddenNameToFile;
55
my $count = 0;
56
foreach my $xmlFile (<data/syntax/*.xml>) {
57 58 59 60 61 62
    # parse the file
    %languageAttributes = ();
    $parser->parsefile( $xmlFile );

    # we need a name!
    my $name = $languageAttributes{'name'};
63 64 65 66 67 68
    if (!defined($name)) {
        print "Skipping $xmlFile as name attribute is missing.\n";
        next;
    }

    # if we have no versions set, we can't handle this file!
69
    my $version = $languageAttributes{'version'};
70 71 72 73
    if (!defined($version)) {
        print "Skipping $xmlFile as version attribute is missing.\n";
        next;
    }
74
    my $kateversion = $languageAttributes{'kateversion'};
75 76 77 78 79
    if (!defined($kateversion)) {
        print "Skipping $xmlFile as kateversion attribute is missing.\n";
        next;
    }

Christoph Cullmann's avatar
Christoph Cullmann committed
80
    # remember attributes
81
    print "Remembering $xmlFile '$name' with version=$version & kateversion=$kateversion\n";
82 83 84
    foreach my $key (keys %languageAttributes) {
        $metaInfo{$xmlFile}{$key} = $languageAttributes{$key};
    }
85 86

    # remember user visible stuff
87 88
    if (!defined($languageAttributes{'hidden'})) {
        $nameToFile{$languageAttributes{'section'}}{$name} = $xmlFile;
89 90
    } else {
        $hiddenNameToFile{$name} = $xmlFile;
91
    }
92
    ++$count;
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
}

# now: generate all needed update-*.xml files
my $minorVersion = 0;
while ($minorVersion <= $currentVersion) {
    # generate one update file
    my $cVersion = "5.$minorVersion";
    print "Generation update-$cVersion.xml...\n";
    open (my $update, ">update-$cVersion.xml");
    print $update "<!DOCTYPE DEFINITIONS>\n";
    print $update "<DEFINITIONS>\n";
    foreach my $def (sort keys %metaInfo) {
        # is this definition allowed here?
        $_ = $metaInfo{$def}{kateversion};
        if ((my $version) = /[0-9]+\.([0-9]+)/) {
           next if ($version > $minorVersion);
        } else {
            next;
        }
        print $update "<Definition name=\"$metaInfo{$def}{name}\" url=\"https://kate-editor.org/syntax/$def\" version=\"$metaInfo{$def}{version}\"/>\n";
    }
    print $update "</DEFINITIONS>\n";
    close $update;

    # add to git
    system("git add update-$cVersion.xml") == 0 || die "Failed to add update-$cVersion.xml to git!\n";

    # next one
    ++$minorVersion;
}
123 124 125 126 127 128 129 130 131

# we create some overview page, too, as markdown to brag about the amount of stuff we know .P
my $syntax_md = "../../content/syntax.md";
open (my $syntax_page, ">$syntax_md");
print $syntax_page
    "---\n".
    "title: Syntax Highlightings\n".
    "author: Christoph Cullmann\n".
    "date: 2019-08-24T12:12:12+00:00\n".
132
    "---\n\n".
133 134
    "<!-- This page is auto-generated by update-syntax.pl -->\n\n".
    "Kate's highlighting powered by [KSyntaxHighlighting](https://api.kde.org/frameworks/syntax-highlighting/html/) supports the following $count languages.\n";
135 136 137 138 139 140
foreach my $section (sort keys %nameToFile) {
    print $syntax_page "\n### $section\n";
    foreach my $name (sort keys %{$nameToFile{$section}}) {
        my $file = $nameToFile{$section}{$name};
        print $syntax_page "* [$name](/syntax/$file)\n";
    }
141
}
142 143 144 145 146 147 148
if (scalar %hiddenNameToFile > 0) {
    print $syntax_page "\n### Hidden Languages\n";
    foreach my $name (sort keys %hiddenNameToFile) {
        my $file = $hiddenNameToFile{$name};
        print $syntax_page "* [$name](/syntax/$file)\n";
    }
}
149 150 151 152
close($syntax_page);

# add to git
system("git add $syntax_md") == 0 || die "Failed to add $syntax_md to git!\n";