1- using System ;
1+ using Magicodes . ExporterAndImporter . Core ;
2+ using Magicodes . ExporterAndImporter . Core . Models ;
3+ using Microsoft . CodeAnalysis . CSharp . Scripting ;
4+ using System ;
25using System . Collections . Generic ;
36using System . IO ;
47using System . Linq ;
5- using System . Threading . Tasks ;
6- using Magicodes . ExporterAndImporter . Core ;
7- using Magicodes . ExporterAndImporter . Core . Models ;
88using System . Reflection ;
9- using RazorEngine ;
10- using RazorEngine . Configuration ;
9+ using System . Text . RegularExpressions ;
10+ using System . Threading . Tasks ;
1111using Encoding = System . Text . Encoding ;
12- using RazorEngine . Templating ;
1312
1413namespace Magicodes . ExporterAndImporter . Html
1514{
1615 /// <summary>
1716 /// HTML导出
1817 /// </summary>
19- public class HtmlExporter : IExporterByTemplate
18+ public class HtmlExporter : IExporterByTemplate
2019 {
2120 /// <summary>
2221 /// 根据模板导出
@@ -26,19 +25,59 @@ public class HtmlExporter: IExporterByTemplate
2625 /// <param name="dataItems"></param>
2726 /// <param name="htmlTemplate">Html模板内容</param>
2827 /// <returns></returns>
29- public Task < TemplateFileInfo > ExportByTemplate < T > ( string fileName , IList < T > dataItems , string htmlTemplate = null ) where T : class
28+ public async Task < TemplateFileInfo > ExportByTemplate < T > ( string fileName , IList < T > dataItems , string htmlTemplate = null ) where T : class
3029 {
3130 if ( string . IsNullOrWhiteSpace ( htmlTemplate ) )
3231 {
33- var defaultHtmlTpl = ReadManifestData < HtmlExporter > ( "default.cshtml" ) ;
34- var config = new TemplateServiceConfiguration ( )
32+ var defaultHtmlTpl = ReadManifestData < HtmlExporter > ( "default.html" ) ;
33+
34+ var script = CSharpScript . Create < string > ( "string htmlStr=\" \" ;" ) ;
35+ var matches = Regex . Matches ( defaultHtmlTpl , @"@foreach[\w\s{}.<>()/\[\]\+\-?|\\*`$]+}" , RegexOptions . Multiline ) ;
36+ foreach ( Match match in matches )
3537 {
36-
37- } ;
38- var service = RazorEngineService . Create ( config ) ;
39- Engine . Razor = service ;
40- var res = Engine . Razor . RunCompile ( defaultHtmlTpl , fileName , typeof ( IList < T > ) , dataItems ) ;
41- var t = new TemplateFileInfo ( ) ;
38+ if ( match . Success )
39+ {
40+ //var sb = new StringBuilder();
41+ var list = Regex . Split ( match . Value , Environment . NewLine ) . Where ( p => p . Trim ( ) != "{" && p . Trim ( ) != "}" )
42+ . ToList ( ) ;
43+ foreach ( var dataItem in dataItems )
44+ {
45+ foreach ( var line in list )
46+ {
47+ script . ContinueWith ( "htmlStr +=\" \\ n\" ;" ) ;
48+ if ( line . Contains ( "{" ) )
49+ {
50+ var codeStr = await CSharpScript . RunAsync < string > ( "var res=$\" " + line + "\" ;" , globals : dataItem ) ;
51+ var codeRes = await codeStr . ContinueWithAsync ( "res" ) ;
52+ script . ContinueWith ( "htmlStr +=$\" " + codeRes . ReturnValue + "\" ;" ) ;
53+ }
54+ else
55+ {
56+ script . ContinueWith ( "htmlStr +=$\" " + line + "\" ;" ) ;
57+ }
58+
59+ //var res = Regex.Matches(line, @"{{[\w\s.()/\[\]\+\-?|\\*`$]+}}", RegexOptions.None);
60+ //if (res.Count == 0)
61+ //{
62+ // script.ContinueWith("htmlStr +=\"" + line + "\"");
63+ //}
64+ //else
65+ //{
66+ // script.ContinueWith("htmlStr +=$\"" + line + "\"");
67+ //}
68+ }
69+ }
70+
71+
72+
73+ }
74+ }
75+ var result = await script . RunAsync ( ) ;
76+ var a = await result . ContinueWithAsync ( "res" ) ;
77+ var test = a . ReturnValue ;
78+ //var result = script.;
79+ //var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(IList<T>));
80+ //script.Compile();
4281 }
4382 throw new NotImplementedException ( ) ;
4483 }
@@ -54,7 +93,7 @@ public static string ReadManifestData<TSource>(string embeddedFileName) where TS
5493 {
5594 throw new InvalidOperationException ( "Could not load manifest resource stream." ) ;
5695 }
57- using ( var reader = new StreamReader ( stream , encoding : Encoding . UTF8 ) )
96+ using ( var reader = new StreamReader ( stream , encoding : Encoding . UTF8 ) )
5897 {
5998 return reader . ReadToEnd ( ) ;
6099 }
0 commit comments