mirror of https://github.com/abpframework/abp.git
9 changed files with 234 additions and 2 deletions
@ -0,0 +1,10 @@ |
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentSectionConsts |
|||
{ |
|||
public const string SectionOpenerPrefix = "[doc-section:"; |
|||
public const string SectionOpenerKeyValueSeparator = "="; |
|||
public const string SectionOpenerPostfix = "]"; |
|||
public const string SectionCloser = "[/doc-section]"; |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentSectionDictionary : Dictionary<string,List<string>> |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentSectionFinder : IDocumentSectionFinder |
|||
{ |
|||
public ILogger<DocumentSectionFinder> Logger { get; set; } |
|||
|
|||
public DocumentSectionFinder() |
|||
{ |
|||
Logger = NullLogger<DocumentSectionFinder>.Instance; |
|||
} |
|||
|
|||
public DocumentSectionDictionary Find(string document) |
|||
{ |
|||
return FindNextSections(document, new DocumentSectionDictionary()); |
|||
try |
|||
{ |
|||
return FindNextSections(document, new DocumentSectionDictionary()); |
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
Logger.LogWarning( |
|||
$"Incorrect usage of \"{DocumentSectionConsts.SectionOpenerPrefix}" +
|
|||
$"KEY{DocumentSectionConsts.SectionOpenerKeyValueSeparator}" + |
|||
$"VALUE{DocumentSectionConsts.SectionOpenerPostfix}..{DocumentSectionConsts.SectionCloser}\" syntax.");
|
|||
return new DocumentSectionDictionary(); |
|||
} |
|||
} |
|||
|
|||
private DocumentSectionDictionary FindNextSections(string targetPartOfDocument, DocumentSectionDictionary sections) |
|||
{ |
|||
var startOfSection = targetPartOfDocument.IndexOf(DocumentSectionConsts.SectionOpenerPrefix, StringComparison.InvariantCulture); |
|||
|
|||
if (startOfSection < 0) |
|||
{ |
|||
return sections; |
|||
} |
|||
|
|||
var endOfSectionOpener = targetPartOfDocument.Substring(startOfSection).IndexOf(DocumentSectionConsts.SectionOpenerPostfix, StringComparison.InvariantCulture); |
|||
|
|||
var keyValueWithSeparator = targetPartOfDocument.Substring(startOfSection) |
|||
[DocumentSectionConsts.SectionOpenerPrefix.Length..endOfSectionOpener]; |
|||
|
|||
var keyValue = keyValueWithSeparator.Split(DocumentSectionConsts.SectionOpenerKeyValueSeparator); |
|||
var key = keyValue[0]; |
|||
var value = keyValue[1]; |
|||
|
|||
if (sections.ContainsKey(key)) |
|||
{ |
|||
sections[key].AddIfNotContains(value); |
|||
} |
|||
else |
|||
{ |
|||
sections.Add(key, new List<string>() { value }); |
|||
} |
|||
|
|||
var endOfSection = targetPartOfDocument.IndexOf(DocumentSectionConsts.SectionCloser, StringComparison.InvariantCulture) |
|||
+ DocumentSectionConsts.SectionCloser.Length; |
|||
|
|||
return FindNextSections(targetPartOfDocument.Substring(endOfSection), sections); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public class DocumentSectionHtmlReplacer : IDocumentSectionHtmlReplacer |
|||
{ |
|||
public string Replace(string document, DocumentSectionDictionary sections) |
|||
{ |
|||
foreach (var key in sections) |
|||
{ |
|||
foreach (var value in key.Value) |
|||
{ |
|||
document = document.Replace( |
|||
DocumentSectionConsts.SectionOpenerPrefix + key.Key + DocumentSectionConsts.SectionOpenerKeyValueSeparator + value + DocumentSectionConsts.SectionOpenerPostfix, |
|||
$"<div class=\"doc-section\" data-key=\"{key.Key}\" data-value=\"{value}\" style=\"display: none\">"); |
|||
} |
|||
} |
|||
|
|||
document = document.Replace(DocumentSectionConsts.SectionCloser, "</div>"); |
|||
|
|||
return document; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public interface IDocumentSectionFinder: ITransientDependency |
|||
{ |
|||
public DocumentSectionDictionary Find(string document); |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Docs.HtmlConverting |
|||
{ |
|||
public interface IDocumentSectionHtmlReplacer : ITransientDependency |
|||
{ |
|||
public string Replace(string document, DocumentSectionDictionary sections); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue