Skip to content

Commit

Permalink
chore: drop nearest TOC login in BuildTocDocument
Browse files Browse the repository at this point in the history
The toc_rel logic is in SystemMetadataGenerator.GetNearestToc. This piece of logic was for dependency map only.
  • Loading branch information
yufeih committed Nov 13, 2023
1 parent 3505f66 commit 7d482d7
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 163 deletions.
157 changes: 1 addition & 156 deletions src/Docfx.Build/TableOfContents/BuildTocDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public class BuildTocDocument : BaseDocumentBuildStep
/// </summary>
public override IEnumerable<FileModel> Prebuild(ImmutableList<FileModel> models, IHostService host)
{
var (resolvedTocModels, includedTocs) = TocHelper.ResolveToc(models, host);

ReportPreBuildDependency(resolvedTocModels, host, 8, includedTocs);

return resolvedTocModels;
return TocHelper.ResolveToc(models, host);
}

public override void Build(FileModel model, IHostService host)
Expand Down Expand Up @@ -106,155 +102,4 @@ private static LinkSourceInfo GetLinkSourceInfo(string path, string anchor, stri
Target = path,
};
}

private static void ReportPreBuildDependency(List<FileModel> models, IHostService host, int parallelism, HashSet<string> includedTocs)
{
var nearest = new ConcurrentDictionary<string, RelativeInfo>(FilePathComparer.OSPlatformSensitiveStringComparer);
models.RunAll(model =>
{
var item = (TocItemViewModel)model.Content;
UpdateNearestToc(host, item, model, nearest);
},
parallelism);

// handle not-in-toc items
UpdateNearestTocForNotInTocItem(models, host, nearest, parallelism);
}

private static void UpdateNearestToc(IHostService host, TocItemViewModel item, FileModel toc, ConcurrentDictionary<string, RelativeInfo> nearest)
{
var tocHref = item.TocHref;
var type = Utility.GetHrefType(tocHref);
if (type == HrefType.MarkdownTocFile || type == HrefType.YamlTocFile)
{
UpdateNearestTocCore(host, UriUtility.GetPath(tocHref), toc, nearest);
}
else if (item.TopicUid == null && Utility.IsSupportedRelativeHref(item.Href))
{
UpdateNearestTocCore(host, UriUtility.GetPath(item.Href), toc, nearest);
}

if (item.Items != null && item.Items.Count > 0)
{
foreach (var i in item.Items)
{
UpdateNearestToc(host, i, toc, nearest);
}
}
}

private static void UpdateNearestTocForNotInTocItem(List<FileModel> models, IHostService host, ConcurrentDictionary<string, RelativeInfo> nearest, int parallelism)
{
var allSourceFiles = host.SourceFiles;
var tocInfos = models.Select(m => new TocInfo(m)).ToArray();
Parallel.ForEach(
EnumerateNotInTocArticles(),
new ParallelOptions { MaxDegreeOfParallelism = parallelism },
item =>
{
var near = (from tocInfo in tocInfos
let rel = new RelativeInfo(tocInfo, allSourceFiles[item])
where rel.TocPathRelativeToArticle.SubdirectoryCount == 0
orderby rel.TocPathRelativeToArticle.ParentDirectoryCount
select rel).FirstOrDefault();
if (near != null)
{
nearest[item] = near;
}
});

IEnumerable<string> EnumerateNotInTocArticles()
{
return from pair in allSourceFiles
where pair.Value.Type == DocumentType.Article && !nearest.ContainsKey(pair.Key)
select pair.Key;
}
}

private static void UpdateNearestTocCore(IHostService host, string item, FileModel toc, ConcurrentDictionary<string, RelativeInfo> nearest)
{
if (!host.SourceFiles.TryGetValue(item, out var itemSource))
{
return;
}

var tocInfo = new RelativeInfo(toc, itemSource);
nearest.AddOrUpdate(
item,
k => tocInfo,
(k, v) => Compare(tocInfo, v) < 0 ? tocInfo : v);
}

private static RelativePath GetOutputPath(FileAndType file)
{
if (file.SourceDir != file.DestinationDir)
{
return (RelativePath)file.DestinationDir + (((RelativePath)file.File) - (RelativePath)file.SourceDir);
}
else
{
return (RelativePath)file.File;
}
}

private static int Compare(RelativeInfo infoA, RelativeInfo infoB)
{
var relativePathA = infoA.TocPathRelativeToArticle;
var relativePathB = infoB.TocPathRelativeToArticle;

int subDirCompareResult = relativePathA.SubdirectoryCount - relativePathB.SubdirectoryCount;
if (subDirCompareResult != 0)
{
return subDirCompareResult;
}

int parentDirCompareResult = relativePathA.ParentDirectoryCount - relativePathB.ParentDirectoryCount;
if (parentDirCompareResult != 0)
{
return parentDirCompareResult;
}

var tocA = infoA.TocInfo;
var tocB = infoB.TocInfo;

var outputPathCompareResult = StringComparer.OrdinalIgnoreCase.Compare(tocA.OutputPath, tocB.OutputPath);
if (outputPathCompareResult != 0)
{
return outputPathCompareResult;
}

return StringComparer.OrdinalIgnoreCase.Compare(tocA.FilePath, tocB.FilePath);
}

private class TocInfo
{
public FileModel Model { get; set; }

public string FilePath { get; set; }

public RelativePath OutputPath { get; set; }

public TocInfo(FileModel tocModel)
{
Model = tocModel;
FilePath = tocModel.FileAndType.File;
OutputPath = GetOutputPath(tocModel.FileAndType);
}
}

private class RelativeInfo
{
public TocInfo TocInfo { get; set; }

public RelativePath TocPathRelativeToArticle { get; set; }

public RelativeInfo(TocInfo tocInfo, FileAndType article)
{
TocInfo = tocInfo;
TocPathRelativeToArticle = tocInfo.OutputPath.RemoveWorkingFolder().MakeRelativeTo(GetOutputPath(article));
}

public RelativeInfo(FileModel tocModel, FileAndType article)
: this(new TocInfo(tocModel), article) { }
}
}
9 changes: 2 additions & 7 deletions src/Docfx.Build/TableOfContents/TocHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public static class TocHelper
YamlDeserializerWithFallback.Create<TocViewModel>()
.WithFallback<TocRootViewModel>();

public static (List<FileModel> tocModels, HashSet<string> includedTocs) ResolveToc(ImmutableList<FileModel> models, IHostService host)
public static List<FileModel> ResolveToc(ImmutableList<FileModel> models, IHostService host)
{
var tocCache = new Dictionary<string, TocItemInfo>(FilePathComparer.OSPlatformSensitiveStringComparer);
var nonReferencedTocModels = new List<FileModel>();
var referencedToc = new HashSet<string>(FilePathComparer.OSPlatformSensitiveStringComparer);

foreach (var model in models)
{
Expand All @@ -40,13 +39,9 @@ public static (List<FileModel> tocModels, HashSet<string> includedTocs) ResolveT
model.Content = tocItemInfo.Content;
nonReferencedTocModels.Add(model);
}
else
{
referencedToc.Add(model.Key);
}
}

return (nonReferencedTocModels, referencedToc);
return nonReferencedTocModels;
}

public static TocItemViewModel LoadSingleToc(string file)
Expand Down

0 comments on commit 7d482d7

Please sign in to comment.