Inheritance: IXRefContainer
Example #1
0
 public string CreateMinor(XRefMap map, IEnumerable <string> names)
 {
     if (map == null)
     {
         throw new ArgumentNullException(nameof(map));
     }
     if (_mode == XRefArchiveMode.Read)
     {
         throw new InvalidOperationException("Cannot create entry for readonly archive.");
     }
     if (names != null)
     {
         foreach (var name in names)
         {
             var entryName = NormalizeName(name);
             if (entryName != null &&
                 HasEntryCore(entryName))
             {
                 return(CreateCore(entryName, map));
             }
         }
     }
     while (true)
     {
         var entryName = Guid.NewGuid().ToString() + ".yml";
         if (HasEntryCore(entryName))
         {
             return(CreateCore(entryName, map));
         }
     }
 }
Example #2
0
        private string CreateCore(string name, XRefMap map)
        {
            var entry = CreateEntry(name);

            using (var sw = new StreamWriter(entry.Open()))
            {
                YamlUtility.Serialize(sw, map);
            }
            return(name);
        }
Example #3
0
        private static IEnumerable <string> GetNames(Uri uri, XRefMap map)
        {
            var name = uri.Segments.LastOrDefault();

            yield return(name);

            if (map.References?.Count > 0)
            {
                yield return(map.References[0].Uid);
            }
        }
Example #4
0
        private void UpdateCore(string name, XRefMap map)
        {
            var entry = _archive.GetEntry(name);

            entry.Delete();
            entry = _archive.CreateEntry(name);
            using (var sw = new StreamWriter(entry.Open()))
            {
                YamlUtility.Serialize(sw, map);
            }
        }
Example #5
0
 public BasicXRefMapReader(XRefMap map)
 {
     Map = map ?? throw new ArgumentNullException(nameof(map));
     if (map.HrefUpdated != true &&
         map.BaseUrl != null)
     {
         if (!Uri.TryCreate(map.BaseUrl, UriKind.Absolute, out Uri baseUri))
         {
             throw new InvalidDataException($"Xref map file has an invalid base url: {map.BaseUrl}.");
         }
         map.UpdateHref(baseUri);
     }
 }
Example #6
0
 public string CreateMajor(XRefMap map)
 {
     if (map == null)
     {
         throw new ArgumentNullException(nameof(map));
     }
     if (_mode == XRefArchiveMode.Read)
     {
         throw new InvalidOperationException("Cannot create entry for readonly archive.");
     }
     if (HasEntryCore(MajorFileName))
     {
         throw new InvalidOperationException("Major entry existed.");
     }
     return(CreateCore(MajorFileName, map));
 }
Example #7
0
        /// <summary>
        /// Export xref map file.
        /// </summary>
        private static void ExportXRefMap(DocumentBuildParameters parameters, DocumentBuildContext context)
        {
            Logger.LogVerbose("Exporting xref map...");
            var xrefMap = new XRefMap();

            xrefMap.References =
                (from xref in context.XRefSpecMap.Values.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                 select new XRefSpec(xref)
            {
                Href = ((RelativePath)context.FileMap[xref.Href]).RemoveWorkingFolder().ToString() + "#" + XRefDetails.GetHtmlId(xref.Uid),
            }).ToList();
            xrefMap.Sort();
            YamlUtility.Serialize(
                Path.Combine(parameters.OutputBaseDir, XRefMapFileName),
                xrefMap);
            Logger.LogInfo("XRef map exported.");
        }
Example #8
0
 public static void UpdateHref(XRefMap map, Uri uri)
 {
     if (!string.IsNullOrEmpty(map.BaseUrl))
     {
         if (!Uri.TryCreate(map.BaseUrl, UriKind.Absolute, out Uri baseUri))
         {
             throw new InvalidDataException($"Xref map file (from {uri.AbsoluteUri}) has an invalid base url: {map.BaseUrl}.");
         }
         map.UpdateHref(baseUri);
         return;
     }
     if (uri.Scheme == "http" || uri.Scheme == "https")
     {
         map.UpdateHref(uri);
         return;
     }
     throw new InvalidDataException($"Xref map file (from {uri.AbsoluteUri}) missing base url.");
 }
