NaturalDocs:: Menu

A package handling the menu’s contents and state. 

Usage and Dependencies

Summary
A package handling the menu’s contents and state.
A hash of the text synonyms for the menu tokens.
A hash of the text synonyms for the index modifiers.
A hash of text equivalents of the possible index types.
Whether the menu changed or not, regardless of why.
Whether the menu file has changed, usually meaning the user edited it.
The parsed menu file.
An existence hash of default titles that have changed, since OnDefaultTitleChange() will be called before LoadAndUpdate().
The title of the menu.
The sub-title of the menu.
The footer for the documentation.
An existence hash of all the defined index types appearing in the menu.
An existence hash of all the indexes that appeared in the menu last time.
The file used to generate the menu.
The file used to store the previous state of the menu so as to detect changes.
Loads the menu file from disk and updates it.
Loads the menu, assuming neither the menu file nor any of the source files have changed, and thus it definitely doesn’t need to be updated.
Writes the changes to the menu files.
Returns whether the menu has changed or not.
Returns the parsed menu as an arrayref of NaturalDocs::Menu::Entry objects.
Returns the title of the menu, or undef if none.
Returns the sub-title of the menu, or undef if none.
Returns the footer of the documentation, or undef if none.
Returns an existence hashref of all the indexes appearing in the menu.
Returns an existence hashref of all the indexes that previously appeared in the menu.
These functions are called by NaturalDocs::Project only.
Called by NaturalDocs::Project if it detects that the menu file has changed.
Called by NaturalDocs::Project if the default menu title of a source file has changed.
Loads and parses the menu file NaturalDocs_Menu.txt.
Saves the current menu to NaturalDocs_Menu.txt.
A recursive function to write the contents of an arrayref of NaturalDocs::Menu::Entry objects to disk.
Loads and parses the previous menu state file.
Saves changes to NaturalDocs.m.
A recursive function to write the contents of an arrayref of NaturalDocs::Menu::Entry objects to disk.
Handles errors appearing in the menu file.
Checks the menu to see if a significant number of file entries didn’t resolve to actual files, and if so, saves a backup of the menu and issues a warning.
Detects if the user manually changed any file titles, and if so, automatically locks them with MENU_FILE_NOAUTOTITLE.
Finds which files have auto-titles that changed and flags their groups for updating with MENU_GROUP_UPDATETITLES and MENU_GROUP_UPDATEORDER.
Adds files to the menu that aren’t already on it, attempting to guess where they belong.
Determines which groups files in certain directories should be placed in.
Removes files from the menu that no longer exist or no longer have Natural Docs content.
Removes groups with less than two entries.
Checks a group and all its sub-groups for life and remove any that are dead.
Where possible, creates sub-groups based on directories for any long groups that have MENU_GROUP_UPDATESTRUCTURE set.
Where possible, creates sub-groups based on name prefix for any long groups that have the MENU_GROUP_UPDATESTRUCTURE flag set.
Detects the order of the entries in all groups that have the MENU_GROUP_UPDATEORDER flag set.
Creates titles for the unlocked file entries in all groups that have the MENU_GROUP_UPDATETITLES flag set.
Resorts all groups that have MENU_GROUP_UPDATEORDER set.
A comparison function for use in sorting.
Returns an array of all the directories shared by the files in the group.

Constants

MAXFILESINGROUPThe maximum number of file entries that can be present in a group before it becomes a candidate for sub-grouping. 
MINFILESINNEWGROUPThe minimum number of file entries that must be present in a group before it will be automatically created.  This is not the number of files that must be in a group before it’s deleted. 

Variables

menuSynonyms

my %menuSynonyms

A hash of the text synonyms for the menu tokens.  The keys are the lowercase synonyms, and the values are one of the Menu Entry Types

indexSynonyms

my %indexSynonyms

A hash of the text synonyms for the index modifiers.  The keys are the all lowercase synonyms, and the values are the associated Topic Types

indexNames

my %indexNames

A hash of text equivalents of the possible index types.  The keys are the Topic Types, and the values are the strings. 

