public IMarkdownToken TryMatch(IMarkdownParser engine, IMarkdownParsingContext context) { var match = AzureIncludeRegex.Match(context.CurrentMarkdown); if (match.Length == 0) { return(null); } var sourceInfo = context.Consume(match.Length); // [!azure.include[title](path "optionalTitle")] // 1. Get include file path var path = match.Groups[2].Value; // 2. Get title var value = match.Groups[1].Value; var title = match.Groups[4].Value; if (!TypeForwardedToPathUtility.IsRelativePath(path)) { Logger.LogWarning($"Azure inline include path {path} is not a relative path, can't expand it"); return(new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo)); } // 3. Apply inline rules to the included content object currentFilePath; if (!engine.Context.Variables.TryGetValue("path", out currentFilePath)) { Logger.LogWarning($"Can't get path for the file that ref azure inline include file, return MarkdownTextToken. Raw: {match.Value}"); return(new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo)); } var includeFilePath = TypeForwardedToPathUtility.NormalizePath(Path.Combine(Path.GetDirectoryName(currentFilePath.ToString()), path)); if (!File.Exists(includeFilePath)) { Logger.LogWarning($"Can't get include file path {includeFilePath} in the file {currentFilePath}, return MarkdownTextToken. Raw: {match.Value}"); return(new MarkdownTextToken(this, engine.Context, match.Value, sourceInfo)); } return(new TwoPhaseBlockToken(this, engine.Context, sourceInfo, (p, t) => { var inlineTokens = p.Tokenize(SourceInfo.Create(MarkdownEngine.Normalize(File.ReadAllText(includeFilePath)), includeFilePath)); return new AzureIncludeInlineToken(t.Rule, t.Context, path, value, title, inlineTokens, match.Groups[0].Value, t.SourceInfo); })); }
private static bool GenerateAzureFileInfoForMigration( string repositoryRoot, RewriterToolArguments rewriterToolArguments, string azureDocumentUriPrefix, AzureFileInformationCollection azureFileInformationCollection) { var azureMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var azureResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var azureIncludeMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var azureIncludeResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); bool hasDupliateMdFileName = false; var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories); Parallel.ForEach( files, new ParallelOptions { MaxDegreeOfParallelism = 8 }, file => { var relativePath = TypeForwardedToPathUtility.MakeRelativePath(repositoryRoot, file); if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration)) { return; } var filePath = TypeForwardedToPathUtility.NormalizePath(file); var fileName = Path.GetFileName(file); var azureFileInfo = new AzureFileInfo { FileName = fileName, FilePath = TypeForwardedToPathUtility.NormalizePath(file), NeedTransformToAzureExternalLink = false, UriPrefix = string.Empty }; var isIncludeFile = filePath.Split(new[] { Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries) .Any(folder => folder.Equals("includes", StringComparison.OrdinalIgnoreCase)); var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase); AzureFileInfo conflictFile = null; var isSucceed = true; if (!isIncludeFile && isMarkdownFile) { isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo); azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile); } else if (!isIncludeFile && !isMarkdownFile) { // For resource file, even if has conflicts, we regards that as succeed azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo); } else if (isIncludeFile && isMarkdownFile) { isSucceed = azureIncludeMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo); azureIncludeMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile); } else { // For resource file, even if has conflicts, we regards that as succeed azureIncludeResourceFileInfoMapping.TryAdd(fileName, azureFileInfo); } if (!isSucceed) { hasDupliateMdFileName = true; Logger.LogError($"Error: GenerateAzureFileInfo failed. File: {file} name confilicts with: {conflictFile?.FilePath}"); } }); azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); azureFileInformationCollection.AzureIncludeMarkdownFileInfoMapping = azureIncludeMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); azureFileInformationCollection.AzureIncludeResourceFileInfoMapping = azureIncludeResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); return !hasDupliateMdFileName; }
private static bool GenerateAzureFileInfo( string repositoryRoot, RewriterToolArguments rewriterToolArguments, string azureDocumentUriPrefix, AzureFileInformationCollection azureFileInformationCollection) { var azureMarkdownFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var azureResourceFileInfoMapping = new ConcurrentDictionary<string, AzureFileInfo>(); var files = Directory.GetFiles(repositoryRoot, "*", SearchOption.AllDirectories); Parallel.ForEach( files, new ParallelOptions { MaxDegreeOfParallelism = 8 }, file => { var relativePath = TypeForwardedToPathUtility.MakeRelativePath(repositoryRoot, file); if (IsIgnoreFile(relativePath, rewriterToolArguments.IsMigration)) { return; } var isSucceed = true; var azureTransformArguments = rewriterToolArguments.AzureTransformArgumentsList.FirstOrDefault(a => TypeForwardedToPathUtility.IsPathUnderSpecificFolder(file, a.SourceDir)); // By default, all the link should be transformed to external link with azure uri prefix // However, if we find that the file is under one of the folder that need to be transformed. Then the prefix uri should be docs but not auzre var needTransformToAzureExternalLink = true; var uriPrefix = azureDocumentUriPrefix; if (azureTransformArguments != null) { needTransformToAzureExternalLink = false; uriPrefix = azureTransformArguments.DocsHostUriPrefix; } var fileName = Path.GetFileName(file); var azureFileInfo = new AzureFileInfo { FileName = fileName, FilePath = TypeForwardedToPathUtility.NormalizePath(file), NeedTransformToAzureExternalLink = needTransformToAzureExternalLink, UriPrefix = uriPrefix }; AzureFileInfo conflictFile; var isMarkdownFile = Path.GetExtension(relativePath).Equals(MarkdownExtension, StringComparison.OrdinalIgnoreCase); if (isMarkdownFile) { isSucceed = azureMarkdownFileInfoMapping.TryAdd(fileName, azureFileInfo); azureMarkdownFileInfoMapping.TryGetValue(fileName, out conflictFile); } else { isSucceed = azureResourceFileInfoMapping.TryAdd(fileName, azureFileInfo); azureResourceFileInfoMapping.TryGetValue(fileName, out conflictFile); } if (!isSucceed) { Console.WriteLine($"GenerateAzureFileInfo warning: can't insert file: {file}, confilicts with: {conflictFile?.FilePath}"); } }); azureFileInformationCollection.AzureMarkdownFileInfoMapping = azureMarkdownFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); azureFileInformationCollection.AzureResourceFileInfoMapping = azureResourceFileInfoMapping.ToDictionary(m => m.Key, m => m.Value); return true; }