use v6-alpha;

module CGI::Util-0.0.1;

sub make_attribute(%attrs, Bool $escape = 0) is export {
    gather {
        for %attrs.keys.sort -> $key {
            my $copy = lc $key;
            
            $copy ~~ s:P5:g/_/-/;
            
            my $value = ($escape) ?? simple_escape(%attrs{$key}) !! %attrs{$key};
            
            take((%attrs{$key}.defined) ?? qq/$copy="$value"/ !! qq/$copy/);
        }
    };
}

sub simple_escape (Str $string is copy) returns Str {
    $string ~~ s:P5:g/&/&amp;/;
    $string ~~ s:P5:g/</&lt;/;
    $string ~~ s:P5:g/>/&gt;/;
    $string ~~ s:P5:g/"/&quot;/;
    
    return $string;
}

1;
