shlomi-fish-homepage / bin / sort-check-spelling-file.pl

#!/usr/bin/perl

use strict;
use warnings;

use autodie;

use utf8;

use HTML::Parser 3.00 ();
use List::MoreUtils qw(any);
use IO::All;

binmode STDOUT, ":encoding(utf8)";

my @general_whitelist;
my @records;

my $filename = 'lib/hunspell/whitelist1.txt';

{
    my $rec;
    open my $fh, '<:encoding(utf8)', $filename;
    while (my $l = <$fh>)
    {
        chomp($l);
        # Whitespace or comment - skip.
        if ($l !~ /\S/ or ($l =~ /\A\s*#/))
        {
            # Do nothing.
        }
        elsif ($l =~ /\A====\s*(.*)/)
        {
            if (defined($rec))
            {
                push @records, $rec;
            }
            $rec = {
                'files' => [ sort { $a cmp $b } split /\s*,\s*/, $1],
                'words' => [],
            },
        }
        else
        {
            if (defined($rec))
            {
                push @{$rec->{'words'}}, $l;
            }
            else
            {
                push @general_whitelist, $l;
            }
        }
    }
    push @records, $rec;
    close ($fh);
}

sub rec_sorter
{
    my ($a_aref, $b_aref, $idx) = @_;

    return (
        (@$a_aref == $idx) ? -1
        : (@$b_aref == $idx) ? 1
        : (($a_aref->[$idx] cmp $b_aref->[$idx])
        ||
        rec_sorter($a_aref, $b_aref, $idx+1))
    );
}

io($filename)->encoding('utf8')->print(
    map { "$_\n" }
    (
        @general_whitelist, '',
        (map
            { ("==== ".join(' , ', @{$_->{files}})), '', @{$_->{words}}, '' }
            sort { rec_sorter($a->{files}, $b->{files}, 0) }
            @records
        )
    )
);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.