hasChanged

my $hasChanged

Whether the menu changed or not, regardless of why. 

fileChanged

my $fileChanged

Whether the menu file has changed, usually meaning the user edited it. 

menu

my $menu

The parsed menu file.  Is stored as a MENU_GROUP NaturalDocs::Menu::Entry object, with the top-level entries being stored as the group’s content.  This is done because it makes a number of functions simpler to implement, plus it allows group flags to be set on the top-level.  However, it is exposed externally via Content() as an arrayref. 

This structure will not contain objects for MENU_TITLE, MENU_SUBTITLE, or MENU_FOOTER entries.  Those will be stored in the title, subTitle, and footer variables instead. 

defaultTitlesChanged

my %defaultTitlesChanged

An existence hash of default titles that have changed, since OnDefaultTitleChange() will be called before LoadAndUpdate().  Collects them to be applied later.  The keys are the file names. 

title

my $title

The title of the menu. 

subTitle

my $subTitle

The sub-title of the menu. 

footer

my $footer

The footer for the documentation. 

indexes

my %indexes

An existence hash of all the defined index types appearing in the menu.  Keys are the Topic Types or * for the general index. 

previousIndexes

my %previousIndexes

An existence hash of all the indexes that appeared in the menu last time.  Keys are the Topic Types or * for the general index. 

Files

NaturalDocs_Menu.txt

The file used to generate the menu. 

Format

The file is plain text.  Blank lines can appear anywhere and are ignored.  Tags and their content must be completely contained on one line with the exception of Group’s braces. 

# [comment]

The file supports single-line comments via #.  They can appear alone on a line or after content. 

Format: [version]
Title: [title]
SubTitle: [subtitle]
Footer: [footer]

The file format version, menu title, subtitle, and footer are specified as above.  Each can only be specified once, with subsequent ones being ignored.  Subtitle is ignored if Title is not present.  Format must be the first entry in the file.  If it’s not present, it’s assumed the menu is from version 0.95 or earlier, since it was added with 1.0. 

File: [title] ([file name])
File: [title] (auto-title, [file name])
File: [title] (no auto-title, [file name])

Files are specified as above.  If “no auto-title” is specified, the title on the line is used.  If not, the title is ignored and the default file title is used instead.  Auto-title defaults to on, so specifying “auto-title” is for compatibility only. 

Group: [title]
Group: [title] { ... }

Groups are specified as above.  If no braces are specified, the group’s content is everything that follows until the end of the file, the next group (braced or unbraced), or the closing brace of a parent group.  Group braces are the only things in this file that can span multiple lines. 

There is no limitations on where the braces can appear.  The opening brace can appear after the group tag, on its own line, or preceding another tag on a line.  Similarly, the closing brace can appear after another tag or on its own line.  Being bitchy here would just get in the way of quick and dirty editing; the package will clean it up automatically when it writes it back to disk. 

Text: [text]

Arbitrary text is specified as above.  As with other tags, everything must be contained on the same line. 

Link: [URL]
Link: [title] ([URL])

External links can be specified as above.  If the titled form is not used, the URL is used as the title. 

Index: [name]
[modifier] Index: [name]

Indexes are specified with the types above.  Valid modifiers are defined in indexSynonyms and include Function and Class.  If no modifier is specified, the line specifies a general index. 

Revisions

Prior to 1.0, there was no Format line.  Use that to detect pre-1.0 files.  Also:

  • File lines defaulted to auto-title being off.  Specifying “auto-title” turned it on, but there was no “no auto-title” attribute. 
  • Since there was no auto-organization, all groups were manually added.  The exception is a top-level Other group, where new files were automatically added if there were groups defined. 

Prior to 0.9, there were no Index entries. 

NaturalDocs.m

The file used to store the previous state of the menu so as to detect changes.  Is named NaturalDocs.m instead of something like NaturalDocs.menu to avoid confusion with NaturalDocs_Menu.txt.  This one is not user-editable so we don’t want people opening it by accident. 

