コード例 #1
0
        /// <summary>
        /// Generates all web page files using templates and data accumulated in this class.
        /// </summary>
        /// <param name="destinationFolderName"></param>
        /// <param name="physicalBounds"></param>
        public void generateAllPageContentFiles(string destinationFolderName, Rectangle physicalBounds, bool appendToMaster)
        {
            StringBuilder indexPageContent = new StringBuilder();

            // fill in the last missing piece - the click map:
            string clickMapContent = Project.mainCommand.doGenerateClickMap(physicalBounds);

            m_dictionary.Add("{{ClickMap}}", clickMapContent);

            /*
             * indexPageContent.Append("---------- index page HTML -----------<br><br>\r\n");
             *
             * indexPageContent.Append("shortName: <b>" + shortName + "</b><br>\r\n");
             * indexPageContent.Append("heading: <b>" + heading + "</b><br>\r\n");
             * indexPageContent.Append("description: <b>" + description + "</b><br>\r\n");
             * indexPageContent.Append("templateFolder: <b>" + m_templateFolder + "</b><br>\r\n");
             */

            string destIndex           = FromDictionary("index.html");
            string destinationFilename = Path.Combine(destinationFolderName, destIndex);
            string templateFilename    = Path.Combine(m_templateFolder, FromDictionary("template.index.html"));

            // read in the template and fill-the-blanks there:

            FileStream fs         = new FileStream(templateFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
            TextReader textReader = new StreamReader(fs);

            int    lineCounter = 0;
            string str;

            while ((str = textReader.ReadLine()) != null)
            {
                str = str.Trim();
                lineCounter++;

again:
                int pos = str.IndexOf("{{");
                if (pos == -1)
                {
                    indexPageContent.Append(str);
                }
                else
                {
                    indexPageContent.Append(str.Substring(0, pos));
                    str = str.Substring(pos + 2);
                    int pos1 = str.IndexOf("}}");
                    if (pos1 == -1)
                    {
                        // no closing }} -- bad tag. Try recovering.
                        indexPageContent.Append("{{");
                    }
                    else
                    {
                        // ok, we are at the point where replacement token needs to be determined
                        string keyToken = "{{" + str.Substring(0, pos1) + "}}";
                        indexPageContent.Append(FromDictionary(keyToken));
                        str = str.Substring(pos1 + 2);
                    }
                    goto again;
                }
                indexPageContent.Append("\r\n");
            }

            fs.Close();

            Project.writeTextFile(destinationFilename, indexPageContent.ToString());

            // take care of stylesheet:
            string ss = FromDictionary("{{StyleSheet}}");

            if (!"{{StyleSheet}}".Equals(ss))
            {
                File.Copy(Path.Combine(m_templateFolder, ss), Path.Combine(destinationFolderName, ss), true);
            }

            if (appendToMaster)
            {
                DirectoryInfo destinationFolder = new DirectoryInfo(destinationFolderName);
                string        masterFile        = FromDictionary("../index.html");
                string        masterFileName    = Path.Combine(destinationFolder.Parent.FullName, masterFile.Replace("../", ""));

                if (!File.Exists(masterFileName))
                {
                    Project.ErrorBox(null, masterFileName + " not found, will skip inserion to " + masterFile);
                }
                else
                {
                    // find the ../index.html and append relative url of the generated page between <!--trips--> and <!--/trips-->
                    string relativeUrl = destinationFolder.Name + "/" + destIndex;
                    string toInsert    = "<li><a href=\"" + relativeUrl + "\">" + heading + "</a></li>\r\n";
                    bool   masterDirty = false;
                    indexPageContent = new StringBuilder();

                    fs         = new FileStream(masterFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                    textReader = new StreamReader(fs);

                    lineCounter = 0;
                    int state = 0;
                    while ((str = textReader.ReadLine()) != null)
                    {
                        str = str.Trim();
                        lineCounter++;

                        switch (state)
                        {
                        case 0:
                            if (str.Replace(" ", "").StartsWith("<!--trips--><ul>"))
                            {
                                state = 1;
                            }
                            break;

                        case 1:
                            if (str.Replace(" ", "").StartsWith("<!--/trips--></ul>"))
                            {
                                indexPageContent.Append(toInsert);
                                masterDirty = true;
                                state       = 2;
                            }
                            else if (str.IndexOf(toInsert) >= 0)
                            {
                                Project.ErrorBox(null, masterFile + " already has " + toInsert);
                                state = 2;
                            }
                            break;

                        default:
                            break;
                        }

                        indexPageContent.Append(str);
                        indexPageContent.Append("\r\n");
                    }

                    fs.Close();

                    if (masterDirty)
                    {
                        Project.writeTextFile(masterFileName, indexPageContent.ToString());
                    }
                }
            }
        }