docs: #307 some refactoring

This commit is contained in:
Hamid Reza Mohammadi 2022-04-23 16:51:20 +04:30
parent d6add8f275
commit 1b9913b4a7
8 changed files with 65 additions and 333 deletions

View File

@ -752,9 +752,6 @@ namespace GanjooRazor.Pages
case "vazn":
await _GenerateVaznHtmlText();
break;
case "simi":
await _GenerateSimiHtmlText();
break;
}
}
breadCrumbList.AddItem(GanjoorPage.Title, GanjoorPage.FullUrl, "https://i.ganjoor.net/cat.png");

View File

@ -13,6 +13,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Ganjoor.ViewModels;
using RMuseum.Services.Implementation;
using RMuseum.Utils;
using RSecurityBackend.Models.Generic;
@ -208,7 +209,7 @@ namespace GanjooRazor.Pages
foreach (var poem in Poems)
{
poem.HtmlText = _GetPoemTextExcerpt(poem.HtmlText);
poem.HtmlText = GanjoorPoemTools.GetPoemHtmlExcerpt(poem.HtmlText);
}
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
@ -249,144 +250,6 @@ namespace GanjooRazor.Pages
return Page();
}
public string HtmlText { get; set; }
/// <summary>
/// اشعار مشابه
/// </summary>
/// <returns></returns>
private async Task _GenerateSimiHtmlText()
{
if (string.IsNullOrEmpty(Request.Query["v"]) || string.IsNullOrEmpty(Request.Query["g"]))
{
LastError = "<p>موردی با مشخصات انتخاب شده یافت نشد.</p>";
return;
}
int pageNumber = 1;
if (!string.IsNullOrEmpty(Request.Query["page"]))
{
pageNumber = int.Parse(Request.Query["page"]);
}
string metre = Request.Query["v"];
string rhyme = Request.Query["g"];
string author = string.IsNullOrEmpty(Request.Query["a"]) ? "0" : Request.Query["a"];
string url = $"{APIRoot.Url}/api/ganjoor/poems/similar?PageNumber={pageNumber}&PageSize=20&metre={metre}&rhyme={rhyme}&poetId={author}";
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return;
}
var poems = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoemCompleteViewModel>>();
string title = "شعرها یا ابیات ";
if (author != "0")
{
var poetInfo = Poets.Where(p => p.Id == int.Parse(author)).SingleOrDefault();
if (poetInfo != null)
{
title += $"{poetInfo.Nickname} ";
}
}
title += $"با وزن «{metre}»";
title += $" و حروف قافیهٔ «{rhyme}»";
string htmlText = "";
htmlText += $"<div class=\"sitem\" id=\"all\">{Environment.NewLine}";
htmlText += $"<h2>{title}</h2>";
htmlText += $"</div>{Environment.NewLine}";
foreach (var poem in poems)
{
htmlText += $"<div class=\"sitem\">{Environment.NewLine}<h2><a href=\"{poem.FullUrl}\" rel=\"bookmark\">{poem.FullTitle}</a>{Environment.NewLine}</h2>{Environment.NewLine}" +
$"<div class=\"spacer\">&nbsp;</div>{Environment.NewLine}"
+
$"<div class=\"sit\">{Environment.NewLine} {_GetPoemTextExcerpt(poem.HtmlText)}" +
$"<p><br /><a href=\"{poem.FullUrl}\">متن کامل شعر را ببینید ...</a></p>" +
$"</div>{Environment.NewLine}" +
$"<img src=\"{APIRoot.InternetUrl}{poem.Category.Poet.ImageUrl}\" alt=\"{poem.Category.Poet.Name}\" />"
+
$"<div class=\"spacer\">&nbsp;</div>{Environment.NewLine}"
+
$"</div>{Environment.NewLine}";
}
htmlText += "<p style=\"text-align: center;\">";
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata);
if (paginationMetadata.totalPages > 1)
{
string authorParam = author != "0" ? $"&amp;a={author}" : "";
title += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
if (paginationMetadata.currentPage > 3)
{
htmlText += $"[<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page=1{authorParam}\">صفحهٔ اول</a>] …";
}
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
{
if (i >= 1 && i <= paginationMetadata.totalPages)
{
htmlText += " [";
if (i == paginationMetadata.currentPage)
{
htmlText += i.ToPersianNumbers();
}
else
{
htmlText += $"<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page={i}{authorParam}\">{i.ToPersianNumbers()}</a>";
}
htmlText += "] ";
}
}
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
{
htmlText += $"… [<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page={paginationMetadata.totalPages}{authorParam}\">صفحهٔ آخر</a>]";
}
}
htmlText += $"</p>{Environment.NewLine}";
ViewData["Title"] = $"گنجور » {title}";
HtmlText = htmlText;
}
private string _GetPoemTextExcerpt(string poemText)
{
while (poemText.IndexOf("id=\"bn") != -1)
{
int idxbn1 = poemText.IndexOf(" id=\"bn");
int idxbn2 = poemText.IndexOf("\"", idxbn1 + " id=\"bn".Length);
poemText = poemText.Substring(0, idxbn1) + poemText.Substring(idxbn2 + 1);
}
poemText = poemText.Replace("<div class=\"b\">", "").Replace("<div class=\"b2\">", "").Replace("<div class=\"m1\">", "").Replace("<div class=\"m2\">", "").Replace("</div>", "");
int index = poemText.IndexOf("<p>");
int count = 0;
while (index != -1 && count < 5)
{
index = poemText.IndexOf("<p>", index + 1);
count++;
}
if (index != -1)
{
poemText = poemText.Substring(0, index);
poemText += "<p>[...]</p>";
}
return poemText;
}
}
}