[BINARY_FORMAT]

The file is binary, so the first byte is the BINARY_FORMAT token. 

[app version]

Immediately after is the application version it was generated with.  Manage with the binary functions in NaturalDocs::Version

[UInt8: 0 (end group)]
[UInt8: MENU_FILE] [UInt8: noAutoTitle] [AString16: title] [AString16: target]
[UInt8: MENU_GROUP] [AString16: title]
[UInt8: MENU_INDEX] [AString16: title] [UInt8: type (0 for general)]
[UInt8: MENU_LINK] [AString16: title] [AString16: url]
[UInt8: MENU_TEXT] [AString16: text]

The first UInt8 of each following line is either zero or one of the Menu Entry Types.  What follows is contextual.  AString16s are big-endian UInt16’s followed by that many ASCII characters. 

There are no entries for title, subtitle, or footer.  Only the entries present in menu

Dependencies

  • Because the type is represented by a UInt8, the Menu Entry Types must all be <= 255. 
  • Because the index target is represented by a UInt8, the Topic Types must all be <= 255. 

Revisions

Prior to 1.0, the file was a text file consisting of the app version and a line which was a tab-separated list of the indexes present in the menu.  * meant the general index. 

Prior to 0.95, the version line was 1.  Test for “1” instead of “1.0” to distinguish. 

Prior to 0.9, this file didn’t exist. 

File Functions

LoadAndUpdate

sub LoadAndUpdate

Loads the menu file from disk and updates it.  Will add, remove, rearrange, and remove auto-titling from entries as necessary. 

LoadUnchanged

sub LoadUnchanged

Loads the menu, assuming neither the menu file nor any of the source files have changed, and thus it definitely doesn’t need to be updated. 

Save

sub Save

Writes the changes to the menu files. 

Information Functions

HasChanged

sub HasChanged

Returns whether the menu has changed or not. 

Content

sub Content

Returns the parsed menu as an arrayref of NaturalDocs::Menu::Entry objects.  Do not change the arrayref. 

The arrayref will not contain MENU_TITLE and MENU_SUBTITLE entries.  Use the Title() and SubTitle() functions instead. 

Title

sub Title

Returns the title of the menu, or undef if none. 

SubTitle

sub SubTitle

Returns the sub-title of the menu, or undef if none. 

Footer

sub Footer

Returns the footer of the documentation, or undef if none. 

Indexes

sub Indexes

Returns an existence hashref of all the indexes appearing in the menu.  The keys are the Topic Types or * for the general index.  Do not change the arrayref. 

PreviousIndexes

sub PreviousIndexes

Returns an existence hashref of all the indexes that previously appeared in the menu.  The keys are the Topic Types or * for the general index.  Do not change the arrayref. 

Event Handlers

These functions are called by NaturalDocs::Project only.  You don’t need to worry about calling them.  For example, when changing the default menu title of a file, you only need to call NaturalDocs::Project::SetDefaultMenuTitle().  That function will handle calling OnDefaultTitleChange()

OnFileChange

sub OnFileChange

Called by NaturalDocs::Project if it detects that the menu file has changed. 

OnDefaultTitleChange

sub OnDefaultTitleChange #(file)

Called by NaturalDocs::Project if the default menu title of a source file has changed. 

Parameters

fileThe source file that had its default menu title changed. 

Support Functions

LoadMenuFile

sub LoadMenuFile

Loads and parses the menu file NaturalDocs_Menu.txt.  This will fill menu, title, subTitle, footer, and indexes

Returns

The array ( errors, filesInMenu, oldLockedTitles ). 

errorsAn arrayref of errors appearing in the file, each one being an NaturalDocs::Menu::Error object.  Undef if none. 
filesInMenuA hashref of all the source files that appear in the menu.  The keys are the file names, and the values are references to their entries in menu.  This parameter will always exist. 
oldLockedTitlesA hashref of all the locked titles in pre-1.0 menu files.  The keys are the file names, and the values are the old locked titles.  Will be undef if none.  If a file entry from a pre-1.0 file was locked, it’s entry in menu is unlocked and its title is placed here instead, so that it can be compared with the generated title and only locked again if absolutely necessary. 

