Example #1
0
        public static string ConvertToUbb(string htmlContent, ContentConvert beforeConvert, ContentConvert afterConvert)
        {
            string msg = string.Empty;

            htmlContent = htmlContent.Replace("\n", string.Empty);
            htmlContent = htmlContent.Replace("\r", string.Empty);
            htmlContent = regScript.Replace(htmlContent, string.Empty);
            htmlContent = regStyle.Replace(htmlContent, string.Empty);
            // htmlContent = htmlContent.Trim();

            if (beforeConvert != null)
            {
                htmlContent = beforeConvert(htmlContent);
            }

            MatchCollection matchs   = regTag.Matches(htmlContent);
            int             tagIndex = 0;

            List <HtmlTagBase> tags = new List <HtmlTagBase>()
            , beginTags             = new List <HtmlTagBase>()
            , endTages   = new List <HtmlTagBase>()
            , singleTags = new List <HtmlTagBase>();

            foreach (Match m in matchs)
            {
                HtmlTagBase t;
                string      tagName = m.Groups[1].Value;

                //这里也可以根据HTML标签来反射生成Html标签类, 代码可以少很多, 但是这样性能不好。
                switch (tagName.ToLower())
                {
                case "img":
                    t = new ImgTag(tagIndex, m.Value, m.Index);
                    break;

                case "a":
                    t = new ATag(tagIndex, m.Value, m.Index);
                    break;

                case "font":
                    t = new FontTag(tagIndex, m.Value, m.Index);
                    break;

                case "span":
                    t = new SpanTag(tagIndex, m.Value, m.Index);
                    break;

                case "br":
                    t = new BrTag(tagIndex, m.Value, m.Index);
                    break;

                case "div":
                    t = new DivTag(tagIndex, m.Value, m.Index);
                    break;

                case "p":
                    t = new PTag(tagIndex, m.Value, m.Index);
                    break;

                case "table":
                    t = new TableTag(tagIndex, m.Value, m.Index);
                    break;

                case "ol":
                case "ul":
                    t = new ListTag(tagIndex, m.Value, m.Index);
                    break;

                case "td":
                case "tr":
                case "s":
                case "b":
                case "i":
                case "sub":
                case "sup":
                case "u":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    break;

                case "th":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    ((PairTag)t).UbbTag = "td";
                    break;

                case "strong":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    ((PairTag)t).UbbTag = "b";
                    break;

                case "em":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    ((PairTag)t).UbbTag = "i";
                    break;

                case "strike":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    ((PairTag)t).UbbTag = "s";
                    break;

                case "blockquote":
                    t = new PairTag(tagIndex, m.Value, m.Index);
                    ((PairTag)t).UbbTag = "indent";
                    break;

                case "h1":
                case "h2":
                case "h3":
                case "h4":
                case "h5":
                case "h6":
                    t = new HTag(tagIndex, m.Value, m.Index);
                    break;

                case "embed":
                    t = new EmbedTag(tagIndex, m.Value, m.Index);
                    break;

                default:
                    t = new HtmlTagBase(tagIndex, m.Value, m.Index);
                    break;
                }
                tagIndex++;

                t.TagList = tags;
                tags.Add(t);

                if (t.IsSingleTag)
                {
                    singleTags.Add(t);
                }
                else
                {
                    if (!t.IsEndTag)
                    {
                        beginTags.Add(t);
                    }
                    else
                    {
                        int flag = -1;
                        for (int i = beginTags.Count - 1; i >= 0; i--)
                        {
                            if (beginTags[i].TagName == t.TagName)//匹配开始标记和结束标记
                            {
                                flag = i;
                                beginTags[i].Pair = t;
                                t.Pair            = beginTags[i];
                                break;
                            }
                        }

                        if (flag >= 0)
                        {
                            for (int i = beginTags.Count - 1; i >= flag; i--)
                            {
                                beginTags.RemoveAt(i);
                            }
                        }
                    }
                }
            }

            StringBuilder builder = new StringBuilder(htmlContent);

            for (int i = 0; i < tags.Count; i++)
            {
                if (!tags[i].IsSingleTag)
                {
                    if (tags[i].Pair == null)
                    {
                        msg += tags[i].TagName + "没有对应的" + (tags[i].IsEndTag ? "开始" : "结束") + "标记\r\n";
                    }
                }
                if (!tags[i].IsEndTag || tags[i].IsSingleTag)
                {
                    builder = tags[i].ConvertToUBB(builder);
                }
            }

            //builder.Replace(" ", string.Empty);
            builder.Replace("\t", string.Empty);

            htmlContent = builder.ToString();
            htmlContent = new Regex("<.*?>").Replace(htmlContent, string.Empty);
            htmlContent = HttpUtility.HtmlDecode(htmlContent);


            if (afterConvert != null)
            {
                htmlContent = afterConvert(htmlContent);
            }

            return(htmlContent);
        }
        private void But_ScoreDisplay_Click(object sender, EventArgs e) //处理HTML并调用浏览器显示工分
        {
            DateTime first = dtp_First.Value;
            DateTime last  = dtp_Last.Value;

            Save();
            //写入html文档
            {
                HDoc doc = new HDoc();
                doc.AddChild("html");
                doc["html"].AddChildren(new HTag("head"), new HTag("body"));
                doc["html"]["head"].AddChild(new HTag("title", "工分计算"));
                doc["html"]["head"].AddChild(new HTag("meta", new HProp("charset", "utf-8")));
                doc["html"]["head"].AddChild("style", @"    body{
     background:#fef9e7;
      }
     .main{
        margin:0% 15%;
      }
     .title{
        text-align:center;
        font-family:'黑体';
        font - size:20px
      }
     h1.title{
        margin-left:40px;
        font-size:35px;
      }
     .worker{
        font-size:25px;
        font-family:'楷体';
      }
    table{
        text-align:center;
        font-size:18px;
      }
");
                doc["html"]["body"].AddChild(new HTag("h1", "工分计算"));
                doc["html"]["body"]["h1"].Properties.Add("class", "title");
                doc["html"]["body"].AddChild(new HTag("div", new HProp("class", "main")));
                doc["html"]["body"].AddChild("ul");
                for (int i = 0; i < PublicValue.Workers.Count; i++)
                {
                    Worker worker = PublicValue.Workers[i];
                    doc["html"]["body"]["ul"].AddChild("li");
                    HTag item = doc["html"]["body"]["ul"]["li", i];
                    item.AddChildren(new HTag("p", worker.Name), new HTag("p", $"总工分:{worker.GetScore(first, last).ToString()}"));
                    item[0].AddProperties(new HProp("class", "worker"));
                    item[1].AddProperties(new HProp("class", "score"));
                    item.AddChild("table", new HProp("border", "1"), new HProp("width", "100%"));
                    item["table"].AddChild("caption", "工分详情");
                    item["table"]["caption"].AddProperties(new HProp("class", "title"));
                    HTag table = item["table"];
                    table.AddChild("tr");
                    table["tr", 0].AddChildren(new HTag("th", "序号"), new HTag("th", "时间"), new HTag("th", "站名"), new HTag("th", "工作任务"), new HTag("th", "工作类型"), new HTag("th", "对应工分"));
                    var tasks = worker.GetTasks(first, last);
                    int add   = 0;  //定义重复任务附加值
                    for (int j = 0; j < tasks.Count; j++)
                    {
                        Task task = tasks[j];
                        for (int k = 0; k < worker.Types[task].Count; k++)
                        {
                            if (k > 0)
                            {
                                add++;
                            }
                            int index = j + 1 + add;
                            table.AddChild("tr");
                            WorkerType wt = worker.Types[task][k];
                            string     station;
                            if (task.Station == null)
                            {
                                station = "&nbsp;";
                            }
                            else
                            {
                                station = task.Station.Name;
                            }
                            table["tr", index].AddChildren(new HTag("td", index.ToString()), new HTag("td", task.Time.ToString()), new HTag("td", station),
                                                           new HTag("td", task.Name), new HTag("td", Function.GetWorkerTypeString(wt)), new HTag("td", task.GetScore(wt).ToString()));
                        }
                    }
                }
                WriteFiles.Write(doc.GenerateHtml(), PublicValue.ProgramPath + "/html.html"); //写入文件
            }
            Process.Start(PublicValue.ProgramPath + "/html.html");                            //调用浏览器
        }