コード例 #1
0
        /// <summary>
        /// 将 HTML文件并成单个文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool ConvertHtmToSingleFile(string filename, string htmfile)
        {
            try
            {
                string htm = HtmlImgSrcFullPath(filename);

                //返回 <img src="123_files/ss.gif" border="0">
                //中的 123_files/ss.gif

                foreach (Match m in Regex.Matches(htm, "<img.+?src=[\"'](.+?)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline))
                {
                    string src = m.Groups[1].Value;

                    if (File.Exists(src))
                    {
                        htm = htm.Replace(src, HtmlClass.ImgScrToBase64(src));
                    }
                }
                File.WriteAllText(htmfile, htm, Encoding.UTF8);

                successCount++;
                return(true);
            }
            catch (Exception ex)
            {
                exceptionText = ex.Message;
                errorCount++;
                return(false);
            }
        }
コード例 #2
0
        public void ConvertAttributesToString_MultipleMixed_ReturnSeparated()
        {
            // Arrange
            List <HtmlAttribute> attributes = new List <HtmlAttribute>()
            {
                HtmlClass.Create("no-border"),
                HtmlAttribute.Create("att-1", "1"),
                HtmlAttribute.Create("att-1", "2"),
                HtmlStyle.Create("display", "table"),
                HtmlStyle.Create("border", "1px solid #999"),
                HtmlAttribute.Create("att-1", "2"),
                HtmlStyle.Create("font-family", "Tahoma"),
                HtmlStyle.Create("color", "#555"),
                HtmlStyle.Create("font-family", "Arial"),
                HtmlAttribute.Create("att-2", "1"),
                HtmlAttribute.Create("att-3", "3"),
                HtmlClass.Create("no-border"),
                HtmlClass.Create("bold"),
                HtmlStyle.Create("font-family", "Arial")
            };
            var output = @" att-1=""1 2"" att-2=""1"" att-3=""3"" class=""bold no-border"" style=""border: 1px solid #999; color: #555; display: table; font-family: Arial Tahoma;""";

            // Act
            var result = HtmlHelper.ConvertAttributesToString(attributes);

            // Assert
            Assert.Equal(output, result);
        }
コード例 #3
0
        public void Equals_Null()
        {
            // Arrange
            var orig = HtmlClass.Create(value);

            // Assert
            Assert.False(orig.Equals(null));
        }
コード例 #4
0
        public void ToString_ReturnString()
        {
            // Act
            var obj = HtmlClass.Create(value);

            // Assert
            Assert.Equal($@"{name}=""{value}""", obj.ToString());
        }
コード例 #5
0
ファイル: CssParser.cs プロジェクト: adrianillo/HtmlToPdf
        ///<summary>
        ///Converts a style sheet to HtmlClass
        ///</summary>
        ///<param name="sec">Style sheet</param>
        ///<returns>HtmlClass</returns>
        public HtmlClass Parser(String CascadingStyleSheet)
        {
            HtmlClass result=null;

            if (!String.IsNullOrEmpty(CascadingStyleSheet))
            {
                 result = new HtmlClass();
                //Remove comments
                MatchCollection MatchList = rStyles.Matches(Regex.Replace(CascadingStyleSheet, CSSComments, String.Empty));
                foreach (Match item in MatchList)
                {
                    if (item != null && item.Groups != null && item.Groups["selector"] != null &&
                        item.Groups["selector"].Captures != null && item.Groups["selector"].Captures[0] != null
                        && !String.IsNullOrEmpty(item.Groups["selector"].Value))
                    {
                        String classname = item.Groups["selector"].Captures[0].Value.Trim();
                        HtmlStyle style = new HtmlStyle();

                        if (classname.Trim().Length > 0)
                        {
                            foreach (string cn in classname.Split(','))
                            {
                                string nameclass = cn;
                                nameclass = nameclass.Replace("\n", "");
                                nameclass = nameclass.Replace("\r", "");
                                classType typeOfClass = classType.styleClass;
                                if (nameclass.StartsWith("."))
                                {
                                    nameclass = nameclass.Substring(1, nameclass.Length - 1);
                                    typeOfClass = classType.styleClass;
                                }
                                else if (nameclass.StartsWith("#"))
                                {
                                    nameclass = nameclass.Substring(1, nameclass.Length - 1);
                                    typeOfClass = classType.styleIdElement;
                                }
                                else
                                    typeOfClass = classType.styleTypeElement;

                                for (int i = 0; i < item.Groups["name"].Captures.Count; i++)
                                {
                                    String styleproperty = item.Groups["name"].Captures[i].Value;
                                    String valueproperty = item.Groups["value"].Captures[i].Value;
                                    style = new SetStyle().AddPropertyValue(styleproperty, valueproperty, style);

                                }
                                result.AddClass(nameclass, style, typeOfClass);
                            }
                        }
                    }
                }
            }
            //up here are implemented styles in style tags

            //from here i implement elements what not are implemented in style tag
            //todo ...
            return result;
        }
コード例 #6
0
        public void Equals_OtherObject()
        {
            // Arrange
            var orig = HtmlClass.Create(value);
            var str  = $@"{name}=""{value}""";

            // Assert
            Assert.False(orig.Equals(str));
        }
コード例 #7
0
        public void Equals_IsNotEqual()
        {
            // Arrange
            var orig  = HtmlClass.Create(value);
            var other = HtmlClass.Create("not same");

            // Assert
            Assert.False(orig.Equals(other));
        }
コード例 #8
0
        public void Equals_IsEqual()
        {
            // Arrange
            var orig = HtmlClass.Create(value);
            var copy = HtmlClass.Create(value);

            // Assert
            Assert.True(orig.Equals(copy));
        }
コード例 #9
0
        public void Constructor_SetValues()
        {
            // Act
            var obj = new HtmlClass(value);

            // Assert
            Assert.Equal(name, obj.Name);
            Assert.Equal(value, obj.Value);
        }
コード例 #10
0
        public void Constructor_Empty()
        {
            // Act
            var obj = new HtmlClass();

            // Assert
            Assert.Equal(name, obj.Name);
            Assert.Null(obj.Value);
        }
コード例 #11
0
        public void Create_ReturnNewInstance()
        {
            // Act
            var obj = HtmlClass.Create(value);

            // Assert
            Assert.Equal(name, obj.Name);
            Assert.Equal(value, obj.Value);
            Assert.IsType <HtmlClass>(obj);
        }
コード例 #12
0
        public void ConvertAttributesToString_MultipleClasses_ReturnClassCombined()
        {
            // Arrange
            List <HtmlAttribute> attributes = new List <HtmlAttribute>()
            {
                HtmlClass.Create("no-border"),
                HtmlClass.Create("bold"),
                HtmlClass.Create("no-border")
            };
            var output = @" class=""bold no-border""";

            // Act
            var result = HtmlHelper.ConvertAttributesToString(attributes);

            Assert.Equal(output, result);
        }
コード例 #13
0
        /// <summary>
        /// TXTY文件转换成HTML
        /// </summary>
        /// <param name="textfile">@"D:\Administrator\Desktop\123.txt"</param>
        /// <param name="htmlfile">@"D:\Administrator\Desktop\123.html"</param>
        /// <returns></returns>
        private bool ConvertTextFileToHtmlFile(string textfile, string htmlfile)
        {
            try
            {
                string text  = HtmlClass.TextToHtml(File.ReadAllText(textfile, Encoding.Default));
                string title = FileCore.GetHTMLTitleTag(text);
                if (title != "")
                {
                    text = text.Replace(title, Path.GetFileNameWithoutExtension(textfile));
                }

                string html = string.Format("<html><head><title>{0}</title></head><body style=\"margin: 5px\">{1}</body></html>", title, text);

                File.WriteAllText(htmlfile, html, Encoding.UTF8);
                successCount++;
                return(true);
            }
            catch (Exception ex)
            {
                exceptionText = ex.Message;
                errorCount++;
                return(false);
            }
        }