|
|
|
@ -4,94 +4,102 @@ using System.Linq; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Scriban; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Microsoft.Extensions.Logging.Abstractions; |
|
|
|
|
|
|
|
namespace Volo.Docs.HtmlConverting |
|
|
|
{ |
|
|
|
public class ScribanDocumentSectionRenderer : IDocumentSectionRenderer |
|
|
|
{ |
|
|
|
public async Task<string> Render(string document, DocumentRenderParameters parameters = null) |
|
|
|
private const string jsonOpener = "````json"; |
|
|
|
private const string jsonCloser = "````"; |
|
|
|
private const string docs_param = "//[doc-params]"; |
|
|
|
|
|
|
|
public ILogger<ScribanDocumentSectionRenderer> Logger { get; set; } |
|
|
|
|
|
|
|
public ScribanDocumentSectionRenderer() |
|
|
|
{ |
|
|
|
Template scribanTemplate; |
|
|
|
if (parameters == null) |
|
|
|
{ |
|
|
|
scribanTemplate = Template.Parse(document); |
|
|
|
return scribanTemplate.Render(); |
|
|
|
} |
|
|
|
Logger = NullLogger<ScribanDocumentSectionRenderer>.Instance; |
|
|
|
} |
|
|
|
|
|
|
|
var p2 = new Dictionary<string, string>(); |
|
|
|
public async Task<string> RenderAsync(string document, DocumentRenderParameters parameters = null) |
|
|
|
{ |
|
|
|
Template scribanTemplate = Template.Parse(document); |
|
|
|
|
|
|
|
foreach (var item in parameters) |
|
|
|
if (parameters == null) |
|
|
|
{ |
|
|
|
p2.Add(item.Key, item.Value); |
|
|
|
return await scribanTemplate.RenderAsync(); |
|
|
|
} |
|
|
|
|
|
|
|
scribanTemplate = Template.Parse(document); |
|
|
|
var result = scribanTemplate.Render(p2); |
|
|
|
var result = await scribanTemplate.RenderAsync(parameters); |
|
|
|
return RemoveOptionsJson(result); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Dictionary<string, List<string>>> GetAvailableParametersAsync(string document) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
return await RemoveOptionsJson(result); |
|
|
|
if (!document.Contains(jsonOpener) || !document.Contains(docs_param)) |
|
|
|
{ |
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
} |
|
|
|
|
|
|
|
var (jsonBeginningIndex, JsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document); |
|
|
|
|
|
|
|
if (jsonBeginningIndex < 0 || JsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) |
|
|
|
{ |
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
} |
|
|
|
|
|
|
|
var pureJson = insideJsonSection.Replace(docs_param, "").Trim(); |
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(pureJson); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
return scribanTemplate.Render(); |
|
|
|
Logger.LogWarning("Unable to parse parameters of document."); |
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Dictionary<string, List<string>>> GetAvailableParametersAsync(string document) |
|
|
|
private string RemoveOptionsJson(string document) |
|
|
|
{ |
|
|
|
var orgDocument = document; |
|
|
|
try |
|
|
|
{ |
|
|
|
var jsonOpener = "````json"; |
|
|
|
var jsonCloser = "````"; |
|
|
|
var docs_param = "//[doc-params]"; |
|
|
|
|
|
|
|
if (!document.Contains(jsonOpener)) |
|
|
|
if (!document.Contains(jsonOpener) || !document.Contains(docs_param)) |
|
|
|
{ |
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
return orgDocument; |
|
|
|
} |
|
|
|
|
|
|
|
var searchedIndex = 0; |
|
|
|
while (searchedIndex < document.Length) |
|
|
|
{ |
|
|
|
var jsonBeginningIndex = document.Substring(searchedIndex).IndexOf(jsonOpener) + jsonOpener.Length + searchedIndex; |
|
|
|
var JsonEndingIndex = document.Substring(jsonBeginningIndex).IndexOf(jsonCloser) + jsonBeginningIndex; |
|
|
|
var insideJsonSection = document[jsonBeginningIndex..JsonEndingIndex]; |
|
|
|
|
|
|
|
if (insideJsonSection.IndexOf(docs_param) < 0) |
|
|
|
{ |
|
|
|
searchedIndex = JsonEndingIndex + jsonCloser.Length; |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
var pureJson = insideJsonSection.Replace(docs_param, "").Trim(); |
|
|
|
var (jsonBeginningIndex, JsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document); |
|
|
|
|
|
|
|
return JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(pureJson); |
|
|
|
if (jsonBeginningIndex < 0 || JsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) |
|
|
|
{ |
|
|
|
return orgDocument; |
|
|
|
} |
|
|
|
|
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
return document.Remove( |
|
|
|
jsonBeginningIndex - jsonOpener.Length, (JsonEndingIndex + jsonCloser.Length) - (jsonBeginningIndex - jsonOpener.Length) |
|
|
|
); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
//log
|
|
|
|
return new Dictionary<string, List<string>>(); |
|
|
|
return orgDocument; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private async Task<string> RemoveOptionsJson(string document) |
|
|
|
private (int, int, string) GetJsonBeginEndIndexesAndPureJson(string document) |
|
|
|
{ |
|
|
|
var jsonOpener = "````json"; |
|
|
|
var jsonCloser = "````"; |
|
|
|
var docs_param = "//[doc-params]"; |
|
|
|
|
|
|
|
var searchedIndex = 0; |
|
|
|
|
|
|
|
while (searchedIndex < document.Length) |
|
|
|
{ |
|
|
|
var jsonBeginningIndex = document.Substring(searchedIndex).IndexOf(jsonOpener) + jsonOpener.Length + searchedIndex; |
|
|
|
|
|
|
|
if (jsonBeginningIndex < 0) |
|
|
|
{ |
|
|
|
return document; |
|
|
|
return (-1,-1,""); |
|
|
|
} |
|
|
|
|
|
|
|
var JsonEndingIndex = document.Substring(jsonBeginningIndex).IndexOf(jsonCloser) + jsonBeginningIndex; |
|
|
|
@ -103,13 +111,10 @@ namespace Volo.Docs.HtmlConverting |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
return document.Remove( |
|
|
|
jsonBeginningIndex - jsonOpener.Length, (JsonEndingIndex + jsonCloser.Length) - (jsonBeginningIndex - jsonOpener.Length) |
|
|
|
); |
|
|
|
|
|
|
|
return (jsonBeginningIndex, JsonEndingIndex, insideJsonSection); |
|
|
|
} |
|
|
|
|
|
|
|
return document; |
|
|
|
return (-1, -1, ""); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|