Skip to content

Instantly share code, notes, and snippets.

@hikiko hikiko/ext_snippets.pl
Last active Mar 16, 2019

Embed
What would you like to do?
greps the extension related code snippets from Vulkan-Docs (ifdef VK_EXT_... to #endif)
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $dir;
my $extname;
my $help;
my $out;
# -d <directory> -o <output> -e <extension name> -h <help>
GetOptions("d=s" => \$dir,
"e=s" => \$extname,
"o=s" => \$out,
"h" => \$help);
if($help) {
print qq[Help:
-d <Vulkan-Docs directory>
-e <extension_name>
-o <output_file>
-h print this help and exit.]."\n";
exit;
}
if(!$dir) {
die "You need to specify the Vulkan-Docs directory.\n";
}
if(!$extname) {
die "You need to specify the extension name.\n";
}
my @filelist;
my $ifdefext = qq[ifdef::$extname];
my $endifext = qq[endif::$extname];
if($dir) {
@filelist = `rgrep -l $ifdefext $dir`;
}
my $output = $out ? $out : "results.txt";
print "results file: $output\n";
#open the output file for writing
open(my $ofh, '>', $output) or die "Could not open $output: $!\n";
print $ofh qq[
<!DOCTYPE html>
<html>
<head>
<style>
* {
font-size:13pt;
}
body{
color:#128495 !important;
background-color:#002B36 !important;
}
a{
color:#EDBB1D!important;
}
b {
color:#6AD74E!important;
}
</style>
</head>
<body>];
foreach(@filelist) {
chomp;
if(index($_, "Changelog") != -1) {
print "index changelog\n";
next;
}
if(index($_, "man/") != -1) {
next;
}
if(index($_, "logfile") != -1) {
next;
}
if(index($_, "$extname.txt") != -1) {
next;
}
my $fname = `pwd`."/$_";
my $short = $fname;
$short = ~/\/([^\/]+)$/;
$short = $1;
my $intro = "\n".qq[<br/><b>FILE:</b><br/><a href="$fname">$short</a><br/><br/>]."\n\n";
print $ofh $intro;
my $ifdef = qq[ifdef::];
my $ifndef = qq[ifndef::];
my $ifdef_ext = qq[ifdef::$extname];
my $ifndef_ext = qq[ifndef::$extname];
my $endif_ext = qq[endif::$extname];
my $endif = qq[endif::];
my $inside_other_ifdef = 0;
my $inside_other_ifndef = 0;
my $inside_extension_ifdef = 0;
my $inside_extension_ifndef = 0;
my $outside = 1;
print $ofh qq[<pre>];
open(my $fh, $_) or die "Could not open $_: $!.\n";
while(!eof($fh)) {
defined(my $line = readline $fh) or die "Failed to read line\n";
if($outside) {
if($line =~ m/^$ifdef_ext/) {
print $ofh "<b>";
print $ofh "\n";
print $ofh $line;
$inside_extension_ifdef = 1;
$outside = 0;
} elsif($line =~ m/^$ifndef_ext/) {
print $ofh "<b>";
print $ofh "\n";
print $ofh $line;
$inside_extension_ifndef = 1;
$outside = 0;
} elsif($line =~ m/^$ifdef/) {
$inside_other_ifdef = 1;
$outside = 0;
} elsif($line =~ m/^$ifndef/) {
$inside_other_ifndef = 1;
$outside = 0;
} elsif($line =~ m/^$endif/) {
if($inside_other_ifdef) {
$inside_other_ifdef = 0;
if(!$inside_extension_ifndef && !$inside_extension_ifndef && !$inside_other_ifndef) {
$outside = 1;
}
} elsif($inside_other_ifndef) {
$inside_other_ifndef = 0;
if(!$inside_extension_ifdef && !$inside_extension_ifndef && !$inside_other_ifndef) {
$outside = 1;
}
}
} else {
print $ofh $line;
}
} else {
if($inside_extension_ifdef) {
print $ofh $line;
if($line =~ m/^$endif_ext/) {
$inside_extension_ifdef = 0;
if(!$inside_extension_ifndef && !$inside_other_ifdef && !$inside_other_ifndef) {
$outside = 1;
}
print $ofh "\n";
print $ofh "</b>";
}
} elsif($inside_extension_ifndef) {
print $ofh $line;
if($line =~ m/^$endif_ext/) {
$inside_extension_ifndef = 0;
if(!$inside_extension_ifdef && !$inside_other_ifdef && !$inside_other_ifndef) {
$outside = 1;
}
print $ofh "\n";
print $ofh "</b>";
}
} elsif($inside_other_ifdef) {
if($line =~ m/^$endif/) {
$inside_other_ifdef = 0;
if(!$inside_other_ifndef && !$inside_extension_ifdef && !$inside_extension_ifndef) {
$outside = 1;
}
}
} elsif($inside_other_ifndef) {
if($line =~ m/^$endif/) {
$inside_other_ifndef = 0;
if(!$inside_other_ifdef && !$inside_extension_ifdef && !$inside_extension_ifndef) {
$outside = 1;
}
}
} else {
print "$line shouldn't be reached.\n";
}
}
}
close($fh);
print $ofh qq[</pre>];
}
print $ofh qq[</body></html>];
close ($ofh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.