Browse Source

document sections support and/or

pull/2297/head
Yunus Emre Kalkan 7 years ago
parent
commit
bb6121824a
  1. 2
      modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionConsts.cs
  2. 40
      modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionFinder.cs
  3. 43
      modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionHtmlReplacer.cs
  4. 2
      modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs
  5. 2
      modules/docs/src/Volo.Docs.Web/HtmlConverting/IDocumentSectionHtmlReplacer.cs
  6. 2
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs
  7. 72
      modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js

2
modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionConsts.cs

@ -4,6 +4,8 @@
{
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]";
}

40
modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionFinder.cs

@ -30,7 +30,7 @@ namespace Volo.Docs.HtmlConverting
}
}
private DocumentSectionDictionary FindNextSections(string targetPartOfDocument, DocumentSectionDictionary sections)
private DocumentSectionDictionary FindNextSections(string targetPartOfDocument, DocumentSectionDictionary sections)
{
var startOfSection = targetPartOfDocument.IndexOf(DocumentSectionConsts.SectionOpenerPrefix, StringComparison.InvariantCulture);
@ -41,10 +41,44 @@ namespace Volo.Docs.HtmlConverting
var endOfSectionOpener = targetPartOfDocument.Substring(startOfSection).IndexOf(DocumentSectionConsts.SectionOpenerPostfix, StringComparison.InvariantCulture);
var keyValueWithSeparator = targetPartOfDocument.Substring(startOfSection)
var keysValues = targetPartOfDocument.Substring(startOfSection)
[DocumentSectionConsts.SectionOpenerPrefix.Length..endOfSectionOpener];
sections = ParseAndAddContidionToSections(keysValues, sections);
return FindNextSections(targetPartOfDocument.Substring(endOfSectionOpener), sections);
}
private DocumentSectionDictionary ParseAndAddContidionToSections(string keysValues, DocumentSectionDictionary sections)
{
if (keysValues.Contains(DocumentSectionConsts.SectionOpenerAndCondition, StringComparison.InvariantCulture))
{
var keysValuesSplitted = keysValues.Split(DocumentSectionConsts.SectionOpenerAndCondition);
foreach (var keyValue in keysValuesSplitted)
{
sections = AddContidionToSections(keyValue, sections);
}
}
else if (keysValues.Contains(DocumentSectionConsts.SectionOpenerOrCondition, StringComparison.InvariantCulture))
{
var keysValuesSplitted = keysValues.Split(DocumentSectionConsts.SectionOpenerOrCondition);
foreach (var keyValue in keysValuesSplitted)
{
sections = AddContidionToSections(keyValue, sections);
}
}
else
{
sections = AddContidionToSections(keysValues, sections);
}
return sections;
}
private DocumentSectionDictionary AddContidionToSections(string keyValueWithSeparator, DocumentSectionDictionary sections)
{
var keyValue = keyValueWithSeparator.Split(DocumentSectionConsts.SectionOpenerKeyValueSeparator);
var key = keyValue[0];
var value = keyValue[1];
@ -57,7 +91,7 @@ namespace Volo.Docs.HtmlConverting
sections.Add(key, new List<string>() { value });
}
return FindNextSections(targetPartOfDocument.Substring(endOfSectionOpener), sections);
return sections;
}
}
}

43
modules/docs/src/Volo.Docs.Web/HtmlConverting/DocumentSectionHtmlReplacer.cs

