Skip to content

Instantly share code, notes, and snippets.

@guardrex
Last active May 20, 2017 02:36
Show Gist options
  • Save guardrex/57c6c6b4d5435383a646f84a6fc54f27 to your computer and use it in GitHub Desktop.
Save guardrex/57c6c6b4d5435383a646f84a6fc54f27 to your computer and use it in GitHub Desktop.
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