private static Matcher CreateDynamicServerMatcher(DynamicServer server)
    {
        return((url) => {
            Match match;


            match = server.Pattern.Match(url);

            if (match.Success)
            {
                Hash hash;


                // The URL matched the pattern. Render the templates to get the HTTP
                // and SSH URLs, making the match available for the templates to use.
                hash = TemplateData.Create().Add(match).ToHash();

                return new StaticServer(
                    server.Http.Render(hash),
                    server.Ssh.Render(hash)
                    );
            }

            return null;
        });
    }
 public void CanRenderTemplateWithRegexGroups()
 {
     Assert.Equal(
         "Hello there, Bob!",
         Render(
             "Hello {{ match.groups.greeting }}, {{ match.groups.name }}!",
             TemplateData.Create().Add(Regex.Match("there once was a man named Bob", @"^(?<greeting>\w+)\s.+\s(?<name>\w+)$")).ToHash()
             )
         );
 }
 public void CanRenderTemplateWithRegexMatches()
 {
     Assert.Equal(
         "Hello there, Bob!",
         Render(
             "Hello {{ match[1] }}, {{ match[2] }}!",
             TemplateData.Create().Add(Regex.Match("there once was a man named Bob", @"^(\w+)\s.+\s(\w+)$")).ToHash()
             )
         );
 }
Esempio n. 4
0
        public HttpResponseMessage GetNustachePage()
        {
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "article.html");

            using (StreamReader reader = new StreamReader(templatePath))
            {
                string strTemp = reader.ReadToEnd();

                var    data   = TemplateData.Create();
                string result = Render.StringToString(strTemp, data);

                var response = new HttpResponseMessage();
                response.Content = new StringContent(result);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
Esempio n. 5
0
        public HttpResponseMessage GetliquidPage()
        {
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "article3.html");

            using (StreamReader reader = new StreamReader(templatePath))
            {
                string strTemp = reader.ReadToEnd();

                var data = TemplateData.Create();

                DotLiquid.Template template = DotLiquid.Template.Parse(strTemp);               // Parses and compiles the template
                string             result   = template.Render(Hash.FromAnonymousObject(data)); // Renders the output

                var response = new HttpResponseMessage();
                response.Content = new StringContent(result);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
Esempio n. 6
0
        public HttpResponseMessage GetHandlebarsPage()
        {
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "article2.html");

            using (StreamReader reader = new StreamReader(templatePath))
            {
                string strTemp = reader.ReadToEnd();

                var data = TemplateData.Create();
                RegisterHelpers();
                var    template = Handlebars.Compile(strTemp);
                string result   = template(data);

                var response = new HttpResponseMessage();
                response.Content = new StringContent(result);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
Esempio n. 7
0
        public HttpResponseMessage GetMustacheSharpPage()
        {
            string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "article2.html");

            using (StreamReader reader = new StreamReader(templatePath))
            {
                string strTemp = reader.ReadToEnd();

                var data = TemplateData.Create();

                FormatCompiler compiler = new FormatCompiler();
                compiler.RegisterTag(new ListTagDefinition <Item>(), true);
                Generator generator = compiler.Compile(strTemp);
                string    result    = generator.Render(data);

                var response = new HttpResponseMessage();
                response.Content = new StringContent(result);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

                return(response);
            }
        }
    public async Task <string> CreateUrlAsync(Repository repository, FileInfo file, LinkOptions options)
    {
        if (repository.Remote is null)
        {
            throw new InvalidOperationException("The repository must have a remote.");
        }

        string       remote;
        StaticServer address;
        LinkType     type;
        string       url;
        TemplateData data;
        Hash         hash;


        // If a link type wasn't specified, then we'll use
        // the default type that's defined in the settings.
        type = options.Type ?? await _settings.GetDefaultLinkTypeAsync();

        // Adjust the remote URL so that it's in a
        // standard format that we can manipulate.
        remote = UrlHelpers.Normalize(repository.Remote.Url);

        address = await GetAddressAsync(remote);

        data = TemplateData
               .Create()
               .Add("base", address.Http)
               .Add("repository", GetRepositoryPath(remote, address))
               .Add("ref", await GetRefAsync(type, repository.Root, repository.Remote))
               .Add("commit", await GetRefAsync(LinkType.Commit, repository.Root, repository.Remote))
               .Add("file", GetRelativePath(repository.Root, file.FilePath))
               .Add("type", type == LinkType.Commit ? "commit" : "branch");

        if (file.Selection is not null)
        {
            data.Add("startLine", file.Selection.StartLine);
            data.Add("startColumn", file.Selection.StartColumn);
            data.Add("endLine", file.Selection.EndLine);
            data.Add("endColumn", file.Selection.EndColumn);
        }

        foreach (string key in _definition.SettingsKeys)
        {
            data.Add(key, await _settings.GetHandlerSettingAsync(key));
        }

        hash = data.ToHash();
        url  = _definition.Url.Render(hash);

        if (file.Selection is not null)
        {
            url += _definition.Selection.Render(hash);
        }

        url = ApplyModifications(
            url,
            _definition.Query.Where((x) => x.Pattern.IsMatch(file.FilePath)).ToList()
            );

        return(url);
    }