using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Song.Extend
{
public class Questions
{
///
/// 处理试题的文本,用于前端显示
///
///
///
public static Song.Entities.Questions TranText(Song.Entities.Questions qs)
{
if (qs == null) return qs;
qs.Qus_Title = _ClearAttr(qs.Qus_Title, new string[] {"p","span","pre","font" });
qs.Qus_Title = Extend.Html.ClearHTML(qs.Qus_Title, "pre");
qs.Qus_Title = _tran(qs.Qus_Title);
qs.Qus_Explain = _ClearAttr(qs.Qus_Explain, new string[] { "p", "span", "pre", "font" });
qs.Qus_Explain = _tran(qs.Qus_Explain);
qs.Qus_Answer = _ClearAttr(qs.Qus_Answer, new string[] { "p", "span", "pre", "font" });
//qs.Qus_Answer = _tranAnswer(qs.Qus_Answer);
return qs;
}
public static Song.Entities.QuesAnswer TranText(Song.Entities.QuesAnswer qa)
{
qa.Ans_Context = _tran(qa.Ans_Context);
return qa;
}
///
/// 试题答题项的处理
///
///
///
private static string _tranAnswer(string text)
{
if (string.IsNullOrWhiteSpace(text)) return "";
text = text.Replace("\r", "");
text = text.Replace("\n", "");
text = text.Replace("\t", "");
//text = text.Replace("\"", """);
text = text.Replace(" ", " ");
text = text.Replace("'", "'");
//text = text.Replace("<", "<");
//text = text.Replace(">", ">");
return text;
}
private static string _tran(string text)
{
if (string.IsNullOrWhiteSpace(text)) return "";
text = text.Replace("\r", "");
text = text.Replace("\n", "");
text = text.Replace("\t", "");
//text = text.Replace("\"", """);
//text = text.Replace(" ", " ");
text = text.Replace("'", "'");
//text = text.Replace("<", "<");
//text = text.Replace(">", ">");
return text;
}
///
/// 清理HTML标题中的属性
///
///
/// 清除指定的标签中的属性
///
private static string _ClearAttr(string html,string tag)
{
if (string.IsNullOrWhiteSpace(html)) return html;
return Regex.Replace(html, @"<("+tag+@")\s*[^><]*>", @"<$1>",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
}
private static string _ClearAttr(string html)
{
if (string.IsNullOrWhiteSpace(html)) return html;
return Regex.Replace(html, @"<([a-zA-Z]+)\s*[^><]*>", @"<$1>",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
}
private static string _ClearAttr(string html,string[] tag)
{
if (string.IsNullOrWhiteSpace(html)) return html;
foreach (string s in tag)
_ClearAttr(html, s);
return html;
}
///
/// 理解HTML标签
///
///
///
private static string _ClearHTML(string html)
{
if (string.IsNullOrWhiteSpace(html)) return html;
//删除脚本
html = Regex.Replace(html, @"", "", RegexOptions.IgnoreCase);
//删除HTML
html = Regex.Replace(html, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
html = Regex.Replace(html, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
html = Regex.Replace(html, @"-->", "", RegexOptions.IgnoreCase);
html = Regex.Replace(html, @"