Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
> seems like a TOC with both a few keywords for each topic AND the markup needed would be appropriate... but a big PITA.

For a start:

Hacked some code augmenting H-tags and generating a TOC:

YMMV

HTH! :)

use strict; use warnings; use feature 'say'; use HTML::Entities; use Data::Dump qw/pp/; my $text; my %anchor_count; my @aoh_toc; my ($min,$max) = (3,6); my @stack; $stack[$min-1] = \@aoh_toc; # root while (my $line = <DATA>) { if ( $line =~ m# ^ (\s*) < \s* h([$min-$max]) \s* > (.*) </ \s* h(\ +2) \s* > \s*$ #xi ) { #say $line; my ($indent,$level,$text) = ($1,$2,$3); if ( $indent ) { # ignore indented <h*> warn "Skipping $line"; } else { my $anchor = text2anchor($text); warn "Duplicate '$anchor' " if $anchor_count{$anchor}++; $line = "<h$level><a name='$anchor'>$text</a></h$level>\n"; my $a_sub = []; push @{$stack[$level-1]}, { text => $text, link => $anchor, sub => $a_sub, level => $level, }; $stack[$level] = $a_sub; } #say $line; } $text .= $line } # pp \@aoh_toc; say "<ul>"; create_toc(\@aoh_toc); say "</ul>"; print $text; sub text2anchor { my ($text) = @_; # trim $text =~ s/^\s+//; $text =~ s/\s+$//; # make valid indentifier my $encoded = encode_entities( $text ); # replace whitespaces $encoded =~ s/\s/_/g; return $encoded; } sub create_toc { my ($aoh_toc) = @_; for my $h_line (@$aoh_toc) { my $indent = " " x ($h_line->{level}-$min); say qq~$indent<li><a href='#$h_line->{link}'>$h_line->{text}</a> +</li>~; my $a_sub = $h_line->{sub}; if ( @$a_sub ) { say "$indent<ul>"; create_toc($a_sub); say "$indent</ul>"; } } } __DATA__ ...

update:

something is wrong, for a reason I do not understand yet the anchors are filtered ...

need to check the allowed tags in the monastery... (under construction)

produced HTML see reply here Re^3: TOCs and deeplinks for our house rules

update

needed extra <a name="$target"> tags

update

Forgot to mention, I had to change the input at some places:

  • Some h3 needed to be h4 to be properly nested
  • some heading were just examples and were indented to be ignored
An included heading needs to start a line without leading whitespace and can't be followed by anything else but whitespace.

In reply to Re^2: TOCs and deeplinks for our house rules by LanX
in thread TOCs and deeplinks for our house rules by LanX

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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2019-12-10 11:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?