/
diev
/
FransBouma-DocNet
Обзор
Документация
Войти
/
diev
/
FransBouma-DocNet
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/DocNet/StringExtensions.cs
62 строки
2 KB
Geert van Horrik
Respect PathSpecification when resolving relative urls in the source files to ensure they don't result in 404
20 авг 2017, 21:52
20 авг 2017, 21:52
b07c2a0
Код
Авторство
О чём код?
using System; using System.Text.RegularExpressions; namespace Docnet { internal static class StringExtensions { public static string ApplyUrlFormatting(this string value, UrlFormatting urlFormatting) { var finalValue = string.Empty; string replacementValue = null; switch (urlFormatting) { case UrlFormatting.None: finalValue = value; break; case UrlFormatting.Strip: replacementValue = string.Empty; break; case UrlFormatting.Dashes: replacementValue = "-"; break; default: throw new ArgumentOutOfRangeException(nameof(urlFormatting), urlFormatting, null); } if (replacementValue != null) { var doubleReplacementValue = replacementValue + replacementValue; var regEx = new Regex("[^a-zA-Z0-9 -]"); var splitted = value.Split(new[] { "/", "\\" }, StringSplitOptions.RemoveEmptyEntries); for(var i = 0; i < splitted.Length; i++) { var splittedValue = splitted[i]; if (string.Equals(splittedValue, ".") || string.Equals(splittedValue, "..")) { continue; } splittedValue = regEx.Replace(splittedValue, replacementValue).Replace(" ", replacementValue); if (!string.IsNullOrEmpty(replacementValue)) { while (splittedValue.Contains(doubleReplacementValue)) { splittedValue = splittedValue.Replace(doubleReplacementValue, replacementValue); } } splitted[i] = splittedValue.ToLower(); } finalValue = string.Join("/", splitted); } return finalValue; } } }