Skip to content

add Guideline xml to md project #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions XMLtoMD/GuidelineXmlToMD/Guideline.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;


namespace GuidelineXmlToMD
{
public class Guideline
{
public string Key { get; set; } = "";
public string Text { get; set; } = "";

public string Severity { get; set; } = "";

public string Section { get; set; } = "";
public string Subsection { get; set; } = "";

public List<string> Comments { get; set; } = new List<string>();

public override int GetHashCode()
{
return Subsection.GetHashCode();
}

public override bool Equals(object obj)
{
Guideline otherGuideline = obj as Guideline;

return otherGuideline != null && string.Equals(otherGuideline.Key, this.Key);
}

}

}
42 changes: 42 additions & 0 deletions XMLtoMD/GuidelineXmlToMD/GuidelineXmlFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace GuidelineXmlToMD
{
static class GuidelineXmlFileReader
{
public const string _Guideline = "guideline";
public const string _Key = "key";
public const string _Severity = "severity";

public const string _Section = "section";
public const string _Subsection = "subsection";
public const string _Comments = "comments";


static public ICollection<Guideline> ReadExisitingGuidelinesFile(string pathToExistingGuidelinesXml)
{

XDocument previousGuidelines = XDocument.Load(pathToExistingGuidelinesXml);

HashSet<Guideline> guidelines = new HashSet<Guideline>();

foreach (XElement guidelineFromXml in previousGuidelines.Root.DescendantNodes().OfType<XElement>())
{
Guideline guideline = new Guideline();
guideline.Severity = guidelineFromXml.Attribute(_Severity)?.Value;
guideline.Subsection = guidelineFromXml.Attribute(_Subsection)?.Value;
guideline.Section = guidelineFromXml.Attribute(_Section)?.Value;
guideline.Text = guidelineFromXml?.Value;
guideline.Key = guidelineFromXml.Attribute(_Key)?.Value;

guidelines.Add(guideline);
}
return guidelines;
}

}
}
12 changes: 12 additions & 0 deletions XMLtoMD/GuidelineXmlToMD/GuidelineXmlToMD.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\MarkdownOut\MarkdownOut.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions XMLtoMD/GuidelineXmlToMD/GuidelineXmlToMD.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30225.117
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuidelineXmlToMD", "GuidelineXmlToMD.csproj", "{D9C7CC15-01DB-46FE-922A-6EB41CE1759B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarkdownOut", "..\MarkdownOut\MarkdownOut.csproj", "{FCAD1B53-2C32-4790-96B8-98AAE7027637}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D9C7CC15-01DB-46FE-922A-6EB41CE1759B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9C7CC15-01DB-46FE-922A-6EB41CE1759B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9C7CC15-01DB-46FE-922A-6EB41CE1759B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9C7CC15-01DB-46FE-922A-6EB41CE1759B}.Release|Any CPU.Build.0 = Release|Any CPU
{FCAD1B53-2C32-4790-96B8-98AAE7027637}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FCAD1B53-2C32-4790-96B8-98AAE7027637}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FCAD1B53-2C32-4790-96B8-98AAE7027637}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FCAD1B53-2C32-4790-96B8-98AAE7027637}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A1FCF330-0100-466C-A566-FF92F0B59E10}
EndGlobalSection
EndGlobal
166 changes: 166 additions & 0 deletions XMLtoMD/GuidelineXmlToMD/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using MarkdownOut;


