Monday 15 March 2010

Perl-based HTML id attribute generator -



Perl-based HTML id attribute generator -

i have web page containing table of info sourced back-end database. in table (usually) unique key, provided 3rd party have little command of.

because html id attributes case insensitive, must unique, , can contain characters, i've resorted using md5 hash create them based on unique database keys. keys far meeting html id tag standards.

#!/usr/bin/perl utilize strict; utilize warnings; utilize digest::md5 qw(md5_hex); @extens = qw( g h j k l m n o p q ); sub genhtmlid { $id = md5_hex( $_[0] ); # html 4.01 not allow id's begin numbers $id =~ s/(^[0-9])/$extens[$1]/; homecoming $id; }

this seems work cases, isn't ideal situation due length (32 characters) of keys beingness created.

my guess generator used entire set of lower-case alphabetic characters need far fewer characters unique.

i've been searching through cpan , net module create these tags, haven't found one. happen know of one, or have solution require far fewer characters?

additional info: db engine sybase. keys (provided 3rd party) 6 7 fields, separated slash (/) character. though many fields may contain specific values, 2 of fields 'free hand', , contain characters not want appear in html id tag, like: ( ) [ ] < > ! ~ $ % , # , 'space'. entire key (all fields , separators) stored in simple varchar, after minor translation - prevent sybase complaining. 'id' tag required 3rd party web apps, can not alter 'data'.

if don't mind increased memory footprint of @choroba's solution, recommend go that.

otherwise, can take hint git's usage of commit hashes: of time, first 7 hex digits enough.

for example,

#!/usr/bin/env perl utilize strict; utilize warnings; utilize digest::sha1 qw(sha1_hex); run(); sub run { %id; $byte (0 .. 255) { $id{ genhtmlid(chr $byte) } = 1; } print scalar keys %id, "\n"; } sub genhtmlid { 'id' . substr( sha1_hex( $_[0] ), 0, 8) }

will print 256. not know database key space looks like. if had idea, test collisions in key space.

if can rule out collisions, version of genhtmlid give bijection between database keys , html id's without memory footprint of lookup table.

html perl

No comments:

Post a Comment