protected void Page_Load(object sender, System.EventArgs e) { int macroID = cms.businesslogic.macro.Macro.GetByAlias(helper.Request("umb_macroAlias")).Id; int pageID = int.Parse(helper.Request("umbPageId")); Guid pageVersion = new Guid(helper.Request("umbVersionId")); System.Web.HttpContext.Current.Items["macrosAdded"] = 0; System.Web.HttpContext.Current.Items["pageID"] = pageID.ToString(); // Collect attributes Hashtable attributes = new Hashtable(); foreach (string key in Request.QueryString.AllKeys) { if (key.IndexOf("umb_") > -1) { attributes.Add(key.Substring(4, key.Length - 4), Request.QueryString[key]); } } page p = new page(pageID, pageVersion); macro m = new macro(macroID); Control c = m.renderMacro(attributes, p.Elements, p.PageID); PlaceHolder1.Controls.Add(c); }
protected void renderMacro_Click(object sender, EventArgs e) { int pageID = int.Parse(UmbracoContext.Current.Request["umbPageId"]); string macroAttributes = "macroAlias=\"" + m.Alias + "\""; Guid pageVersion = new Guid(UmbracoContext.Current.Request["umbVersionId"]); Hashtable attributes = new Hashtable(); attributes.Add("macroAlias", m.Alias); macro mRender = new macro(m.Id); foreach (Control c in _dataFields) { try { IMacroGuiRendering ic = (IMacroGuiRendering)c; attributes.Add(c.ID, ic.Value); macroAttributes += " " + c.ID + "=\"" + ic.Value.Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\r", "\\r") + "\""; } catch { } } // document this, for gods sake! HttpContext.Current.Items["macrosAdded"] = 0; HttpContext.Current.Items["pageID"] = pageID.ToString(); page p = new page(pageID, pageVersion); string div = macro.renderMacroStartTag(attributes, pageID, pageVersion).Replace("\\", "\\\\").Replace("'", "\\'"); string macroContent = macro.MacroContentByHttp(pageID, pageVersion, attributes).Replace("\\", "\\\\").Replace("'", "\\'"). Replace("/", "\\/").Replace("\n", "\\n"); if (macroContent.Length > 0 && macroContent.ToLower().IndexOf("<script") > -1) { macroContent = "<b>Macro rendering contains script code</b><br/>This macro won\\'t be rendered in the editor because it contains script code. It will render correct during runtime."; } div += macroContent; div += macro.renderMacroEndTag(); _scriptOnLoad += "\t\tumbracoEditMacroDo('" + macroAttributes.Replace("'", "\\'") + "', '" + m.Name.Replace("'", "\\'") + "', '" + div + "');\n"; /* * ClientScript.RegisterStartupScript(GetType(), "postbackScript", * "<script>\n umbracoEditMacroDo('" + macroAttributes.Replace("'", "\\'") + * "', '" + m.Name.Replace("'", "\\'") + "', '" + div + "');\n</script>"); * ClientScript.RegisterStartupScript(GetType(), "postbackScriptWindowClose", * "<script>\n //setTimeout('window.close()',300);\n</script>"); */ //theForm.Visible = false; }
/// <summary> /// Removes the cache amongst servers for a macro item /// </summary> /// <param name="dc"></param> /// <param name="macro"></param> public static void RemoveMacroCache(this DistributedCache dc, macro macro) { if (macro != null && macro.Model != null) { dc.RefreshByJson(new Guid(DistributedCache.MacroCacheRefresherId), MacroCacheRefresher.SerializeToJsonPayload(macro)); } }
public static void RemoveMacroCache(this DistributedCache dc, macro macro) { if (macro == null || macro.Model == null) { return; } dc.RefreshByJson(DistributedCache.MacroCacheRefresherGuid, MacroCacheRefresher.SerializeToJsonPayload(macro)); }
private string parseMacrosToHtml(string input) { int nodeId = _nodeId; Guid versionId = _versionId; string content = input; string pattern = @"(<\?UMBRACO_MACRO\W*[^>]*/>)"; MatchCollection tags = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); // Page for macro rendering page p = new page(nodeId, versionId); HttpContext.Current.Items["macrosAdded"] = 0; HttpContext.Current.Items["pageID"] = nodeId.ToString(); foreach (Match tag in tags) { // Create div Hashtable attributes = helper.ReturnAttributes(tag.Groups[1].Value); string div = macro.renderMacroStartTag(attributes, nodeId, versionId).Replace(""", "&quot;"); // Insert macro contents here... macro m; if (helper.FindAttribute(attributes, "macroID") != "") { m = new macro(int.Parse(helper.FindAttribute(attributes, "macroID"))); } else { m = new macro(Macro.GetByAlias(helper.FindAttribute(attributes, "macroAlias")).Id); } if (helper.FindAttribute(attributes, "macroAlias") == "") { attributes.Add("macroAlias", m.Alias); } try { div += macro.MacroContentByHttp(nodeId, versionId, attributes); } catch { div += "<span style=\"color: green\">No macro content available for WYSIWYG editing</span>"; } div += macro.renderMacroEndTag(); content = content.Replace(tag.Groups[1].Value, div); } return(content); }
/// <summary> /// Converts a macro to a jsonPayload object /// </summary> /// <param name="macro"></param> /// <returns></returns> private static JsonPayload FromMacro(macro macro) { var payload = new JsonPayload { Alias = macro.Alias, Id = macro.Model.Id }; return(payload); }
/// <summary> /// Parses the content of the templateOutput stringbuilder, and matches any tags given in the /// XML-file /umbraco/config/umbracoTemplateTags.xml. /// Replaces the found tags in the StringBuilder object, with "real content" /// </summary> /// <param name="umbPage"></param> public void Parse(page umbPage) { System.Web.HttpContext.Current.Trace.Write("umbracoTemplate", "Start parsing"); // First parse for known umbraco tags // <?UMBRACO_MACRO/> - macros // <?UMBRACO_GETITEM/> - print item from page, level, or recursive MatchCollection tags = Regex.Matches(_templateOutput.ToString(), "<\\?UMBRACO_MACRO[^>]*/>|<\\?UMBRACO_GETITEM[^>]*/>|<\\?(?<tagName>[\\S]*)[^>]*/>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match tag in tags) { Hashtable attributes = helper.ReturnAttributes(tag.Value.ToString()); if (tag.ToString().ToLower().IndexOf("umbraco_macro") > -1) { String macroID = helper.FindAttribute(attributes, "macroid"); if (macroID != "") { macro tempMacro = getMacro(macroID); _templateOutput.Replace(tag.Value.ToString(), tempMacro.MacroContent.ToString()); } } else { if (tag.ToString().ToLower().IndexOf("umbraco_getitem") > -1) { try { String tempElementContent = umbPage.Elements[helper.FindAttribute(attributes, "field")].ToString(); MatchCollection tempMacros = Regex.Matches(tempElementContent, "<\\?UMBRACO_MACRO(?<attributes>[^>]*)><img[^>]*><\\/\\?UMBRACO_MACRO>", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match tempMacro in tempMacros) { Hashtable tempAttributes = helper.ReturnAttributes(tempMacro.Groups["attributes"].Value.ToString()); String macroID = helper.FindAttribute(tempAttributes, "macroid"); if (Convert.ToInt32(macroID) > 0) { macro tempContentMacro = getMacro(macroID); _templateOutput.Replace(tag.Value.ToString(), tempContentMacro.MacroContent.ToString()); } } _templateOutput.Replace(tag.Value.ToString(), tempElementContent); } catch (Exception e) { System.Web.HttpContext.Current.Trace.Warn("umbracoTemplate", "Error reading element (" + helper.FindAttribute(attributes, "field") + ")", e); } } } } System.Web.HttpContext.Current.Trace.Write("umbracoTemplate", "Done parsing"); }
private HttpResponseMessage GetMacroResultAsHtml(string macroAlias, int pageId, IDictionary <string, object> macroParams) { // note - here we should be using the cache, provided that the preview content is in the cache... var doc = Services.ContentService.GetById(pageId); if (doc == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //need to get a legacy macro object - eventually we'll have a new format but nto yet var macro = new macro(macroAlias); if (macro == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //if it isn't supposed to be rendered in the editor then return an empty string if (macro.DontRenderInEditor) { var response = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. response.Content = new StringContent(string.Empty, Encoding.UTF8, "text/html"); return(response); } //because macro's are filled with insane legacy bits and pieces we need all sorts of wierdness to make them render. //the 'easiest' way might be to create an IPublishedContent manually and populate the legacy 'page' object with that //and then set the legacy parameters. var legacyPage = new global::umbraco.page(doc); UmbracoContext.HttpContext.Items["pageID"] = doc.Id; UmbracoContext.HttpContext.Items["pageElements"] = legacyPage.Elements; UmbracoContext.HttpContext.Items[global::Umbraco.Core.Constants.Conventions.Url.AltTemplate] = null; var renderer = new UmbracoComponentRenderer(UmbracoContext); var result = Request.CreateResponse(); //need to create a specific content result formatted as html since this controller has been configured //with only json formatters. result.Content = new StringContent( renderer.RenderMacro(macro, macroParams, legacyPage).ToString(), Encoding.UTF8, "text/html"); return(result); }
/// <summary> /// Executes the specified macro. /// </summary> /// <param name="macro">The macro.</param> /// <param name="currentPage">The current page.</param> /// <returns>Returns a string of the executed macro XSLT.</returns> public string Execute(MacroModel macro, INode currentPage) { try { string fileLocation = null; if (!string.IsNullOrEmpty(macro.ScriptName)) { fileLocation = string.Concat("../", macro.ScriptName); } else if (!string.IsNullOrEmpty(macro.ScriptCode)) { var xslt = CheckXsltFragment(macro.ScriptCode.Trim()); var md5 = library.md5(xslt); var filename = string.Concat("inline-", md5, ".xslt"); fileLocation = this.CreateTemporaryFile(xslt, filename, true).Replace("~", ".."); } if (string.IsNullOrEmpty(fileLocation)) { return(string.Empty); } var tempMacro = new macro { Model = { Xslt = fileLocation } }; // copy the macro properties across foreach (var property in macro.Properties) { tempMacro.Model.Properties.Add(new MacroPropertyModel(property.Key, property.Value)); } var ctrl = tempMacro.loadMacroXSLT(tempMacro, macro, (Hashtable)HttpContext.Current.Items["pageElements"]); this.Success = true; return(ctrl.RenderToString()); } catch (Exception ex) { this.ResultException = ex; this.Success = false; return(string.Format("<div style=\"border: 1px solid #990000\">Error loading XSLT {0}<br />{1}</div>", macro.ScriptName, GlobalSettings.DebugMode ? ex.Message : string.Empty)); } }
private void buttonAdd_Click(object sender, EventArgs e) { macro m = new macro(textBoxMacroName.Text, exRichTextBox.Text); var items = listBoxMacro.Items; if (m.Name == "") { MessageBox.Show("Please input macro name", "Alert"); return; } else if (listBoxMacro.Items.Cast <macro>().Count(o => o.Name == m.Name) != 0) { if (MessageBox.Show("The name already exists. Do you replace the macro?", "Alert", MessageBoxButtons.YesNo) == DialogResult.Yes) { items[items.IndexOf(items.Cast <macro>().First(item => item.Name == m.Name))] = m; } setMenuItemOfMain(); } else { items.Add(m); setMenuItemOfMain(); } }
private string parseMacrosToHtml(string input) { int nodeId = _nodeId; Guid versionId = _versionId; string content = input; string pattern = @"(<\?UMBRACO_MACRO\W*[^>]*/>)"; MatchCollection tags = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); // Page for macro rendering page p = new page(nodeId, versionId); HttpContext.Current.Items["macrosAdded"] = 0; HttpContext.Current.Items["pageID"] = nodeId.ToString(); foreach (Match tag in tags) { try { // Create div Hashtable attributes = helper.ReturnAttributes(tag.Groups[1].Value); string div = macro.renderMacroStartTag(attributes, nodeId, versionId).Replace(""", "&quot;"); // Insert macro contents here... macro m; if (helper.FindAttribute(attributes, "macroID") != "") m = new macro(int.Parse(helper.FindAttribute(attributes, "macroID"))); else { // legacy: Check if the macroAlias is typed in lowercasing string macroAlias = helper.FindAttribute(attributes, "macroAlias"); if (macroAlias == "") { macroAlias = helper.FindAttribute(attributes, "macroalias"); attributes.Remove("macroalias"); attributes.Add("macroAlias", macroAlias); } if (macroAlias != "") m = new macro(Macro.GetByAlias(macroAlias).Id); else throw new ArgumentException("umbraco is unable to identify the macro. No id or macroalias was provided for the macro in the macro tag.", tag.Groups[1].Value); } if (helper.FindAttribute(attributes, "macroAlias") == "") attributes.Add("macroAlias", m.Alias); try { div += macro.MacroContentByHttp(nodeId, versionId, attributes); } catch { div += "<span style=\"color: green\">No macro content available for WYSIWYG editing</span>"; } div += macro.renderMacroEndTag(); content = content.Replace(tag.Groups[1].Value, div); } catch (Exception ee) { Log.Add(LogTypes.Error, this._nodeId, "Macro Parsing Error: " + ee.ToString()); string div = "<div class=\"umbMacroHolder mceNonEditable\"><p style=\"color: red\"><strong>umbraco was unable to parse a macro tag, which means that parts of this content might be corrupt.</strong> <br /><br />Best solution is to rollback to a previous version by right clicking the node in the tree and then try to insert the macro again. <br/><br/>Please report this to your system administrator as well - this error has been logged.</p></div>"; content = content.Replace(tag.Groups[1].Value, div); } } return content; }
/// <summary> /// Renders the macro with the specified alias, passing in the specified parameters. /// </summary> /// <param name="m">The macro.</param> /// <param name="parameters">The parameters.</param> /// <param name="umbracoPage">The legacy umbraco page object that is required for some macros</param> /// <returns></returns> internal IHtmlString RenderMacro(macro m, IDictionary <string, object> parameters, page umbracoPage) { if (umbracoPage == null) { throw new ArgumentNullException("umbracoPage"); } if (m == null) { throw new ArgumentNullException("m"); } if (_umbracoContext.PageId == null) { throw new InvalidOperationException("Cannot render a macro when UmbracoContext.PageId is null."); } var macroProps = new Hashtable(); foreach (var i in parameters) { //TODO: We are doing at ToLower here because for some insane reason the UpdateMacroModel method of macro.cs // looks for a lower case match. WTF. the whole macro concept needs to be rewritten. //NOTE: the value could have html encoded values, so we need to deal with that macroProps.Add(i.Key.ToLowerInvariant(), (i.Value is string) ? HttpUtility.HtmlDecode(i.Value.ToString()) : i.Value); } var macroControl = m.renderMacro(macroProps, umbracoPage.Elements, _umbracoContext.PageId.Value); string html; if (macroControl is LiteralControl) { // no need to execute, we already have text html = (macroControl as LiteralControl).Text; } else { var containerPage = new FormlessPage(); containerPage.Controls.Add(macroControl); using (var output = new StringWriter()) { // .Execute() does a PushTraceContext/PopTraceContext and writes trace output straight into 'output' // and I do not see how we could wire the trace context to the current context... so it creates dirty // trace output right in the middle of the page. // // The only thing we can do is fully disable trace output while .Execute() runs and restore afterwards // which means trace output is lost if the macro is a control (.ascx or user control) that is invoked // from within Razor -- which makes sense anyway because the control can _not_ run correctly from // within Razor since it will never be inserted into the page pipeline (which may even not exist at all // if we're running MVC). // // I'm sure there's more things that will get lost with this context changing but I guess we'll figure // those out as we go along. One thing we lose is the content type response output. // http://issues.umbraco.org/issue/U4-1599 if it is setup during the macro execution. So // here we'll save the content type response and reset it after execute is called. var contentType = _umbracoContext.HttpContext.Response.ContentType; var traceIsEnabled = containerPage.Trace.IsEnabled; containerPage.Trace.IsEnabled = false; _umbracoContext.HttpContext.Server.Execute(containerPage, output, true); containerPage.Trace.IsEnabled = traceIsEnabled; //reset the content type _umbracoContext.HttpContext.Response.ContentType = contentType; //Now, we need to ensure that local links are parsed html = TemplateUtilities.ParseInternalLinks(output.ToString()); } } return(new HtmlString(html)); }
private string parseMacrosToHtml(string input) { int nodeId = _nodeId; Guid versionId = _versionId; string content = input; string pattern = @"(<\?UMBRACO_MACRO\W*[^>]*/>)"; MatchCollection tags = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); // Page for macro rendering // page p = new page(nodeId, versionId); System.Web.HttpContext.Current.Items["macrosAdded"] = 0; System.Web.HttpContext.Current.Items["pageID"] = nodeId.ToString(); foreach (Match tag in tags) { try { // Create div Hashtable attributes = helper.ReturnAttributes(tag.Groups[1].Value); string div = macro.renderMacroStartTag(attributes, nodeId, versionId); //.Replace(""", "&quot;"); // Insert macro contents here... macro m; if (helper.FindAttribute(attributes, "macroID") != "") { m = new macro(int.Parse(helper.FindAttribute(attributes, "macroID"))); } else { // legacy: Check if the macroAlias is typed in lowercasing string macroAlias = helper.FindAttribute(attributes, "macroAlias"); if (macroAlias == "") { macroAlias = helper.FindAttribute(attributes, "macroalias"); attributes.Remove("macroalias"); attributes.Add("macroAlias", macroAlias); } if (macroAlias != "") { m = new macro(Macro.GetByAlias(macroAlias).Id); } else { throw new ArgumentException("umbraco is unable to identify the macro. No id or macroalias was provided for the macro in the macro tag.", tag.Groups[1].Value); } } if (helper.FindAttribute(attributes, "macroAlias") == "") { attributes.Add("macroAlias", m.Alias); } try { div += macro.MacroContentByHttp(nodeId, versionId, attributes); } catch { div += "<span style=\"color: green\">No macro content available for WYSIWYG editing</span>"; } div += macro.renderMacroEndTag(); content = content.Replace(tag.Groups[1].Value, div); } catch (Exception ee) { Log.Add(LogTypes.Error, this._nodeId, "Macro Parsing Error: " + ee.ToString()); string div = "<div class=\"umbMacroHolder mceNonEditable\"><p style=\"color: red\"><strong>umbraco was unable to parse a macro tag, which means that parts of this content might be corrupt.</strong> <br /><br />Best solution is to rollback to a previous version by right clicking the node in the tree and then try to insert the macro again. <br/><br/>Please report this to your system administrator as well - this error has been logged.</p></div>"; content = content.Replace(tag.Groups[1].Value, div); } } return(content); }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { // collect all attributes set on the control var keys = Attributes.Keys; foreach (string key in keys) MacroAttributes.Add(key.ToLower(), HttpUtility.HtmlDecode(Attributes[key])); if (!MacroAttributes.ContainsKey("macroalias") && !MacroAttributes.ContainsKey("macroAlias")) MacroAttributes.Add("macroalias", Alias); // set pageId to int.MinValue if no pageID was found, // e.g. if the macro was rendered on a custom (non-Umbraco) page int pageId = Context.Items["pageID"] == null ? int.MinValue : int.Parse(Context.Items["pageID"].ToString()); if ((!String.IsNullOrEmpty(Language) && Text != "") || !string.IsNullOrEmpty(FileLocation)) { var tempMacro = new macro(); tempMacro.GenerateMacroModelPropertiesFromAttributes(MacroAttributes); if (string.IsNullOrEmpty(FileLocation)) { tempMacro.Model.ScriptCode = Text; tempMacro.Model.ScriptLanguage = Language; } else { tempMacro.Model.ScriptName = FileLocation; } tempMacro.Model.MacroType = MacroTypes.Script; if (!String.IsNullOrEmpty(Attributes["Cache"])) { var cacheDuration = 0; if (int.TryParse(Attributes["Cache"], out cacheDuration)) tempMacro.Model.CacheDuration = cacheDuration; else System.Web.HttpContext.Current.Trace.Warn("Template", "Cache attribute is in incorect format (should be an integer)."); } var c = tempMacro.renderMacro((Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Exceptions = tempMacro.Exceptions; Controls.Add(c); } else System.Web.HttpContext.Current.Trace.Warn("Template", "Result of inline macro scripting is null"); } else { var tempMacro = macro.GetMacro(Alias); if (tempMacro != null) { try { var c = tempMacro.renderMacro(MacroAttributes, (Hashtable)Context.Items["pageElements"], pageId); if (c != null) Controls.Add(c); else System.Web.HttpContext.Current.Trace.Warn("Template", "Result of macro " + tempMacro.Name + " is null"); } catch (Exception ee) { System.Web.HttpContext.Current.Trace.Warn("Template", "Error adding macro " + tempMacro.Name, ee); throw; } } } }
protected void renderMacro_Click(object sender, EventArgs e) { int pageID = int.Parse(UmbracoContext.Current.Request["umbPageId"]); string macroAttributes = "macroAlias=\"" + m.Alias + "\""; Guid pageVersion = new Guid(UmbracoContext.Current.Request["umbVersionId"]); Hashtable attributes = new Hashtable(); attributes.Add("macroAlias", m.Alias); macro mRender = new macro(m.Id); foreach (Control c in _dataFields) { try { IMacroGuiRendering ic = (IMacroGuiRendering)c; attributes.Add(c.ID, ic.Value); macroAttributes += " " + c.ID + "=\"" + ic.Value.Replace("\"", "\\\"").Replace("\n", "\\n").Replace("\r", "\\r") + "\""; } catch { } } // document this, for gods sake! HttpContext.Current.Items["macrosAdded"] = 0; HttpContext.Current.Items["pageID"] = pageID.ToString(); page p = new page(pageID, pageVersion); string div = macro.renderMacroStartTag(attributes, pageID, pageVersion).Replace("\\", "\\\\").Replace("'", "\\'"); string macroContent = macro.MacroContentByHttp(pageID, pageVersion, attributes).Replace("\\", "\\\\").Replace("'", "\\'"). Replace("/", "\\/").Replace("\n", "\\n"); if (macroContent.Length > 0 && macroContent.ToLower().IndexOf("<script") > -1) macroContent = "<b>Macro rendering contains script code</b><br/>This macro won\\'t be rendered in the editor because it contains script code. It will render correct during runtime."; div += macroContent; div += macro.renderMacroEndTag(); _scriptOnLoad += "\t\tumbracoEditMacroDo('" + macroAttributes.Replace("'", "\\'") + "', '" + m.Name.Replace("'", "\\'") + "', '" + div + "');\n"; /* ClientScript.RegisterStartupScript(GetType(), "postbackScript", "<script>\n umbracoEditMacroDo('" + macroAttributes.Replace("'", "\\'") + "', '" + m.Name.Replace("'", "\\'") + "', '" + div + "');\n</script>"); ClientScript.RegisterStartupScript(GetType(), "postbackScriptWindowClose", "<script>\n //setTimeout('window.close()',300);\n</script>"); */ //theForm.Visible = false; }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { // collect all attributes set on the control var keys = Attributes.Keys; foreach (string key in keys) { MacroAttributes.Add(key.ToLower(), Attributes[key]); } if (!MacroAttributes.ContainsKey("macroalias") && !MacroAttributes.ContainsKey("macroAlias")) { MacroAttributes.Add("macroalias", Alias); } // set pageId to int.MinValue if no pageID was found, // e.g. if the macro was rendered on a custom (non-Umbraco) page int pageId = Context.Items["pageID"] == null ? int.MinValue : int.Parse(Context.Items["pageID"].ToString()); if ((!String.IsNullOrEmpty(Language) && Text != "") || !string.IsNullOrEmpty(FileLocation)) { var tempMacro = new macro(); tempMacro.GenerateMacroModelPropertiesFromAttributes(MacroAttributes); if (string.IsNullOrEmpty(FileLocation)) { tempMacro.Model.ScriptCode = Text; tempMacro.Model.ScriptLanguage = Language; } else { tempMacro.Model.ScriptName = FileLocation; } tempMacro.Model.MacroType = MacroTypes.Script; if (!String.IsNullOrEmpty(Attributes["Cache"])) { var cacheDuration = 0; if (int.TryParse(Attributes["Cache"], out cacheDuration)) { tempMacro.Model.CacheDuration = cacheDuration; } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Cache attribute is in incorect format (should be an integer)."); } } var c = tempMacro.renderMacro((Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Exceptions = tempMacro.Exceptions; Controls.Add(c); } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Result of inline macro scripting is null"); } } else { var tempMacro = macro.GetMacro(Alias); if (tempMacro != null) { try { var c = tempMacro.renderMacro(MacroAttributes, (Hashtable)Context.Items["pageElements"], pageId); if (c != null) { Controls.Add(c); } else { System.Web.HttpContext.Current.Trace.Warn("Template", "Result of macro " + tempMacro.Name + " is null"); } } catch (Exception ee) { System.Web.HttpContext.Current.Trace.Warn("Template", "Error adding macro " + tempMacro.Name, ee); } } } }
private string parseMacrosToHtml(string input) { int nodeId = _nodeId; Guid versionId = _versionId; string content = input; string pattern = @"(<\?UMBRACO_MACRO\W*[^>]*/>)"; MatchCollection tags = Regex.Matches(content, pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); // Page for macro rendering page p = new page(nodeId, versionId); HttpContext.Current.Items["macrosAdded"] = 0; HttpContext.Current.Items["pageID"] = nodeId.ToString(); foreach (Match tag in tags) { // Create div Hashtable attributes = helper.ReturnAttributes(tag.Groups[1].Value); string div = macro.renderMacroStartTag(attributes, nodeId, versionId).Replace(""", "&quot;"); // Insert macro contents here... macro m; if (helper.FindAttribute(attributes, "macroID") != "") m = new macro(int.Parse(helper.FindAttribute(attributes, "macroID"))); else m = new macro(Macro.GetByAlias(helper.FindAttribute(attributes, "macroAlias")).Id); if (helper.FindAttribute(attributes, "macroAlias") == "") attributes.Add("macroAlias", m.Alias); try { div += macro.MacroContentByHttp(nodeId, versionId, attributes); } catch { div += "<span style=\"color: green\">No macro content available for WYSIWYG editing</span>"; } div += macro.renderMacroEndTag(); content = content.Replace(tag.Groups[1].Value, div); } return content; }