Esempio n. 1
0
    public override void Render()
    {
        if (this.Document.OutputFile is TextFile)
        {
            TextFile outputFile = (TextFile)this.Document.OutputFile;

            //opening tag
            outputFile.Write("<img");
            outputFile.Write(EmitID());

            if (Source != null)
            {
                try
                {
                    //get image absolute path
                    string absolutePath = FileSystemServices.GetAbsolutePath(Source, this.Document.SourcePath);
                    string finalPath = "";
                    //check if the image has already been saved
                    if (savedImages.ContainsKey(absolutePath.ToLower()))
                    {
                        finalPath = savedImages[absolutePath.ToLower()];
                    }
                    else
                    {
                        var imgObj = this.Document.OutputDirectory.Get("images");
                        if (imgObj == null)
                        {
                            imgObj = new OutputDirectory("images");
                            this.Document.OutputDirectory.Add(imgObj, true);
                        }
                        if (imgObj is OutputDirectory)
                        {
                            string imgName = Path.GetFileNameWithoutExtension(absolutePath);
                            string imgExt = Path.GetExtension(absolutePath);

                            var ImgDir = (OutputDirectory)imgObj;

                            string uniqueName = imgName + imgExt;
                            var imgOutput = ImgDir.Get(uniqueName);
                            int copyCount = 2;
                            while (imgOutput != null) //while an image with that name exists
                            {
                                //try names like "<img-name>2.png", "<img-name>3.png", "<img-name>4.png",...
                                uniqueName = imgName + copyCount + imgExt;
                                imgOutput = ImgDir.Get(uniqueName);
                                copyCount++;
                            }
                            //if (imgOutput == null) //not necessary since the while loop above ensures that it MUST be null on this line
                            {
                                imgOutput = new BytesFile(uniqueName);
                                //Bitmap image = new Bitmap(absolutePath);
                                //MemoryStream stream = new MemoryStream();
                                //image.Save(stream, ImageFormat.Png);
                                //((BytesOutputFile)imgOutput).Bytes = stream.GetBuffer();
                                ((BytesFile)imgOutput).Bytes = FileSystemServices.ReadBytesFromFile(absolutePath);
                                ImgDir.Add(imgOutput, true);

                                finalPath = "images\\" + uniqueName;
                                savedImages.Add(absolutePath.ToLower(), finalPath);
                            }
                        }
                    }

                    outputFile.Write(" src=\"" + finalPath + "\"");
                }
                catch (Exception ex)
                {
                    Messenger.Notify(
                            new QuicException(ex.Message,
                                this.Document.SourcePath, this.Line, this.Column, ex));
                }
            }
            outputFile.Write(EmitClasses());
            if (Hint != null)
            {
                outputFile.Write(string.Format(" alt=\"{0}\"", Hint));
            }

            //custom properties
            outputFile.Write(EmitCustomProperties());

            //closing tag
            outputFile.Write(">");
        }
    }
Esempio n. 2
0
    void CheckForNecessaryFiles()
    {
        //css
        var cssObj = this.Document.OutputDirectory.Get("css");
        if (cssObj == null) //ensure the css dir exists
        {
            cssObj = new OutputDirectory("css");
            this.Document.OutputDirectory.Add(cssObj, true);
        }
        if (cssObj is OutputDirectory)
        {
            var cssDir = (OutputDirectory)cssObj;

            //ensure the relevant css files exist
            var bootstrapCss = cssDir.Get("bootstrap.css");
            if (bootstrapCss == null)
            {
                bootstrapCss = new CssOutputFile("bootstrap.css");
                ((CssOutputFile)bootstrapCss).Text = Properties.Resources.BootstrapDotCss;
                cssDir.Add(bootstrapCss, true);
            }

            var bootstrapCssMap = cssDir.Get("bootstrap.css.map");
            if (bootstrapCssMap == null)
            {
                bootstrapCssMap = new CssOutputFile("bootstrap.css.map");
                ((CssOutputFile)bootstrapCssMap).Text = Properties.Resources.BootstrapDotCssDotMap;
                cssDir.Add(bootstrapCssMap, true);
            }

            var bootstrapMinCss = cssDir.Get("bootstrap.min.css");
            if (bootstrapMinCss == null)
            {
                bootstrapMinCss = new CssOutputFile("bootstrap.min.css");
                ((CssOutputFile)bootstrapMinCss).Text = Properties.Resources.BootstrapDotMinDotCss;
                cssDir.Add(bootstrapMinCss, true);
            }

            var bootstrapThemeCss = cssDir.Get("bootstrap-theme.css");
            if (bootstrapThemeCss == null)
            {
                bootstrapThemeCss = new CssOutputFile("bootstrap-theme.css");
                ((CssOutputFile)bootstrapThemeCss).Text = Properties.Resources.BootstrapDashThemeDotCss;
                cssDir.Add(bootstrapThemeCss, true);
            }

            var bootstrapThemeCssMap = cssDir.Get("bootstrap-theme.css.map");
            if (bootstrapThemeCssMap == null)
            {
                bootstrapThemeCssMap = new CssOutputFile("bootstrap-theme.css.map");
                ((CssOutputFile)bootstrapThemeCssMap).Text = Properties.Resources.BootstrapDashThemeDotCssDotMap;
                cssDir.Add(bootstrapThemeCssMap, true);
            }

            var bootstrapThemeMinCss = cssDir.Get("bootstrap-theme.min.css");
            if (bootstrapThemeMinCss == null)
            {
                bootstrapThemeMinCss = new CssOutputFile("bootstrap-theme.min.css");
                ((CssOutputFile)bootstrapThemeMinCss).Text = Properties.Resources.BootstrapDashThemeDotMinDotCss;
                cssDir.Add(bootstrapThemeMinCss, true);
            }
        }

        //javascript
        var jsObj = this.Document.OutputDirectory.Get("js");
        if (jsObj == null) //ensure the js dir exists
        {
            jsObj = new OutputDirectory("js");
            this.Document.OutputDirectory.Add(jsObj, true);
        }
        if (jsObj is OutputDirectory)
        {
            var jsDir = (OutputDirectory)jsObj;

            //ensure the relevant js files exist
            var bootstrapJs = jsDir.Get("bootstrap.js");
            if (bootstrapJs == null)
            {
                bootstrapJs = new JsOutputFile("bootstrap.js");
                ((JsOutputFile)bootstrapJs).Text = Properties.Resources.BootstrapDotJs;
                jsDir.Add(bootstrapJs, true);
            }

            var bootstrapMinJs = jsDir.Get("bootstrap.min.js");
            if (bootstrapMinJs == null)
            {
                bootstrapMinJs = new JsOutputFile("bootstrap.min.js");
                ((JsOutputFile)bootstrapMinJs).Text = Properties.Resources.BootstrapDotMinDotJs;
                jsDir.Add(bootstrapMinJs, true);
            }

            var jqueryJs = jsDir.Get("jquery.js");
            if (jqueryJs == null)
            {
                jqueryJs = new JsOutputFile("jquery.js");
                ((JsOutputFile)jqueryJs).Text = Properties.Resources.JqueryDotJs;
                jsDir.Add(jqueryJs, true);
            }

            var npmJs = jsDir.Get("npm.js");
            if (npmJs == null)
            {
                npmJs = new JsOutputFile("npm.js");
                ((JsOutputFile)npmJs).Text = Properties.Resources.NpmDotJs;
                jsDir.Add(npmJs, true);
            }
        }

        //fonts
        var fontsObj = this.Document.OutputDirectory.Get("fonts");
        if (fontsObj == null) //ensure the fonts dir exists
        {
            fontsObj = new OutputDirectory("fonts");
            this.Document.OutputDirectory.Add(fontsObj, true);
        }
        if (fontsObj is OutputDirectory)
        {
            var fontsDir = (OutputDirectory)fontsObj;

            //ensure the relevant font files exist
            var glyphEot = fontsDir.Get("glyphicons-halflings-regular.eot");
            if (glyphEot == null)
            {
                glyphEot = new BytesFile("glyphicons-halflings-regular.eot");
                ((BytesFile)glyphEot).Bytes = Properties.Resources.GlyphiconsDashHalflingsDashRegularDotEot;
                fontsDir.Add(glyphEot, true);
            }

            var glyphSvg = fontsDir.Get("glyphicons-halflings-regular.svg");
            if (glyphSvg == null)
            {
                glyphSvg = new BytesFile("glyphicons-halflings-regular.svg");
                ((BytesFile)glyphSvg).Bytes = Properties.Resources.GlyphiconsDashHalflingsDashRegularDotSvg;
                fontsDir.Add(glyphSvg, true);
            }

            var glyphTtf = fontsDir.Get("glyphicons-halflings-regular.ttf");
            if (glyphTtf == null)
            {
                glyphTtf = new BytesFile("glyphicons-halflings-regular.ttf");
                ((BytesFile)glyphTtf).Bytes = Properties.Resources.GlyphiconsDashHalflingsDashRegularDotTtf;
                fontsDir.Add(glyphTtf, true);
            }

            var glyphWoff = fontsDir.Get("glyphicons-halflings-regular.woff");
            if (glyphWoff == null)
            {
                glyphWoff = new BytesFile("glyphicons-halflings-regular.woff");
                ((BytesFile)glyphWoff).Bytes = Properties.Resources.GlyphiconsDashHalflingsDashRegularDotWoff;
                fontsDir.Add(glyphWoff, true);
            }

            var glyphWoff2 = fontsDir.Get("glyphicons-halflings-regular.woff2");
            if (glyphWoff2 == null)
            {
                glyphWoff2 = new BytesFile("glyphicons-halflings-regular.woff2");
                ((BytesFile)glyphWoff2).Bytes = Properties.Resources.GlyphiconsDashHalflingsDashRegularDotWoff2;
                fontsDir.Add(glyphWoff2, true);
            }
        }
    }
Esempio n. 3
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);
            }
        }