namespace GuidelineXmlToMD
{
class Program
{
static MarkdownOut.MdWriter _MdWriter;
static void Main(string[] args)
{
Console.WriteLine(AssemblyDirectory);
Match match = Regex.Match(AssemblyDirectory, @".*CodingGuidelines");
string guidelineXmlLocation = match.Value + @"\\docs\\Guidelines(8th Edition).xml";

ICollection<Guideline> guidelines = GuidelineXmlFileReader.ReadExisitingGuidelinesFile(guidelineXmlLocation);
_MdWriter = new MdWriter(match.Value + @"\\docs\\coding\\CSharpGuidelines.md");

PrintSections(guidelines);
_MdWriter.WriteLine("Guidelines", format: MdFormat.Heading1);
_MdWriter.WriteLine("");
PrintGuidelinesBySection(guidelines);

_MdWriter.Close();
_MdWriter.Dispose();



//C: \Users\saffron\source\repos\CodingGuidelines\XMLtoMD\GuidelineXmlToMD\bin\Debug\netcoreapp3.1
//C: \Users\saffron\source\repos\CodingGuidelines\docs


}

private static void PrintGuidelinesBySection(ICollection<Guideline> guidelines)
{
foreach (string section in GetSections(guidelines))
{
_MdWriter.WriteLine("");
Console.WriteLine(section);
_MdWriter.WriteLine(section, format: MdFormat.Heading2,style: MdStyle.BoldItalic);



IEnumerable<Guideline> guidelinesInSections = (from guideline in guidelines
where string.Equals(guideline.Section, section)
select guideline);

foreach (string subsection in GetSubSections(guidelines))
{

IEnumerable<Guideline> guidelinesInSubsection = (from guideline in guidelinesInSections
where string.Equals(guideline.Subsection, subsection)
select guideline);

if (guidelinesInSubsection.Count() > 0)
{
Console.WriteLine($" { subsection}");
_MdWriter.WriteLine(subsection, format: MdFormat.Heading3, numNewLines: 1);

foreach (Guideline guideline in guidelinesInSubsection)
{
_MdWriter.WriteUnorderedListItem(GetGuidelineEmoji(guideline) + " " + guideline.Text.Trim('"'), listIndent: 0);
}
}
_MdWriter.WriteLine("", numNewLines: 1);

}
}
}

private static string GetGuidelineEmoji(Guideline guideline)
{
string emoji = "";
switch (guideline.Severity)
{
case "AVOID":
emoji = ":no_entry:";
break;
case "DO NOT":
emoji = ":x:";
break;
case "DO":
emoji = ":heavy_check_mark:";
break;
case "CONSIDER":
emoji = ":grey_question:";
break;

default:
break;
}
return emoji;
}

private static void PrintSections(ICollection<Guideline> guidelines)
{
_MdWriter.WriteLine("Sections", format: MdFormat.Heading2);

List<string> subSections = new List<string>();

List<string> sections = GetSections(guidelines);
foreach (string section in sections)
{
Console.WriteLine(section);

_MdWriter.WriteUnorderedListItem(section, format: MdFormat.InternalLink, listIndent: 0);


subSections = (from guideline in guidelines
where string.Equals(guideline.Section, section)
select guideline.Subsection).Distinct().OrderBy(x => x).ToList();

foreach (string subsection in subSections)
{
Console.WriteLine($" { subsection}");
_MdWriter.WriteUnorderedListItem(subsection, format: MdFormat.InternalLink, listIndent: 1);
}
_MdWriter.WriteLine("", numNewLines: 1);



}



}

public static List<string> GetSections(ICollection<Guideline> guidelines)
{

List<string> sections = (from guideline in guidelines
select guideline.Section).Distinct().OrderBy(x => x).ToList();
return sections;
}

public static List<string> GetSubSections(ICollection<Guideline> guidelines)
{

List<string> subSections = (from guideline in guidelines
select guideline.Subsection).Distinct().OrderBy(x => x).ToList();
return subSections;
}

public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
}





}
58 changes: 58 additions & 0 deletions XMLtoMD/MarkdownOut/MarkdownOut.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FCAD1B53-2C32-4790-96B8-98AAE7027637}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MarkdownOut</RootNamespace>
<AssemblyName>MarkdownOut</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\MarkdownOut.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MdExtensions.cs" />
<Compile Include="MdFormat.cs" />
<Compile Include="MdStyle.cs" />
<Compile Include="MdText.cs" />
<Compile Include="MdWriter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions XMLtoMD/MarkdownOut/MdExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace MarkdownOut {

/// <summary>
/// A container for extension methods related to Markdown styling and formatting.
/// </summary>
public static class MdExtensions {

/// <summary>
/// Styles one or more substrings of the provided string using the specified
/// <see cref="MdStyle"/>.
/// </summary>
/// <param name="str">The string containing the substring to style.</param>
/// <param name="substring">The substring to style.</param>
/// <param name="style">The Markdown style to apply.</param>
/// <param name="firstOnly">
/// If true, only the first occurrence of the substring is styled; otherwise, all
/// occurrences of the substring are styled.
/// </param>
/// <returns>
/// The selectively styled string. If <paramref name="str"/> does not contain any
/// occurrences of <paramref name="substring"/>, it is returned unchanged.
/// </returns>
public static string StyleSubstring(this string str, string substring, MdStyle style,
bool firstOnly = false) {
if (!firstOnly) {
return str.Replace(substring, MdText.Style(substring, style));
}
int pos = str.IndexOf(substring);
if (pos < 0) {
return str;
}
return str.Substring(0, pos) + MdText.Style(substring, style)
+ str.Substring(pos + substring.Length);
}
}
}
Loading