MakeRelativePath() public static method

Creates a relative path to get from fromPath to toPath. If one of them is empty, the emptystring is returned. If there's no common path, toPath is returned.
Only works with file paths, which is ok, as it's used to create the {{Path}} macro.
public static MakeRelativePath ( string fromPath, string toPath ) : string
fromPath string From path.
toPath string To path.
return string
Example #1
0
        private NavigationLevel CreateGeneratedLevel(string path)
        {
            var root = new NavigationLevel(_rootDirectory)
            {
                ParentContainer = this,
                IsAutoGenerated = true
            };

            foreach (var mdFile in Directory.GetFiles(path, "*.md", SearchOption.TopDirectoryOnly))
            {
                var name = FindTitleInMdFile(mdFile);
                if (string.IsNullOrWhiteSpace(name))
                {
                    continue;
                }

                var item = new SimpleNavigationElement
                {
                    Name            = name,
                    Value           = Path.Combine(Utils.MakeRelativePath(_rootDirectory, path), Path.GetFileName(mdFile)),
                    ParentContainer = root,
                    IsAutoGenerated = true
                };

                root.Value.Add(item);
            }

            foreach (var directory in Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly))
            {
                var subDirectoryNavigationElement = CreateGeneratedLevel(directory);
                subDirectoryNavigationElement.Name            = new DirectoryInfo(directory).Name;
                subDirectoryNavigationElement.ParentContainer = root;

                root.Value.Add(subDirectoryNavigationElement);
            }

            return(root);
        }
Example #2
0
 /// <summary>
 /// As <see cref="MakeRelativePath"/> but it also converts '\' to '/'.
 /// </summary>
 /// <param name="fromPath">From path.</param>
 /// <param name="toPath">To path.</param>
 /// <returns></returns>
 /// <remarks>Only works with file paths, which is ok, as it's used to create the {{Path}} macro.</remarks>
 public static string MakeRelativePathForUri(string fromPath, string toPath)
 {
     return(Utils.MakeRelativePath(fromPath, toPath).Replace(@"\", @"/"));
 }
Example #3
0
        /// <summary>
        /// Generates the output for this navigation element
        /// </summary>
        /// <param name="activeConfig">The active configuration to use for the output.</param>
        /// <param name="activePath">The active path navigated through the ToC to reach this element.</param>
        public override void GenerateOutput(Config activeConfig, NavigatedPath activePath)
        {
            // if we're the __index element, we're not pushing ourselves on the path, as we're representing the container we're in, which is already on the path.
            if (!this.IsIndexElement)
            {
                activePath.Push(this);
            }
            _relativeH2LinksOnPage.Clear();
            var sourceFile      = Utils.MakeAbsolutePath(activeConfig.Source, this.Value);
            var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.TargetURL);
            var sb      = new StringBuilder(activeConfig.PageTemplateContents.Length + 2048);
            var content = string.Empty;

            this.MarkdownFromFile = string.Empty;
            if (File.Exists(sourceFile))
            {
                this.MarkdownFromFile = File.ReadAllText(sourceFile);
                content = Utils.ConvertMarkdownToHtml(this.MarkdownFromFile, _relativeH2LinksOnPage);
            }
            else
            {
                // if we're not the index element, the file is missing and potentially it's an error in the config page.
                // Otherwise we can simply assume we are a missing index page and we'll generate default markdown so the user has something to look at.
                if (this.IsIndexElement)
                {
                    // replace with default markdown snippet. This is the name of our container and links to the elements in that container as we are the index page that's not
                    // specified / existend.
                    var defaultMarkdown = new StringBuilder();
                    defaultMarkdown.AppendFormat("# {0}{1}{1}", this.ParentContainer.Name, Environment.NewLine);
                    defaultMarkdown.AppendFormat("Please select one of the topic in this section:{0}{0}", Environment.NewLine);
                    foreach (var sibling in this.ParentContainer.Value)
                    {
                        if (sibling == this)
                        {
                            continue;
                        }
                        defaultMarkdown.AppendFormat("* [{0}]({1}){2}", sibling.Name, HttpUtility.UrlPathEncode(sibling.TargetURL), Environment.NewLine);
                    }
                    defaultMarkdown.Append(Environment.NewLine);
                    content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), _relativeH2LinksOnPage);
                }
                else
                {
                    // target not found. See if there's a content producer func to produce html for us. If not, we can only conclude an error in the config file.
                    if (this.ContentProducerFunc == null)
                    {
                        throw new FileNotFoundException(string.Format("The specified markdown file '{0}' couldn't be found. Aborting", sourceFile));
                    }
                    content = this.ContentProducerFunc(this);
                }
            }
            sb.Append(activeConfig.PageTemplateContents);
            sb.Replace("{{Name}}", activeConfig.Name);
            sb.Replace("{{Footer}}", activeConfig.Footer);
            sb.Replace("{{TopicTitle}}", this.Name);
            sb.Replace("{{Content}}", content);
            var relativePathToRoot = Utils.MakeRelativePath(Path.GetDirectoryName(destinationFile), activeConfig.Destination).Replace(@"\", @"/");

            sb.Replace("{{Path}}", relativePathToRoot);
            sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot));
            sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot));
            if (this.ExtraScriptProducerFunc != null)
            {
                sb.Replace("{{ExtraScript}}", this.ExtraScriptProducerFunc(this));
            }
            Utils.CreateFoldersIfRequired(destinationFile);
            File.WriteAllText(destinationFile, sb.ToString());
            if (!this.IsIndexElement)
            {
                activePath.Pop();
            }
        }