Hello everybody

A colleague of mine asked me if I knew how to automatically indent an LDAP filter to make it more readable. E.g.: he wantend something like this:

(&(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=use +r)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person) +(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*)))(&(objectCat +egory=person)(objectClass=contact))(objectCategory=group)(objectCateg +ory=publicFolder)(objectCategory=msExchDynamicDistributionList) )))(o +bjectCategory=contact)(proxyAddresses=smtp:*example.com)))

to become something more similar to this

(& (& (& (& (mailnickname=*) (| (& (objectCategory=person) (objectClass=user) (! (homeMDB=*) ) (! (msExchHomeServerName=*) ) ) (& (objectCategory=person) (objectClass=user) (| (homeMDB=*) (msExchHomeServerName=*) ) ) (& (objectCategory=person) (objectClass=contact) ) (objectCategory=group) (objectCategory=publicFolder) (objectCategory=msExchDynamicDistributionList) ) ) ) (objectCategory=contact) (proxyAddresses=smtp:*example.com) ) )

I didn't want to spend a long time over this, so after a few attempts with the trial-and-error technique and with the help of Devel::ptkdb I came out with this quick and dirty script based on Text::Balanced

#!/usr/bin/perl use strict ; use warnings ; use Text::Balanced qw(extract_multiple) ; die "Uso: $0 filtro\n" unless @ARGV ; my ($begop,$begin,$end) = (qr/\([&|!]\s*/, qr/\(\s*/, qr/\)\s*/) ; my $filter = shift @ARGV ; my @blocks = extract_multiple($filter,[$begop]) ; my $step = 0 ; foreach my $block (@blocks) { if ($block =~ $begop) { # Inizia un operatore print_chunk($step++,$block,1) ; } else { # E` un blocco di match, probabilmente sbilanciato my @matches = extract_multiple($block,[$begin,$end]) ; # Questi sono match while (@matches >= 3) { my @chunks = splice(@matches,0,3) ; # Fai check sui "chunk" e agisci di conseguenza: if ($chunks[1] =~ /=/) { # E` un match: print_chunk($step,join("",@chunks),1) ; } else { # Sfiga while (my $chunk = shift @chunks) { if ($chunk =~ $end) { print_chunk(--$step,$chunk,1) ; } else { # Ricarica gli elementi in @matches e riparti unshift @matches,$chunk,@chunks ; last ; } } } } # Queste sono parentesi che si chiudono drop_parenses(@matches) ; } } sub print_chunk { my ($step,$string,$newline) = @_ ; print " "x$step ; print $string ; print "\n" if $newline ; } sub drop_parenses { while (my $parens = shift @_) { print_chunk(--$step,$parens,1) ; } }

I am pretty sure that there are far better ways to do that, and I am interested on how you'd do it. Anyone?

Ciao!
--bronto


In theory, there is no difference between theory and practice. In practice, there is.

In reply to Making an LDAP filter more readable by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.