View File

@ -1,15 +1,11 @@
using DNTPersianUtils.Core;
using GanjooRazor.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
using RSecurityBackend.Models.Generic;
using RMuseum.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
@ -19,118 +15,6 @@ namespace GanjooRazor.Pages
{
public partial class IndexModel : LoginPartialEnabledPageModel
{
/// <summary>
/// اشعار مشابه
/// </summary>
/// <returns></returns>
private async Task _GenerateSimiHtmlText()
{
if (string.IsNullOrEmpty(Request.Query["v"]) || string.IsNullOrEmpty(Request.Query["g"]))
{
GanjoorPage.HtmlText = "<p>موردی با مشخصات انتخاب شده یافت نشد.</p>";
return;
}
int pageNumber = 1;
if (!string.IsNullOrEmpty(Request.Query["page"]))
{
pageNumber = int.Parse(Request.Query["page"]);
}
string metre = Request.Query["v"];
string rhyme = Request.Query["g"];
string author = string.IsNullOrEmpty(Request.Query["a"]) ? "0" : Request.Query["a"];
string url = $"{APIRoot.Url}/api/ganjoor/poems/similar?PageNumber={pageNumber}&PageSize=20&metre={metre}&rhyme={rhyme}&poetId={author}";
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return;
}
var poems = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoemCompleteViewModel>>();
GanjoorPage.Title = "شعرها یا ابیات ";
if (author != "0")
{
var poetInfo = Poets.Where(p => p.Id == int.Parse(author)).SingleOrDefault();
if(poetInfo != null)
{
GanjoorPage.Title += $"{poetInfo.Nickname} ";
}
}
GanjoorPage.Title += $"با وزن «{metre}»";
GanjoorPage.Title += $" و حروف قافیهٔ «{rhyme}»";
string htmlText = "";
htmlText += $"<div class=\"sitem\" id=\"all\">{Environment.NewLine}";
htmlText += $"<h2>{GanjoorPage.Title}</h2>";
htmlText += $"</div>{Environment.NewLine}";
foreach (var poem in poems)
{
htmlText += $"<div class=\"sitem\">{Environment.NewLine}<h2><a href=\"{poem.FullUrl}\" rel=\"bookmark\">{poem.FullTitle}</a>{Environment.NewLine}</h2>{Environment.NewLine}" +
$"<div class=\"spacer\">&nbsp;</div>{Environment.NewLine}"
+
$"<div class=\"sit\">{Environment.NewLine} {_GetPoemTextExcerpt(poem.HtmlText)}" +
$"<p><br /><a href=\"{poem.FullUrl}\">متن کامل شعر را ببینید ...</a></p>" +
$"</div>{Environment.NewLine}" +
$"<img src=\"{APIRoot.InternetUrl}{poem.Category.Poet.ImageUrl}\" alt=\"{poem.Category.Poet.Name}\" />"
+
$"<div class=\"spacer\">&nbsp;</div>{Environment.NewLine}"
+
$"</div>{Environment.NewLine}";
}
htmlText += "<p style=\"text-align: center;\">";
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata);
if (paginationMetadata.totalPages > 1)
{
string authorParam = author != "0" ? $"&amp;a={author}" : "";
GanjoorPage.Title += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
if (paginationMetadata.currentPage > 3)
{
htmlText += $"[<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page=1{authorParam}\">صفحهٔ اول</a>] …";
}
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
{
if (i >= 1 && i <= paginationMetadata.totalPages)
{
htmlText += " [";
if (i == paginationMetadata.currentPage)
{
htmlText += i.ToPersianNumbers();
}
else
{
htmlText += $"<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page={i}{authorParam}\">{i.ToPersianNumbers()}</a>";
}
htmlText += "] ";
}
}
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
{
htmlText += $"… [<a href=\"/simi/?v={Uri.EscapeDataString(metre)}&amp;g={Uri.EscapeDataString(rhyme)}&amp;page={paginationMetadata.totalPages}{authorParam}\">صفحهٔ آخر</a>]";
}
}
htmlText += $"</p>{Environment.NewLine}";
ViewData["Title"] = $"گنجور » {GanjoorPage.Title}";
GanjoorPage.HtmlText = htmlText;
}
public async Task<ActionResult> OnGetSimilarPoemsPartialAsync(int poemId, int skip, string prosodyMetre, string rhymeLetters, string poemFullUrl, int sectionIndex)
{
string url = $"{APIRoot.Url}/api/ganjoor/section/{poemId}/{sectionIndex}/related?skip={skip}&itemsCount=21";
@ -174,7 +58,7 @@ namespace GanjooRazor.Pages
foreach (var poem in poems)
{
poem.HtmlText = _GetPoemTextExcerpt(poem.HtmlText);
poem.HtmlText = GanjoorPoemTools.GetPoemHtmlExcerpt(poem.HtmlText);
}
return new PartialViewResult()

View File

@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Ganjoor.ViewModels;
using RMuseum.Utils;
using RSecurityBackend.Models.Generic;
using System;
using System.Collections.Generic;
@ -74,7 +75,7 @@ namespace GanjooRazor.Pages
htmlText += $"<div class=\"sitem\" id=\"post-{poem.Id}\">{Environment.NewLine}<h2><a href=\"{poem.FullUrl}\" rel=\"bookmark\">{poem.FullTitle}</a>{Environment.NewLine}</h2>{Environment.NewLine}" +
$"<div class=\"spacer\">&nbsp;</div>{Environment.NewLine}"
+
$"<div class=\"sit\">{Environment.NewLine} {_GetPoemTextExcerpt(poem.HtmlText)}" +
$"<div class=\"sit\">{Environment.NewLine} {GanjoorPoemTools.GetPoemHtmlExcerpt(poem.HtmlText)}" +
$"<p><br /><a href=\"{poem.FullUrl}\">متن کامل شعر را ببینید ...</a></p>" +
$"</div>{Environment.NewLine}" +
$"<img src=\"{APIRoot.InternetUrl}{poem.Category.Poet.ImageUrl}\" alt=\"{poem.Category.Poet.Name}\" />"
@ -128,33 +129,5 @@ namespace GanjooRazor.Pages
GanjoorPage.HtmlText = htmlText;
}
private string _GetPoemTextExcerpt(string poemText)
{
while (poemText.IndexOf("id=\"bn") != -1)
{
int idxbn1 = poemText.IndexOf(" id=\"bn");
int idxbn2 = poemText.IndexOf("\"", idxbn1 + " id=\"bn".Length);
poemText = poemText.Substring(0, idxbn1) + poemText.Substring(idxbn2 + 1);
}
poemText = poemText.Replace("<div class=\"b\">", "").Replace("<div class=\"b2\">", "").Replace("<div class=\"m1\">", "").Replace("<div class=\"m2\">", "").Replace("</div>", "");
int index = poemText.IndexOf("<p>");
int count = 0;
while(index != -1 && count < 5 )
{
index = poemText.IndexOf("<p>", index + 1);
count++;
}
if(index != -1)
{
poemText = poemText.Substring(0, index);
poemText += "<p>[...]</p>";
}
return poemText;
}
}
}

View File

@ -13669,13 +13669,6 @@
<param name="rhyme"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.GetPoemHtmlExcerpt(System.String)">
<summary>
get an excerpt for the poem
</summary>
<param name="poemHtml"></param>
<returns></returns>
</member>
<member name="M:RMuseum.Services.Implementation.GanjoorService.StartGeneratingRelatedPoemsInfo(System.Boolean)">
<summary>
start generating related poems info
@ -15897,6 +15890,18 @@
<param name="userId"></param>
<returns></returns>
</member>
<member name="T:RMuseum.Utils.GanjoorPoemTools">
<summary>
ganjoor poem tools
</summary>
</member>
<member name="M:RMuseum.Utils.GanjoorPoemTools.GetPoemHtmlExcerpt(System.String)">
<summary>
get an excerpt for the poem
</summary>
<param name="poemHtml"></param>
<returns></returns>
</member>
<member name="P:RMuseum.WebServiceUrl.Url">
<summary>
url

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RMuseum.Utils;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services.Implementation;
using System;
@ -113,7 +114,7 @@ namespace RMuseum.Services.Implementation
FullTitle = relatedSection.Poem.FullTitle,
FullUrl = relatedSection.Poem.FullUrl,
PoetMorePoemsLikeThisCount = 0,
HtmlExcerpt = GetPoemHtmlExcerpt(relatedSection.HtmlText)
HtmlExcerpt = GanjoorPoemTools.GetPoemHtmlExcerpt(relatedSection.HtmlText)
};
ganjoorCachedRelatedPoems.Add(newRelatedPoem);
@ -131,39 +132,6 @@ namespace RMuseum.Services.Implementation
}
}
/// <summary>
/// get an excerpt for the poem
/// </summary>
/// <param name="poemHtml"></param>
/// <returns></returns>
public static string GetPoemHtmlExcerpt(string poemHtml)
{
while (poemHtml.IndexOf("id=\"bn") != -1)
{
int idxbn1 = poemHtml.IndexOf(" id=\"bn");
int idxbn2 = poemHtml.IndexOf("\"", idxbn1 + " id=\"bn".Length);
poemHtml = poemHtml.Substring(0, idxbn1) + poemHtml.Substring(idxbn2 + 1);
}
poemHtml = poemHtml.Replace("<div class=\"b\">", "").Replace("<div class=\"b2\">", "").Replace("<div class=\"m1\">", "").Replace("<div class=\"m2\">", "").Replace("</div>", "");
int index = poemHtml.IndexOf("<p>");
int count = 0;
while (index != -1 && count < 5)
{
index = poemHtml.IndexOf("<p>", index + 1);
count++;
}
if (index != -1)
{
poemHtml = poemHtml.Substring(0, index);
poemHtml += "<p>[...]</p>";
}
return poemHtml;
}
/// <summary>
/// start generating related poems info
/// </summary>

View File

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Ganjoor;
using RMuseum.Utils;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Services.Implementation;
using System;
@ -127,7 +128,7 @@ namespace RMuseum.Services.Implementation
FullTitle = relatedSection.Poem.FullTitle,
FullUrl = fullUrl,
PoetMorePoemsLikeThisCount = 0,
HtmlExcerpt = GetPoemHtmlExcerpt(relatedSection.HtmlText),
HtmlExcerpt = GanjoorPoemTools.GetPoemHtmlExcerpt(relatedSection.HtmlText),
TargetPoemId = relatedSection.PoemId,
TargetSectionIndex = relatedSection.Index,
};

View File

@ -0,0 +1,41 @@
namespace RMuseum.Utils
{
/// <summary>
/// ganjoor poem tools
/// </summary>
public static class GanjoorPoemTools
{
/// <summary>
/// get an excerpt for the poem
/// </summary>
/// <param name="poemHtml"></param>
/// <returns></returns>
public static string GetPoemHtmlExcerpt(string poemHtml)
{
while (poemHtml.IndexOf("id=\"bn") != -1)
{
int idxbn1 = poemHtml.IndexOf(" id=\"bn");
int idxbn2 = poemHtml.IndexOf("\"", idxbn1 + " id=\"bn".Length);
poemHtml = poemHtml.Substring(0, idxbn1) + poemHtml.Substring(idxbn2 + 1);
}
poemHtml = poemHtml.Replace("<div class=\"b\">", "").Replace("<div class=\"b2\">", "").Replace("<div class=\"m1\">", "").Replace("<div class=\"m2\">", "").Replace("</div>", "");
int index = poemHtml.IndexOf("<p>");
int count = 0;
while (index != -1 && count < 5)
{
index = poemHtml.IndexOf("<p>", index + 1);
count++;
}
if (index != -1)
{
poemHtml = poemHtml.Substring(0, index);
poemHtml += "<p>[...]</p>";
}
return poemHtml;
}
}
}