private List <MarkdownFile> ReadOrderFiles(string path)
        {
            //read the .order file
            //if there is an entry and a folder with the same name, dive deeper
            var directory = new DirectoryInfo(Path.GetFullPath(path));

            Log($"Reading .order file in directory {directory.Name}");
            var orderFiles = directory.GetFiles(".order", SearchOption.TopDirectoryOnly);

            var result = new List <MarkdownFile>();

            foreach (var orderFile in orderFiles)
            {
                var orders       = File.ReadAllLines(orderFile.FullName);
                var relativePath = orderFile.Directory.FullName.Substring(directory.FullName.Length);
                foreach (var order in orders)
                {
                    MarkdownFile mf = new MarkdownFile();
                    mf.AbsolutePath = $"{orderFile.Directory.FullName}\\{order}.md";
                    mf.RelativePath = $"{relativePath}";
                    result.Add(mf);

                    var childPath = Path.Combine(orderFile.Directory.FullName, order);
                    if (Directory.Exists(childPath))
                    {
                        //recursion
                        result.AddRange(ReadOrderFiles(childPath));
                    }
                }
            }

            return(result);
        }
        private List <MarkdownFile> ReadOrderFiles(string path, int level)
        {
            //read the .order file
            //if there is an entry and a folder with the same name, dive deeper
            var directory = new DirectoryInfo(Path.GetFullPath(path));

            Log($"Reading .order file in directory {path}");
            var orderFiles = directory.GetFiles(".order", SearchOption.TopDirectoryOnly);

            if (orderFiles.Count() > 0)
            {
                Log("Order file found", LogLevel.Debug, 1);
            }

            var result = new List <MarkdownFile>();

            foreach (var orderFile in orderFiles)
            {
                var orders = File.ReadAllLines(orderFile.FullName);
                { Log($"Pages: {orders.Count()}", LogLevel.Information, 2); }
                var relativePath = orderFile.Directory.FullName.Length > directory.FullName.Length ?
                                   orderFile.Directory.FullName.Substring(directory.FullName.Length) :
                                   "/";



                foreach (var order in orders)
                {
                    //skip empty lines
                    if (string.IsNullOrEmpty(order))
                    {
                        continue;
                        //todo add log entry that we skipped an empty line
                    }

                    MarkdownFile mf = new MarkdownFile();
                    mf.AbsolutePath = Path.Combine(orderFile.Directory.FullName, $"{order}.md");
                    mf.RelativePath = $"{relativePath}";
                    mf.Level        = level;
                    result.Add(mf);

                    { Log($"Adding page: {mf.AbsolutePath}", LogLevel.Information, 2); }

                    var childPath = Path.Combine(orderFile.Directory.FullName, order);
                    if (Directory.Exists(childPath))
                    {
                        //recursion
                        result.AddRange(ReadOrderFiles(childPath, level + 1));
                    }
                }
            }

            return(result);
        }
        public void CorrectLinksAndImages(MarkdownObject document, FileInfo file, MarkdownFile mf)
        {
            Log("Correcting Links and Images");
            // walk the document node tree and replace relative image links
            // and relative links to markdown pages
            foreach (var link in document.Descendants().OfType <LinkInline>())
            {
                if (!link.Url.StartsWith("http"))
                {
                    string absPath = null;

                    if (link.Url.StartsWith("/"))
                    {
                        absPath = Path.GetFullPath(_path + link.Url);
                    }
                    else
                    {
                        absPath = Path.GetFullPath(file.Directory.FullName + "/" + link.Url);
                    }
                    //the file is a markdown file, create a link to it
                    var isMarkdown = false;
                    var fileInfo   = new FileInfo(absPath);
                    if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                    {
                        isMarkdown = true;
                    }
                    else if (fileInfo.Exists)
                    {
                        link.Url = $"file:///{absPath}";
                    }

                    fileInfo = new FileInfo($"{absPath}.md");
                    if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                    {
                        isMarkdown = true;
                    }

                    //only markdown files get a pdf internal link
                    if (isMarkdown)
                    {
                        var relPath = mf.RelativePath + "\\" + link.Url;
                        relPath = relPath.Replace("/", "\\");
                        relPath = relPath.Replace("\\", "");
                        relPath = relPath.Replace(".md", "");
                        relPath = relPath.ToLower();
                        Log($"\tMarkdown link: {relPath}");
                        link.Url = $"#{relPath}";
                    }
                }

                CorrectLinksAndImages(link, file, mf);
            }
        }
        public void CorrectLinksAndImages(MarkdownObject document, FileInfo file, MarkdownFile mf)
        {
            Log("Correcting Links and Images", LogLevel.Information, 2);
            // walk the document node tree and replace relative image links
            // and relative links to markdown pages
            foreach (var link in document.Descendants().OfType <LinkInline>())
            {
                if (link.Url != null)
                {
                    if (!link.Url.StartsWith("http"))
                    {
                        string absPath = null;
                        string anchor  = null;

                        //handle --attachments-path case
                        if (!string.IsNullOrEmpty(this._options.AttachmentsPath) && link.Url.StartsWith("/.attachments") || link.Url.StartsWith(".attachments"))
                        {
                            var linkUrl = link.Url.Split('/').Last();

                            //urls could be encoded and contain spaces - they are then not found on disk
                            linkUrl = HttpUtility.UrlDecode(linkUrl);

                            absPath = Path.GetFullPath(Path.Combine(this._options.AttachmentsPath, linkUrl));
                        }
                        else if (link.Url.StartsWith("/"))
                        {
                            //urls could be encoded and contain spaces - they are then not found on disk
                            var linkUrl = HttpUtility.UrlDecode(link.Url);
                            linkUrl = linkUrl.Replace("#", "-");
                            absPath = Path.GetFullPath(_rootWikiPath + linkUrl);
                        }
                        else
                        {
                            var split   = link.Url.Split("#");
                            var linkUrl = split[0];
                            anchor  = split.Length > 1 ? split[1] : null;
                            absPath = Path.GetFullPath(file.Directory.FullName + "/" + linkUrl);
                        }

                        //the file is a markdown file, create a link to it
                        var isMarkdown = false;
                        var fileInfo   = new FileInfo(absPath);
                        if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isMarkdown = true;
                        }
                        else if (fileInfo.Exists)
                        {
                            //convert images to base64 and embed them in the html. Chrome/Puppeter does not show local files because of security reasons.
                            Byte[] bytes  = File.ReadAllBytes(fileInfo.FullName);
                            String base64 = Convert.ToBase64String(bytes);

                            link.Url = $"data:image/{fileInfo.Extension};base64,{base64}";
                        }

                        fileInfo = new FileInfo($"{absPath}.md");
                        if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isMarkdown = true;
                        }

                        //only markdown files get a pdf internal link
                        if (isMarkdown)
                        {
                            var relPath = mf.RelativePath + "\\" + link.Url;

                            //remove anchor
                            relPath = relPath.Split("#")[0];


                            relPath = relPath.Replace("/", "\\");
                            // remove relative part if we are not exporting from the root of the wiki
                            var pathBelowRootWiki = _path.Replace(_rootWikiPath, "");
                            if (!pathBelowRootWiki.IsNullOrEmpty())
                            {
                                relPath = relPath.Replace(pathBelowRootWiki, "");
                            }
                            relPath = relPath.Replace("\\", "");
                            relPath = relPath.Replace(".md", "");
                            relPath = relPath.ToLower();
                            Log($"Markdown link: {relPath}", LogLevel.Information, 2);
                            link.Url = $"#{relPath}";
                        }
                    }
                }
                CorrectLinksAndImages(link, file, mf);
            }
        }
Exemplo n.º 5
0
        public void CorrectLinksAndImages(MarkdownObject document, FileInfo file, MarkdownFile mf)
        {
            Log("Correcting Links and Images");
            // walk the document node tree and replace relative image links
            // and relative links to markdown pages
            foreach (var link in document.Descendants().OfType <LinkInline>())
            {
                if (link.Url != null)
                {
                    if (!link.Url.StartsWith("http"))
                    {
                        string absPath = null;

                        //handle --attachments-path case
                        if (!string.IsNullOrEmpty(this._options.AttachmentsPath) && link.Url.StartsWith("/.attachments") || link.Url.StartsWith(".attachments"))
                        {
                            var linkUrl = link.Url.Split('/').Last();

                            //urls could be encoded and contain spaces - they are then not found on disk
                            linkUrl = HttpUtility.UrlDecode(linkUrl);

                            absPath = Path.GetFullPath(Path.Combine(this._options.AttachmentsPath, linkUrl));
                        }
                        else if (link.Url.StartsWith("/"))
                        {
                            //urls could be encoded and contain spaces - they are then not found on disk
                            var linkUrl = HttpUtility.UrlDecode(link.Url);
                            absPath = Path.GetFullPath(_path + linkUrl);
                        }
                        else
                        {
                            absPath = Path.GetFullPath(file.Directory.FullName + "/" + link.Url);
                        }

                        //the file is a markdown file, create a link to it
                        var isMarkdown = false;
                        var fileInfo   = new FileInfo(absPath);
                        if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isMarkdown = true;
                        }
                        else if (fileInfo.Exists)
                        {
                            link.Url = $"file:///{absPath}";
                        }

                        fileInfo = new FileInfo($"{absPath}.md");
                        if (fileInfo.Exists && fileInfo.Extension.Equals(".md", StringComparison.InvariantCultureIgnoreCase))
                        {
                            isMarkdown = true;
                        }

                        //only markdown files get a pdf internal link
                        if (isMarkdown)
                        {
                            var relPath = mf.RelativePath + "\\" + link.Url;
                            relPath = relPath.Replace("/", "\\");
                            relPath = relPath.Replace("\\", "");
                            relPath = relPath.Replace(".md", "");
                            relPath = relPath.ToLower();
                            Log($"\tMarkdown link: {relPath}");
                            link.Url = $"#{relPath}";
                        }
                    }
                }
                CorrectLinksAndImages(link, file, mf);
            }
        }