< Summary

Information
Class: OzonOrdersWeb.Authorization.Discovery.RazorViewTitleExtractor
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Authorization/Discovery/RazorViewTitleExtractor.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 103
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Authorization/Discovery/RazorViewTitleExtractor.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2
 3namespace OzonOrdersWeb.Authorization.Discovery;
 4
 5/// <summary>
 6/// Пытается получить заголовок страницы из Razor-файла: ViewData["Title"] / ViewBag.Title
 7/// (статическая строка или префикс до { в $"...").
 8/// </summary>
 9public static class RazorViewTitleExtractor
 10{
 11    public const int MaxTitleLength = 300;
 12
 13    public static string? TryGetTitleFromConventionalView(
 14        string contentRoot,
 15        string? area,
 16        string controller,
 17        string action)
 18    {
 019        var path = TryResolveConventionalViewPath(contentRoot, area, controller, action);
 020        return path is null ? null : TryExtractTitleFromFile(path);
 21    }
 22
 23    public static string? TryResolveConventionalViewPath(
 24        string contentRoot,
 25        string? area,
 26        string controller,
 27        string action)
 28    {
 029        var file = action + ".cshtml";
 030        if (string.IsNullOrEmpty(area))
 31        {
 032            var p = Path.Combine(contentRoot, "Views", controller, file);
 033            if (File.Exists(p)) return p;
 34        }
 35        else
 36        {
 037            var p = Path.Combine(contentRoot, "Areas", area, "Views", controller, file);
 038            if (File.Exists(p)) return p;
 39        }
 40
 041        return null;
 42    }
 43
 44    public static string? TryExtractTitleFromFile(string absolutePath)
 45    {
 046        if (string.IsNullOrEmpty(absolutePath) || !File.Exists(absolutePath)) return null;
 047        var text = File.ReadAllText(absolutePath);
 048        if (text.Length > 64_000) text = text[..64_000];
 049        return TryExtractTitleFromViewContent(text);
 50    }
 51
 52    public static string? TryExtractTitleFromViewContent(string text)
 53    {
 54        // 1) ViewData["Title"] = "…"
 055        var m = Regex.Match(
 056            text,
 057            @"ViewData\[\s*""Title""\s*\]\s*=\s*""(?<t>[^""]*)""",
 058            RegexOptions.IgnoreCase);
 059        if (m.Success) return NormalizeTitle(m.Groups["t"].Value);
 60
 061        m = Regex.Match(
 062            text,
 063            @"ViewData\[\s*'Title'\s*\]\s*=\s*""(?<t>[^""]*)""",
 064            RegexOptions.IgnoreCase);
 065        if (m.Success) return NormalizeTitle(m.Groups["t"].Value);
 66
 67        // 2) ViewBag.Title = "…"
 068        m = Regex.Match(
 069            text,
 070            @"ViewBag\.Title\s*=\s*""(?<t>[^""]*)""",
 071            RegexOptions.IgnoreCase);
 072        if (m.Success) return NormalizeTitle(m.Groups["t"].Value);
 73
 74        // 3) $"...": только статический кусок до первой { (или вся строка без {)
 075        m = Regex.Match(
 076            text,
 077            @"ViewData\[\s*""Title""\s*\]\s*=\s*\$""(?<t>[^""{]*)",
 078            RegexOptions.IgnoreCase);
 079        if (m.Success) return NormalizeTitle(m.Groups["t"].Value);
 80
 081        m = Regex.Match(
 082            text,
 083            @"ViewBag\.Title\s*=\s*\$""(?<t>[^""{]*)",
 084            RegexOptions.IgnoreCase);
 085        if (m.Success) return NormalizeTitle(m.Groups["t"].Value);
 86
 087        return null;
 88    }
 89
 90    public static string? NormalizeTitle(string? raw)
 91    {
 092        if (string.IsNullOrWhiteSpace(raw)) return null;
 093        var t = raw.Trim();
 094        t = t.TrimEnd(' ', '\t', '-', '·', '—', ':');
 095        if (t.Length == 0) return null;
 096        if (t.Length > MaxTitleLength) t = t[..MaxTitleLength];
 097        return t;
 98    }
 99
 100    /// <summary>Текст, который discover создаёт до поиска по представлениям.</summary>
 101    public static bool IsLegacyControllerActionLabel(string controller, string action, string displayName) =>
 0102        string.Equals(displayName, $"{controller} / {action}", StringComparison.Ordinal);
 103}