HTML::...avMenu(3pm)

๐Ÿ“ฆ
version 1.1000 ยท 2 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

๐Ÿ’– HTML::Widgets::NavMenu(3pm) ๐Ÿ’–

okay bestie, so this is a User Contributed Perl Documentation page, and it's literally all about HTML::Widgets::NavMenu โ€” a Perl module that builds your site's nav menus for you. slay. ๐ŸŽ€


โœจ NAME โœจ

HTML::Widgets::NavMenu โ€” a Perl module for generating HTML navigation menus. no cap, that's the whole vibe. ๐Ÿ’…

๐ŸŒธ VERSION ๐ŸŒธ

version 1.1000 โ€” yes she's got a version number and yes she's proud of it. ๐Ÿ‘‘

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

here's the whole thing, copy-paste ready, character for character, because we do NOT mess with code in this house:

use HTML::Widgets::NavMenu;

my $nav_menu =
    HTML::Widgets::NavMenu->new(
        'path_info' => "/me/",
        'current_host' => "default",
        'hosts' =>
        {
            'default' =>
            {
                'base_url' => "http://www.hello.com/"
            },
        },
        'tree_contents' =>
        {
            'host' => "default",
            'text' => "Top 1",
            'title' => "T1 Title",
            'expand_re' => "",
            'subs' =>
            [
                {
                    'text' => "Home",
                    'url' => "",
                },
                {
                    'text' => "About Me",
                    'title' => "About Myself",
                    'url' => "me/",
                },
            ],
        },
    );

my $results = $nav_menu->render();

my $nav_menu_html = join("\n", @{$results->{'html'}});

๐ŸŒˆ DESCRIPTION ๐ŸŒˆ

okay so this module generates a navigation menu for a site. that's the main character job. โœจ

but wait, there's more โ€” she can also generate a complete site map, a path of leading components, and she also keeps track of navigation links ("Next", "Prev", "Up", etc.). multitasking icon. ๐Ÿ’…

you can totally start from the example up there in the synopsis ๐Ÿ‘† and then go see more examples in the tests, in the "examples/" directory of the HTML-Widgets-NavMenu tarball, plus complete working sites in the version control repositories at <https://www.shlomifish.org/meta/site-source/> and <https://perl-begin.org/source/> . go look, it's giving reference material. ๐Ÿ“š


๐ŸŽ€ USAGE ๐ŸŽ€

๐Ÿ’œ my $nav_menu = HTML::Widgets::NavMenu->new(@args)

to use this module, call the constructor with the following named arguments:

  • hosts โ€” this should be a hash reference that maps host-IDs to another hash reference that contains information about the hosts. an HTML::Widgets::NavMenu navigation menu can spread across pages in several hosts, and she'll link from one to another using relative URLs if possible, and fully-qualified (i.e: "http://") URLs if not. smart girl behavior fr fr.

currently the only key required in the hash is the "base_url" one, which points to a string containing the absolute URL to the sub-site. the base URL may have trailing components if it does not reside on the domain's root directory. (iykyk)

an optional key that is required only if you wish to use the "site_abs" url_type (see below), is "trailing_url_base", which denotes the component of the site that appears after the hostname. for "http://www.myhost.com/~myuser/" it is "/~myuser/". ๐Ÿซถ

here's an example for a minimal hosts value:

            'hosts' =>
            {
                'default' =>
                {
                    'base_url' => "http://www.hello.com/",
                    'trailing_url_base' => "/",
                },
            },

and here's a two-hosts value from my personal site, which is spread across two sites:

    'hosts' =>
    {
        't2' =>
        {
            'base_url' => "http://www.shlomifish.org/",
            'trailing_url_base' => "/",
        },
        'vipe' =>
        {
            'base_url' => "http://vipe.technion.ac.il/~shlomif/",
            'trailing_url_base' => "/~shlomif/",
        },
    },
  • current_host โ€” this parameter indicates which host-ID of the hosts in "hosts" is the one that the page for which the navigation menu should be generated is. this matters so cross-site and inner-site URLs get handled correctly. no notes. ๐Ÿ’ฏ
  • path_info โ€” this is the path relative to the host's "base_url" of the currently displayed page. the path should start with a "/"-character, or otherwise a re-direction excpetion will be thrown (this is done to aid in using this module from within CGI scripts). yeah, "excpetion" โ€” that's how it's spelled here, we don't touch it. ๐Ÿ™ˆ
  • tree_contents โ€” this item gives the complete tree for the navigation menu. it's a nested Perl data structure, whose syntax is fully explained in the section "The Input Tree of Contents". ๐ŸŒณ
  • ul_classes โ€” this is an optional parameter whose value is a reference to an array that indicates the values of the class="" arguments for the "<ul>" tags whose depthes are the indexes of the array.