SaveMenuFile

sub SaveMenuFile

Saves the current menu to NaturalDocs_Menu.txt

WriteMenuEntries

sub WriteMenuEntries #(entries,
fileHandle,
indentChars)

A recursive function to write the contents of an arrayref of NaturalDocs::Menu::Entry objects to disk. 

Parameters

entriesThe arrayref of menu entries to write. 
fileHandleThe handle to the output file. 
indentCharsThe indentation characters to add before each line.  It is not the number of characters, it is the characters themselves.  Use undef for none. 

LoadPreviousMenuStateFile

sub LoadPreviousMenuStateFile

Loads and parses the previous menu state file. 

Note that this is not affected by NaturalDocs::Settings::RebuildData().  Since this is used to detect user changes, the information here can’t be ditched on a whim. 

Returns

The array ( previousMenu, previousIndexes, previousFiles ). 

previousMenuA MENU_GROUP NaturalDocs::Menu::Entry object, similar to menu, which contains the entire previous menu. 
previousIndexesAn existence hashref of the indexes present in the previous menu.  The keys are the Topic Types or ‘*’ for general. 
previousFilesA hashref of the files present in the previous menu.  The keys are the file names, and the entries are references to its object in previousMenu. 

If there is no data available on a topic, it will be undef.  For example, if the file didn’t exist, all three will be undef.  If the file was from 0.95 or earlier, previousIndexes will be set but the other two would be undef. 

SavePreviousMenuStateFile

sub SavePreviousMenuStateFile

Saves changes to NaturalDocs.m

WritePreviousMenuStateEntries

sub WritePreviousMenuStateEntries #(entries,
fileHandle)

A recursive function to write the contents of an arrayref of NaturalDocs::Menu::Entry objects to disk. 

Parameters

entriesThe arrayref of menu entries to write. 
fileHandleThe handle to the output file. 

HandleErrors

sub HandleErrors #(errors)

Handles errors appearing in the menu file. 

Parameters

errorsAn arrayref of the errors as NaturalDocs::Menu::Error objects. 

CheckForTrashedMenu

sub CheckForTrashedMenu #(numberOriginallyInMenu,
numberRemoved)

Checks the menu to see if a significant number of file entries didn’t resolve to actual files, and if so, saves a backup of the menu and issues a warning. 

Parameters

numberOriginallyInMenuA count of how many file entries were in the menu orignally. 
numberRemovedA count of how many file entries were removed from the menu. 

Auto-Adjustment Functions

LockUserTitleChanges

sub LockUserTitleChanges #(previousMenuFiles)

Detects if the user manually changed any file titles, and if so, automatically locks them with MENU_FILE_NOAUTOTITLE

Parameters

previousMenuFilesA hashref of the files from the previous menu state.  The keys are the file names, and the values are references to their NaturalDocs::Menu::Entry objects. 

FlagAutoTitleChanges

sub FlagAutoTitleChanges

Finds which files have auto-titles that changed and flags their groups for updating with MENU_GROUP_UPDATETITLES and MENU_GROUP_UPDATEORDER

AutoPlaceNewFiles

sub AutoPlaceNewFiles #(fileInMenu)

Adds files to the menu that aren’t already on it, attempting to guess where they belong. 

New files are placed after a dummy MENU_ENDOFORIGINAL entry so that they don’t affect the detected order.  Also, the groups they’re placed in get MENU_GROUP_UPDATETITLES, MENU_GROUP_UPDATESTRUCTURE, and MENU_GROUP_UPDATEORDER flags. 

Parameters

filesInMenuAn existence hash of all the files present in the menu. 

MatchDirectoriesAndGroups

sub MatchDirectoriesAndGroups

Determines which groups files in certain directories should be placed in. 

Returns

A hashref.  The keys are the directory names, and the values are references to the group objects they should be placed in. 

