Remove Blank Lines
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using static System.IO.SearchOption; | |
namespace RemoveBlankLines | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
string path = @"C:\Users\XXXXX\Documents\GitHub\docs-1\docs\"; | |
var files = Directory.EnumerateFiles(path, "*.md", AllDirectories); | |
//using (System.IO.StreamWriter outfile = new System.IO.StreamWriter(@"C:\_DOCS_SCRIPTS\remove-blank-lines\files_changed.txt")) | |
//{ | |
foreach (var file in files) | |
{ | |
var changed = false; | |
var inputLines = File.ReadAllLines(file); | |
var outputLines = new List<string>(); | |
var code = false; | |
var lastLine = string.Empty; | |
foreach (var line in inputLines) | |
{ | |
if (line.TrimStart().StartsWith("```")) | |
{ | |
if (code) | |
{ | |
if (line.TrimStart().StartsWith("```") && lastLine.ToString().Trim().Length == 0) | |
{ | |
changed = true; | |
outputLines.RemoveAt(outputLines.Count - 1); | |
} | |
} | |
code = !code; | |
} | |
if (code) | |
{ | |
if (lastLine.TrimStart().StartsWith("```") && line.ToString().Trim().Length == 0) | |
{ | |
changed = true; | |
} | |
else | |
{ | |
outputLines.Add(line); | |
} | |
} | |
else | |
{ | |
outputLines.Add(line); | |
} | |
lastLine = line; | |
} | |
if (changed) | |
{ | |
//outfile.WriteLine(file.Substring(path.Length)); | |
Console.WriteLine(file.Substring(path.Length)); | |
File.WriteAllLines(file, outputLines); | |
} | |
} | |
//} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment