Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is the sort of job Perl was made for.</p> <pre><code>#! /opt/perl/bin/perl use strict; use warnings; use 5.10.1; { package My::Full; use Moose; use MooseX::Method::Signatures; has 'chapters' =&gt; ( 'is' =&gt; 'rw', 'isa' =&gt; 'ArrayRef[My::Chapter]', 'default' =&gt; sub{[]} ); method add_chapter( Str $name ){ my $chapter = My::Chapter-&gt;new( name =&gt; "$name" ); push @{$self-&gt;chapters}, $chapter; return $chapter; } method latest(){ return $self-&gt;add_chapter('') unless @{$self-&gt;chapters}; return $self-&gt;chapters-&gt;[-1]; } method add_section( Str $name ){ my $latest_chapter = $self-&gt;latest; $latest_chapter-&gt;add_section("$name"); } method add_line( Str $line ){ $self-&gt;latest-&gt;add_line( "$line" ); } method xml(){ my $out = ''; for my $chapter ( @{ $self-&gt;chapters } ){ $out .= $chapter-&gt;xml; } return $out; } } { package My::Chapter; use Moose; use MooseX::Method::Signatures; has 'name' =&gt; ( 'is' =&gt; 'rw', 'isa' =&gt; 'Str', 'required' =&gt; 1 ); has 'sections' =&gt; ( 'is' =&gt; 'rw', 'isa' =&gt; 'ArrayRef[My::Section]', 'default' =&gt; sub{[]} ); method latest(){ return $self-&gt;add_section('') unless @{$self-&gt;sections}; return $self-&gt;sections-&gt;[-1]; } method add_section( Str $name ){ my $section = My::Section-&gt;new(name =&gt; "$name"); push @{$self-&gt;sections}, $section; return $section; } method add_line( Str $line ){ $self-&gt;latest-&gt;add_line( "$line" ); } method xml(){ my $name = $self-&gt;name; $name = '???' unless length $name; my $out = qq'&lt;div role="CHAPTER" title="$name"&gt;\n'; for my $section ( @{ $self-&gt;sections } ){ $out .= $section-&gt;xml; } return $out."&lt;/div&gt;\n"; } } { package My::Section; use Moose; use MooseX::MultiMethods; has 'name' =&gt; ( 'is' =&gt; 'rw', 'isa' =&gt; 'Str', 'required' =&gt; 1 ); has 'lines' =&gt; ( 'is' =&gt; 'rw', 'isa' =&gt; 'ArrayRef[Str]', 'default' =&gt; sub{[]} ); method add_line( Str $line ){ push @{$self-&gt;lines}, "$line" } method xml(){ my $name = $self-&gt;name; $name = '???' unless length $name; my $out = qq' &lt;div role="SECTION" title="$name"&gt;\n'; for my $line ( @{ $self-&gt;lines } ){ $out .= " &lt;p&gt;$line&lt;/p&gt;\n"; } return $out." &lt;/div&gt;\n"; } } </code></pre> <h3>The main loop:</h3> <pre><code>my $full = My::Full-&gt;new; while( my $line = &lt;&gt; ){ chomp $line; given( $line ){ when( /^chap(?:ter)?\s++(.+)/i ){ $full-&gt;add_chapter($1); } when( /^sec(?:tion)?\s++(.+)/i ){ $full-&gt;add_section($1); } default{ $full-&gt;add_line($line); } } } say $full-&gt;xml </code></pre> <p>&nbsp;</p> <pre><code>&lt;div role="CHAPTER" title="check"&gt; &lt;div role="SECTION" title="check"&gt; &lt;p&gt;this is something&lt;/p&gt; &lt;p&gt;this is another&lt;/p&gt; &lt;/div&gt; &lt;div role="SECTION" title="check"&gt; &lt;p&gt;take some xxx&lt;/p&gt; &lt;p&gt;do yyy&lt;/p&gt; &lt;p&gt;and some...&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div role="CHAPTER" title="check"&gt; &lt;div role="SECTION" title="???"&gt; &lt;p&gt;we created...&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload