Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static class ProjectConsts
public const int MaxDefaultDocumentNameLength = 128;
public const int MaxNavigationDocumentNameLength = 128;
public const int MaxParametersDocumentNameLength = 128;
public const int MaxPartialTemplatesDocumentNameLength = 128;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used?

public const int MaxLatestVersionBranchNameLength = 128;
public const int MaxVersionNameLength = 128;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public virtual async Task<Document> GetDocumentAsync(Project project, string doc
LocalDirectory = localDirectory,
FileName = fileName,
Contributors = new List<DocumentContributor>(),
//Contributors = !isNavigationDocument && !isParameterDocument ? await GetContributors(commitHistoryUrl, token, userAgent): new List<DocumentContributor>(),
//Contributors = !isNavigationDocument && !isParameterDocument && !isPartialTemplatesDocumentName ? await GetContributors(commitHistoryUrl, token, userAgent): new List<DocumentContributor>(),
Version = version,
Content = await DownloadWebContentAsStringAsync(rawDocumentUrl, token, userAgent)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Volo.Docs.HtmlConverting
{
public class DocumentPartialTemplateContent
{
public string Path { get; set; }

public string Content { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace Volo.Docs.HtmlConverting
{
public class DocumentPartialTemplateWithValuesDto
{
public string Path { get; set; }

public Dictionary<string, string> Parameters { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace Volo.Docs.HtmlConverting
{
public interface IDocumentSectionRenderer: ITransientDependency
{
Task<string> RenderAsync(string doucment, DocumentRenderParameters parameters);
Task<string> RenderAsync(string doucment, DocumentRenderParameters parameters = null, List<DocumentPartialTemplateContent> partialTemplates = null);

Task<Dictionary<string, List<string>>> GetAvailableParametersAsync(string document);

Task<List<DocumentPartialTemplateWithValuesDto>> GetPartialTemplatesInDocumentAsync(string documentContent);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Scriban;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Docs.Documents;

namespace Volo.Docs.HtmlConverting
{
Expand All @@ -14,6 +16,7 @@ public class ScribanDocumentSectionRenderer : IDocumentSectionRenderer
private const string jsonOpener = "````json";
private const string jsonCloser = "````";
private const string docs_param = "//[doc-params]";
private const string docs_templates = "//[doc-template]";

public ILogger<ScribanDocumentSectionRenderer> Logger { get; set; }

Expand All @@ -22,8 +25,13 @@ public ScribanDocumentSectionRenderer()
Logger = NullLogger<ScribanDocumentSectionRenderer>.Instance;
}

public async Task<string> RenderAsync(string document, DocumentRenderParameters parameters = null)
public async Task<string> RenderAsync(string document, DocumentRenderParameters parameters = null, List<DocumentPartialTemplateContent> partialTemplates = null)
{
if (partialTemplates != null && partialTemplates.Any())
{
document = SetPartialTemplates(document, partialTemplates);
}

var scribanTemplate = Template.Parse(document);

if (parameters == null)
Expand Down Expand Up @@ -99,7 +107,7 @@ private string RemoveOptionsJson(string document)

if (jsonBeginningIndex < 0)
{
return (-1,-1,"");
return (-1, -1, "");
}

var jsonEndingIndex = document.Substring(jsonBeginningIndex).IndexOf(jsonCloser, StringComparison.Ordinal) + jsonBeginningIndex;
Expand All @@ -116,5 +124,78 @@ private string RemoveOptionsJson(string document)

return (-1, -1, "");
}

public async Task<List<DocumentPartialTemplateWithValuesDto>> GetPartialTemplatesInDocumentAsync(string documentContent)
{
var templates = new List<DocumentPartialTemplateWithValuesDto>();

while (documentContent.Contains(jsonOpener))
{
var afterJsonOpener = documentContent.Substring(
documentContent.IndexOf(jsonOpener, StringComparison.Ordinal) + jsonOpener.Length);

var betweenJsonOpenerAndCloser = afterJsonOpener.Substring(0,
afterJsonOpener.IndexOf(jsonCloser, StringComparison.Ordinal));

documentContent = afterJsonOpener.Substring(
afterJsonOpener.IndexOf(jsonCloser, StringComparison.Ordinal) + jsonCloser.Length);

if (!betweenJsonOpenerAndCloser.Contains(docs_templates))
{
continue;
}

var json = betweenJsonOpenerAndCloser.Substring(betweenJsonOpenerAndCloser.IndexOf(docs_templates, StringComparison.Ordinal) + docs_templates.Length);

var template = JsonConvert.DeserializeObject<DocumentPartialTemplateWithValuesDto>(json);

templates.Add(template);
}

return templates;
}

private string SetPartialTemplates(string document, List<DocumentPartialTemplateContent> templates)
{
var newDocument = new StringBuilder();

while (document.Contains(jsonOpener))
{
var beforeJson = document.Substring(0,
document.IndexOf(jsonOpener, StringComparison.Ordinal) + jsonOpener.Length);

var afterJsonOpener = document.Substring(
document.IndexOf(jsonOpener, StringComparison.Ordinal) + jsonOpener.Length);

var betweenJsonOpenerAndCloser = afterJsonOpener.Substring(0,
afterJsonOpener.IndexOf(jsonCloser, StringComparison.Ordinal));

if (!betweenJsonOpenerAndCloser.Contains(docs_templates))
{
document = afterJsonOpener.Substring(
afterJsonOpener.IndexOf(jsonCloser, StringComparison.Ordinal) + jsonCloser.Length);
newDocument.Append(beforeJson + betweenJsonOpenerAndCloser + jsonCloser);
continue;
}

var json = betweenJsonOpenerAndCloser.Substring(betweenJsonOpenerAndCloser.IndexOf(docs_templates, StringComparison.Ordinal) + docs_templates.Length);

var templatePath = JsonConvert.DeserializeObject<DocumentPartialTemplateWithValuesDto>(json)?.Path;

var template = templates.FirstOrDefault(t => t.Path == templatePath);

var beforeTemplate = document.Substring(0,
document.IndexOf(jsonOpener, StringComparison.Ordinal));

newDocument.Append(beforeTemplate + template?.Content + jsonCloser);

document = afterJsonOpener.Substring(
afterJsonOpener.IndexOf(jsonCloser, StringComparison.Ordinal) + jsonCloser.Length);
}

newDocument.Append(document);

return newDocument.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc.Localization;
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages;
using Volo.Abp.Domain.Entities;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class IndexModel : AbpPageModel

public DocumentParametersDto DocumentPreferences { get; set; }

public DocumentRenderParameters UserPreferences { get; set; }
public DocumentRenderParameters UserPreferences { get; set; } = new DocumentRenderParameters();

private readonly IDocumentAppService _documentAppService;
private readonly IDocumentToHtmlConverterFactory _documentToHtmlConverterFactory;
Expand Down Expand Up @@ -394,10 +395,9 @@ private async Task ConvertDocumentContentToHtmlAsync()
await SetDocumentPreferencesAsync();
SetUserPreferences();

UserPreferences.Add("Document_Language_Code", LanguageCode);
UserPreferences.Add("Document_Version", Version);
var partialTemplates = await GetDocumentPartialTemplatesAsync();

Document.Content = await _documentSectionRenderer.RenderAsync(Document.Content, UserPreferences);
Document.Content = await _documentSectionRenderer.RenderAsync(Document.Content, UserPreferences, partialTemplates);

var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format);
var content = converter.Convert(Project, Document, GetSpecificVersionOrLatest(), LanguageCode);
Expand All @@ -418,9 +418,56 @@ private async Task ConvertDocumentContentToHtmlAsync()
Document.Content = content;
}

private async Task<List<DocumentPartialTemplateContent>> GetDocumentPartialTemplatesAsync()
{
var partialTemplatesInDocument = await _documentSectionRenderer.GetPartialTemplatesInDocumentAsync(Document.Content);

if (!partialTemplatesInDocument?.Any(t => t.Parameters != null) ?? true)
{
return null;
}

foreach (var partialTemplates in partialTemplatesInDocument)
{
foreach (var parameter in partialTemplates.Parameters)
{
if (!UserPreferences.ContainsKey(parameter.Key))
{
UserPreferences.Add(parameter.Key, parameter.Value);
}
else
{
UserPreferences[parameter.Key] = parameter.Value;
}
}
}

var contents = new List<DocumentPartialTemplateContent>();

foreach (var partialTemplate in partialTemplatesInDocument)
{
var content = (await _documentAppService.GetAsync(new GetDocumentInput
{
LanguageCode = LanguageCode,
Name = partialTemplate.Path,
ProjectId = Project.Id,
Version = Version
})).Content;

contents.Add(new DocumentPartialTemplateContent
{
Path = partialTemplate.Path,
Content = content
});
}

return contents;
}

private void SetUserPreferences()
{
UserPreferences = new DocumentRenderParameters();
UserPreferences.Add("Document_Language_Code", LanguageCode);
UserPreferences.Add("Document_Version", Version);

var cookie = Request.Cookies["AbpDocsPreferences"];

Expand Down Expand Up @@ -473,6 +520,7 @@ private void SetUserPreferences()
UserPreferences.Add(parameter.Name + "_Value", parameter.Values.FirstOrDefault().Value);
}
}

}

public async Task SetDocumentPreferencesAsync()
Expand All @@ -485,6 +533,7 @@ public async Task SetDocumentPreferencesAsync()
Version = Version
});


if (projectParameters?.Parameters == null)
{
return;
Expand Down