Пример #1
0
 public HtmlOutputFile(string name)
     : base(name)
 {
     head = new TextFile("head");
 }
Пример #2
0
        ///// <summary>
        ///// Builds an object from the specified name and prefix.
        ///// The prefix can be null.
        ///// </summary>
        ///// <param name="name"></param>
        ///// <param name="prefix"></param>
        ///// <returns></returns>
        //public object BuildObject(string name, string prefix)
        //{
        //    if (string.IsNullOrWhiteSpace(name))
        //        throw new QuicException("Object name cannot be null or empty.", this.SourcePath);

        //    //see if the name is actually a prefix
        //    bool nameIsPrefix = true;
        //    string namespaceUrl = this.internalXmlDoc.LastChild.GetNamespaceOfPrefix(name);

        //    if (string.IsNullOrWhiteSpace(namespaceUrl)) //if true, then name is NOT a prefix
        //    {
        //        nameIsPrefix = false;

        //        if (string.IsNullOrWhiteSpace(prefix))
        //            namespaceUrl = this.internalXmlDoc.LastChild.NamespaceURI;
        //        else
        //            namespaceUrl = this.internalXmlDoc.LastChild.GetNamespaceOfPrefix(prefix);

        //        if (string.IsNullOrWhiteSpace(namespaceUrl))
        //            throw new QuicException(string.Format("Cannot find assembly for object '{0}{1}'",
        //                (string.IsNullOrWhiteSpace(prefix) ? "" : prefix + ":"), name), this.SourcePath); 
        //    }

        //    string assemblyPath = null, className = null;
        //    if (namespaceUrl.Contains(':'))
        //    {
        //        string[] namespaceUrlParts = namespaceUrl.Split(':');
        //        if (namespaceUrlParts.Length != 2)
        //            throw new QuicException(string.Format("Badly formed namespace url '{0}'", namespaceUrl), this.SourcePath);
        //        assemblyPath = namespaceUrlParts[0].Trim();
        //        className = namespaceUrlParts[1].Trim();
        //    }
        //    else
        //    {
        //        assemblyPath = namespaceUrl.Trim();
        //        className = "";
        //    }
        //    if (assemblyPath == string.Empty)
        //        throw new QuicException(string.Format("No assembly path was specified for the object '{0}'", name), this.SourcePath);
        //    assemblyPath = FileSystemServices.GetAbsolutePath(assemblyPath);
        //    if (nameIsPrefix == false)
        //        className = (className == string.Empty ? className : className + ".") + name;
        //    if (className == string.Empty)
        //        throw new QuicException(string.Format("No class name was specified for the object '{0}'", name), this.SourcePath);

        //    Assembly assembly = GetCachedOrNewAssembly(assemblyPath);

        //    Type elementType = assembly.GetType(className, true, this.OutputOptions.IgnoreTagCase);
        //    object obj = Activator.CreateInstance(elementType);

        //    //set its document (MUST be done b4 setting its properties)
        //    if (obj is Element)
        //        ((Element)obj).Document = this;

        //    return obj;
        //}

        /// <summary>
        /// Generates the output files.
        /// </summary>
        /// <param name="outputDir"></param>
        public void Render(string outputDir) 
        {
            try 
            {
                //get the root output dir (we dont expose this dir)
                DirectoryInfo dirInfo = new DirectoryInfo(outputDir);
                if (!dirInfo.Parent.Exists)  //should have a parent that exists
                    throw new DirectoryNotFoundException(string.Format("Cannot find parent directory of output directory:\n'{0}'", outputDir));
                OutputDirectory parentOfOutputDir = new OutputDirectory(dirInfo.Parent.FullName);

                //set the output dir (the one we expose)
                this.OutputDirectory = new OutputDirectory(dirInfo.Name);

                //get the output file //Initializer
                XElement rootTag = this.internalXDoc.Root;

                //OutputFile
                XAttribute attri = rootTag.Attribute("OutputFile");
                IXmlLineInfo attriLineInfo = (IXmlLineInfo)attri;
                string outfileAttri = null;
                if (attri != null)
                {
                    outfileAttri = attri.Value;
                }
                //else
                //{
                //    outfileAttri = "{HtmlFileInitializer " + Path.GetFileNameWithoutExtension(new FileInfo(this.SourcePath).Name) + ".html}";
                //}

                if (outfileAttri != null)
                {
                    OutputFile outputFile = null;
                    if (outfileAttri.StartsWith("{") && outfileAttri.EndsWith("}") && !(outfileAttri.StartsWith("{{")))
                    {
                        //remove first char ('{') and last char ('}')
                        outfileAttri = outfileAttri.Substring(1, outfileAttri.Length - 2);

                        //split into file initializer and file name
                        string fileInitializer = null, filename = null;
                        if (outfileAttri.Contains(' '))
                        {
                            fileInitializer = outfileAttri.Substring(0, outfileAttri.IndexOf(' '));
                            filename = outfileAttri.Substring(outfileAttri.IndexOf(' ') + 1); //file name
                        }
                        else
                        {
                            fileInitializer = outfileAttri;
                            filename = null;
                        }

                        object obj = null;
                        try { obj = this.BuildObject(fileInitializer); }
                        catch (TypeLoadException)
                        {
                            try { obj = this.BuildObject(fileInitializer + "Initializer"); }
                            catch (TypeLoadException) 
                            {
                                try
                                { obj = this.BuildObject(fileInitializer + "Initialiser"); }
                                catch (Exception ex)
                                {
                                    throw new QuicException(ex.Message,
                                      this.SourcePath, attriLineInfo.LineNumber, attriLineInfo.LinePosition);
                                } 
                            }
                        }

                        if (!(obj is FileInitializer))
                        {
                            throw new QuicException(string.Format("Object '{0}' is not a file initializer.", fileInitializer),
                                   this.SourcePath, attriLineInfo.LineNumber, attriLineInfo.LinePosition); 
                        }

                        var fileInitObj = (FileInitializer)obj;
                        outputFile = fileInitObj.InitializeFile(this, filename);
                        if (outputFile.ParentDirectory == null)
                        {
                            this.OutputDirectory.Add(outputFile, true);
                        }
                    }
                    else
                    {
                        //if value starts with "{{" and ends with "}", change that "{{" to "{"
                        if (outfileAttri.StartsWith("{{") && outfileAttri.EndsWith("}"))
                        {
                            outfileAttri = outfileAttri.Substring(1);
                        }

                        outputFile = new TextFile(outfileAttri);
                        this.OutputDirectory.Add(outputFile, true);
                    }
                    this.OutputFile = outputFile;
                }
                else
                {
                    HtmlFileInit fileInitObj = new HtmlFileInit();
                    OutputFile outputFile = fileInitObj.InitializeFile(this,
                        Path.GetFileNameWithoutExtension(new FileInfo(this.SourcePath).Name) + ".html");
                    if (outputFile.ParentDirectory == null)
                    {
                        this.OutputDirectory.Add(outputFile, true);
                    }
                    this.OutputFile = outputFile;
                }

                //enter <head>
                if (this.OutputFile is HtmlOutputFile)
                    ((HtmlOutputFile)this.OutputFile).CurrentSection = ((HtmlOutputFile)this.OutputFile).HeadSection;
                //render head
                foreach (var hd in headElements)//(var res in resDic.Values) 
                {
                    hd.BeginRender();
                }

                //enter <body>
                if (this.OutputFile is HtmlOutputFile)
                    ((HtmlOutputFile)this.OutputFile).CurrentSection = ((HtmlOutputFile)this.OutputFile);
                //render body
                foreach (var el in bodyElements)
                {
                    el.BeginRender();
                }

                //save output dir
                parentOfOutputDir.Add(this.OutputDirectory, true);
                this.OutputDirectory.Commit(false, true);
            }
            catch (QuicException)
            {
                throw;
            }
            catch (XmlException ex)
            {
                throw new QuicException(ex.Message, this.SourcePath, ex.LineNumber, ex.LinePosition, ex);
            }
            catch (Exception ex)
            {
                throw new QuicException(ex.Message, this.SourcePath, ex);
            }
        }