for example, assigning:

    'ul_classes' => [ "FirstClass", "second myclass", "3C" ],

will assign "FirstClass" as the class of the top-most ULs, "second myclass" as the classes of the ULs inner to it, and "3C" as the class of the ULs inner to the latter ULs.

if classes are undef, the UL tag will not contain a class parameter. periodt.

  • no_leading_dot โ€” when this parameter is set to 1, the object will try to generate URLs that do not start with "./" when possible. that way, the generated markup will be a little more compact. this option is not enabled by default for backwards compatibility, but is highly recommended. ๐Ÿ”ฅ
  • coords_stop โ€” Experimental ( Boolean ; defaults to false ). TBD: for use by <https://www.shlomifish.org/meta/site-source/> . yeah she's experimental, we don't fully know her yet, leave her alone. ๐Ÿงš

a complete invocation of an HTML::Widgets::NavMenu constructor can be found in the SYNOPSIS above. โฌ†๏ธ

after you _init an instance of the navigation menu object, you need to get the results using the render function.


๐Ÿ“ $results = $nav_menu->render()

render() should be called after a navigation menu object is constructed to prepare the results and return them. she returns a hash reference with the following keys:

  • 'html' โ€” this key points to a reference to an array that contains the tags for the HTML. one can join these tags to get the full HTML. it is possible to delimit them with newlines, if one wishes the markup to be easier to read. ๐Ÿ’–
  • 'leading_path' โ€” this is a reference to an array of node description objects. these indicate the intermediate pages in the site that lead from the front page to the current page. the methods supported by the class of these objects is described below under "The Node Description Component Class". ๐ŸŒธ
  • 'nav_links_obj' โ€” this points to a hash reference whose keys are link IDs for the Firefox "Site Navigation Toolbar" ( <http://www.bolwin.com/software/snb.shtml> ) and compatible programs, and its values are Node Description objects. (see "The Node Description Class" below). here's a sample code that renders the links as "<link rel=...>" into the page header:
    my $nav_links = $results->{'nav_links_obj'};
    # Sort the keys so their order will be preserved
    my @keys = (sort { $a cmp $b } keys(%$nav_links));
    foreach my $key (@keys)
    {
        my $value = $nav_links->{$key};
        my $url = CGI::escapeHTML($value->direct_url());
        my $title = CGI::escapeHTML($value->title());
        print {$fh} "<link rel=\"$key\" href=\"$url\" title=\"$title\" />\n";
    }
  • 'nav_links' โ€” this points to a hash reference whose keys are link IDs compatible with the Firefox Site Navigation ( <http://cdn.mozdev.org/linkToolbar/> ) and its values are the URLs to these links. this key/value pair is provided for backwards compatibility with older versions of HTML::Widgets::NavMenu. in new code, one is recommended to use 'nav_links_obj' instead. (she's the legacy queen, respect her but move on bestie ๐Ÿ’…)

this sample code renders the links as "<link rel=...>" into the page header:

    my $nav_links = $results->{'nav_links'};
    # Sort the keys so their order will be preserved
    my @keys = (sort { $a cmp $b } keys(%$nav_links));
    foreach my $key (@keys)
    {
        my $url = $nav_links->{$key};
        print {$fh} "<link rel=\"$key\" href=\"" .
            CGI::escapeHTML($url) . "\" />\n";
    }

๐Ÿฆ‹ $results = $nav_menu->render_jquery_treeview()

renders a fully expanded tree suitable for input to JQuery's treeview plugin: <http://bassistance.de/jquery-plugins/jquery-plugin-treeview/> โ€” otherwise the same as render() . โœจ


๐ŸŒŸ $text = $nav_menu->gen_site_map()

this function can be called to generate a site map based on the tree of contents. it returns a reference to an array containing the tags of the site map. ๐Ÿ—บ๏ธ


๐Ÿ’ซ $url = $nav_menu->get_cross_host_rel_url_ref({...})

this function can be called to calculate a URL to a different part of the site. it accepts four named arguments, passed as a hash-ref:

  • 'host' โ€” this is the host ID
  • 'host_url' โ€” this is URL within the host.
  • 'url_type' โ€” 'rel', 'full_abs' or 'site_abs'.
  • 'url_is_abs' โ€” a flag that indicates if 'host_url' is already absolute.

๐ŸŒˆ $url = $nav_menu->get_cross_host_rel_url(...)

this is like get_cross_host_rel_url_ref() except that the arguments are clobbered into the arguments list. it is kept here for compatibility sake. ๐Ÿซถ


ok that's part 1 bestie, and there's more coming so don't go anywhere ๐Ÿ’–

๐ŸŒณ The Input Tree of Contents ๐Ÿ’–

okay bestie so the input tree is a nested Perl data structure that reps the tree of the site. Every single node is a Perl hash reference, and its sub-nodes live in an array reference hanging off its 'subs' value. If there's no 'subs', that node is a leaf, periodt โ€” no sub-nodes, no drama. ๐Ÿƒ

The very top-most node is mostly a dummy node, just doing dad duty as the father of all the other nodes. ๐Ÿ‘‘

Here's the full listing of everything you can put inside a node hash and what each thing actually means. no skips, we're doing all of them ๐Ÿซถ

  • 'host' โ€” This is the host-ID of the host, exactly as found in the 'hosts' key of the navigation menu object constructor. It implicitly propagates downwards in the tree, meaning every node of the sub-tree spanning from this node will implicitly have it as their value by default. Generally a host must always be specified, so the first node should specify it. mandatory vibes.
  • 'url' โ€” This holds the URL of the node within the host. The URL should not contain a leading slash. This value does not propagate further. The URL should be specified for every node except separators and the such. โœจ
  • 'text' โ€” This is the text presented to the user as the text of the link inside the navigation bar. E.g.: if 'text' is "Hi There", then the link will look something like this:
<a href="my-url/">Hi There</a>

Or:

<b>Hi There</b>

if it's the current page. Note that this text is rendered into HTML as is, so it should be escaped to prevent HTML-injection attacks. fr fr, escape it. ๐Ÿ˜ญ

  • 'title' โ€” This is the text of the link tag's title attribute. It is also not processed, so the user of the module should make sure it is escaped if needed, to prevent HTML-injection attacks. It's optional, and if not specified, no title will be presented. ๐Ÿ’…
  • 'subs' โ€” This item, if specified, should point to an array reference containing the sub-nodes of this item, in order. ๐Ÿ“‹
  • 'separator' โ€” This key, if specified and true, indicates that the item is a separator, which should just leave a blank line in the HTML. It's best to accompany it with 'skip' (see below). If 'separator' is specified, it is usually meaningless to specify all other node keys except 'skip'. ๐Ÿšง
  • 'skip' โ€” This key, if true, indicates that the node should be skipped when traversing the site using the Mozilla navigation links. Instead the navigation will move to the next or previous nodes. โญ๏ธ
  • 'hide' โ€” This key, if true, indicates that the item should be part of the site's flow and site map, but not displayed in the navigation menu. ๐Ÿ™ˆ
  • 'role' โ€” This indicates a role of an item. It's similar to a CSS class, or to DocBook's "role" attribute, only it induces different HTML markup. The vanilla HTML::Widgets::NavMenu does not distinguish between any roles, but see HTML::Widgets::NavMenu::HeaderRole. ๐ŸŽญ
  • 'expand' โ€” This specifies a predicate (a Perl value that is evaluated to a boolean value, see "Predicate Values" below) to be matched against the path and current host to determine if the navigation menu should be expanded at this node. If it does, all of the nodes up to it will expand as well. ๐ŸŒธ
  • 'show_always' โ€” This value, if true, indicates that the node and all nodes below it (until 'show_always' is explicitly set to false) must be always displayed. Its function is similar to 'expand_re' but its propagation semantics the opposite. ๐Ÿ‘€
  • 'url_type' โ€” This specifies the URL type to use to render this item. It can be:
    1. "rel" โ€” the default. This means a fully relative URL (if possible), like "../../me/about.html".
    2. "site_abs" โ€” this uses a URL absolute to the site, using a slash at the beginning. Like "/~shlomif/me/about.html". For this to work the current host needs to have a 'trailing_url_base' value set.
    3. "full_abs" โ€” this uses a fully qualified URL (e.g: with "http://" at the beginning), even if both the current path and the pointed path belong to the same host. Something like "http://www.shlomifish.org/me/about.html".
  • 'rec_url_type' โ€” This is similar to 'url_type', only it recurses, to the sub-tree of the node. If both 'url_type' and 'rec_url_type' are specified for a node, then the value of 'url_type' will hold. ๐Ÿ”
  • 'url_is_abs' โ€” This flag, if true, indicates that the URL specified by the 'url' key is an absolute URL like "http://www.myhost.com/" and should not be treated as a path within the site. All links to the page associated with this node will contain the URL verbatim.
Note that using absolute URLs as part of the site flow is discouraged, because once they are accessed, the navigation within the primary site is lost. A better idea would be to create a separate page within the site, that will link to the external URL. learn from our mistakes bestie.
  • li_id โ€” This is the HTML ID attribute that will be assigned to the specific "<li>" tag of the navigation menu. So if you have:
'tree_contents' =>
{
    'host' => "default",
    'text' => "Top 1",
    'title' => "T1 Title",
    'expand_re' => "",
    'subs' =>
    [
        {
            'text' => "Home",
            'url' => "",
        },
        {
            'text' => "About Me",
            'title' => "About Myself",
            'url' => "me/",
            'li_id' => 'about_me',
        },
    ],
},

Then the HTML for the About me will look something like:

<li id="about_me">
<a href="me/ title="About Myself">About Me</a>
</li>

๐Ÿงš Predicate Values ๐Ÿ”ฎ

An explicitly specified predicate value is a hash reference that contains one of the following three keys with their appropriate values:

  • 'cb' => \&predicate_func โ€” This specifies a sub-routine reference (or "callback" or "cb") that will be called to determine the result of the predicate. It accepts two named arguments โ€” 'path_info', which is the path of the current page (without the leading slash), and 'current_host', which is the ID of the current host.

Here's an example for such a callback:

sub predicate_cb1
{
    my %args = (@_);
    my $host = $args{'current_host'};
    my $path = $args{'path_info'};
    return (($host eq "true") && ($path eq "mypath/"));
}
  • 're' => $regexp_string โ€” This specifies a regular expression to be matched against the path_info (regardless of what current_host is), to determine the result of the predicate. ๐Ÿ’ซ
  • 'bool' => [ 0 | 1 ] โ€” This specifies the constant boolean value of the predicate. โœ…

Note that if 'cb' is specified then both 're' and 'bool' will be ignored, and 're' over-rides 'bool'. priority order, iykyk.

Orthogonal to these keys is the 'capt' key, which specifies whether this expansion "captures" or not. This is relevant to the behaviour in the breadcrumbs' trails, if one wants the item to appear there or not. The default value is true.

If the predicate is not a hash reference, then HTML::Widgets::NavMenu will try to guess what it is. If it's a sub-routine reference, it will be an implicit callback. If it's one of the values "0", "1", "yes", "no", "true", "false", "True", "False" it will be considered a boolean. If it's a different string, a regular expression match will be attempted. Else, an exception will be thrown. ๐Ÿ’€

Here are some examples for predicates:

# Always expand.
'expand' => { 'bool' => 1, };

# Never expand.
'expand' => { 'bool' => 0, };

# Expand under home/
'expand' => { 're' => "^home/" },

# Expand under home/ when the current host is "foo"
sub expand_path_home_host_foo
{
    my %args = (@_);
    my $host = $args{'current_host'};
    my $path = $args{'path_info'};
    return (($host eq "foo") && ($path =~ m!^home/!));
}

'expand' => { 'cb' => \&expand_path_home_host_foo, },

๐ŸŽ€ The Node Description Class ๐Ÿ‘‘

When retrieving the leading path or the "nav_links_obj", an array of objects is returned. This section describes the class of these objects, so one will know how to use them. ๐Ÿซถ

Basically, it's an object that has several accessors. The accessors are:

  • host โ€” The host ID of this node. ๐Ÿ 
  • host_url โ€” The URL of the node within the host (the one given in its 'url' key). ๐Ÿ”—
  • label โ€” The label of the node (the one given in its 'text' key). This is not SGML-escaped. heads up!
  • title โ€” The title of the node (that can be assigned to the URL title attribute). This is not SGML-escaped.
  • direct_url โ€” A direct URL (usable for inclusion in an A tag) from the current page to this page. ๐Ÿงญ
  • url_type โ€” This is the "url_type" (see above) that holds for this node.

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

See the article Shlomi Fish wrote for Perl.com for a gentle introduction to HTML-Widgets-NavMenu:

<http://www.perl.com/pub/a/2005/07/07/navwidgets.html>

  • HTML::Widgets::NavMenu::HeaderRole โ€” An HTML::Widgets::NavMenu sub-class that contains support for another role. Used for the navigation menu in <http://perl-begin.org/>. ๐Ÿ’œ
  • HTML::Widget::SideBar โ€” A module written by Yosef Meller for maintaining a navigation menu. HTML::Widgets::NavMenu originally utilized it, but no longer does. This module does not make links relative on its own, and tends to generate a lot of JavaScript code by default. It also does not have too many automated test scripts. ๐Ÿ˜ฌ
  • HTML::Menu::Hierarchical โ€” A module by Don Owens for generating hierarchical HTML menus. I could not quite understand its tree traversal semantics, so I ended up not using it. It also seems to require that each of the tree nodes will have a unique ID. ๐Ÿค”
  • HTML::Widgets::Menu โ€” This module also generates a navigation menu. The CPAN version is relatively old, and the author sent me a newer version. After playing with it a bit, I realized that I could not get it to do what I want (but I cannot recall why), so I abandoned it. ๐Ÿ™ˆ

โœจ AUTHORS โœจ

Shlomi Fish, <shlomif@cpan.org>, <http://www.shlomifish.org/> . ๐Ÿ’–


๐ŸŒˆ THANKS ๐ŸŒˆ

Thanks to Yosef Meller (<https://metacpan.org/author/YOSEFM>) for writing the module HTML::Widget::SideBar on which initial versions of these modules were based. (Albeit his code is no longer used here.) ๐Ÿซถ

Copyright 2004, Shlomi Fish. All rights reserved.

You can use, modify and distribute this module under the terms of the MIT Expat license. ( <http://www.opensource.org/licenses/mit-license.php> ).


๐Ÿซถ SUPPORT ๐Ÿซถ

๐ŸŒ Websites ๐ŸŒ

okay bestie, these are the sites that have the tea on this module, and honestly they may be of help to you. And as always โ€” plus these websites, pls use your fave search engine to go discover more resources, because the internet is vast and she contains multitudes ๐Ÿ’…

  • MetaCPAN โ€” a modern, open-source CPAN search engine, useful to view POD in HTML format.

<https://metacpan.org/release/HTML-Widgets-NavMenu>

  • RT: CPAN's Bug Tracker โ€” the RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.

<https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-Widgets-NavMenu>

  • CPANTS โ€” the CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.

<http://cpants.cpanauthors.org/dist/HTML-Widgets-NavMenu>

  • CPAN Testers โ€” the CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.

<http://www.cpantesters.org/distro/H/HTML-Widgets-NavMenu>

  • CPAN Testers Matrix โ€” the CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.

<http://matrix.cpantesters.org/?dist=HTML-Widgets-NavMenu>

  • CPAN Testers Dependencies โ€” the CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.

<http://deps.cpantesters.org/?module=HTML::Widgets::NavMenu>

๐Ÿ› Bugs / Feature Requests ๐Ÿ›

Found a bug? Got a feature request burning a hole in your notes app? Please report any bugs or feature requests by email to bug-html-widgets-navmenu at rt.cpan.org, or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-Widgets-NavMenu>. And the best part โ€” you will be automatically notified of any progress on the request by the system. no ghosting, fr fr โœจ

๐Ÿงš Source Code ๐Ÿงš

The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. It's giving open source main character energy ๐Ÿ‘‘

If you want to contribute patches, please send me a diff or prod me to pull from your repository :)

<https://github.com/shlomif/perl-HTML-Widgets-NavMenu>

git clone git://github.com/shlomif/perl-HTML-Widgets-NavMenu.git

๐ŸŒธ AUTHOR ๐ŸŒธ

Shlomi Fish <shlomif@cpan.org>


๐Ÿ’€ BUGS ๐Ÿ’€

Please report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/perl-HTML-Widgets-NavMenu/issues>

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. (Yes this is the whole ask. A lil repro goes a LONG way and we love you for it ๐Ÿฅบ)


This software is Copyright (c) 2005 by Shlomi Fish.

This is free software, licensed under:

The MIT (X11) License

and that's a wrap, bestie!! ๐ŸŽ€ that was literally the whole page, you read the entire thing, iconic behavior honestly ๐Ÿ’– this lil gem lives in HTML::...avMenu(3pm), it's the perl v5.34.0 docs for this module, and it was generated on 2022-08-20 โ€” so if you're reading this way later, hi, you're part of history now, slay ๐Ÿฆ‹ go build something cute with it ๐Ÿ’