Beispiel #1
0
        public static void extract_favicon1(string filename)
        {
            string html = System.IO.File.ReadAllText(filename);

            afh.HTML.HTMLDocument doc = afh.HTML.HTMLDocument.Parse(html);

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("bm_favicons.css")){
                sw.WriteLine("/* favicons */");
                sw.WriteLine("li[favicon]{padding-left:20px;background-position:left top;background-repeat:no-repeat;margin-bottom:2px;}");

                //Gen::Dictionary<string,string> dict=new Gen::Dictionary<string,string>();
                //foreach(afh.HTML.HTMLElement a in doc.enumElementsByTagName("a",false)){
                //  string k=a.getAttribute("icon_uri",false);
                //  if(k==null||k==""||dict.ContainsKey(k))continue;
                //  string data=a.getAttribute("icon",false);
                //  if(data=="")data=k;
                //  dict.Add(k,data);
                //  sw.WriteLine("li[favicon=\"{0}\"]{{background-image:url({1});}}",k,data);
                //}

                //foreach(afh.HTML.HTMLElement a in doc.enumElementsByTagName("a",false)){
                //  string k=a.getAttribute("icon_uri",false);
                //  if(k==null||k=="")continue;
                //  string data=a.getAttribute("icon",false);
                //  if(data=="")data=k;
                //  sw.WriteLine("li[favicon=\"{0}\"]{{background-image:url({1});}}",k,data);
                //}

                Gen::SortedList <string, string> dict = new Gen::SortedList <string, string>();
                foreach (afh.HTML.HTMLElement a in doc.enumElementsByTagName("a", false))
                {
                    string k = a.getAttribute("icon_uri", false);
                    if (k == null || k == "")
                    {
                        continue;
                    }

                    string data = a.getAttribute("icon", false);
                    if (dict.ContainsKey(k) && dict[k] != "")
                    {
                        continue;
                    }

                    dict[k] = data;
                }
                foreach (Gen::KeyValuePair <string, string> p in dict)
                {
                    string k = p.Key;
                    string d = p.Value;
                    if (d == "")
                    {
                        d = k;
                    }
                    sw.WriteLine("li[favicon=\"{0}\"]{{background-image:url({1});}}", k, d);
                }
            }
        }
Beispiel #2
0
        public static void extract_bookmarks2(string filename)
        {
            string html = System.IO.File.ReadAllText(filename);

            afh.HTML.HTMLDocument doc    = afh.HTML.HTMLDocument.Parse(html);
            BookmarkTree          result = BookmarkTree.CreateFromBookmarkHtml(doc);

            result.ExportToXml("bm_all2.xml");
        }
Beispiel #3
0
        public static void extract_favicon2(string filename)
        {
            string html = System.IO.File.ReadAllText(filename);

            afh.HTML.HTMLDocument doc = afh.HTML.HTMLDocument.Parse(html);

            // read bookmarks.html
            Gen::SortedList <string, string> dict = new Gen::SortedList <string, string>();

            foreach (afh.HTML.HTMLElement a in doc.enumElementsByTagName("a", false))
            {
                string k = a.getAttribute("icon_uri", false);
                if (k == null || k == "")
                {
                    continue;
                }

                string data = a.getAttribute("icon", false);
                if (dict.ContainsKey(k) && dict[k] != "")
                {
                    continue;
                }

                dict[k] = data;
            }

            // output xml
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("bm_fav.xml")){
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine("<favicons>");
                foreach (Gen::KeyValuePair <string, string> p in dict)
                {
                    string k = p.Key;
                    string d = p.Value;
                    sw.WriteLine("  <favicon url=\"{0}\" data=\"{1}\" />", k, d);
                }
                sw.WriteLine("</favicons>");
            }

            // output css
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("bm_fav.css")){
                sw.WriteLine("/* favicons */");
                sw.WriteLine("li[favicon]{padding-left:20px;background-position:left top;background-repeat:no-repeat;margin-bottom:2px;}");
                foreach (Gen::KeyValuePair <string, string> p in dict)
                {
                    string k = p.Key;
                    string d = p.Value;
                    if (d == "")
                    {
                        d = k;
                    }
                    sw.WriteLine("li[favicon=\"{0}\"]{{background-image:url({1});}}", k, d);
                }
            }
        }
Beispiel #4
0
 private void test_ParseHtml(string htmlSource)
 {
     afh.HTML.HTMLDocument doc = afh.HTML.HTMLDocument.Parse(htmlSource);
     //foreach(KeyValuePair<TextRange,AnalyzeError> pair in hp.wreader.LinearReader.EnumErrors()){
     //  string loc=string.Format("エラー {0}-{1}",pair.Key.start,pair.Key.end);
     //  this.listView1.Items.Add(new ListViewItem(new string[]{pair.Value.message,loc}));
     //}
     foreach (afh.HTML.HTMLError err in doc.ErrorList)
     {
         string loc = string.Format("Error {0}-{1}", err.start, err.end);
         this.listView1.Items.Add(new ListViewItem(new string[] { err.message, loc }));
     }
     this.domViewer1.Add(doc);
 }
Beispiel #5
0
        private void button1_Click(object sender, System.EventArgs e)
        {
            if (Forms::DialogResult.OK != this.openFileDialog1.ShowDialog(this))
            {
                return;
            }
            string html = System.IO.File.ReadAllText(this.openFileDialog1.FileName);

            afh.HTML.HTMLDocument document = afh.HTML.HTMLDocument.Parse(html);
            Bookmark.BookmarkTree tree     = mwg.Link.Bookmark.BookmarkTree.CreateFromBookmarkHtml(document);

            BookmarkTreeNode root = new BookmarkTreeNode(tree.RootNode);

            root.SetupAutoDD();
            this.treeView1.Nodes.Add(root);
        }
Beispiel #6
0
        public static void extract_favicon3(string filename)
        {
            string html = System.IO.File.ReadAllText(filename);

            afh.HTML.HTMLDocument doc = afh.HTML.HTMLDocument.Parse(html);

            // read bookmarks.html
            FaviconList flist = new FaviconList();

            foreach (afh.HTML.HTMLElement a in doc.enumElementsByTagName("a", false))
            {
                string k = a.getAttribute("icon_uri", false);
                if (k == null || k == "")
                {
                    continue;
                }
                flist.Add(k, a.getAttribute("icon", false));
            }

            flist.SaveXml("bm_fav.xml");
            flist.WriteCss("bm_fav.css");
        }
Beispiel #7
0
        public static void extract_bookmarks1(string filename)
        {
            string html = System.IO.File.ReadAllText(filename);

            afh.HTML.HTMLDocument doc = afh.HTML.HTMLDocument.Parse(html);

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("bm_all.xml")){
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine("<bookmarks>");
                string indent = "  ";
                foreach (afh.HTML.HTMLElement elem in doc.enumAllElements(false))
                {
                    switch (elem.tagName)
                    {
                    case "H3":
                        sw.Write(indent);
                        sw.WriteLine("<directory name=\"{0}\">",
                                     afh.Text.TextUtils.EscapeXml(elem.innerText)
                                     );
                        indent += "  ";
                        break;

                    case "HR":
                        sw.Write(indent);
                        sw.WriteLine("<separator/>");
                        break;

                    case "DT":
                        string name = null;
                        string url  = null;
                        foreach (afh.HTML.HTMLElement a in elem.enumElementsByTagName("A", true))
                        {
                            name = a.innerText;
                            url  = a.getAttribute("href", false);
                            break;
                        }
                        if (url == null)
                        {
                            break;
                        }

                        sw.Write(indent);
                        sw.WriteLine("<link name=\"{0}\" url=\"{1}\"/>",
                                     afh.Text.TextUtils.EscapeXml(name),
                                     afh.Text.TextUtils.EscapeXml(url)
                                     );
                        break;

                    case "DD":
                        string text = "";
                        foreach (afh.HTML.HTMLNode node in elem.childNodes)
                        {
                            afh.HTML.HTMLTextNode textNode = node as afh.HTML.HTMLTextNode;
                            if (textNode == null)
                            {
                                continue;
                            }

                            text = textNode.data;
                            break;
                        }
                        if (text != "")
                        {
                            sw.Write(indent);
                            sw.WriteLine("<desc>{0}</desc>",
                                         afh.Text.TextUtils.EscapeXml(text.Trim())
                                         );
                        }
                        break;
                    }

                    if (elem.parentNode.lastChild == elem)
                    {
                        afh.HTML.HTMLElement directory = null;
                        // DL>last or DL>last-child:DD>last
                        if (elem.tagName == "p")
                        {
                            if (elem.childNodes.Count == 0)
                            {
                                directory = elem.parentNode;
                            }
                        }
                        else if (elem.parentNode.tagName == "p")
                        {
                            afh.HTML.HTMLElement p = elem.parentNode;
                            if (p.parentNode != null && p.parentNode.lastChild == p)
                            {
                                directory = p.parentNode;
                            }
                        }

                        if (directory != null)
                        {
                            if (directory.tagName == "DD" && directory.parentNode != null)
                            {
                                directory = directory.parentNode;
                            }
                            if (directory.tagName == "DL" && directory.parentNode != null && directory.parentNode.nodeType == afh.HTML.nodeType.ELEMENT_NODE)
                            {
                                indent = indent.Substring(2);
                                sw.Write(indent);
                                sw.WriteLine("</directory>");
                            }
                        }
                    }
                }
                sw.WriteLine("</bookmarks>");
            }
        }
