Skip to content

Instantly share code, notes, and snippets.

@scottoffen
Last active August 29, 2015 14:00
Show Gist options
  • Save scottoffen/11220901 to your computer and use it in GitHub Desktop.
Save scottoffen/11220901 to your computer and use it in GitHub Desktop.
Perl parses the Apache mime.types file for use in ContentTypes.cs (https://gist.github.com/scottoffen/11197961)
#!/usr/bin/perl
use strict;
use warnings;
# input file taken from:
# http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
my %extensions;
my $input = "./mimetypes.txt";
my $output = "./contenttypes.txt";
#----------------------------------------------------------------------------------#
# Parse Input #
#----------------------------------------------------------------------------------#
open(INPUT, $input);
my @input = <INPUT>;
close(INPUT);
foreach my $line (@input)
{
chop($line);
$line =~ s/^#//;
my ($type, $extensions) = split("\t+", $line, 2);
next unless ($extensions);
my @extensions = split(" +", $extensions);
foreach my $extension (@extensions)
{
$extensions{uc($extension)} = $type;
}
}
#----------------------------------------------------------------------------------#
#----------------------------------------------------------------------------------#
# Write Output #
#----------------------------------------------------------------------------------#
open(OUTPUT, ">$output");
foreach my $extension (sort keys %extensions)
{
next if ($extension =~ /^\d/);
next if ($extension =~ /[\-\=]/);
my $type = $extensions{$extension};
$type =~ s/\s+$//;
$type =~ s/^\s+//;
my $flag = (($type =~ /^text/i) || ($type =~ /\+?xml$/i) || ($type =~ /(json|javascript)/)) ? "IsText" : "IsBinary";
print OUTPUT "[Metadata(Value=\"$type\", $flag = true)]\n";
print OUTPUT "$extension,\n\n";
}
close(OUTPUT);
#----------------------------------------------------------------------------------#
print "Extensions : " . (scalar (keys %extensions)) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment