static void ConvertFile(string SourcePath, string TargetPath, string ImageAttachPath, Dictionary <string, string> LinkPageMapping) { var TwikiToMarkDownConverter = new TwikiToMarkdownFile.TwikiToMarkdownFile(); TwikiToMarkDownConverter.TwikiMarkdownLookup = LinkPageMapping; string Output; string Expected; string ActualPath; string BackupPath; SupportedLanguages Language; int NoChangesCount = 0; int NewFileCount = 0; int WhiteSpaceChangesCount = 0; int SignificantChangesCount = 0; int Total = 0; //Check directory exists log.Info(String.Format("Running Twiki Conversion on folder " + SourcePath)); if (!Directory.Exists(SourcePath)) { log.Error("Directory does not exist, skipping transform"); return; } if (!Directory.Exists(TargetPath)) { Directory.CreateDirectory(TargetPath); } //Backup old files directory string BackupDirectory = Path.Combine(TargetPath, "OldConversions"); if (!Directory.Exists(BackupDirectory)) { Directory.CreateDirectory(BackupDirectory); } foreach (string FileName in Directory.GetFiles(SourcePath, "*.txt")) { //Filename must exist in the mapping directory or it should not be converted. and can not have extension with ,v if (!FileName.ToLower().EndsWith(",v") && LinkPageMapping.ContainsKey(Path.GetFileNameWithoutExtension(FileName))) { ++Total; //Name of output file depends on the language, the path for the output file is found in the mapping variables dictionary if (FileName.ToLower().EndsWith("ch.txt")) { //Chinese so CH.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.CH.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.CH.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".CH.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".CH.udn"); } Language = SupportedLanguages.CH; } else if (FileName.ToLower().EndsWith("kr.txt")) { //Korean so KR.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.KR.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.KR.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".KR.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".KR.udn"); } Language = SupportedLanguages.KR; } else if (FileName.ToLower().EndsWith("jp.txt")) { //Japanese so JP.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.JP.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.JP.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".JP.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".JP.udn"); } Language = SupportedLanguages.JP; } else { //assume english so INT.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.INT.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.INT.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".INT.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".INT.udn"); } Language = SupportedLanguages.INT; } //Create Directory if (!Directory.Exists(new DirectoryInfo(ActualPath).Parent.FullName)) { Directory.CreateDirectory(new DirectoryInfo(ActualPath).Parent.FullName); Directory.CreateDirectory(new DirectoryInfo(BackupPath).Parent.FullName); } Expected = FileContents(ActualPath); Output = TwikiToMarkDownConverter.Transform(FileContents(FileName)); if (Output == Expected) { NoChangesCount++; } else if (Expected == "" & Output != "") { NewFileCount++; if (!File.Exists(ActualPath)) { File.WriteAllText(ActualPath, Output); } } else if (RemoveWhitespace(Output) == RemoveWhitespace(Expected)) { WhiteSpaceChangesCount++; if (File.Exists(ActualPath)) { //Move existing file for comparison if (File.Exists(BackupPath)) { File.Delete(BackupPath); } File.Move(ActualPath, BackupPath); File.WriteAllText(ActualPath, Output); } else { File.WriteAllText(ActualPath, Output); } } else { SignificantChangesCount++; if (File.Exists(ActualPath)) { Console.Write("\n"); log.Info(@"Conversion detected changes since last run for " + Path.GetFileName(FileName)); //Move existing file for comparison if (File.Exists(BackupPath)) { File.Delete(BackupPath); } File.Move(ActualPath, BackupPath); File.WriteAllText(ActualPath, Output); } else { File.WriteAllText(ActualPath, Output); } } //Copy images and attachments, if the images directory does not already exist for this file... //copy images once, but allow twiki conversion process to be updated if needs be and convert the twiki files repeatedly. string SourceImageAttachPathThisFile = Path.Combine(ImageAttachPath, Path.GetFileNameWithoutExtension(FileName)); string TargetImagePath; string TargetAttachPath; switch (Language) { case SupportedLanguages.KR: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Images"), "KR"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "KR"); break; case SupportedLanguages.JP: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Images"), "JP"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "JP"); break; case SupportedLanguages.CH: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Images"), "CH"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "CH"); break; default: //SupportedLanguages.INT: TargetImagePath = Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Images"); TargetAttachPath = Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"); break; } if (!Directory.Exists(TargetImagePath)) { Directory.CreateDirectory(TargetImagePath); } if (!Directory.Exists(TargetAttachPath)) { Directory.CreateDirectory(TargetAttachPath); } if (Directory.Exists(SourceImageAttachPathThisFile)) { foreach (string ImageOrAttachmentFile in Directory.GetFiles(SourceImageAttachPathThisFile)) { //if the filename extenstion has a ,v in it ignore the file if (!Path.GetExtension(ImageOrAttachmentFile).ToLower().EndsWith(",v")) { //if image type file if (Regex.IsMatch(ImageOrAttachmentFile, @"\.png|\.gif|\.tga|\.bmp|\.jpg", RegexOptions.IgnoreCase)) { if (File.Exists(Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)))) { File.SetAttributes(Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)), FileAttributes.Normal); } File.Copy(ImageOrAttachmentFile, Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)), true); } else { //attachment if (File.Exists(Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)))) { File.SetAttributes(Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)), FileAttributes.Normal); } File.Copy(ImageOrAttachmentFile, Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)), true); } } } } Console.Write("."); } } Console.Write("\n"); log.Info(String.Format("=============================== Summary ==========================\n")); log.Info(String.Format("{0:000} files converted in {1,-55}", Total, SourcePath)); log.Info(String.Format("Files output to {0,-55}\n", TargetPath)); if (NoChangesCount > 0) { log.Info(String.Format("{0:000}/{1:000} files did not change.", NoChangesCount, Total)); } if (NewFileCount > 0) { log.Info(String.Format("{0:000} new files.", NewFileCount)); } if (WhiteSpaceChangesCount > 0) { log.Info(String.Format("{0:000} had whitespace differences.", WhiteSpaceChangesCount)); } if (SignificantChangesCount > 0) { log.Info(String.Format("{0:000} had significant file changes.", SignificantChangesCount)); } log.Info(String.Format("=========================== Summary End ==========================\n")); }
static void ConvertFile(string SourcePath, string TargetPath, string ImageAttachPath, Dictionary<string, string> LinkPageMapping) { var TwikiToMarkDownConverter = new TwikiToMarkdownFile.TwikiToMarkdownFile(); TwikiToMarkDownConverter.TwikiMarkdownLookup = LinkPageMapping; string Output; string Expected; string ActualPath; string BackupPath; SupportedLanguages Language; int NoChangesCount = 0; int NewFileCount = 0; int WhiteSpaceChangesCount = 0; int SignificantChangesCount = 0; int Total = 0; //Check directory exists log.Info(String.Format("Running Twiki Conversion on folder " + SourcePath)); if (!Directory.Exists(SourcePath)) { log.Error("Directory does not exist, skipping transform"); return; } if (!Directory.Exists(TargetPath)) { Directory.CreateDirectory(TargetPath); } //Backup old files directory string BackupDirectory = Path.Combine(TargetPath, "OldConversions"); if (!Directory.Exists(BackupDirectory)) { Directory.CreateDirectory(BackupDirectory); } foreach (string FileName in Directory.GetFiles(SourcePath, "*.txt")) { //Filename must exist in the mapping directory or it should not be converted. and can not have extension with ,v if (!FileName.ToLower().EndsWith(",v") && LinkPageMapping.ContainsKey(Path.GetFileNameWithoutExtension(FileName))) { ++Total; //Name of output file depends on the language, the path for the output file is found in the mapping variables dictionary if (FileName.ToLower().EndsWith("ch.txt")) { //Chinese so CH.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.CH.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.CH.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".CH.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".CH.udn"); } Language = SupportedLanguages.CH; } else if (FileName.ToLower().EndsWith("kr.txt")) { //Korean so KR.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.KR.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.KR.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".KR.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".KR.udn"); } Language = SupportedLanguages.KR; } else if (FileName.ToLower().EndsWith("jp.txt")) { //Japanese so JP.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.JP.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.JP.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".JP.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".JP.udn"); } Language = SupportedLanguages.JP; } else { //assume english so INT.udn if (LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)] == "%ROOT%") { //Home directory ActualPath = Path.Combine(TargetPath, "UE4Home.INT.udn"); BackupPath = Path.Combine(BackupDirectory, "UE4Home.INT.udn"); } else { ActualPath = Path.Combine(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".INT.udn"); BackupPath = Path.Combine(Path.Combine(BackupDirectory, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]), (new DirectoryInfo(Path.Combine(TargetPath, LinkPageMapping[Path.GetFileNameWithoutExtension(FileName)]))).Name + ".INT.udn"); } Language = SupportedLanguages.INT; } //Create Directory if (!Directory.Exists(new DirectoryInfo(ActualPath).Parent.FullName)) { Directory.CreateDirectory(new DirectoryInfo(ActualPath).Parent.FullName); Directory.CreateDirectory(new DirectoryInfo(BackupPath).Parent.FullName); } Expected = FileContents(ActualPath); Output = TwikiToMarkDownConverter.Transform(FileContents(FileName)); if (Output == Expected) { NoChangesCount++; } else if (Expected == "" & Output != "") { NewFileCount++; if (!File.Exists(ActualPath)) File.WriteAllText(ActualPath, Output); } else if (RemoveWhitespace(Output) == RemoveWhitespace(Expected)) { WhiteSpaceChangesCount++; if (File.Exists(ActualPath)) { //Move existing file for comparison if (File.Exists(BackupPath)) { File.Delete(BackupPath); } File.Move(ActualPath, BackupPath); File.WriteAllText(ActualPath, Output); } else { File.WriteAllText(ActualPath, Output); } } else { SignificantChangesCount++; if (File.Exists(ActualPath)) { Console.Write("\n"); log.Info(@"Conversion detected changes since last run for " + Path.GetFileName(FileName)); //Move existing file for comparison if (File.Exists(BackupPath)) { File.Delete(BackupPath); } File.Move(ActualPath, BackupPath); File.WriteAllText(ActualPath, Output); } else { File.WriteAllText(ActualPath, Output); } } //Copy images and attachments, if the images directory does not already exist for this file... //copy images once, but allow twiki conversion process to be updated if needs be and convert the twiki files repeatedly. string SourceImageAttachPathThisFile = Path.Combine(ImageAttachPath, Path.GetFileNameWithoutExtension(FileName)); string TargetImagePath; string TargetAttachPath; switch (Language) { case SupportedLanguages.KR: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName,"Images"),"KR"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "KR"); break; case SupportedLanguages.JP: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName,"Images"),"JP"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "JP"); break; case SupportedLanguages.CH: TargetImagePath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName,"Images"),"CH"); TargetAttachPath = Path.Combine(Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"), "CH"); break; default: //SupportedLanguages.INT: TargetImagePath = Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName,"Images"); TargetAttachPath = Path.Combine(new DirectoryInfo(ActualPath).Parent.FullName, "Attachments"); break; } if (!Directory.Exists(TargetImagePath)) { Directory.CreateDirectory(TargetImagePath); } if (!Directory.Exists(TargetAttachPath)) { Directory.CreateDirectory(TargetAttachPath); } if (Directory.Exists(SourceImageAttachPathThisFile)) { foreach (string ImageOrAttachmentFile in Directory.GetFiles(SourceImageAttachPathThisFile)) { //if the filename extenstion has a ,v in it ignore the file if (!Path.GetExtension(ImageOrAttachmentFile).ToLower().EndsWith(",v")) { //if image type file if (Regex.IsMatch(ImageOrAttachmentFile, @"\.png|\.gif|\.tga|\.bmp|\.jpg", RegexOptions.IgnoreCase)) { if (File.Exists(Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)))) { File.SetAttributes(Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)), FileAttributes.Normal); } File.Copy(ImageOrAttachmentFile, Path.Combine(TargetImagePath, Path.GetFileName(ImageOrAttachmentFile)), true); } else { //attachment if (File.Exists(Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)))) { File.SetAttributes(Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)), FileAttributes.Normal); } File.Copy(ImageOrAttachmentFile, Path.Combine(TargetAttachPath, Path.GetFileName(ImageOrAttachmentFile)), true); } } } } Console.Write("."); } } Console.Write("\n"); log.Info(String.Format("=============================== Summary ==========================\n")); log.Info(String.Format("{0:000} files converted in {1,-55}", Total, SourcePath)); log.Info(String.Format("Files output to {0,-55}\n", TargetPath)); if (NoChangesCount > 0) { log.Info(String.Format("{0:000}/{1:000} files did not change.", NoChangesCount, Total)); } if (NewFileCount > 0) { log.Info(String.Format("{0:000} new files.", NewFileCount)); } if (WhiteSpaceChangesCount > 0) { log.Info(String.Format("{0:000} had whitespace differences.", WhiteSpaceChangesCount)); } if (SignificantChangesCount > 0) { log.Info(String.Format("{0:000} had significant file changes.", SignificantChangesCount)); } log.Info(String.Format("=========================== Summary End ==========================\n")); }