-
I recently wanted to make the website static, so I wrote a few methods myself and posted them for discussion with everyone.
/// <summary>
/// Website static function class
/// </summary>
public class CreateHtml
{
/// <summary>
/// Read template content
/// </summary>
/// <param name="template">Template relative path</param>
/// <returns>Template content, read failure returns ""</returns>
public string Html_ReadTemplate(string template)
{
#region
StringBuilder str = new StringBuilder();//Storage pattern content
if (template != null && template != "" && template.Length > 0)//if
{
try
{
using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(template), Encoding.GetEncoding("utf-8")))
{
string tempStr;
while ((tempStr = sr.ReadLine()) != null)//If the end of the file is not read
{
str.Append(tempStr);
}
sr.Close();
}
}
catch (Exception ex)
{
return null;
}
}
return str.ToString();
#endregion
}
/// <summary>
/// Generate files based on the absolute path and file name of the generated file
/// </summary>
/// <param name="fileAbsolutePath">File absolute path</param>
/// <param name="htmlName">Generate file name</param>
/// <param name="htmlNote">Write file content</param>
/// <returns>Generate file status, 1: success 0: failure</returns>
public int Html_WriteTemplate(string fileAbsolutePath, string htmlName,string htmlNote)
{
#region
if (fileAbsolutePath != null && fileAbsolutePath != "" && fileAbsolutePath.Length > 0 && htmlName != null && htmlName != "" && htmlName.Length > 0)
{
try
{
using (FileStream fs = new FileStream(fileAbsolutePath + "\" + htmlName, FileMode.Create, FileAccess.Write, FileShare.Write))
{
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(htmlNote);
sw.Close();
fs.Close();
return 1;//File generated successfully
}
}
catch (Exception ex)
{
return 0;
}
}
return 0;//File generation failed
#endregion
}
/// <summary>
/// Read in pages according to all the content read, and separate each page with "|"
/// </summary>
/// <param name="contentText">Content</param>
/// <param name="pageLogo">Paging logo</param>
/// <returns>The string after the content is paginated, each page is separated by "|"</returns>
public string Html_BackPageData(string contentText,string pageLogo)
{
#region
int tempcount = 0;
StringBuilder sb = new StringBuilder();
while (tempcount >= 0)
{
int index = contentText.IndexOf(pageLogo);//Paging logo
if (index == -1)//If there is no paging
{
sb.Append(contentText);
break;
}
else
{
sb.Append(contentText.Substring(0,index)+"|");
int start = index + pageLogo.Length + 1;
int end = contentText.Length-index-pageLogo.Length - 1;
contentText=contentText.Substring(start, end);
}
tempcount++;
}
return sb.ToString();
#endregion
}
/// <summary>
/// Generate corresponding paging flags for the content, home page, previous page, page number, next page, last page (for a piece of content)
/// </summary>
/// <param name="currPageIndex">Current page index</param>
/// <param name="pageCount">Total number of pages</param>
/// <param name="noteName">File name (only the file name without the suffix)</param>
/// <returns>The corresponding paging flag generated based on the content</returns>
public string Html_Pager_Sign(int currPageIndex, int pageCount, string noteName)
{
#region
string allSeprNote = "<table><tr><td> $firstpage$</td><td> $uppage$</td><td> $pagenumber$</ td><td> $drownpage$</td><td> $lastpage$</td></tr></table>";
string seprTempStr = "";
for (int i = 1; i <= pageCount; i++)//Loop to generate page numbers for each page (this loop only generates page numbers)
{
int n = i - 1;
//If it is the first page
if(i==1)
{
seprTempStr += "<a href='" + noteName + ".html'>" + i+" "+ "</a>";
}
else
{
seprTempStr += "<a href='" + noteName +"-"+n+ ".html'>" + i+" " + "</a>";
}
}
allSeprNote = allSeprNote.Replace("$pagenumber$", seprTempStr);//Replace the page number
//Generate home page and previous page
if (currPageIndex == 0)//If the current page is the first page, there will be no link to the previous page or home page.
{
allSeprNote = allSeprNote.Replace("$firstpage$", "Homepage");//Replace the homepage
allSeprNote = allSeprNote.Replace("$uppage$", "previous page");//Replace the previous page
}
else if (currPageIndex == 1)//If the current page is the second page, there are links to the previous page and the home page, and the links to the previous page and the home page are the same, that is, the functions are the same
{
allSeprNote = allSeprNote.Replace("$firstpage$", "<a href='" + noteName + ".html'>Homepage</a>");//Replace the homepage with links
allSeprNote = allSeprNote.Replace("$uppage$", "<a href='" + noteName + ".html'>Previous page</a>");//Replace the previous page because it is the second page The link is the same as the homepage
}
else//if it is another page
{
int temppageindex = currPageIndex - 1; //Previous page number
allSeprNote = allSeprNote.Replace("$firstpage$", "<a href='" + noteName + ".html'>Homepage</a>");//Replace the homepage
allSeprNote = allSeprNote.Replace("$uppage$", "<a href='" + noteName +"-"+temppageindex+ ".html'>Previous page</a>");//Replace the previous page
}
//Generate last page and next page
if (currPageIndex == pageCount - 1)//If the current page is the last page, the next page and the last page have the same functions and have no links.
{
allSeprNote = allSeprNote.Replace("$lastpage$", "last page");//Replace the last page
allSeprNote = allSeprNote.Replace("$drownpage$", "next page");//Replace the next page
}
else//if it is another page
{
int temppageindex = currPageIndex+1;//next page number
allSeprNote=allSeprNote.Replace("$lastpage$", "<a href='" + noteName + "-" + (pageCount - 1) + ".html'>Last page</a>");//Generate tail Page
allSeprNote=allSeprNote.Replace("$drownpage$", "<a href='" + noteName + "-" + temppageindex + ".html'>Next page</a>");
}
return allSeprNote;
#endregion
}
/// <summary>
/// Generate paging flags for list pages
/// </summary>
/// <param name="pageTotal">Total number of pages</param>
/// <param name="currentPage">Current page index (starting from zero)</param>
/// <param name="pageSize">Number of content items displayed on each page</param>
/// <param name="name">File name (without suffix)</param>
/// <returns>Paging flag</returns>
public string Html_Pager_Multi(int pageTotal, int currentPage, int pageSize, string name, string path)
{
#region
pageTotal = pageTotal - 1;
StringBuilder sb = new StringBuilder();
//Generate homepage and previous page links
if (currentPage == 0)//If the current page is the first page
{
sb.Append(" Homepage Previous Page ");
}
else if (currentPage == 1)//If the current page is the second page
{
sb.Append(" <a href='" + name + ".html" + "'>Homepage</a>");
sb.Append(" <a href='" + name + ".html" + "'>Previous page</a> ");
}
else
{
sb.Append(" <a href='" + name + ".html" + "'>Homepage</a>");
sb.Append(" <a href='" + name + "-" + (currentPage - 1) + ".html'>Previous page</a> ");
}
int indexStart = currentPage - 5;
int indexEnd = currentPage + 5;
if (indexStart <= 0)
{
indexStart = 0;
indexEnd = 10;
}
if (indexStart > 0)
{
indexStart = currentPage - 5;
indexEnd = currentPage + 5;
}
if (currentPage >= pageTotal-10)
{
indexStart = pageTotal - 10;
indexEnd = pageTotal;
}
//Generate detailed page numbers
for (int i = indexStart; i <= indexEnd; i++)
{
if (i == currentPage)
{
sb.Append(" " + (i+1) + " ");
}
else if (i == 0)//If the current page is the second page, it needs to be processed separately
{
sb.Append(" <a href='" + name + ".html'>" + (i + 1) + "</a> ");
}
else
{
sb.Append(" <a href='" + name + "-" + i + ".html'>" + (i + 1) + "</a> ");
}
}
//Generate next page and last page
if (currentPage == pageTotal)//If the current page is the last page
{
sb.Append(" Next page ");
sb.Append(" Last page ");
}
else
{
sb.Append(" <a href='"+name+"-"+(currentPage+1)+".html'>Next page</a> ");
sb.Append(" <a href='"+name+"-"+pageTotal+".html'>Last page</a>");
}
sb.Append(" " + (currentPage + 1) + "/" + (pageTotal + 1));
sb.Append(" Go to:<select name="qq" id="ddlIndexList" onchange="javascript:location.href=this.value;">" );
string absolutePaths = HttpContext.Current.Request.Url.ToString();//Get url
absolutePaths = absolutePaths.Substring(0, absolutePaths.LastIndexOf("/"))+"\" + path;//Get the website root path url
for (int j = 1; j <= (pageTotal+1); j++)
{
if(j==1)
{
sb.Append("<option value='" + absolutePaths+" \"+name + ".html'>" + j + "</option>");
}
else
{
sb.Append("<option value='" + absolutePaths+" \"+name + "-" + (j - 1) + ".html'>" + j + "</option>");
}
}
sb.Append("</select>");
sb.Append("<script type="text/javascript" language="javascript">");
if (currentPage == 0)//First page
{
string d = "document.getElementById("ddlIndexList").value = "" + name + ".html";";
sb.Append("document.getElementById("ddlIndexList").value = ""+name+".html";");
}
else
{
string k = " document.getElementById("ddlIndexList").value = "" + name + "-" + currentPage + ".html";";
sb.Append(" document.getElementById("ddlIndexList").value = "" + name+"-"+currentPage + ".html";");
}
sb.Append("</script>");
return sb.ToString();
#endregion