Example #9
0
            public async Task <IXRefContainerReader> CreateAsync()
            {
                AddToDownloadList(_uris);
                var dict = new Dictionary <string, IXRefContainer>();

                while (_processing.Count > 0)
                {
                    var task = await Task.WhenAny(_processing.Keys);

                    var uri = _processing[task];
                    _processing.Remove(task);
                    try
                    {
                        var container = await task;
                        if (!container.IsEmbeddedRedirections)
                        {
                            AddToDownloadList(
                                from r in container.GetRedirections()
                                where r != null
                                select GetUri(uri, r.Href) into u
                                where u != null
                                select u);
                        }
                        dict[uri.IsAbsoluteUri ? uri.AbsoluteUri : uri.OriginalString] = container;
                    }
                    catch (Exception ex)
                    {
                        Logger.LogWarning($"Unable to download xref map file from {uri}, details: {ex.Message}");
                    }
                }
                var fakeEntry = Guid.NewGuid().ToString();

                dict[fakeEntry] = new XRefMap
                {
                    HrefUpdated  = true,
                    Redirections = (from pair in dict
                                    select new XRefMapRedirection
                    {
                        Href = pair.Key,
                    }).ToList()
                };
                return(new XRefMapReader(fakeEntry, dict));
            }
Example #10
0
        /// <summary>
        /// Export xref map file.
        /// </summary>
        private static string ExportXRefMap(DocumentBuildParameters parameters, DocumentBuildContext context)
        {
            Logger.LogVerbose("Exporting xref map...");
            var xrefMap = new XRefMap();

            xrefMap.References =
                (from xref in context.XRefSpecMap.Values.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                 select new XRefSpec(xref)
            {
                Href = context.UpdateHref(xref.Href, RelativePath.WorkingFolder)
            }).ToList();
            xrefMap.Sort();
            string xrefMapFileNameWithVersion = GetXrefMapFileNameWithGroup(parameters);

            YamlUtility.Serialize(
                Path.GetFullPath(Environment.ExpandEnvironmentVariables(Path.Combine(parameters.OutputBaseDir, xrefMapFileNameWithVersion))),
                xrefMap,
                YamlMime.XRefMap);
            Logger.LogInfo("XRef map exported.");
            return(xrefMapFileNameWithVersion);
        }
Example #11
0
 public async Task<IXRefContainerReader> CreateAsync()
 {
     AddToDownloadList(_uris);
     var dict = new Dictionary<string, IXRefContainer>();
     while (_processing.Count > 0)
     {
         var task = await Task.WhenAny(_processing.Keys);
         var uri = _processing[task];
         _processing.Remove(task);
         try
         {
             var container = await task;
             if (!container.IsEmbeddedRedirections)
             {
                 AddToDownloadList(
                     from r in container.GetRedirections()
                     where r != null
                     select GetUri(uri, r.Href) into u
                     where u != null
                     select u);
             }
             dict[uri.IsAbsoluteUri ? uri.AbsoluteUri : uri.OriginalString] = container;
         }
         catch (Exception ex)
         {
             Logger.LogWarning($"Unable to download xref map file from {uri}, details: {ex.Message}");
         }
     }
     var fakeEntry = Guid.NewGuid().ToString();
     dict[fakeEntry] = new XRefMap
     {
         HrefUpdated = true,
         Redirections = (from pair in dict
                         select new XRefMapRedirection
                         {
                             Href = pair.Key,
                         }).ToList()
     };
     return new XRefMapReader(fakeEntry, dict);
 }
Example #12
0
        public void Update(string name, XRefMap map)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (map == null)
            {
                throw new ArgumentNullException(nameof(map));
            }
            if (_mode == XRefArchiveMode.Read)
            {
                throw new InvalidOperationException("Cannot create entry for readonly archive.");
            }
            var entryName = GetEntry(name);

            if (entryName == null)
            {
                throw new InvalidOperationException($"Entry {name} not found.");
            }
            UpdateCore(name, map);
        }
