Esempio n. 1
0
        private async Task ExtractPageSectionAsync(List <string> lines, bool build)
        {
            for (; lines.Count > 0;)
            {
                // get the menu path (site url)
                string urlLine = lines.First();
                if (urlLine.StartsWith("::"))
                {
                    break; // start of a new section
                }
                ++_LineCounter; lines.RemoveAt(0);
                if (urlLine.Trim() == "")
                {
                    continue;
                }
                string[] s = urlLine.Split(new char[] { ' ' }, 2);
                if (s.Length != 2)
                {
                    throw TemplateError("Url {0} invalid", urlLine);
                }

                string user = s[0].Trim();
                string url  = s[1].Trim();

                // validate url
                if (url.Length == 0 || url[0] != '/')
                {
                    throw TemplateError("Url must start with / and can't be indented");
                }

                PageDefinition page = null;
                if (build)
                {
                    page = await PageDefinition.CreatePageDefinitionAsync(url);
                }
                else
                {
                    page = await PageDefinition.LoadFromUrlAsync(url);

                    if (page != null)
                    {
                        await PageDefinition.RemovePageDefinitionAsync(page.PageGuid);
                    }
                    page = new PageDefinition();
                }
                _CurrentPage = page;
                _CurrentUrl  = url;

                while (TryPageTitle(page, lines) || TryPageDescription(page, lines))
                {
                    ;
                }

                // Get the Menu Text
                if (lines.Count < 1)
                {
                    throw TemplateError("Menu missing");
                }
                string menu = lines.First(); ++_LineCounter; lines.RemoveAt(0);
                if (string.IsNullOrWhiteSpace(menu))
                {
                    throw TemplateError("Menu missing");
                }
                if (!char.IsWhiteSpace(menu[0]))
                {
                    throw TemplateError("Menu line must be indented");
                }
                menu = menu.Trim();

                // add a menu entry to the site menu
                if (menu != "-")
                {
                    if (build)
                    {
                        ModuleAction action = await AddMenuAsync(menu);

                        //if (string.IsNullOrWhiteSpace(action.Url)) // I don't remember why this was here
                        action.Url = url;
                    }
                    else
                    {
                        await RemoveMenuAsync(menu);
                    }
                }

                // Get the skin
                if (lines.Count < 1)
                {
                    throw TemplateError("Skin missing");
                }
                string skin = lines.First(); ++_LineCounter; lines.RemoveAt(0);
                if (string.IsNullOrWhiteSpace(skin))
                {
                    throw TemplateError("Skin missing");
                }
                if (!char.IsWhiteSpace(skin[0]))
                {
                    throw TemplateError("Skin line must be indented");
                }
                skin = skin.Trim();
                if (skin == "-")
                {
                    skin = ",,,";
                }
                string[] skinparts = skin.Split(new char[] { ',' });
                if (skinparts.Length != 4)
                {
                    throw TemplateError("Invalid skin format");
                }

                page.SelectedSkin.Collection = skinparts[0].Trim();
                if (page.SelectedSkin.Collection == "-")
                {
                    page.SelectedSkin.Collection = null;
                }
                page.SelectedSkin.FileName        = skinparts[1].Trim();
                page.SelectedPopupSkin.Collection = skinparts[2].Trim();
                if (page.SelectedPopupSkin.Collection == "-")
                {
                    page.SelectedPopupSkin.Collection = null;
                }
                page.SelectedPopupSkin.FileName = skinparts[3].Trim();

                SerializableList <PageDefinition.AllowedRole> roles = new SerializableList <PageDefinition.AllowedRole>();
                switch (user)
                {
                case "Administrator":
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAdministratorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes, Remove = PageDefinition.AllowedEnum.Yes,
                    });
                    break;

                case "User":
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetUserRoleId(), View = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetEditorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAdministratorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes, Remove = PageDefinition.AllowedEnum.Yes,
                    });
                    break;

                case "Anonymous":
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAnonymousRoleId(), View = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetEditorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAdministratorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes, Remove = PageDefinition.AllowedEnum.Yes,
                    });
                    break;

                case "Anonymous+User":
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAnonymousRoleId(), View = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetUserRoleId(), View = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetEditorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes,
                    });
                    roles.Add(new PageDefinition.AllowedRole {
                        RoleId = Resource.ResourceAccess.GetAdministratorRoleId(), View = PageDefinition.AllowedEnum.Yes, Edit = PageDefinition.AllowedEnum.Yes, Remove = PageDefinition.AllowedEnum.Yes,
                    });
                    break;

                default:
                    throw TemplateError("User {0} invalid", user);
                }
                page.AllowedRoles = roles;

                // Get zero, one or more modules (add to Main section)
                for (; lines.Count > 0;)
                {
                    string modsLine = lines.First();
                    if (string.IsNullOrWhiteSpace(modsLine))
                    {
                        ++_LineCounter; lines.RemoveAt(0);
                        continue;
                    }
                    if (!char.IsWhiteSpace(modsLine[0]))
                    {
                        break;// not a module - module lines must be indented
                    }
                    // Load assembly
                    ++_LineCounter; lines.RemoveAt(0);// accept as module line
                    string[] parts  = modsLine.Split(new char[] { ':' });
                    string   pane   = null;
                    string   modDef = null;
                    if (parts.Length == 1)
                    {
                        pane   = Globals.MainPane;
                        modDef = parts[0].Trim();
                    }
                    else if (parts.Length == 2)
                    {
                        pane = parts[0].Trim();
                        if (pane == "-")
                        {
                            pane = Globals.MainPane;
                        }
                        modDef = parts[1].Trim();
                    }
                    else
                    {
                        throw TemplateError("Invalid \"pane: module definition\"");
                    }
                    ModuleDefinition mod = await EvaluateModuleAsmTypeExpression(modDef);

                    page.AddModule(pane, mod);

                    for (; lines.Count > 0;)
                    {
                        // add variable customizations (if any)
                        string varLine = lines.First();
                        if (string.IsNullOrWhiteSpace(varLine))
                        {
                            ++_LineCounter; lines.RemoveAt(0);
                            continue;
                        }
                        // if this line is more indented than the module line, it must be a variable line
                        if (varLine.Length - varLine.TrimStart(new char[] { ' ' }).Length <= modsLine.Length - modsLine.TrimStart(new char[] { ' ' }).Length)
                        {
                            break;
                        }
                        // process variables
                        ++_LineCounter; lines.RemoveAt(0);// accept as variable line
                        if (varLine.Trim() == "")
                        {
                            continue;
                        }
                        string[] sv = varLine.Split(new char[] { '=' }, 2);
                        if (varLine.Trim().EndsWith(")"))  // method call
                        {
                            varLine = varLine.Trim();
                            if (build)
                            {
                                await InvokeMethodAsync(varLine, varLine, mod);
                            }
                        }
                        else if (sv.Length >= 2)     // variable assignment
                        {
                            if (build)
                            {
                                AssignVariable(mod, sv[0].Trim(), sv[1].Trim());
                            }
                        }
                        else
                        {
                            throw TemplateError("Variable assignment invalid");
                        }
                    }
                }
                if (build)
                {
                    await page.SaveAsync();
                }
                _CurrentPage = null;
                _CurrentUrl  = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Builds the current site from the template file
        /// </summary>
        /// <returns></returns>
        private async Task BuildSiteFromTextAsync(List <string> lines, bool build)
        {
            // Build site from a template
            if (lines.Count() > 0)
            {
                lines = RemoveComments(lines);
                // process all lines
                for (; lines.Count > 0;)
                {
                    string section = lines.First().Trim(); ++_LineCounter; lines.RemoveAt(0);
                    if (section == "")
                    {
                        continue;
                    }
                    else if (section == "::PAGE-SECTION::")
                    {
                        await ExtractPageSectionAsync(lines, build);
                    }
                    else if (section == "::UNIQUE-MODULE-SECTION::")
                    {
                        await ExtractUniqueModuleSectionAsync(lines, build);
                    }
                    else if (section == "::NON-UNIQUE-MODULE-SECTION::")
                    {
                        await ExtractUniqueModuleSectionAsync(lines, build, Unique : false);
                    }
                    else if (section == "::LINK-SECTION::")
                    {
                        await ExtractLinkSectionAsync(lines, build);
                    }
                    else if (section == "::WEBCONFIG-SECTION::")
                    {
                        ExtractWebconfigSection(lines, build);
                    }
                    else if (section == "::COMMAND-SECTION::")
                    {
                        await ExtractCommandSectionAsync(lines, build);
                    }
                    else if (section == "::SQL-SECTION::")
                    {
                        ExtractSQLSection(lines, build);
                    }
                    else if (section.StartsWith("::INC "))
                    {
                        await ExtractINCSectionAsync(lines, build, section);
                    }
                    else
                    {
                        throw TemplateError("Invalid section statement");
                    }
                }
            }
            else
            {
                if (build)
                {
                    // Just build a site with a home page
                    // Build a site with a few pages
                    //PageDefinition page = new PageDefinition() { Title = "Home Page", Url = "/", PageGuid = new Guid(Globals.HomePageGuid) };
                    PageDefinition page = await PageDefinition.CreatePageDefinitionAsync("/");

                    await page.SaveAsync();

#if DEBUG
                    page = await PageDefinition.CreatePageDefinitionAsync("/page1");

                    await page.SaveAsync();

                    page = await PageDefinition.CreatePageDefinitionAsync("/page2");

                    await page.SaveAsync();

                    page = await PageDefinition.CreatePageDefinitionAsync("/page3");

                    await page.SaveAsync();
#endif
                }
            }
        }