private static IEnumerable <int> CalculateMajorVersions(YamlMetaData yamlMetaData) { // Try to find out which major versions are supported var currentDocVersion = GetCurrentDocVersion(); var semverCurrent = new Semver.SemVersion(currentDocVersion); bool hasFrom = Semver.SemVersion.TryParse(yamlMetaData.VersionFrom, out Semver.SemVersion semverFrom); bool hasTo = Semver.SemVersion.TryParse(yamlMetaData.VersionTo, out Semver.SemVersion semverTo); if (hasFrom == false) { semverFrom = new Semver.SemVersion(currentDocVersion); } if (hasTo == false) { semverTo = semverFrom < semverCurrent ? semverCurrent : semverFrom; } if (semverFrom > semverTo) { semverFrom = semverTo; } var matchingMajorVersions = new List <int>(); for (int i = semverFrom.Major; i <= semverTo.Major; i++) { yield return(i); } }
/// <summary> /// Add all YAML fields to the index /// </summary> /// <param name="simpleDataSet"></param> /// <param name="lines"></param> /// <returns>The linenumber of the second YAML marker</returns> private static int AddYamlFields(SimpleDataSet simpleDataSet, List <string> lines) { // Check if the first line is a YAML marker // YAML is only accepted if it's on the top of the document // because empty lines are already removed, the index needs to be 0 bool hasYaml = lines.ElementAt(0).TrimEnd() == "---"; int secondYamlMarker = 0; if (hasYaml) { // Find the "next" triple dash starting from the second line // But first trim all trailing spaces as this only creates issues which are hard to debug // and unclear for users. Make sure you have a ToList because IEnumerable has no IndexOf() secondYamlMarker = lines .Select(l => l.TrimEnd()) .ToList() .IndexOf("---", 1); // add all yaml together and parse YAML meta data YamlMetaData yamlMetaData = new YamlMetaData(); if (secondYamlMarker > 0) { // we found a second marker, so we have YAML data available var yamlInput = new StringBuilder(); for (int i = 1; i < secondYamlMarker; i++) { yamlInput.AppendLine(lines.ElementAt(i)); } ; // Try to convert the YAML text to a strongly typed model using YamlDotNet var deserializer = new DeserializerBuilder() .WithNamingConvention(new YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention()) .IgnoreUnmatchedProperties() .Build(); yamlMetaData = deserializer.Deserialize <YamlMetaData>(yamlInput.ToString()); } // Add Yaml stuff to the LUCENE index simpleDataSet.RowData.Add("tags", yamlMetaData.Tags); simpleDataSet.RowData.Add("keywords", yamlMetaData.Keywords); simpleDataSet.RowData.Add("versionFrom", yamlMetaData.VersionFrom); simpleDataSet.RowData.Add("versionTo", yamlMetaData.VersionTo); var matchingMajorVersions = CalculateMajorVersions(yamlMetaData); simpleDataSet.RowData.Add("majorVersion", string.Join(" ", matchingMajorVersions)); } else { // no YAML information, add the current version as majorVersion simpleDataSet.RowData.Add("majorVersion", GetCurrentDocVersion().ToString()); } return(secondYamlMarker); }
/// <summary> /// Add all YAML fields to the index /// </summary> /// <param name="simpleDataSet"></param> /// <param name="lines"></param> /// <returns>The linenumber of the second YAML marker</returns> private static int AddYamlFields(SimpleDataSet simpleDataSet, List <string> lines) { // Check if the first line is a YAML marker // YAML is only accepted if it's on the top of the document // because empty lines are already removed, the index needs to be 0 bool hasYaml = lines.ElementAt(0).TrimEnd() == "---"; int secondYamlMarker = 0; if (hasYaml) { // Find the "next" triple dash starting from the second line // But first trim all trailing spaces as this only creates issues which are hard to debug // and unclear for users. Make sure you have a ToList because IEnumerable has no IndexOf() secondYamlMarker = lines .Select(l => l.TrimEnd()) .ToList() .IndexOf("---", 1); // add all yaml together and parse YAML meta data YamlMetaData yamlMetaData = new YamlMetaData(); if (secondYamlMarker > 0) { // we found a second marker, so we have YAML data available var yamlInput = new StringBuilder(); for (int i = 1; i < secondYamlMarker; i++) { // the line must contain some valid yaml, key-value pairs are // separated by a ":" so only add lines that have that var line = lines.ElementAt(i); if (line.InvariantContains(":")) { yamlInput.AppendLine(line); } } ; // Try to convert the YAML text to a strongly typed model using YamlDotNet var deserializer = new DeserializerBuilder() .WithNamingConvention(new YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention()) .IgnoreUnmatchedProperties() .Build(); try { yamlMetaData = deserializer.Deserialize <YamlMetaData>(yamlInput.ToString()); } catch (SemanticErrorException ex) { LogHelper.Error(typeof(ExamineHelper), "Could not parse the YAML meta data {0}" + yamlInput, ex); yamlMetaData.Tags = "yamlissue"; } } // Add Yaml stuff to the LUCENE index simpleDataSet.RowData.Add("tags", yamlMetaData.Tags); simpleDataSet.RowData.Add("keywords", yamlMetaData.Keywords); simpleDataSet.RowData.Add("versionFrom", yamlMetaData.VersionFrom); simpleDataSet.RowData.Add("versionTo", yamlMetaData.VersionTo); simpleDataSet.RowData.Add("assetID", yamlMetaData.AssetId); simpleDataSet.RowData.Add("product", yamlMetaData.Product); simpleDataSet.RowData.Add("topics", yamlMetaData.Topics); simpleDataSet.RowData.Add("audience", yamlMetaData.Topics); simpleDataSet.RowData.Add("complexity", yamlMetaData.Complexity); simpleDataSet.RowData.Add("meta.Title", yamlMetaData.MetaTitle); simpleDataSet.RowData.Add("meta.Description", yamlMetaData.MetaDescription); simpleDataSet.RowData.Add("versionRemoved", yamlMetaData.VersionRemoved); simpleDataSet.RowData.Add("needsV8Update", yamlMetaData.NeedsV8Update); var matchingMajorVersions = CalculateMajorVersions(yamlMetaData); simpleDataSet.RowData.Add("majorVersion", string.Join(" ", matchingMajorVersions)); } else { // no YAML information, add the current version as majorVersion simpleDataSet.RowData.Add("majorVersion", GetCurrentDocVersion().ToString()); } return(secondYamlMarker); }