Example #13
0
        private static async Task <XRefMap> DownloadBySchemeAsync(Uri uri)
        {
            XRefMap result = null;

            if (uri.IsFile)
            {
                result = DownloadFromLocal(uri);
            }
            else if (uri.Scheme == "http" || uri.Scheme == "https" || uri.Scheme == "ftp")
            {
                result = await DownloadFromWebAsync(uri);
            }
            else
            {
                throw new ArgumentException($"Unsupported scheme {uri.Scheme}, expected: http, https, ftp, file.", nameof(uri));
            }
            if (result == null)
            {
                throw new InvalidDataException($"Invalid yaml file from {uri}.");
            }
            return(result);
        }
        /// <summary>
        /// Export xref map file.
        /// </summary>
        private static string ExportXRefMap(DocumentBuildParameters parameters, DocumentBuildContext context)
        {
            Logger.LogVerbose("Exporting xref map...");
            var xrefMap = new XRefMap();

            xrefMap.References =
                (from xref in context.XRefSpecMap.Values.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
                 select new XRefSpec(xref)
            {
                Href = ((RelativePath)context.GetFilePath(UriUtility.GetNonFragment(xref.Href))).RemoveWorkingFolder() + UriUtility.GetFragment(xref.Href)
            }).ToList();
            xrefMap.Sort();
            string xrefMapFileNameWithVersion = string.IsNullOrEmpty(parameters.VersionName) ?
                                                XRefMapFileName :
                                                parameters.VersionName + "." + XRefMapFileName;

            YamlUtility.Serialize(
                xrefMapFileNameWithVersion,
                xrefMap,
                YamlMime.XRefMap);
            Logger.LogInfo("XRef map exported.");
            return(xrefMapFileNameWithVersion);
        }
Example #15
0
 public void UpdateMajor(XRefMap map) => Update(MajorFileName, map);
Example #16
0
 private string CreateCore(string name, XRefMap map)
 {
     var entry = CreateEntry(name);
     using (var sw = new StreamWriter(entry.Open()))
     {
         YamlUtility.Serialize(sw, map, YamlMime.XRefMap);
     }
     return name;
 }
Example #17
0
 public BasicXRefMapReader(XRefMap map)
 {
     Map = map ?? throw new ArgumentNullException(nameof(map));
 }
Example #18
0
 private void UpdateCore(string name, XRefMap map)
 {
     var entry = _archive.GetEntry(name);
     entry.Delete();
     entry = _archive.CreateEntry(name);
     using (var sw = new StreamWriter(entry.Open()))
     {
         YamlUtility.Serialize(sw, map, YamlMime.XRefMap);
     }
 }
Example #19
0
 public string CreateMajor(XRefMap map)
 {
     if (map == null)
     {
         throw new ArgumentNullException(nameof(map));
     }
     if (_mode == XRefArchiveMode.Read)
     {
         throw new InvalidOperationException("Cannot create entry for readonly archive.");
     }
     if (HasEntryCore(MajorFileName))
     {
         throw new InvalidOperationException("Major entry existed.");
     }
     return CreateCore(MajorFileName, map);
 }
Example #20
0
 /// <summary>
 /// Export xref map file.
 /// </summary>
 private static string ExportXRefMap(DocumentBuildParameters parameters, DocumentBuildContext context)
 {
     Logger.LogVerbose("Exporting xref map...");
     var xrefMap = new XRefMap();
     xrefMap.References =
         (from xref in context.XRefSpecMap.Values.AsParallel().WithDegreeOfParallelism(parameters.MaxParallelism)
          select new XRefSpec(xref)
          {
              Href = ((RelativePath)context.FileMap[UriUtility.GetNonFragment(xref.Href)]).RemoveWorkingFolder() + UriUtility.GetFragment(xref.Href)
          }).ToList();
     xrefMap.Sort();
     string xrefMapFileNameWithVersion = string.IsNullOrEmpty(parameters.VersionName) ?
         XRefMapFileName :
         parameters.VersionName + "." + XRefMapFileName;
     YamlUtility.Serialize(
         Path.Combine(parameters.OutputBaseDir, xrefMapFileNameWithVersion),
         xrefMap,
         YamlMime.XRefMap);
     Logger.LogInfo("XRef map exported.");
     return xrefMapFileNameWithVersion;
 }
Example #21
0
 public void UpdateMajor(XRefMap map) => Update(MajorFileName, map);
Example #22
0
 public static void UpdateHref(XRefMap map, Uri uri)
 {
     if (!string.IsNullOrEmpty(map.BaseUrl))
     {
         Uri baseUri;
         if (!Uri.TryCreate(map.BaseUrl, UriKind.Absolute, out baseUri))
         {
             throw new InvalidDataException($"Xref map file (from {uri.AbsoluteUri}) has an invalid base url: {map.BaseUrl}.");
         }
         map.UpdateHref(baseUri);
         return;
     }
     if (uri.Scheme == "http" || uri.Scheme == "https")
     {
         map.UpdateHref(uri);
         return;
     }
     throw new InvalidDataException($"Xref map file (from {uri.AbsoluteUri}) missing base url.");
 }
Example #23
0
 public BasicXRefMapReader(XRefMap map)
 {
     Map = map;
 }
Example #24
0
 public void Update(string name, XRefMap map)
 {
     if (name == null)
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (map == null)
     {
         throw new ArgumentNullException(nameof(map));
     }
     if (_mode == XRefArchiveMode.Read)
     {
         throw new InvalidOperationException("Cannot create entry for readonly archive.");
     }
     var entryName = GetEntry(name);
     if (entryName == null)
     {
         throw new InvalidOperationException($"Entry {name} not found.");
     }
     UpdateCore(name, map);
 }
Example #25
0
 private async Task <List <XRefMapRedirection> > RewriteRedirections(Uri uri, XRefArchive xa, XRefMap map) =>
 (from list in
  await Task.WhenAll(
      from r in map.Redirections
          where !string.IsNullOrEmpty(r.Href) || !string.IsNullOrEmpty(r.UidPrefix)
      group r by r.Href into g
      let href = GetHrefUri(uri, g.Key)
                     where href != null
                 select RewriteRedirectionsCore(g.ToList(), href, xa))
  from r in list
  orderby r.UidPrefix.Length descending, r.UidPrefix
  select r).ToList();
Example #26
0
 private static IEnumerable<string> GetNames(Uri uri, XRefMap map)
 {
     var name = uri.Segments.LastOrDefault();
     yield return name;
     if (map.References?.Count > 0)
     {
         yield return map.References[0].Uid;
     }
 }
Example #27
0
 public BasicXRefMapReader(XRefMap map)
 {
     Map = map;
 }
Example #28
0
 private async Task<List<XRefMapRedirection>> RewriteRedirections(Uri uri, XRefArchive xa, XRefMap map) =>
     (from list in
         await Task.WhenAll(
             from r in map.Redirections
             where !string.IsNullOrEmpty(r.Href)
             group r by r.Href into g
             let href = GetHrefUri(uri, g.Key)
             where href != null
             select RewriteRedirectionsCore(g.ToList(), href, xa))
      from r in list
      orderby (r.UidPrefix ?? string.Empty).Length descending, (r.UidPrefix ?? string.Empty)
      select r).ToList();
Example #29
0
 public string CreateMinor(XRefMap map, IEnumerable<string> names)
 {
     if (map == null)
     {
         throw new ArgumentNullException(nameof(map));
     }
     if (_mode == XRefArchiveMode.Read)
     {
         throw new InvalidOperationException("Cannot create entry for readonly archive.");
     }
     if (names != null)
     {
         foreach (var name in names)
         {
             var entryName = NormalizeName(name);
             if (entryName != null &&
                 !HasEntryCore(entryName))
             {
                 return CreateCore(entryName, map);
             }
         }
     }
     while (true)
     {
         var entryName = Guid.NewGuid().ToString() + ".yml";
         if (!HasEntryCore(entryName))
         {
             return CreateCore(entryName, map);
         }
     }
 }