Personal website
at master 81 lines 2.3 kB view raw
1/* 2 This file is part of NKK. 3 4 NKK is free software: you can redistribute it and/or modify it under the 5 terms of the GNU Affero General Public License as published by the Free 6 Software Foundation, either version 3 of the License, or (at your option) 7 any later version. 8 9 NKK is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 11 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for 12 more details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with NKK. If not, see <http://www.gnu.org/licenses/>. 16*/ 17 18#pragma warning disable BL0006 19 20using System.Text; 21using Microsoft.AspNetCore.Components; 22using Microsoft.AspNetCore.Components.Rendering; 23using Microsoft.AspNetCore.Components.RenderTree; 24 25using NUglify; 26using NUglify.Html; 27 28namespace NKK; 29 30public static class Utils { 31 private static readonly HtmlSettings HtmlSettings = new HtmlSettings() { 32 RemoveComments = false, 33 RemoveOptionalTags = false, 34 RemoveInvalidClosingTags = false, 35 RemoveEmptyAttributes = false, 36 RemoveScriptStyleTypeAttribute = false, 37 ShortBooleanAttribute = false, 38 IsFragmentOnly = true, 39 MinifyJs = false, 40 MinifyJsAttributes = false, 41 MinifyCss = false, 42 MinifyCssAttributes = false, 43 }; 44 45 public static String OptimizeHtml(String html) { 46 return Uglify.Html(html, HtmlSettings).Code ?? String.Empty; 47 } 48 49 extension<T>(IEnumerable<T> enumerable) { 50 public T RandomElement() { 51 int index = (new Random()).Next(0, enumerable.Count()); 52 return enumerable.ElementAt(index); 53 } 54 } 55 56 extension(RenderFragment fragment) { 57 public String RenderString() { 58 StringBuilder builder = new StringBuilder(); 59 RenderTreeBuilder renderer = new RenderTreeBuilder(); 60 fragment(renderer); 61 62 renderer.GetFrames().Array.ToList() 63 .ForEach(f => { 64 // this seems like the only types that have actual content? 65 // idk tho 66 switch (f.FrameType) { 67 case RenderTreeFrameType.Markup: 68 builder.Append(f.MarkupContent); 69 break; 70 case RenderTreeFrameType.Text: 71 builder.Append(f.TextContent); 72 break; 73 default: 74 break; 75 } 76 }); 77 78 return builder.ToString() ?? String.Empty; 79 } 80 } 81}