ConvertMarkdownToHtml() public static method

Converts the markdown to HTML.
public static ConvertMarkdownToHtml ( string toConvert, string destinationDocumentPath, string siteRoot, string sourceDocumentFilename, string>.List createdAnchorCollector ) : string
toConvert string The markdown string to convert.
destinationDocumentPath string The document path (without the document filename).
siteRoot string The site root.
sourceDocumentFilename string the filename of the source markdown file
createdAnchorCollector string>.List The created anchor collector, for ToC sublinks for H2 headers.
return string
Exemplo n.º 1
0
        private string GenerateContent(SimpleNavigationElement element, Config config, NavigationContext navigationContext)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("# Page not found (404)");
            stringBuilder.AppendLine();

            stringBuilder.AppendLine("Unfortunately the page you were looking for does not exist. In order to help you out ");
            stringBuilder.AppendLine("in the best way possible, below is the table of contents:");
            stringBuilder.AppendLine();

            foreach (var sibling in this.ParentContainer.Value)
            {
                if (sibling == this)
                {
                    continue;
                }

                stringBuilder.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, "/" /* pathRelativeToRoot */,
                                           sibling.GetFinalTargetUrl(navigationContext), Environment.NewLine);
            }

            var markdownContent = stringBuilder.ToString();

            var destinationFile = Utils.MakeAbsolutePath(config.Destination, this.GetTargetURL(navigationContext));

            var htmlContent = Utils.ConvertMarkdownToHtml(markdownContent, Path.GetDirectoryName(destinationFile), config.Destination,
                                                          string.Empty, new List <Heading>(), config.ConvertLocalLinks);

            return(htmlContent);
        }
Exemplo n.º 2
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>
        /// <param name="navigationContext">The navigation context.</param>
        /// <exception cref="FileNotFoundException"></exception>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        public override void GenerateOutput(Config activeConfig, NavigatedPath activePath, NavigationContext navigationContext)
        {
            // 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);
            }
            _relativeLinksOnPage.Clear();
            var sourceFile      = Utils.MakeAbsolutePath(activeConfig.Source, this.Value);
            var destinationFile = Utils.MakeAbsolutePath(activeConfig.Destination, this.GetTargetURL(navigationContext));
            var sb      = new StringBuilder(activeConfig.PageTemplateContents.Length + 2048);
            var content = string.Empty;

            this.MarkdownFromFile = string.Empty;
            var relativePathToRoot = Utils.MakeRelativePathForUri(Path.GetDirectoryName(destinationFile), activeConfig.Destination);

            if (File.Exists(sourceFile))
            {
                this.MarkdownFromFile = File.ReadAllText(sourceFile, Encoding.UTF8);
                // Check if the content contains @@include tag
                content = Utils.IncludeProcessor(this.MarkdownFromFile, Utils.MakeAbsolutePath(activeConfig.Source, activeConfig.IncludeFolder));
                content = Utils.ConvertMarkdownToHtml(content, Path.GetDirectoryName(destinationFile), activeConfig.Destination, sourceFile, _relativeLinksOnPage, activeConfig.ConvertLocalLinks);
            }
            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 topics in this section:{0}{0}", Environment.NewLine);
                    foreach (var sibling in this.ParentContainer.Value)
                    {
                        if (sibling == this)
                        {
                            continue;
                        }
                        defaultMarkdown.AppendFormat("* [{0}]({1}{2}){3}", sibling.Name, relativePathToRoot,
                                                     sibling.GetFinalTargetUrl(navigationContext), Environment.NewLine);
                    }
                    defaultMarkdown.Append(Environment.NewLine);
                    content = Utils.ConvertMarkdownToHtml(defaultMarkdown.ToString(), Path.GetDirectoryName(destinationFile), activeConfig.Destination, string.Empty, _relativeLinksOnPage, activeConfig.ConvertLocalLinks);
                }
                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, activeConfig, navigationContext);
                }
            }
            sb.Append(activeConfig.PageTemplateContents);
            sb.Replace("{{Name}}", activeConfig.Name);
            sb.Replace("{{Footer}}", activeConfig.Footer);
            sb.Replace("{{TopicTitle}}", this.Name);
            sb.Replace("{{Path}}", relativePathToRoot);
            sb.Replace("{{RelativeSourceFileName}}", Utils.MakeRelativePathForUri(activeConfig.Destination, sourceFile).TrimEnd('/'));
            sb.Replace("{{RelativeTargetFileName}}", Utils.MakeRelativePathForUri(activeConfig.Destination, destinationFile).TrimEnd('/'));
            sb.Replace("{{Breadcrumbs}}", activePath.CreateBreadCrumbsHTML(relativePathToRoot, navigationContext));
            sb.Replace("{{ToC}}", activePath.CreateToCHTML(relativePathToRoot, navigationContext));
            sb.Replace("{{ExtraScript}}", (this.ExtraScriptProducerFunc == null) ? string.Empty : this.ExtraScriptProducerFunc(this, activeConfig, navigationContext));

            // the last action has to be replacing the content marker, so markers in the content which we have in the template as well aren't replaced
            sb.Replace("{{Content}}", content);
            Utils.CreateFoldersIfRequired(destinationFile);
            File.WriteAllText(destinationFile, sb.ToString());
            if (!this.IsIndexElement)
            {
                activePath.Pop();
            }
        }