Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: TOCs and deeplinks for our house rules

by LanX (Archbishop)
on Nov 02, 2017 at 17:28 UTC ( #1202628=note: print w/replies, xml ) Need Help??


in reply to Re: TOCs and deeplinks for our house rules
in thread TOCs and deeplinks for our house rules

> 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.

Replies are listed 'Best First'.
Re^3: TOCs and deeplinks for our house rules
by LanX (Archbishop) on Nov 02, 2017 at 17:36 UTC
    replaced id with name, think id is not allowed in the monastery.

    update

    Approved HTML tags don't allow id or name attributes inside <h.> tags so I used additional <a name=""> tags.

    seems to work now:

Re^3: TOCs and deeplinks for our house rules
by RonW (Parson) on Nov 03, 2017 at 23:28 UTC
    Some h3 needed to be h4 to be properly nested

    Sounds like your TOC generator has gone beyond mine. That great!

    FWIW, I didn't try dealing with header nesting because a lot of the HTML to be processed was very poorly written. If I had, the TOC would have looked haphazard.

      > FWIW, I didn't try dealing with header nesting because a lot of the HTML to be processed was very poorly written. If I had, the TOC would have looked haphazard.

      actually I don't know yet how to deal best with wrong nesting.

      E.g a h6 following a h4 would be ignored now (or added to the last h5 which is nonsense*)

      I don't know if it's better to

      • include the missing intermediate levels
      Or
      • ignore the heading
      A warning should be emitted anyway.

      Another nice to have would be to parse already available anchors, to allow multiple runs.

      Again the problem arises if a new anchor should be generated or the last one taken.

      I think all of this should be configurable, but so many options make this already a candidate for a proper module. ...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

      *) actually this explains weird problems I saw with latex beamer and wrong nesting. .. cool insight into alien code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2019-11-22 05:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (108 votes). Check out past polls.

    Notices?