I wrote a web-based html page editor in C#/VB.Net a while ago when I used to use Windows IIS web server. The idea in the C#/VB.Net is still good. You may find it useful and borrow ideas from it. The C# code is more advanced than VB.NET code because it was developed from VB.Net code. I used them to generate Delphi Programming Technologies and Tips. Just want to share the code with friends.

Source code of htmlbuilder.cs

using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.VisualBasic;

public class HBPage : Page {
public DropDownList path;
public TextBox cssfile, jsfile, fname, title, ctext, chapter, page;
public LinkButton submit;
public Label result;
public RadioButton plaintext;
public HyperLink preview;

public void Page_Load(Object sender, EventArgs e) {

// Explicitly set the encoding to UTF-8.
Response.ContentEncoding = Encoding.UTF8;

if (IsPostBack==false) {
foreach (string subdirectory in Directory.GetDirectories(Server.MapPath("."))) {
string dirname;
int lastslash = subdirectory.LastIndexOf("\\");
dirname = subdirectory.Substring(lastslash+1);
path.Items.Add(new ListItem(dirname,dirname));
}
submit.Text="Save the current HTML file";
result.Text = "No file was saved yet.";
jsfile.Text = "../delphitips.js";
cssfile.Text = "../delphitips.css";
plaintext.Checked = true;
fname.Columns = 68;
cssfile.Columns = 68;
jsfile.Columns = 68;
title.Columns = 68;
ctext.Columns = 68;
ctext.Rows = 15;
preview.NavigateUrl = "";
preview.Text = "Preview";
}
}

public void submit_click(object sender, EventArgs e) {

string tempstr;
Encoding chnen = Encoding.GetEncoding(936);

if (fname.Text=="") preview.NavigateUrl = "";
if (ctext.Text != "") {
tempstr = "<html>" + "\n";
tempstr += "<head>" + "\n";
tempstr += "<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">" + "\n";
if (cssfile.Text != "") {
tempstr += "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + cssfile.Text + "\">" + "\n";
}
if (jsfile.Text != "" ) {
tempstr += "<script language=\"javascript\" src=\"" + jsfile.Text + "\"></script>" + "\n";
}
tempstr += "<title>" + StrFilter(title.Text) + "</title>" + "\n";
tempstr += "</head>" + "\n";
tempstr += "<body>" + "\n";
tempstr += "<h2>" + StrFilter(title.Text) + "</h2>" + "\n";
if (plaintext.Checked) {
tempstr += StrFilter(ctext.Text);
}
else {
tempstr += ctext.Text;
}
tempstr += "<div class=\"footer\">" + "\n";
tempstr += "<script language=\"javascript\">nextpage("+chapter.Text+","+page.Text+");";
tempstr += "prevpage("+chapter.Text+","+page.Text+");chaptertoc("+chapter.Text+");booktoc();</script&ggt;" + "\n";
tempstr += "</div>" + "\n";
tempstr += "</body>" + "\n";
tempstr += "</html>" + "\n";
string whichfile = Server.MapPath(path.SelectedItem.Value + "/" + fname.Text);
//string whichfile = path.SelectedItem.Value + "/" + fname.Text;
FileStream fs = new FileStream(whichfile,FileMode.Create,FileAccess.Write);
StreamWriter f = new StreamWriter(fs,chnen);
f.WriteLine(tempstr);
f.Close();
f = null;
fs = null;

// midify the javascript file
whichfile = Server.MapPath("./delphitips.js");
fs = new FileStream(whichfile,FileMode.Open,FileAccess.Read);
StreamReader fr = new StreamReader(fs, chnen);
fr.BaseStream.Seek(0, SeekOrigin.Begin);
string chstr = "ch" + chapter.Text + "toc";
string temp = "";
bool chfound,chend;
chfound = false;
chend = false;
while (fr.Peek() > -1) {
tempstr = fr.ReadLine();
if (tempstr.IndexOf(chstr)>0) { chfound = true; }
if (chfound) {
if ( ! chend ) {
if ((tempstr.IndexOf("\"")>0) & !(tempstr.IndexOf("\",")>0)) {
tempstr += ",\r\n" + "\t\"" + fname.Text + "::" + title.Text + "\"";
chend = true;
}
}
}
temp += tempstr +"\r\n";
}
fr.Close();
fr = null;
fs = new FileStream(whichfile,FileMode.Create,FileAccess.Write);
StreamWriter fw = new StreamWriter(fs,chnen);
fw.WriteLine(temp);
fw.Close();
fw = null;
fs = null;
preview.NavigateUrl = path.SelectedItem.Value + "/" + fname.Text;
result.Text = "The HTML file, " + fname.Text + " ,was saved.";
fname.Text = "";
ctext.Text = "";
title.Text = "";
page.Text = (double.Parse(page.Text) + 1).ToString();
}
}

private string StrFilter(string AStr) {
string TempStr;
TempStr = AStr;
TempStr = TempStr.Replace("'", "&#39;");
TempStr = TempStr.Replace("\"", "&quot;");
TempStr = TempStr.Replace("<", "&lt;");
TempStr = TempStr.Replace("$gt;", "&gt;");
TempStr = TempStr.Replace("\n","
");
return TempStr;
}
}

You can download the APSX and C#/VB.NET code here – htmlbuilder.

Share

Tags: , , , ,