Example #1
0
        // Read one HTML page and return a slide show
        public SlideShow ReadSlideShow(string aSlideFile, out string aDiagnostic)
        {
            aDiagnostic = null;

            // Determine the name of the future XML slide show file
            string    xmlFilePath = aSlideFile.Replace(".htm", ".xml");
            SlideShow slideShow   = new SlideShow(xmlFilePath);

            //Console.WriteLine("     HtmlReader ReadSlideShow: parsing " + aSlideFile);
            string html = ReadFile(aSlideFile);

            if (html == null)
            {
                aDiagnostic = "ReadSlideShow: bad HTML slideshow file " + aSlideFile;
                return(null);
            }
            else
            {
                ParseHTML parse = new ParseHTML();
                parse.Source = html;

                HtmlPreprocess htmlPreprocess = new HtmlPreprocess();

                // Default overall title for the slide show, hopefully replaced with something better
                string title           = "A most peculiar day";
                bool   collectingTitle = false;

                bool    collectingCaption = false;
                Caption caption           = new Caption();
                string  link = "";
                while (!parse.Eof())
                {
                    char ch = parse.Parse();
                    if (ch == 0)
                    {
                        AttributeList tag = parse.GetTag();
                        if (tag.Name.Equals("title", StringComparison.CurrentCultureIgnoreCase))
                        {
                            collectingTitle = true;       // Start collecting title
                            title           = string.Empty;
                        }
                        else if (tag.Name.Equals("/title", StringComparison.CurrentCultureIgnoreCase))
                        {
                            collectingTitle = false;      // Title now complete
                            slideShow.Title = title.Trim();
                        }
                        if (tag.Name.Equals("td", StringComparison.CurrentCultureIgnoreCase))
                        {
                            collectingCaption = true;       // Start collecting new caption
                            caption           = new Caption();
                        }
                        else if (tag.Name.Equals("/td", StringComparison.CurrentCultureIgnoreCase))
                        {
                            collectingCaption = false;      // Any caption is now complete
                            if (!link.Equals(""))
                            {
                                // Got a link to go with the caption
                                slideShow.Add(link, caption);
                                link = "";
                            }
                        }
                        else if (collectingCaption &&
                                 tag.Name.Equals("p", StringComparison.CurrentCultureIgnoreCase))
                        {
                            // HTML paragraph tag within caption
                            caption.NewLine();
                        }
                        else if (tag["href"] != null)
                        {
                            string href = tag["href"].Value.Replace('/', '\\');
                            if (IsPhoto(href))
                            {
                                //Console.WriteLine("     + HtmlReader ReadSlideShow: add " + href +
                                //                  " from tag " + tag.Name);
                                link = href;
                            }
                        }

                        // Preprocessing of regular character stream starts with clean sheet after tag
                        htmlPreprocess.Reset();
                    }
                    else
                    {
                        // Got a character
                        ch = htmlPreprocess.Add(ch);
                        if (ch != HtmlPreprocess.NullChar)
                        {
                            if (collectingTitle)
                            {
                                title += ch;
                            }
                            else if (collectingCaption)
                            {
                                caption.AddChar(ch);
                            }
                        }
                    }
                }

                return(slideShow);
            }
        }