This only repreesents directories that currently have files on the menu, so it shouldn’t be assumed that every possible directory will exist.  To match, you should first try to match the directory, and then strip the deepest directories one by one until there’s a match or there’s none left.  If there’s none left, use the root group menu

RemoveDeadFiles

sub RemoveDeadFiles

Removes files from the menu that no longer exist or no longer have Natural Docs content. 

Returns

The number of file entries removed. 

RemoveDeadGroups

sub RemoveDeadGroups

Removes groups with less than two entries.  It will always remove empty groups, and it will remove groups with one entry if it has the MENU_GROUP_UPDATESTRUCTURE flag. 

RemoveIfDead

sub RemoveIfDead #(groupEntry,
parentGroupEntry,
parentGroupIndex)

Checks a group and all its sub-groups for life and remove any that are dead.  Empty groups are removed, and groups with one entry and the MENU_GROUP_UPDATESTRUCTURE flag have their entry moved to the parent group. 

Parameters

groupEntryThe group to check for possible deletion. 
parentGroupEntryThe parent group to move the single entry to if necessary. 
parentGroupIndexThe index of the group in its parent. 

Returns

Whether the group was removed or not. 

CreateDirectorySubGroups

sub CreateDirectorySubGroups

Where possible, creates sub-groups based on directories for any long groups that have MENU_GROUP_UPDATESTRUCTURE set.  Clears the flag afterwards on groups that are short enough to not need any more sub-groups, but leaves it for the rest. 

CreatePrefixSubGroups

sub CreatePrefixSubGroups

Where possible, creates sub-groups based on name prefix for any long groups that have the MENU_GROUP_UPDATESTRUCTURE flag set.  Clears the flag for any group that becomes short enough to not need any more sub-groups, but leaves it for the rest. 

Important

This function isn’t ready for prime time yet.  While it works somewhat on its own, it needs to be improved and to integrate better with the directory sub-groups.  Probably most importantly, AutoPlaceNewFiles() needs to be aware of it. 

DetectOrder

sub DetectOrder #(forceAll)

Detects the order of the entries in all groups that have the MENU_GROUP_UPDATEORDER flag set.  Will set one of the MENU_GROUP_FILESSORTED, MENU_GROUP_FILESANDGROUPSSORTED, MENU_GROUP_EVERYTHINGSORTED, or MENU_GROUP_UNSORTED flags.  It will always go for the most comprehensive sort possible, so if a group only has one entry, it will be flagged as MENU_GROUP_EVERYTHINGSORTED

The sort detection stops if it reaches a MENU_ENDOFORIGINAL entry, so new entries can be added to the end while still allowing the original sort to be detected. 

Parameters

forceAllIf set, the order will be detected for all groups regardless of whether MENU_GROUP_UPDATEORDER is set. 

GenerateAutoFileTitles

sub GenerateAutoFileTitles #(forceAll)

Creates titles for the unlocked file entries in all groups that have the MENU_GROUP_UPDATETITLES flag set.  It clears the flag afterwards so it can be used efficiently for multiple sweeps. 

Parameters

forceAllIf set, forces all the unlocked file titles to update regardless of whether the group has the MENU_GROUP_UPDATETITLES flag set. 

ResortGroups

sub ResortGroups #(forceAll)

Resorts all groups that have MENU_GROUP_UPDATEORDER set.  Assumes DetectOrder() and GenerateAutoFileTitles() have already been called.  Will clear the flag and any MENU_ENDOFORIGINAL entries on reordered groups. 

Parameters

forceAllIf set, resorts all groups regardless of whether MENU_GROUP_UPDATEORDER is set. 

CompareEntries

sub CompareEntries #(a,
b)

A comparison function for use in sorting.  Compares the two entries by their titles with StringCompare(), but in the case of a tie, puts MENU_FILE entries above MENU_GROUP entries. 

SharedDirectoriesOf

sub SharedDirectoriesOf #(group)

Returns an array of all the directories shared by the files in the group.  If none, returns an empty array.