Beispiel #8
0
        /// <summary>
        /// Firefox から出力される bookmark.html を解析して、
        /// その内容に対応する BookmarkTree を生成します。
        /// </summary>
        /// <param name="document">Firefox から出力される bookmarks.html の内容を保持する HTMLDocument を指定します。</param>
        /// <returns>生成した BookmarkTree を返します。</returns>
        public static BookmarkTree CreateFromBookmarkHtml(afh.HTML.HTMLDocument document)
        {
            BookmarkTree ret         = new BookmarkTree();
            BookmarkDir  currentDir  = ret.RootNode;
            BookmarkNode currentNode = ret.RootNode;

            foreach (afh.HTML.HTMLElement elem in document.enumAllElements(false))
            {
                switch (elem.tagName)
                {
                case "H3": {
                    BookmarkDir newDir = new BookmarkDir();
                    newDir.name  = elem.innerText;
                    newDir.ctime = BookmarkNode.UnixTime2DateTime(elem.getAttribute("add_date", false));
                    newDir.utime = BookmarkNode.UnixTime2DateTime(elem.getAttribute("last_modified", false));

                    currentDir.Nodes.Add(newDir);
                    currentDir  = newDir;
                    currentNode = newDir;
                    break;
                }

                case "HR":
                    currentDir.Nodes.Add(currentNode = new BookmarkSep());
                    break;

                case "DT": {
                    afh.HTML.HTMLElement a = elem.enumElementsByTagName("A", true).First();
                    if (a == null)
                    {
                        break;
                    }

                    BookmarkLink newNode = new BookmarkLink();
                    newNode.name    = a.innerText;
                    newNode.url     = a.getAttribute("href", false);
                    newNode.ctime   = BookmarkNode.UnixTime2DateTime(a.getAttribute("add_date", false));
                    newNode.utime   = BookmarkNode.UnixTime2DateTime(a.getAttribute("last_modified", false));
                    newNode.charset = a.getAttribute("last_charset", false);

                    currentDir.Nodes.Add(newNode);
                    currentNode = newNode;
                    break;
                }

                case "DD":
                    string text = "";
                    foreach (afh.HTML.HTMLNode node in elem.childNodes)
                    {
                        afh.HTML.HTMLTextNode textNode = node as afh.HTML.HTMLTextNode;
                        if (textNode == null)
                        {
                            continue;
                        }

                        text = textNode.data.Trim();
                        break;
                    }
                    if (text == "")
                    {
                        break;
                    }

                    BookmarkDir isDir = currentNode as BookmarkDir;
                    if (isDir != null)
                    {
                        isDir.description = text;
                        break;
                    }

                    BookmarkLink isLnk = currentNode as BookmarkLink;
                    if (isLnk != null)
                    {
                        isLnk.description = text;
                        break;
                    }
                    break;
                }

                // directory 終了判定 (本当にこれで大丈夫か?)
                if (elem.parentNode.lastChild == elem)
                {
                    afh.HTML.HTMLElement directory = null;
                    // DL>last or DL>last-child:DD>last
                    if (elem.tagName == "p")
                    {
                        if (elem.childNodes.Count == 0)
                        {
                            directory = elem.parentNode;
                        }
                    }
                    else if (elem.parentNode.tagName == "p")
                    {
                        afh.HTML.HTMLElement p = elem.parentNode;
                        if (p.parentNode != null && p.parentNode.lastChild == p)
                        {
                            directory = p.parentNode;
                        }
                    }

                    if (directory != null)
                    {
                        if (directory.tagName == "DD" && directory.parentNode != null)
                        {
                            directory = directory.parentNode;
                        }
                        if (directory.tagName == "DL" && directory.parentNode != null && directory.parentNode.nodeType == afh.HTML.nodeType.ELEMENT_NODE)
                        {
                            // go to outer directory
                            if (currentDir != ret.RootNode)
                            {
                                currentDir = (BookmarkDir)currentDir.Parent;
                            }
                        }
                    }
                }
            }

            return(ret);
        }