mirror of https://github.com/abpframework/abp.git
committed by
GitHub
25 changed files with 494 additions and 19 deletions
@ -0,0 +1,13 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Docs.Documents |
|||
{ |
|||
public class DocumentParameterDto |
|||
{ |
|||
public string Name { get; set; } |
|||
|
|||
public string DisplayName { get; set; } |
|||
|
|||
public Dictionary<string, string> Values { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Docs.Documents |
|||
{ |
|||
public class DocumentParametersDto |
|||
{ |
|||
public List<DocumentParameterDto> Parameters {get;set;} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
using System; |
|||
using System.ComponentModel.DataAnnotations; |
|||
using Volo.Docs.Language; |
|||
using Volo.Docs.Projects; |
|||
|
|||
namespace Volo.Docs.Documents |
|||
{ |
|||
public class GetParametersDocumentInput |
|||
{ |
|||
public Guid ProjectId { get; set; } |
|||
|
|||
[StringLength(ProjectConsts.MaxVersionNameLength)] |
|||
public string Version { get; set; } |
|||
|
|||
[Required] |
|||
[StringLength(LanguageConsts.MaxLanguageCodeLength)] |
|||
public string LanguageCode { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentRenderParameters : Dictionary<string,string> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentSectionConsts |
|||
{ |
|||
public const string SectionOpenerPrefix = "[doc-section:"; |
|||
public const string SectionOpenerKeyValueSeparator = "="; |
|||
public const string SectionOpenerAndCondition = "&"; |
|||
public const string SectionOpenerOrCondition = "|"; |
|||
public const string SectionOpenerPostfix = "]"; |
|||
public const string SectionCloser = "[/doc-section]"; |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public interface IDocumentSectionRenderer: ITransientDependency |
|||
{ |
|||
Task<string> RenderAsync(string doucment, DocumentRenderParameters parameters); |
|||
|
|||
Task<Dictionary<string, List<string>>> GetAvailableParametersAsync(string document); |
|||
} |
|||
} |
|||
@ -0,0 +1,120 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
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 |
|||
{ |
|||
private const string jsonOpener = "````json"; |
|||
private const string jsonCloser = "````"; |
|||
private const string docs_param = "//[doc-params]"; |
|||
|
|||
public ILogger<ScribanDocumentSectionRenderer> Logger { get; set; } |
|||
|
|||
public ScribanDocumentSectionRenderer() |
|||
{ |
|||
Logger = NullLogger<ScribanDocumentSectionRenderer>.Instance; |
|||
} |
|||
|
|||
public async Task<string> RenderAsync(string document, DocumentRenderParameters parameters = null) |
|||
{ |
|||
Template scribanTemplate = Template.Parse(document); |
|||
|
|||
if (parameters == null) |
|||
{ |
|||
return await scribanTemplate.RenderAsync(); |
|||
} |
|||
|
|||
var result = await scribanTemplate.RenderAsync(parameters); |
|||
return RemoveOptionsJson(result); |
|||
} |
|||
|
|||
public async Task<Dictionary<string, List<string>>> GetAvailableParametersAsync(string document) |
|||
{ |
|||
try |
|||
{ |
|||
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) |
|||
{ |
|||
Logger.LogWarning("Unable to parse parameters of document."); |
|||
return new Dictionary<string, List<string>>(); |
|||
} |
|||
} |
|||
|
|||
private string RemoveOptionsJson(string document) |
|||
{ |
|||
var orgDocument = document; |
|||
try |
|||
{ |
|||
if (!document.Contains(jsonOpener) || !document.Contains(docs_param)) |
|||
{ |
|||
return orgDocument; |
|||
} |
|||
|
|||
var (jsonBeginningIndex, JsonEndingIndex, insideJsonSection) = GetJsonBeginEndIndexesAndPureJson(document); |
|||
|
|||
if (jsonBeginningIndex < 0 || JsonEndingIndex <= 0 || string.IsNullOrWhiteSpace(insideJsonSection)) |
|||
{ |
|||
return orgDocument; |
|||
} |
|||
|
|||
return document.Remove( |
|||
jsonBeginningIndex - jsonOpener.Length, (JsonEndingIndex + jsonCloser.Length) - (jsonBeginningIndex - jsonOpener.Length) |
|||
); |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
return orgDocument; |
|||
} |
|||
} |
|||
|
|||
private (int, int, string) GetJsonBeginEndIndexesAndPureJson(string document) |
|||
{ |
|||
var searchedIndex = 0; |
|||
|
|||
while (searchedIndex < document.Length) |
|||
{ |
|||
var jsonBeginningIndex = document.Substring(searchedIndex).IndexOf(jsonOpener) + jsonOpener.Length + searchedIndex; |
|||
|
|||
if (jsonBeginningIndex < 0) |
|||
{ |
|||
return (-1,-1,""); |
|||
} |
|||
|
|||
var JsonEndingIndex = document.Substring(jsonBeginningIndex).IndexOf(jsonCloser) + jsonBeginningIndex; |
|||
var insideJsonSection = document[jsonBeginningIndex..JsonEndingIndex]; |
|||
|
|||
if (insideJsonSection.IndexOf(docs_param) < 0) |
|||
{ |
|||
searchedIndex = JsonEndingIndex + jsonCloser.Length; |
|||
continue; |
|||
} |
|||
|
|||
return (jsonBeginningIndex, JsonEndingIndex, insideJsonSection); |
|||
} |
|||
|
|||
return (-1, -1, ""); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue