Skip to content

Instantly share code, notes, and snippets.

@atifaziz
Created March 3, 2015 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atifaziz/3b4c57e98d3d71c5f222 to your computer and use it in GitHub Desktop.
Save atifaziz/3b4c57e98d3d71c5f222 to your computer and use it in GitHub Desktop.
DirectivesParser from Razor Generator changeset fd2494a3d3f4
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace RazorGenerator.Core
{
internal static class DirectivesParser
{
private const string GlobalDirectivesFileName = "razorgenerator.directives";
public static Dictionary<string, string> ParseDirectives(string baseDirectory, string fullPath)
{
var directives = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string directivesPath;
if (TryFindGlobalDirectivesFile(baseDirectory, fullPath, out directivesPath))
{
ParseGlobalDirectives(directives, directivesPath);
}
ParseFileDirectives(directives, fullPath);
return directives;
}
/// <summary>
/// Attempts to locate the nearest global directive file by
/// </summary>
private static bool TryFindGlobalDirectivesFile(string baseDirectory, string fullPath, out string path)
{
baseDirectory = baseDirectory.TrimEnd(Path.DirectorySeparatorChar);
var directivesDirectory = Path.GetDirectoryName(fullPath).TrimEnd(Path.DirectorySeparatorChar);
while (directivesDirectory != null && directivesDirectory.Length >= baseDirectory.Length)
{
path = Path.Combine(directivesDirectory, GlobalDirectivesFileName);
if (File.Exists(path))
{
return true;
}
directivesDirectory = Path.GetDirectoryName(directivesDirectory).TrimEnd(Path.DirectorySeparatorChar);
}
path = null;
return false;
}
private static void ParseGlobalDirectives(Dictionary<string, string> directives, string directivesPath)
{
var fileContent = File.ReadAllText(directivesPath);
ParseKeyValueDirectives(directives, fileContent);
}
private static void ParseFileDirectives(Dictionary<string, string> directives, string filePath)
{
var inputFileContent = File.ReadAllText(filePath);
int index = inputFileContent.IndexOf("*@", StringComparison.OrdinalIgnoreCase);
if (inputFileContent.TrimStart().StartsWith("@*") && index != -1)
{
string directivesLine = inputFileContent.Substring(0, index).TrimStart('*', '@');
ParseKeyValueDirectives(directives, directivesLine);
}
}
private static void ParseKeyValueDirectives(Dictionary<string, string> directives, string directivesLine)
{
// TODO: Make this better.
var regex = new Regex(@"\b(?<Key>\w+)\s*:\s*(?<Value>[~\\\/\w\.]+)\b", RegexOptions.ExplicitCapture);
foreach (Match item in regex.Matches(directivesLine))
{
var key = item.Groups["Key"].Value;
var value = item.Groups["Value"].Value;
directives[key] = value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment