コード例 #1
0
        /// <inheritdoc />
        public override ShortcodeResult Execute(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context)
        {
            IMetadataDictionary arguments = args.ToDictionary(
                Path,
                IncludeHost,
                Host,
                Root,
                Scheme,
                UseHttps,
                HideIndexPages,
                HideExtensions,
                Lowercase);

            arguments.RequireKeys(Path);

            string path = arguments.GetString(Path);

            if (LinkGenerator.TryGetAbsoluteHttpUri(path, out string absoluteUri))
            {
                return(absoluteUri);
            }
            NormalizedPath filePath = new NormalizedPath(path);

            // Use "Host" if it's provided, otherwise use Host setting if "IncludeHost" is true
            string host = arguments.GetString(Host, arguments.GetBool(IncludeHost) ? context.Settings.GetString(Keys.Host) : null);

            // Use "Root" if it's provided, otherwise LinkRoot setting
            NormalizedPath root = arguments.GetPath(Root, context.Settings.GetPath(Keys.LinkRoot));

            // Use "Scheme" if it's provided, otherwise if "UseHttps" is true use "https" or use LinksUseHttps setting
            string scheme = arguments.GetString(Scheme, arguments.ContainsKey(UseHttps)
                ? (arguments.GetBool(UseHttps) ? "https" : null)
                : (context.Settings.GetBool(Keys.LinksUseHttps) ? "https" : null));

            // If "HideIndexPages" is provided and true use default hide pages, otherwise use default hide pages if LinkHideIndexPages is true
            string[] hidePages = arguments.ContainsKey(HideIndexPages)
                ? (arguments.GetBool(HideIndexPages) ? LinkGenerator.DefaultHidePages : null)
                : (context.Settings.GetBool(Keys.LinkHideIndexPages) ? LinkGenerator.DefaultHidePages : null);

            // If "HideExtensions" is provided and true use default hide extensions, otherwise use default hide extensions if LinkHideExtensions is true
            string[] hideExtensions = arguments.ContainsKey(HideExtensions)
                ? (arguments.GetBool(HideExtensions) ? LinkGenerator.DefaultHideExtensions : null)
                : (context.Settings.GetBool(Keys.LinkHideExtensions) ? LinkGenerator.DefaultHideExtensions : null);

            // If "Lowercase" is provided use that, otherwise use LinkLowercase setting
            bool lowercase = arguments.ContainsKey(Lowercase)
                ? arguments.GetBool(Lowercase)
                : context.Settings.GetBool(Keys.LinkLowercase);

            return(LinkGenerator.GetLink(filePath, host, root, scheme, hidePages, hideExtensions, lowercase));
        }
コード例 #2
0
        /// <inheritdoc />
        public override async Task <IDocument> ExecuteAsync(KeyValuePair <string, string>[] args, string content, IDocument document, IExecutionContext context)
        {
            IMetadataDictionary arguments = args.ToDictionary(
                "Path",
                "IncludeHost",
                "Host",
                "Root",
                "Scheme",
                "UseHttps",
                "HideIndexPages",
                "HideExtensions",
                "Lowercase");

            arguments.RequireKeys("Path");

            string path = arguments.GetString("Path");

            if (LinkGenerator.TryGetAbsoluteHttpUri(path, out string absoluteUri))
            {
                return(context.CreateDocument(await context.GetContentProviderAsync(absoluteUri)));
            }
            NormalizedPath filePath = new NormalizedPath(path);

            // Use "Host" if it's provided, otherwise use Host setting if "IncludeHost" is true
            string host = arguments.GetString("Host", arguments.GetBool("IncludeHost") ? context.Settings.GetString(Keys.Host) : null);

            // Use "Root" if it's provided, otherwise LinkRoot setting
            NormalizedPath root = arguments.GetPath("Root", context.Settings.GetPath(Keys.LinkRoot));

            // Use "Scheme" if it's provided, otherwise if "UseHttps" is true use "https" or use LinksUseHttps setting
            string scheme = arguments.GetString("Scheme", arguments.ContainsKey("UseHttps")
                ? (arguments.GetBool("UseHttps") ? "https" : null)
                : (context.Settings.GetBool(Keys.LinksUseHttps) ? "https" : null));

            // If "HideIndexPages" is provided and true use default hide pages, otherwise use default hide pages if LinkHideIndexPages is true
            string[] hidePages = arguments.ContainsKey("HideIndexPages")
                ? (arguments.GetBool("HideIndexPages") ? LinkGenerator.DefaultHidePages : null)
                : (context.Settings.GetBool(Keys.LinkHideIndexPages) ? LinkGenerator.DefaultHidePages : null);

            // If "HideExtensions" is provided and true use default hide extensions, otherwise use default hide extensions if LinkHideExtensions is true
            string[] hideExtensions = arguments.ContainsKey("HideExtensions")
                ? (arguments.GetBool("HideExtensions") ? LinkGenerator.DefaultHideExtensions : null)
                : (context.Settings.GetBool(Keys.LinkHideExtensions) ? LinkGenerator.DefaultHideExtensions : null);

            // If "Lowercase" is provided use that, otherwise use LinkLowercase setting
            bool lowercase = arguments.ContainsKey("Lowercase")
                ? arguments.GetBool("Lowercase")
                : context.Settings.GetBool(Keys.LinkLowercase);

            return(context.CreateDocument(await context.GetContentProviderAsync(LinkGenerator.GetLink(filePath, host, root, scheme, hidePages, hideExtensions, lowercase))));
        }