Copyright 2021 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package frontend

import (
	
	
	

	
	
)
Directories is the directory listing for all directories in the unit, which is listed in the directories section of the main page.
External contains all of the non-internal directories for the unit.
Internal contains the top level internal directory for the unit, if any.
Directory is either a nested module or subdirectory of a unit, organized in a two level tree structure. This content is used in the directories section of the unit page.
Prefix is the prefix of the unit path for the subdirectories.
Root is the package located at prefix, nil for a directory.
Subdirectories contains subdirectories with prefix trimmed from their suffix.
DirectoryInfo contains information about a package or nested module, relative to the path of a given unit. This content is used in the Directories section of the unit page.
unitDirectories zips the subdirectories and nested modules together in a two level tree hierarchy.
func ( []*DirectoryInfo) *Directories {
	if len() == 0 {
		return nil
Organize the subdirectories into a two level tree hierarchy. The first part of the unit path suffix for a subdirectory becomes the prefix under which matching subdirectories are grouped.
	 := make(map[string]*Directory)
	for ,  := range  {
		 := strings.SplitN(.Suffix, "/", 2)[0]
Skip internal directories that are not in the top level internal directory. For example, foo/internal and foo/internal/bar should be skipped, but internal/foo should be included.
		if  != "internal" && (strings.HasSuffix(.Suffix, "/internal") ||
			strings.Contains(.Suffix, "/internal/")) {
			continue
		}
		if ,  := []; ! {
			[] = &Directory{Prefix: }
		}
		.Suffix = strings.TrimPrefix(.Suffix, +"/")
		if  == .Suffix {
			[].Root = 
		} else {
			[].Subdirectories = append([].Subdirectories, )
		}
	}

	 := &Directories{}
	for ,  := range  {
		if  == "internal" {
			.Internal = 
		} else {
			.External = append(.External, )
		}
	}
	sort.Slice(.External, func(,  int) bool {
		return .External[].Prefix < .External[].Prefix
	})
	return 
}

func ( context.Context,  internal.DataSource,  *internal.UnitMeta,  []*DirectoryInfo) ([]*DirectoryInfo, error) {
	,  := .GetNestedModules(, .ModulePath)
	if  != nil {
		return nil, 
Build a map of existing suffixes in subdirectories to filter out nested modules which have the same suffix.
	 := make(map[string]bool)
	for ,  := range  {
		[.Suffix] = true
	}
	var  []*DirectoryInfo
	for ,  := range  {
		if .SeriesPath() == internal.SeriesPathForModule(.ModulePath) {
			continue
		}
		if !strings.HasPrefix(.ModulePath, .Path+"/") {
			continue
		}
		 := internal.Suffix(.SeriesPath(), .Path)
		if [] {
			continue
		}
		 = append(, &DirectoryInfo{
			URL:      constructUnitURL(.ModulePath, .ModulePath, internal.LatestVersion),
			Suffix:   ,
			IsModule: true,
		})
	}
	return , nil
}

func ( *internal.UnitMeta,  []*internal.PackageMeta) []*DirectoryInfo {
	var  []*DirectoryInfo
	for ,  := range  {
		if .Path == .Path {
			continue
		}
Omit "cmd" from the directory listing on pkg.go.dev/std, since go list std does not list them.
			continue
		}
		 = append(, &DirectoryInfo{
			URL:      constructUnitURL(.Path, .ModulePath, linkVersion(.Version, .ModulePath)),
			Suffix:   internal.Suffix(.Path, .Path),
			Synopsis: .Synopsis,
		})
	}
	sort.Slice(, func(,  int) bool { return [].Suffix < [].Suffix })
	return