Пример #1
0
        public static IEnumerable<FolderItem> ParseFile(string filePath)
        {
            List<DocumentationFile> docFiles;
            try
            {
                var contents = File.ReadAllText(filePath);
                docFiles = JsonConvert.DeserializeObject<List<DocumentationFile>>(contents);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Could not parse file: " + filePath, e);
            }

            foreach (var file in docFiles)
            {
                var path = file.Path;
                var name = file.Name;
                var isFolder = path.StartsWith("/");
                var item = new FolderItem(isFolder)
                {
                    Language = Language.All,
                    Description = name,
                    Name = isFolder ? path.Substring(1, path.Length - 1) : path.Substring(0, path.Length - Constants.MarkdownFileExtension.Length),
                    Mappings = file.Mappings
                };

                yield return item;
            }
        }
Пример #2
0
 public FolderItem(FolderItem item)
 {
     IsFolder = item.IsFolder;
     Name = item.Name;
     Description = item.Description;
     Language = item.Language;
     Mappings = item.Mappings;
 }
Пример #3
0
        private ArticlePage CompileArticlePage(FolderItem page, string directory)
        {
            var path = Path.Combine(directory, page.Name + FileExtensionHelper.GetLanguageFileExtension(page.Language) + Constants.MarkdownFileExtension);
            var fileInfo = new FileInfo(path);

            if (fileInfo.Exists == false)
                throw new FileNotFoundException(string.Format("Documentaiton file '{0}' not found.", path));

            return _articleCompiler.Compile(fileInfo, page, "articles", new List<DocumentationMapping>());
        }
Пример #4
0
        public DocumentationPage Compile(FileInfo file, FolderItem page, string documentationVersion, List<DocumentationMapping> mappings)
        {
            try
            {
                var key = ExtractKey(file, page, documentationVersion);
                var category = CategoryHelper.ExtractCategoryFromPath(key);
                var images = new HashSet<DocumentationImage>();

                _parser.PrepareImage = (tag, b) => PrepareImage(images, file.DirectoryName, _options.ImagesUrl, documentationVersion, tag);

                var content = File.ReadAllText(file.FullName);
                content = TransformLegacyBlocks(file, content);
                content = _parser.Transform(content);
                content = TransformBlocks(content, documentationVersion);

                var htmlDocument = HtmlHelper.ParseHtml(content);

                var title = ExtractTitle(htmlDocument);
                var textContent = ExtractTextContent(htmlDocument);

                var caseSensitiveFileName = PathHelper.GetProperFilePathCapitalization(file.FullName);

                var fullName = caseSensitiveFileName ?? file.FullName;

                var repoRelativePath = _repoAnalyzer.MakeRelativePathInRepository(fullName);

                var relativeUrl = repoRelativePath.Replace(@"\", @"/");

                var lastCommit = _repoAnalyzer.GetLastCommitThatAffectedFile(repoRelativePath);

                return new DocumentationPage
                {
                    Key = key,
                    Title = title,
                    Version = documentationVersion,
                    HtmlContent = content,
                    TextContent = textContent,
                    Language = page.Language,
                    Category = category,
                    Images = images,
                    LastCommitSha = lastCommit,
                    RelativePath = relativeUrl,
                    Mappings = mappings.OrderBy(x => x.Version).ToList()
                };
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(string.Format("Could not compile '{0}'.", file.FullName), e);
            }
        }
Пример #5
0
        private IEnumerable<ArticlePage> CompileArticleDirectory(string directory)
        {
            var docsFilePath = Path.Combine(directory, Constants.DocumentationFileName);
            if (File.Exists(docsFilePath) == false)
                yield break;

            foreach (var item in DocumentationFileHelper.ParseFile(docsFilePath))
            {
                if (item.IsFolder)
                {
                    foreach (var page in CompileArticleDirectory(Path.Combine(directory, item.Name)))
                    {
                        yield return page;
                    }

                    continue;
                }

                foreach (var pageToCompile in DocumentationDirectoryCompiler.GetPages(directory, item))
                {
                    yield return CompileArticlePage(pageToCompile, directory);
                }
            }

            var indexFilePath = Path.Combine(directory, "index" + Constants.MarkdownFileExtension);
            if (File.Exists(indexFilePath) == false)
                yield break;

            var indexItem = new FolderItem(isFolder: false)
            {
                Description = string.Empty,
                Language = Language.All,
                Name = "index"
            };

            yield return CompileArticlePage(indexItem, directory);
        }
Пример #6
0
        private static IEnumerable<FolderItem> GetPages(string directory, FolderItem item)
        {
            var path = Path.Combine(directory, item.Name + Constants.MarkdownFileExtension);
            if (File.Exists(path))
            {
                yield return item;
                yield break;
            }

            var languageFileExtensions = FileExtensionHelper.GetLanguageFileExtensions();

            foreach (var language in languageFileExtensions.Keys)
            {
                var extension = languageFileExtensions[language];
                var name = item.Name + extension;
                path = Path.Combine(directory, name + Constants.MarkdownFileExtension);
                if (File.Exists(path))
                {
                    yield return new FolderItem(item)
                                     {
                                         Language = language
                                     };
                }
            }
        }
Пример #7
0
        private string ExtractKey(FileInfo file, FolderItem page, string documentationVersion)
        {
            var pathToDocumentationPagesDirectory = _options.GetPathToDocumentationPagesDirectory(documentationVersion);
            var key = file.FullName.Substring(pathToDocumentationPagesDirectory.Length, file.FullName.Length - pathToDocumentationPagesDirectory.Length);
            key = key.Substring(0, key.Length - file.Extension.Length);
            key = key.Replace(@"\", @"/");
            key = key.StartsWith(@"/") ? key.Substring(1) : key;

            var extension = FileExtensionHelper.GetLanguageFileExtension(page.Language);
            if (string.IsNullOrEmpty(extension) == false)
            {
                key = key.Substring(0, key.Length - extension.Length);
            }

            return key;
        }