Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Automagically indenting (implementation, code)

by deprecated (Priest)
on May 11, 2001 at 06:25 UTC ( [id://79631]=perlquestion: print w/replies, xml ) Need Help??

deprecated has asked for the wisdom of the Perl Monks concerning the following question:

Please reference the code on my home node for details on what I am doing here.

I am making pretty extensive use of heredocs to fit VRML into scalars. I then take these scalars and pass them to parent scalars. These scalars are called, in the VRML terminology, 'Nodes'. So lets look at this code:

sub makemeanode { my $parameter = shift; return << "EOR"; node myNode { fooParam $parameter } EOR }
You'd be surprised just how well this works. Let's extrapolate this a little further though.
sub makeMeContainer { my $parameter = shift; # see, muzakfetch? we're not all so uptight... my $child = makemeanode( "DogPoopMonk" ); return <<"EOR"; node myContainer { someParam $parameter $child } EOR }
While this may produce PML (PoopModellingLanguage) correct code, it is also ugly and difficult to read:
node myContainer { someParam WhateverWeWerePassed node myNode { fooParam DogPoopMonk } }
I am hoping to put this to the monastery as something akin to the HTML::Pretty (or was that CGI::Pretty?) module. I would like to be able to prettify the output of my code (optionally) so that it is easier to read for VRML hackers when they get to reading it. I think probably a regular expression would be able to embody rules like this:
  • lines ending in an open curly brace cause lines afterwards to be indented.
  • lines ending in a close curly brace reduce the level of indentation by 1.
  • nodes with only one line of parameters are moved up to the line of the preceding open curly brace in addition to the closing curly brace provided they fit within 78 characters, like so:
    node myNode { parm thisParm }
I think these are acceptable, but I havent ever written VRML by hand, either. These are generally acceptable by perl coders and even some c coders. I leave that up to you. I'm going to work on the guts of the module.

thanks for your help everyone,
dep.

ps. and isnt this more productive than golf? working together on a project? and just as fun?

--
Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
Re: Automagically indenting (implementation, code)
by perlmonkey (Hermit) on May 11, 2001 at 08:18 UTC
    I dont know of any magic regex that can do this. You have to be careful about balancing and all sort of fun stuff like that. I did something like this for formatting some config files at my work and I used Text::Balanced with a twist of recursion. This may not be the best solution for you, but it might give you a start:
    use Text::Balanced qw(gen_extract_tagged); my $extract = gen_extract_tagged( 'node\s+\w+\s+\{', # start tag '\}', # end tag '(?s).*?(?=node\s+\w+\s+\{)' # prefix ); # $text is your file contents print indent($extract, $text); sub indent { my( $extract, $text, $depth ) = @_; $text =~ s/^\s+//mg if $depth == 0; if( index($text, '{') == -1 ) { #no more blocks $text =~ s/^/" "x$depth/gme; return $text; } #extract block out of $text my($extracted, $remainder, $prefix, $start, $block, $end) = $extract->($text); $prefix =~ s/^/" "x$depth/gme; my $formatted = $prefix; $formatted .= " " x $depth . $start; # parse the inner block $formatted .= indent($extract, $block, $depth+1) if $block; $formatted .= " " x $depth . $end; # parse what is after this block $formatted .= indent($extract, $remainder, $depth) if $remainder; return $formatted; }

    For an input $text of
    node myContainer { someParam WhateverWeWerePassed node myNode { fooParam DogPoopMonk node foo2 { stuff more stuff even more stuff node mode2 { stuff } } } }


    I got the results of:
    node myContainer { someParam WhateverWeWerePassed node myNode { fooParam DogPoopMonk node foo2 { stuff more stuff even more stuff node mode2 { stuff } } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://79631]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found