Exemple #1
0
        public PageSection(string fileName, PageVariables pageVariables)
        {
            this.FileName    = string.IsNullOrWhiteSpace(fileName) ? "" : fileName;
            this.DisplayName = StringUtils.GetItemDisplayName(fileName: this.FileName);
            this.UrlName     = StringUtils.GetItemUrlName(displayName: this.DisplayName);

            this.PageVariables = pageVariables;
            this.IsHidden      = IsPageSectionHidden(fileName, pageVariables);
        }
Exemple #2
0
        public FieldUpdateResult Update(PageVariables variables)
        {
            this.UpdateCore(variables);

            var w = this.Width;

            _content  = this.GetContent();
            this.Size = _textStyle.MeasureText(_content);

            return(w < this.Size.Width
                ? FieldUpdateResult.NoChange
                : FieldUpdateResult.Resized);
        }
Exemple #3
0
        public FieldUpdateResult Update(PageVariables variables)
        {
            foreach (var segment in _segments)
            {
                var result = segment.Update(variables);
                if (result == ReconstructionNecessary)
                {
                    return(result);
                }
            }

            return(NoChange);
        }
        public static LineSegment CreateLineSegment(
            this Stack <LineElement> fromElements,
            HorizontalSpace space,
            LineAlignment lineAlignment,
            double defaultLineHeight,
            PageVariables variables)
        {
            var overflow = lineAlignment == LineAlignment.Justify ? 2 : 0;
            var elements = fromElements
                           .GetElementsToFitMaxWidth(space.Width + overflow, variables)
                           .ToArray();

            return(new LineSegment(elements, lineAlignment, space, defaultLineHeight));
        }
Exemple #5
0
        /// <summary>
        /// Gets the variable collection for the current page.
        /// </summary>
        /// <param name="Session">Session</param>
        /// <param name="Resource">Resource part of the URL of the page.</param>
        /// <returns>Page variables.</returns>
        public static Variables GetPageVariables(Variables Session, string Resource)
        {
            if (!Session.TryGetVariable(Page.VariableName, out Variable v) ||
                !(v.ValueObject is Variables PageVariables))
            {
                Session[Page.LastPageVariableName] = Resource;
                Session[Page.VariableName]         = PageVariables = new Variables();
            }
            else if (!Session.TryGetVariable(Page.LastPageVariableName, out v) ||
                     !(v.ValueObject is string LastPageUrl) ||
                     LastPageUrl != Resource)
            {
                Session[Page.LastPageVariableName] = Resource;
                PageVariables.Clear();
            }

            return(PageVariables);
        }
        private static IEnumerable <LineElement> GetElementsToFitMaxWidth(
            this Stack <LineElement> fromElements,
            double maxWidth,
            PageVariables variables)
        {
            var aggregatedWidth = 0.0;
            var elements        = new List <LineElement>();
            var spaces          = new List <SpaceElement>();

            while (fromElements.Count > 0 && aggregatedWidth <= maxWidth)
            {
                var element = fromElements.Pop();
                if (element is Field field)
                {
                    field.Update(variables);
                }

                if (element is SpaceElement space)
                {
                    spaces.Add(space);
                    continue;
                }

                aggregatedWidth += spaces.TotalWidth() + element.Size.Width;
                if (aggregatedWidth < maxWidth)
                {
                    elements.AddRange(spaces);
                    elements.Add(element);
                    spaces.Clear();
                }
                else
                {
                    fromElements.Push(element);
                }

                if (element is NewLineElement)
                {
                    break;
                }
            }

            return(elements.Union(spaces));
        }
Exemple #7
0
        private static (LineSegment[], double) CreateLineSegments(
            this Stack <LineElement> fromElements,
            LineAlignment lineAlignment,
            double relativeYOffset,
            IEnumerable <Rectangle> fixedDrawings,
            double availableWidth,
            double defaultLineHeight,
            PageVariables variables)
        {
            var reserveSpaceHelper = new LineReservedSpaceHelper(fixedDrawings, relativeYOffset, availableWidth);

            var expectedLineHeight = 0.0;
            var finished           = false;

            do
            {
                var segmentSpaces = reserveSpaceHelper
                                    .GetLineSegments()
                                    .ToArray();

                var lineSegments = segmentSpaces
                                   .Select((space, i) => fromElements.CreateLineSegment(space, lineAlignment, defaultLineHeight, variables))
                                   .ToArray();

                var maxHeight = lineSegments.Max(l => l.Size.Height);
                expectedLineHeight = Math.Max(maxHeight, expectedLineHeight);
                var hasChanged = reserveSpaceHelper.UpdateLineHeight(expectedLineHeight);

                if (!hasChanged)
                {
                    return(lineSegments, expectedLineHeight);
                }
                else
                {
                    foreach (var ls in lineSegments.Reverse())
                    {
                        ls.ReturnElementsToStack(fromElements);
                    }
                }
            } while (!finished);

            return(new LineSegment[0], expectedLineHeight);
        }
Exemple #8
0
        public FieldUpdateResult Update(PageVariables pageVariables)
        {
            var justifyIsNecessary = false;

            foreach (var e in _elements.OfType <Field>())
            {
                var resized = e.Update(pageVariables) == Resized;
                justifyIsNecessary = justifyIsNecessary || resized;
            }

            if (!justifyIsNecessary)
            {
                return(NoChange);
            }

            var result = _space.Width < this.CalculateTotalWidthOfElements()
                ? ReconstructionNecessary
                : NoChange;

            return(result);
        }
Exemple #9
0
        public static Line CreateLine(
            this Stack <LineElement> fromElements,
            LineAlignment lineAlignment,
            double relativeYOffset,
            IEnumerable <FixedDrawing> fixedDrawings,
            double availableWidth,
            double defaultLineHeight,
            PageVariables variables,
            LineSpacing lineSpacing)
        {
            var(lineSegments, lineHeight) = fromElements
                                            .CreateLineSegments(lineAlignment, relativeYOffset, fixedDrawings.Select(d => d.BoundingBox), availableWidth, defaultLineHeight, variables);

            var baseLineOffset = lineSegments.Max(ls => ls.GetBaseLineOffset());

            foreach (var ls in lineSegments)
            {
                ls.JustifyElements(baseLineOffset, lineHeight);
            }

            return(new Line(lineSegments, lineSpacing));
        }
Exemple #10
0
 public static bool IsPageSectionHidden(string fileName, PageVariables variables)
 => fileName.StartsWith(".") || variables.HiddenFilePatterns.Any(p => p.IsMatch(fileName));
 public HtmlSnippetSection(string fileName, PageVariables pageVariables, HtmlSnippet snippet)
     : base(fileName, pageVariables)
 {
     this.Snippet = snippet ?? throw new ArgumentNullException(nameof(snippet));
 }
 public ExternalHtmlSection(string fileName, PageVariables pageVariables, HtmlPage page) : base(fileName, pageVariables)
 {
     this.Page = page ?? throw new ArgumentNullException(nameof(page));
 }
 public MultiFormatSection(IEnumerable <PageSection> formats, PageVariables pageVariables)
     : base(
         fileName: "",                 //Don't want to display the name twice //TODO: Investigate & clarify/change
         pageVariables: pageVariables,
         labelledSections: formats.Select(x => (section: x, label: x.Format))
Exemple #14
0
 protected abstract void UpdateCore(PageVariables variables);
Exemple #15
0
 public PdfSection(string fileName, PageVariables pageVariables, Pdf pdf) : base(fileName, pageVariables)
 {
     this.Pdf = pdf ?? throw new ArgumentNullException(nameof(pdf));
 }
Exemple #16
0
        /// <summary>
        /// Performs the actual conversion.
        /// </summary>
        /// <param name="FromContentType">Content type of the content to convert from.</param>
        /// <param name="From">Stream pointing to binary representation of content.</param>
        /// <param name="FromFileName">If the content is coming from a file, this parameter contains the name of that file.
        /// Otherwise, the parameter is the empty string.</param>
        /// <param name="ResourceName">Local resource name of file, if accessed from a web server.</param>
        /// <param name="URL">URL of resource, if accessed from a web server.</param>
        /// <param name="ToContentType">Content type of the content to convert to.</param>
        /// <param name="To">Stream pointing to where binary representation of content is to be sent.</param>
        /// <param name="Session">Session states.</param>
        /// <returns>If the result is dynamic (true), or only depends on the source (false).</returns>
        public bool Convert(string FromContentType, Stream From, string FromFileName, string ResourceName, string URL, string ToContentType,
                            Stream To, Variables Session)
        {
            HttpRequest Request = null;
            string      Markdown;
            bool        b;

            using (StreamReader rd = new StreamReader(From))
            {
                Markdown = rd.ReadToEnd();
            }

            if (!(Session is null) && Session.TryGetVariable("Request", out Variable v))
            {
                Request = v.ValueObject as HttpRequest;

                if (!(Request is null))
                {
                    string Url = Request.Header.ResourcePart;

                    if (!Session.TryGetVariable(" PageVariables ", out v) ||
                        !(v.ValueObject is Variables PageVariables))
                    {
                        Session[" LastPage "]      = Url;
                        Session[" PageVariables "] = new Variables();
                    }
                    else if (!Session.TryGetVariable(" LastPage ", out v) ||
                             !(v.ValueObject is string LastPageUrl) ||
                             LastPageUrl != Url)
                    {
                        Session[" LastPage "] = Url;
                        PageVariables.Clear();
                    }

                    int i = Markdown.IndexOf("\r\n\r\n");
                    if (i < 0)
                    {
                        i = Markdown.IndexOf("\n\n");
                    }

                    if (i > 0)
                    {
                        string Header = Markdown.Substring(0, i);
                        string Parameter;

                        foreach (string Row in Header.Split(CommonTypes.CRLF, StringSplitOptions.RemoveEmptyEntries))
                        {
                            if (!Row.StartsWith("Parameter:", StringComparison.OrdinalIgnoreCase))
                            {
                                continue;
                            }

                            Parameter = Row.Substring(10).Trim();
                            if (Request.Header.TryGetQueryParameter(Parameter, out string Value))
                            {
                                Value = System.Net.WebUtility.UrlDecode(Value);
                                if (double.TryParse(Value.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out double d))
                                {
                                    Session[Parameter] = d;
                                }
                                else if (bool.TryParse(Value, out b))
                                {
                                    Session[Parameter] = b;
                                }
                                else
                                {
                                    Session[Parameter] = Value;
                                }
                            }
                            else
                            {
                                Session[Parameter] = string.Empty;
                            }
                        }
                    }
                }
            }

            MarkdownSettings Settings = new MarkdownSettings(emojiSource, true, Session)
            {
                RootFolder   = rootFolder,
                HtmlSettings = htmlSettings
            };

            if (!string.IsNullOrEmpty(bareJid))
            {
                Settings.HttpxProxy             = "/HttpxProxy/%URL%";
                Settings.LocalHttpxResourcePath = "httpx://" + bareJid + "/";
            }

            MarkdownDocument Doc = new MarkdownDocument(Markdown, Settings, FromFileName, ResourceName, URL, typeof(HttpException));

            if (Doc.TryGetMetaData("UserVariable", out KeyValuePair <string, bool>[] MetaValues))
            {
                object User       = null;
                bool   Authorized = true;

                if (!Doc.TryGetMetaData("Login", out KeyValuePair <string, bool>[] Login))
                {
                    Login = null;
                }

                if (!Doc.TryGetMetaData("Privilege", out KeyValuePair <string, bool>[] Privilege))
                {
                    Privilege = null;
                }

                foreach (KeyValuePair <string, bool> P in MetaValues)
                {
                    if (Session is null)
                    {
                        Authorized = false;
                        break;
                    }

                    if (Session.TryGetVariable(P.Key, out v))
                    {
                        User = v.ValueObject;
                    }
                    else
                    {
                        Uri    LoginUrl      = null;
                        string LoginFileName = null;
                        string FromFolder    = Path.GetDirectoryName(FromFileName);

                        if (!(Login is null))
                        {
                            foreach (KeyValuePair <string, bool> P2 in Login)
                            {
                                LoginFileName = Path.Combine(FromFolder, P2.Key.Replace('/', Path.DirectorySeparatorChar));
                                LoginUrl      = new Uri(new Uri(URL), P2.Key.Replace(Path.DirectorySeparatorChar, '/'));

                                if (File.Exists(LoginFileName))
                                {
                                    break;
                                }
                                else
                                {
                                    LoginFileName = null;
                                }
                            }
                        }

                        if (!(LoginFileName is null))
                        {
                            string           LoginMarkdown = File.ReadAllText(LoginFileName);
                            MarkdownDocument LoginDoc      = new MarkdownDocument(LoginMarkdown, Settings, LoginFileName, LoginUrl.AbsolutePath,
                                                                                  LoginUrl.ToString(), typeof(HttpException));

                            if (!Session.TryGetVariable(P.Key, out v))
                            {
                                Authorized = false;
                                break;
                            }
                        }
                        else
                        {
                            Authorized = false;
                            break;
                        }
                    }
Exemple #17
0
 public SiteMapSection(string fileName, PageVariables pageVariables) : base(fileName, pageVariables)
 {
 }
Exemple #18
0
 public RawTextSection(string fileName, PageVariables pageVariables, string text) : base(fileName, pageVariables)
 {
     this.Text = text;
 }
Exemple #19
0
 protected override void UpdateCore(PageVariables variables)
 {
 }
Exemple #20
0
        public static IEnumerable <PageSection> GetPageSections(IReadOnlyList <MultiFormatFile> pageContent, PageVariables pageVariables)
        {
            for (int i = 0; i < pageContent.Count; i++)
            {
                var    multiFile = pageContent[i];
                string fileName  = multiFile.ExtensionlessFileName;

                var formats = new List <PageSection>();

                foreach (var f in multiFile.Files)
                {
                    if (FileTypes.IsRawTextDocument(f.Extension))
                    {
                        formats.Add(new RawTextSection(fileName, pageVariables, f.FileInfo.OpenText().ReadToEnd()));
                        continue;
                    }

                    if (FileTypes.IsImage(f.Extension))
                    {
                        formats.Add(new ImageSection(fileName, pageVariables, new[] { new Image(f) }));
                        continue;
                    }

                    if (FileTypes.IsPdf(f.Extension))
                    {
                        formats.Add(new PdfSection(fileName, pageVariables, new Pdf(f)));
                        continue;
                    }

                    if (FileTypes.IsExternalPage(f.Extension))
                    {
                        formats.Add(new ExternalHtmlSection(fileName, pageVariables, new HtmlPage(f)));
                        continue;
                    }

                    if (FileTypes.IsHtmlSnippet(f.Extension))
                    {
                        formats.Add(new HtmlSnippetSection(fileName, pageVariables, new HtmlSnippet(f.FileInfo.OpenText().ReadToEnd())));
                        continue;
                    }

                    formats.Add(
                        new RawTextSection(
                            fileName,
                            pageVariables,
                            "ERROR: PAGE CONTENT ITEM TYPE \"" + f.Extension + "\" IS NOT RECOGNISED\r\n"
                            + "FILE LOCATION IN WEBSITE GENERATOR INPUT: \"" + f.Path + "\"\r\n"
                            + "FILE CONTENTS:\r\n"
                            + f.FileInfo.OpenText().ReadToEnd()
                            )
                        );
                }

                if (formats.Count == 1)
                {
                    yield return(formats[0]);
                }
                else
                {
                    yield return(new MultiFormatSection(formats, pageVariables));
                }
            }
        }
Exemple #21
0
 public ImageSection(string fileName, PageVariables pageVariables, IList <Image> images) : base(fileName, pageVariables)
 {
     this.Images = new ReadOnlyCollection <Image>(images ?? throw new ArgumentNullException(nameof(images)));
 }