• 00
  • :
  • 00
  • :
  • 00

test

 1 using ActiproSoftware.CodeHighlighter;
 2 using ActiproSoftware.SyntaxEditor;
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Configuration;
 6 using System.Linq;
 7 using System.Net;
 8 using System.Net.Http;
 9 using System.Web;
10 using System.Web.Http;
11 
12 namespace GZUtilsServer.Controllers
13 {
14     public class CodeHighlighterController : ApiController
15     {
16         public class P_Highlight
17         {
18 
19             /// <summary>
20             /// 语言
21             /// </summary>
22             public string Language { get; set; }
23             /// <summary>
24             /// 折叠
25             /// </summary>
26             public string IsCollapse { get; set; }
27             /// <summary>
28             /// 是否显示行号
29             /// </summary>
30             public bool IsShowLineNumber { get; set; }
31             /// <summary>
32             /// 代码块
33             /// </summary>
34             public string CodeText { get; set; }
35         }
36         public string Highlight(P_Highlight data)
37         {
38             string str = GenerateHtmlInline(data.Language, data.IsShowLineNumber, data.CodeText);
39             return str;
40         }
41 
42         string GenerateHtmlInline(string Language, bool IsShowLineNumber, string CodeText)
43         {
44             SyntaxLanguage lang = null;
45 
46 
47             CodeHighlighterConfiguration config = HttpContext.Current.Cache["CodeHighlighterConfig"] as CodeHighlighterConfiguration;
48 
49             //缓存不存在,重新从 web.config 获取并保存缓存
50             if (config == null)
51             {
52                 config = (CodeHighlighterConfiguration)ConfigurationManager.GetSection("codeHighlighter");
53                 HttpContext.Current.Cache.Insert("CodeHighlighterConfig", config);
54             }
55             //获取语言
56             foreach (string key in config.LanguageConfigs.Keys)
57             {
58                 if (key.ToLower() == Language.ToLower())
59                 {
60                     lang = CodeHighlighter.GetLanguage(config, key);
61                     break;
62                 }
63             }
64 
65             //不明语言,不理会返回
66             if (lang == null) return CodeText;
67 
68             CodeHighlighterEngine engine = new CodeHighlighterEngine();
69             engine.OutliningEnabled = false;
70             engine.LineNumberMarginVisible = IsShowLineNumber;
71             engine.LineNumberMarginForeColor = System.Drawing.Color.Red;
72             //engine.SpacesInTabs = (byte)char.Parse("|");
73 
74             string htmlStr = engine.GenerateHtmlInline(string.Empty, CodeText, lang);
75             return htmlStr;
76         }
77     }
78 }

 

posted @ 2021-01-30 15:57  Garson_Zhang  阅读(67)  评论(0编辑  收藏  举报