Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 } } } }

In reply to Re: Automagically indenting (implementation, code) by perlmonkey
in thread Automagically indenting (implementation, code) by deprecated

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found