Skip to content

Instantly share code, notes, and snippets.

@guardrex
Last active May 29, 2017 15:24
Show Gist options
  • Save guardrex/d6a8c2dc4dab0053ceeb1c7848ec6045 to your computer and use it in GitHub Desktop.
Save guardrex/d6a8c2dc4dab0053ceeb1c7848ec6045 to your computer and use it in GitHub Desktop.
Manually updating code blocks without lang idents.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using static System.IO.SearchOption;
namespace AddLanguageIdentifiers
{
class Program
{
enum langIdents
{
csharp, fsharp, vb, xml, console, html, javascript, json, xaml, powershell, azurecli, azcopy, cpp, nodejs, objc, php, python, ruby, sql, swift, skip, stop
};
static void Main()
{
//string path = @"C:\Users\XXXXXXXXXX\Documents\GitHub\docs-1\docs\";
string path = @"C:\Users\XXXXXXXXXX\Documents\GitHub\docs-1\docs\csharp\language-reference\compiler-options\";
//string path = @"C:\_DOCS_SCRIPTS\add-lang-ids\";
var files = Directory.EnumerateFiles(path, "*.md", AllDirectories);
Regex regExp = new Regex(@"^(csharp|fsharp|vb|xml|console|html|javascript|json|xaml|powershell|azurecli|azcopy|cpp|nodejs|objc|php|python|ruby|sql|swift)", RegexOptions.Compiled);
foreach (var file in files)
{
Console.WriteLine(file);
Console.WriteLine();
var changed = false;
var fileText = File.ReadAllText(file);
var brokenText = fileText.Split("```");
var joinedText = new StringBuilder();
var code = false;
var continueProcessingFiles = true;
foreach (var segment in brokenText)
{
if (code)
{
if (regExp.IsMatch(segment))
{
joinedText.Append($"```{segment}```");
}
else
{
Console.Clear();
Console.WriteLine(file);
Console.WriteLine();
Console.WriteLine(segment);
Console.WriteLine();
Console.WriteLine("csharp=0 fsharp=1 vb=2 xml=3 console=4 html=5 javascript=6 json=7 xaml=8 powershell=9 azurecli=10 azcopy=11 cpp=12 nodejs=13 objc=14 php=15 python=16 ruby=17 sql=18 swift=19 skip=20 stop=21");
int x;
do
{
x = ReadLine();
} while (!Enum.IsDefined(typeof(langIdents), x));
if (x == 21)
{
changed = false;
continueProcessingFiles = false;
break;
}
if (x == 20)
{
joinedText.Append($"```{segment}```");
}
else
{
joinedText.Append($"```{(langIdents)x}{segment}```");
changed = true;
}
}
}
else
{
joinedText.Append($"{segment}");
}
code = !code;
}
if (changed)
{
File.WriteAllText(file, joinedText.ToString());
}
if (!continueProcessingFiles)
{
break;
}
Console.WriteLine();
}
}
static int ReadLine()
{
while (true)
{
var ch = Console.ReadLine();
int result;
if (int.TryParse(ch, out result))
{
return result;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment