Exemplo n.º 1
0
        private MarkdownDocument GetMarkdown(MultimediaItem Item, string ParentURL)
        {
            int    i = Item.Url.IndexOf('?');
            string Query;
            string FileName;

            if (i < 0)
            {
                Query    = null;
                FileName = Item.Url;
            }
            else
            {
                Query    = Item.Url.Substring(i + 1);
                FileName = Item.Url.Substring(0, i);
            }

            if (!string.IsNullOrEmpty(ParentURL))
            {
                if (Uri.TryCreate(new Uri(ParentURL), FileName, out Uri NewUri))
                {
                    ParentURL = NewUri.ToString();
                }
            }

            FileName = Path.Combine(Path.GetDirectoryName(Item.Document.FileName), FileName);

            if (!string.IsNullOrEmpty(Query))
            {
                Variables Variables = Item.Document.Settings.Variables;
                string    Value;

                if (Variables != null)
                {
                    foreach (string Part in Query.Split('&'))
                    {
                        i = Part.IndexOf('=');
                        if (i < 0)
                        {
                            Variables[Part] = string.Empty;
                        }
                        else
                        {
                            Value = Part.Substring(i + 1);

                            if (double.TryParse(Value.Replace(".", System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator), out double d))
                            {
                                Variables[Part.Substring(0, i)] = d;
                            }
                            else if (bool.TryParse(Value, out bool b))
                            {
                                Variables[Part.Substring(0, i)] = b;
                            }
                            else
                            {
                                Variables[Part.Substring(0, i)] = Value;
                            }
                        }
                    }
                }
            }

            string           MarkdownText = File.ReadAllText(FileName);
            MarkdownDocument Markdown     = new MarkdownDocument(MarkdownText, Item.Document.Settings, FileName, string.Empty, ParentURL)
            {
                Master = Item.Document
            };

            MarkdownDocument Loop = Item.Document;

            while (Loop != null)
            {
                if (Loop.FileName == FileName)
                {
                    throw new Exception("Circular reference detected.");
                }

                Loop = Loop.Master;
            }

            return(Markdown);
        }
        /**************************************************************************/

        public static string MakeUrlAbsolute(
            string BaseUrl,
            string Url
            )
        {
            string UrlFixed;
            Uri    BaseUri     = null;
            string BaseUriPort = "";
            Uri    NewUri      = null;

            Regex reHTTP              = new Regex("^https?:");
            Regex reDoubleSlash       = new Regex("^//");
            Regex reSlash             = new Regex("^/");
            Regex reQuery             = new Regex("^\\?");
            Regex reHash              = new Regex("^#");
            Regex reUnsupportedScheme = new Regex("^[^:]+:");

            BaseUrl = HtmlEntity.DeEntitize(BaseUrl);
            BaseUrl = Uri.UnescapeDataString(BaseUrl);

            Url = HtmlEntity.DeEntitize(Url);
            Url = Uri.UnescapeDataString(Url);

            try
            {
                BaseUri = new Uri(BaseUrl, UriKind.Absolute);

                if (BaseUri.Port > 0)
                {
                    BaseUriPort = string.Format(":{0}", BaseUri.Port);
                }
            }
            catch (UriFormatException ex)
            {
                DebugMsgStatic(string.Format("MakeUrlAbsolute: {0}", ex.Message));
            }
            catch (Exception ex)
            {
                DebugMsgStatic(string.Format("MakeUrlAbsolute: {0}", ex.Message));
            }

            if (BaseUri == null)
            {
                throw new MacroscopeUriFormatException("Malformed Base URI");
            }

            if (!Regex.IsMatch(Url, "^(https?:|/|#)"))
            {
                DebugMsgStatic(string.Format("STRANGE URL: 1: {0}", BaseUrl));
                DebugMsgStatic(string.Format("STRANGE URL: 2: {0}", Url));
            }

            if (!reHTTP.IsMatch(Url))
            {
                bool IsSuspect = false;
                if (
                    (!reDoubleSlash.IsMatch(Url)) &&
                    (!reSlash.IsMatch(Url)) &&
                    (!reQuery.IsMatch(Url)) &&
                    (!reHash.IsMatch(Url)))
                {
                    if (reUnsupportedScheme.IsMatch(Url))
                    {
                        IsSuspect = true;
                    }
                }
                if (IsSuspect)
                {
                    DebugMsgStatic(string.Format("STRANGE URL: IS SUSPECT: {0}", Url));
                    return(null);
                }
            }

            if (reDoubleSlash.IsMatch(Url))
            {
                try
                {
                    NewUri = new Uri(
                        string.Format(
                            "{0}:{1}",
                            BaseUri.Scheme,
                            Url
                            ),
                        UriKind.Absolute
                        );
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }
            else
            if (reSlash.IsMatch(Url))
            {
                try
                {
                    NewUri = new Uri(
                        string.Format(
                            "{0}://{1}{2}{3}",
                            BaseUri.Scheme,
                            BaseUri.Host,
                            BaseUriPort,
                            Url
                            ),
                        UriKind.Absolute
                        );
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }
            else
            if (reQuery.IsMatch(Url))
            {
                try
                {
                    NewUri = new Uri(
                        string.Format(
                            "{0}://{1}{2}{3}{4}",
                            BaseUri.Scheme,
                            BaseUri.Host,
                            BaseUriPort,
                            BaseUri.AbsolutePath,
                            Url
                            ),
                        UriKind.Absolute
                        );
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }
            else
            if (reHash.IsMatch(Url))
            {
                string NewUrl       = Url;
                Regex  reHashRemove = new Regex("#.*$", RegexOptions.Singleline);
                NewUrl = reHashRemove.Replace(NewUrl, "");

                try
                {
                    NewUri = new Uri(
                        string.Format(
                            "{0}://{1}{2}{3}",
                            BaseUri.Scheme,
                            BaseUri.Host,
                            BaseUriPort,
                            NewUrl
                            ),
                        UriKind.Absolute
                        );
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }
            else
            if (reHTTP.IsMatch(Url))
            {
                try
                {
                    NewUri = new Uri(Url, UriKind.Absolute);
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }
            else
            if (reUnsupportedScheme.IsMatch(Url))
            {
                ; // NO-OP, for now.
            }
            else
            {
                DebugMsgStatic(string.Format("RELATIVE URL 1: {0}", Url));

                string BasePath = Regex.Replace(BaseUri.AbsolutePath, "/[^/]+$", "/");
                string NewPath  = string.Join("", BasePath, Url);

                DebugMsgStatic(string.Format("RELATIVE URL 2: {0}", BasePath));
                DebugMsgStatic(string.Format("RELATIVE URL 3: {0}", NewPath));

                try
                {
                    NewUri = new Uri(
                        string.Format(
                            "{0}://{1}{2}{3}",
                            BaseUri.Scheme,
                            BaseUri.Host,
                            BaseUriPort,
                            NewPath
                            ),
                        UriKind.Absolute
                        );
                }
                catch (InvalidOperationException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
                catch (UriFormatException ex)
                {
                    DebugMsgStatic(ex.Message);
                }
            }

            if (NewUri != null)
            {
                UrlFixed = NewUri.ToString();
            }
            else
            {
                UrlFixed = Url;
            }

            UrlFixed = SanitizeUrl(UrlFixed);

            return(UrlFixed);
        }
Exemplo n.º 3
0
        private MarkdownDocument GetMarkdown(MultimediaItem Item, string ParentURL)
        {
            int    i = Item.Url.IndexOf('?');
            string Query;
            string FileName;

            if (i < 0)
            {
                Query    = null;
                FileName = Item.Url;
            }
            else
            {
                Query    = Item.Url.Substring(i + 1);
                FileName = Item.Url.Substring(0, i);
            }

            if (!string.IsNullOrEmpty(ParentURL))
            {
                if (Uri.TryCreate(new Uri(ParentURL), FileName, out Uri NewUri))
                {
                    ParentURL = NewUri.ToString();
                }
            }

            FileName = Item.Document.Settings.GetFileName(Item.Document.FileName, FileName);

            if (!string.IsNullOrEmpty(Query))
            {
                Variables Variables = Item.Document.Settings.Variables;
                string    Value;

                if (!(Variables is null))
                {
                    foreach (string Part in Query.Split('&'))
                    {
                        i = Part.IndexOf('=');
                        if (i < 0)
                        {
                            Variables[Part] = string.Empty;
                        }
                        else
                        {
                            Value = Part.Substring(i + 1);

                            if (CommonTypes.TryParse(Value, out double d))
                            {
                                Variables[Part.Substring(0, i)] = d;
                            }
                            else if (bool.TryParse(Value, out bool b))
                            {
                                Variables[Part.Substring(0, i)] = b;
                            }
                            else
                            {
                                Variables[Part.Substring(0, i)] = Value;
                            }
                        }
                    }
                }
            }

            string           MarkdownText = File.ReadAllText(FileName);
            MarkdownDocument Markdown     = new MarkdownDocument(MarkdownText, Item.Document.Settings, FileName, string.Empty, ParentURL)
            {
                Master = Item.Document
            };

            MarkdownDocument Loop = Item.Document;

            while (!(Loop is null))
            {
                if (Loop.FileName == FileName)
                {
                    throw new Exception("Circular reference detected.");
                }

                MarkdownDocument.CopyMetaDataTags(Loop, Markdown);

                Loop = Loop.Master;
            }

            return(Markdown);
        }