@ -1,17 +1,46 @@
namespace Volo.Docs.HtmlConverting
using System;
namespace Volo.Docs.HtmlConverting
{
public class DocumentSectionHtmlReplacer : IDocumentSectionHtmlReplacer
{
public string Replace(string document, DocumentSectionDictionary sections)
public string Replace(string document)
{
foreach (var key in sections)
while (document.Contains(DocumentSectionConsts.SectionOpenerPrefix))
{
foreach (var value in key.Value)
var sectionIndex = document.IndexOf(DocumentSectionConsts.SectionOpenerPrefix, StringComparison.InvariantCulture);
var sectionCloserIndex = document.Substring(sectionIndex + DocumentSectionConsts.SectionOpenerPrefix.Length)
.IndexOf(DocumentSectionConsts.SectionOpenerPostfix, StringComparison.InvariantCulture);
var keysValues = document.Substring(sectionIndex + DocumentSectionConsts.SectionOpenerPrefix.Length)
[0..sectionCloserIndex];
var andOperation = keysValues.Contains(DocumentSectionConsts.SectionOpenerAndCondition, StringComparison.InvariantCulture);
var orOperation = keysValues.Contains(DocumentSectionConsts.SectionOpenerOrCondition, StringComparison.InvariantCulture);
var splitterChar = andOperation ? DocumentSectionConsts.SectionOpenerAndCondition : DocumentSectionConsts.SectionOpenerOrCondition;
string[] keysValuesSplitted = keysValues.Split(splitterChar);
var keys = new string[keysValuesSplitted.Length];
var values = new string[keysValuesSplitted.Length];
for (int i = 0; i < keysValuesSplitted.Length; i++)
{
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\">");
keys[i] = keysValuesSplitted[i].Split(DocumentSectionConsts.SectionOpenerKeyValueSeparator)[0];
values[i] = keysValuesSplitted[i].Split(DocumentSectionConsts.SectionOpenerKeyValueSeparator)[1];
}
var div = $"<div class=\"doc-section\" data-keys=\"" +
$"{string.Join(splitterChar, keys)}" +
$"\" data-values=\"" +
$"{string.Join(splitterChar, values)}" +
$"\" style=\"display: none\">";
document = document.Remove(sectionIndex, sectionCloserIndex + DocumentSectionConsts.SectionOpenerPrefix.Length +1);
document = document.Insert(sectionIndex, div);
}
document = document.Replace(DocumentSectionConsts.SectionCloser, "</div>");

2
modules/docs/src/Volo.Docs.Web/HtmlConverting/HtmlNormalizer.cs

@ -28,7 +28,6 @@ namespace Volo.Docs.HtmlConverting
}, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Multiline);
return content;
}
@ -36,6 +35,5 @@ namespace Volo.Docs.HtmlConverting
{
return Regex.Replace(content, "<code class=\"" + currentLanguage + "\">", "<code class=\"" + newLanguage + "\">", RegexOptions.IgnoreCase);
}
}
}

2
modules/docs/src/Volo.Docs.Web/HtmlConverting/IDocumentSectionHtmlReplacer.cs

@ -4,6 +4,6 @@ namespace Volo.Docs.HtmlConverting
{
public interface IDocumentSectionHtmlReplacer : ITransientDependency
{
public string Replace(string document, DocumentSectionDictionary sections);
public string Replace(string document);
}
}

2
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs

@ -384,7 +384,7 @@ namespace Volo.Docs.Pages.Documents.Project
{
DocumentSections = _documentSectionFinder.Find(Document.Content);
Document.Content = _documentSectionHtmlReplacer.Replace(Document.Content, DocumentSections);
Document.Content = _documentSectionHtmlReplacer.Replace(Document.Content);
var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format);
var content = converter.Convert(Project, Document, GetSpecificVersionOrLatest(), LanguageCode);

72
modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js

@ -102,7 +102,6 @@
);
};
var initSections = function () {
var getQueryStringParameterByName = function (name) {
var url = window.location.href;
@ -139,20 +138,73 @@
window.history.replaceState({}, document.title, new_uri);
};
var changeSections = function (combobox) {
var key = $(combobox).data('key');
var value = combobox.value;
var setSections = function () {
var sections = $(".doc-section");
var comboboxes = $(".doc-section-combobox");
var comboboxKeys = [];
var comboboxSelectedValues = [];
for (var i = 0; i < comboboxes.length; i++) {
comboboxKeys.push($(comboboxes[i]).data("key"));
comboboxSelectedValues.push(comboboxes[i].value);
}
for (i = 0; i < sections.length; i++) {
var keys = $(sections[i]).data("keys");
var values = $(sections[i]).data("values");
var show = false;
var keysSplitted;
var valuesSplitted;
if (keys.indexOf("&") >= 0) {
keysSplitted = keys.split("&");
valuesSplitted = values.split("&");
var hide = false;
$(".doc-section[data-key=" + key + "]").hide();
$(".doc-section[data-key=" + key + "][data-value=" + value + "]").show();
for (var k = 0; k < keysSplitted.length; k++) {
if (valuesSplitted[k] !== comboboxSelectedValues[comboboxKeys.indexOf(keysSplitted[k])]) {
hide = true;
break;
}
}
localStorage["abp-doc-section-" + key] = value;
show = !hide;
}
else if (keys.indexOf("|") >= 0) {
keysSplitted = keys.split("|");
valuesSplitted = values.split("|");
for (k = 0; k < keysSplitted.length; k++) {
if (valuesSplitted[k] === comboboxSelectedValues[comboboxKeys.indexOf(keysSplitted[k])]) {
show = true;
break;
}
}
}
else {
if (values === comboboxSelectedValues[comboboxKeys.indexOf(keys)]) {
show = true;
}
}
if (show) {
$(sections[i]).show();
}
else {
$(sections[i]).hide();
}
}
setQueryString();
};
$(".doc-section-combobox").change(function () {
changeSections(this);
localStorage["abp-doc-section-" + $(this).data("key")] = this.value;
setSections();
});
var comboboxes = $(".doc-section-combobox");
@ -172,9 +224,7 @@
}
}
for (var k = 0; k < comboboxes.length; k++) {
changeSections(comboboxes[k]);
}
setSections();
};

Loading…
Cancel
Save