コード例 #1
0
ファイル: PreviewCommand.cs プロジェクト: zyj0021/docfx
        public static PreviewJsonConfig ParsePreviewCommand(string workspacePath)
        {
            PreviewJsonConfig config = new PreviewJsonConfig();

            config.References      = new Dictionary <string, string>(PreviewConstants.References);
            config.TocMetadataName = PreviewConstants.TocMetadataName;
            return(config);
        }
コード例 #2
0
        public static PreviewJsonConfig ParsePreviewCommand(string workspacePath)
        {
            string            configFilePath = Path.Combine(workspacePath, PreviewConstants.ConfigFilename);
            PreviewJsonConfig config         = null;

            try
            {
                if (File.Exists(configFilePath))
                {
                    config = JsonUtility.Deserialize <PreviewJsonConfig>(configFilePath);
                }
            }
            catch (JsonException ex)
            {
                throw new HandlerServerException($"Error happened while parsing config file, {ex.Message}");
            }
            return(MergeDefaultConfig(config));
        }
コード例 #3
0
        private string Preview(string workspacePath, string relativePath, string markdownContent,
                               bool isFirstTime     = false, string previewFilePath = null, string pageUpdateJsFilePath = null,
                               string builtHtmlPath = null)
        {
            if (string.IsNullOrEmpty(workspacePath))
            {
                throw new HandlerClientException("Base directory should not be null or empty");
            }
            if (string.IsNullOrEmpty(relativePath))
            {
                throw new HandlerClientException("Relative path should not be null or empty");
            }
            var markupResult = DfmMarkup(workspacePath, relativePath, markdownContent);

            if (!isFirstTime)
            {
                return(markupResult);
            }

            if (string.IsNullOrEmpty(builtHtmlPath))
            {
                throw new HandlerClientException("Built Html path should not be null or empty");
            }
            if (string.IsNullOrEmpty(pageUpdateJsFilePath))
            {
                throw new HandlerClientException("Page update js file path should not be null or empty");
            }

            PreviewJsonConfig config = PreviewCommand.ParsePreviewCommand(workspacePath);

            builtHtmlPath        = new Uri(builtHtmlPath).LocalPath;
            pageUpdateJsFilePath = new Uri(pageUpdateJsFilePath).LocalPath;
            previewFilePath      = new Uri(previewFilePath).LocalPath;

            string htmlString = File.ReadAllText(builtHtmlPath);

            CQ dom = htmlString;

            CQ addElements = $"<script type='text/javascript' src='{pageUpdateJsFilePath}'></script>" +
                             $"<meta name='pageRefreshFunctionName' content ='{config.PageRefreshFunctionName}'>" +
                             $"<meta name='port' content='{config.NavigationPort}'>" +
                             $"<meta name='filePath' content='{relativePath}'>" +
                             $"<meta name='markupTagType' content='{config.MarkupTagType}'>" +
                             $"<meta name='markupClassName' content='{config.MarkupClassName}'>";

            foreach (var addElement in addElements)
            {
                dom.Select(addElement.NodeName)
                .Last()
                .After(addElement);
            }

            // Replace 'https' to 'http' for that VS Code don't support reference which use https protocol now
            // Replace reference relative path to absolute path
            foreach (var item in config.References)
            {
                dom.Select(item.Key).Each((i, e) =>
                {
                    var path = e.GetAttribute(item.Value);
                    if (string.IsNullOrEmpty(path))
                    {
                        return;
                    }
                    // VSCode bug: https://github.com/Microsoft/vscode/issues/23020
                    // Remove when bug fixed
                    if (path.StartsWith("http"))
                    {
                        if (path.StartsWith("https"))
                        {
                            e.SetAttribute(item.Value, ReplaceFirstOccurrence(path, "https", "http"));
                        }
                        return;
                    }
                    if (Path.IsPathRooted(path))
                    {
                        e.SetAttribute(item.Value, path);
                    }
                    else
                    {
                        e.SetAttribute(item.Value, GetAbsolutePath(builtHtmlPath, path));
                    }
                });
            }

            // For Docs
            // Replace toc relative path to absolute path
            dom.Select("meta").Each((i, e) =>
            {
                // TODO: Implement breadcrumb
                var metaName = e.GetAttribute("name");
                if (metaName == config.TocMetadataName)
                {
                    e.SetAttribute("content", GetAbsolutePath(builtHtmlPath, e.GetAttribute("content")));
                }
            });

            File.WriteAllText(previewFilePath, dom.Render());
            return(markupResult);
        }
コード例 #4
0
        private string Preview(CommandMessage contextMessage)
        {
            if (string.IsNullOrEmpty(contextMessage.WorkspacePath))
            {
                throw new HandlerClientException("Base directory should not be null or empty");
            }
            if (string.IsNullOrEmpty(contextMessage.RelativePath))
            {
                throw new HandlerClientException("Relative path should not be null or empty");
            }
            string result = DfmMarkup(contextMessage.WorkspacePath, contextMessage.RelativePath, contextMessage.MarkdownContent);

            if (contextMessage.ShouldSeparateMarkupResult)
            {
                var htmlInfo = HtmlDocumentUtility.SeparateHtml(result);
                var separatedMarkupResult =
                    new { rawTitle = htmlInfo.RawTitle, contentWithoutRawTitle = htmlInfo.Content };
                result = JsonConvert.SerializeObject(separatedMarkupResult);
            }
            if (string.IsNullOrEmpty(contextMessage.TempPreviewFilePath))
            {
                return(result);
            }

            // TODO: move this part to client
            if (string.IsNullOrEmpty(contextMessage.OriginalHtmlPath))
            {
                throw new HandlerClientException("Built Html path should not be null or empty");
            }
            if (string.IsNullOrEmpty(contextMessage.PageRefreshJsFilePath))
            {
                throw new HandlerClientException("Page update js file path should not be null or empty");
            }

            PreviewJsonConfig config = PreviewCommand.ParsePreviewCommand(contextMessage.WorkspacePath);

            var originalHtmlPath      = new Uri(contextMessage.OriginalHtmlPath).LocalPath;
            var pageRefreshJsFilePath = new Uri(contextMessage.PageRefreshJsFilePath).LocalPath;
            var tempPreviewFilePath   = new Uri(contextMessage.TempPreviewFilePath).LocalPath;

            string htmlString = File.ReadAllText(originalHtmlPath);

            CQ dom = htmlString;

            CQ addElements = $"<script type='text/javascript' src='{pageRefreshJsFilePath}'></script>" +
                             $"<meta name='port' content='{contextMessage.NavigationPort}'>" +
                             $"<meta name='filePath' content='{contextMessage.RelativePath}'>";

            foreach (var addElement in addElements)
            {
                dom.Select(addElement.NodeName)
                .Last()
                .After(addElement);
            }

            // Replace 'https' to 'http' for that VS Code don't support reference which use https protocol now
            // Replace reference relative path to absolute path
            foreach (var item in config.References)
            {
                dom.Select(item.Key).Each((i, e) =>
                {
                    var path = e.GetAttribute(item.Value);
                    if (string.IsNullOrEmpty(path))
                    {
                        return;
                    }
                    // VSCode bug: https://github.com/Microsoft/vscode/issues/23020
                    // Remove when bug fixed
                    if (path.StartsWith("http"))
                    {
                        if (path.StartsWith("https"))
                        {
                            e.SetAttribute(item.Value, ReplaceFirstOccurrence(path, "https", "http"));
                        }
                        return;
                    }
                    if (Path.IsPathRooted(path))
                    {
                        e.SetAttribute(item.Value, path);
                    }
                    else
                    {
                        e.SetAttribute(item.Value, GetAbsolutePath(originalHtmlPath, path));
                    }
                });
            }

            // For Docs
            // Replace toc relative path to absolute path
            dom.Select("meta").Each((i, e) =>
            {
                // TODO: Implement breadcrumb
                var metaName = e.GetAttribute("name");
                if (metaName == config.TocMetadataName)
                {
                    e.SetAttribute("content", GetAbsolutePath(originalHtmlPath, e.GetAttribute("content")));
                }
            });

            File.WriteAllText(tempPreviewFilePath, dom.Render());
            return(result);
        }
コード例 #5
0
        private static PreviewJsonConfig MergeDefaultConfig(PreviewJsonConfig config)
        {
            if (config == null)
            {
                config = new PreviewJsonConfig();
                config.BuildSourceFolder       = PreviewConstants.BuildSourceFolder;
                config.BuildOutputSubFolder    = PreviewConstants.BuildOutputSubfolder;
                config.MarkupTagType           = PreviewConstants.MarkupTagType;
                config.MarkupClassName         = PreviewConstants.MarkupClassName;
                config.OutputFolder            = PreviewConstants.OutputFolder;
                config.PageRefreshFunctionName = PreviewConstants.PageRefreshFunctionName;
                config.ServerPort      = PreviewConstants.ServerPort;
                config.NavigationPort  = PreviewConstants.NavigationPort;
                config.References      = new Dictionary <string, string>(PreviewConstants.References);
                config.TocMetadataName = PreviewConstants.TocMetadataName;
                return(config);
            }

            if (string.IsNullOrEmpty(config.BuildSourceFolder))
            {
                config.BuildSourceFolder = PreviewConstants.BuildSourceFolder;
            }

            if (string.IsNullOrEmpty(config.BuildOutputSubFolder))
            {
                config.BuildOutputSubFolder = PreviewConstants.BuildOutputSubfolder;
            }

            if (string.IsNullOrEmpty(config.MarkupTagType))
            {
                config.MarkupTagType = PreviewConstants.MarkupTagType;
            }

            if (string.IsNullOrEmpty(config.MarkupClassName))
            {
                config.MarkupClassName = PreviewConstants.MarkupClassName;
            }

            if (string.IsNullOrEmpty(config.OutputFolder))
            {
                config.OutputFolder = PreviewConstants.OutputFolder;
            }

            if (string.IsNullOrEmpty(config.PageRefreshFunctionName))
            {
                config.PageRefreshFunctionName = PreviewConstants.PageRefreshFunctionName;
            }

            if (string.IsNullOrEmpty(config.ServerPort))
            {
                config.ServerPort = PreviewConstants.ServerPort;
            }

            if (string.IsNullOrEmpty(config.NavigationPort))
            {
                config.NavigationPort = PreviewConstants.NavigationPort;
            }

            if (config.References == null)
            {
                config.References = new Dictionary <string, string>(PreviewConstants.References);
            }
            else
            {
                foreach (var reference in PreviewConstants.References)
                {
                    if (!config.References.ContainsKey(reference.Key))
                    {
                        config.References[reference.Key] = reference.Value;
                    }
                }
            }

            if (string.IsNullOrEmpty(config.TocMetadataName))
            {
                config.TocMetadataName = PreviewConstants.TocMetadataName;
            }

            return(config);
        }