Exemplo n.º 1
0
        /// <summary>
        /// Transpiles the given value into html
        /// </summary>
        /// <param name="value">The parsed jlml document</param>
        /// <returns>An html string that represents the given document</returns>
        public string ToScript(JLMLDocument value)
        {
            PageContext    context = new();
            ElementVisitor visitor = new(context);

            return(value.Accept(visitor));
        }
Exemplo n.º 2
0
        static async Task DoTask(Lang lang, string filePath, string outputDir)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("File does not exist");
            }
            if (!Directory.Exists(outputDir))
            {
                Console.WriteLine("Output directory does not exist. It will be created");
            }

            string fileName = Path.GetFileNameWithoutExtension(filePath);

            CancellationTokenSource source = new();
            CancellationToken       token  = source.Token;

            string fileText = await File.ReadAllTextAsync(filePath, Encoding.UTF8, token);

            JLMLDocument script = JLMLDocumentLoader.Load(fileText);

            IScriptable transpiler = lang switch
            {
                Lang.Html => new HtmlTranspiler(),
                _ => throw new NotImplementedException(),
            };
            string contents = transpiler.ToScript(script);

            Directory.CreateDirectory(outputDir);
            await File.WriteAllTextAsync(Path.Join(outputDir, fileName + ".html"), contents, Encoding.UTF8, token);
        }
Exemplo n.º 3
0
        public async Task TestDocumentAsync()
        {
            string fileText = await File.ReadAllTextAsync("test.jlml", Encoding.UTF8);

            JLMLDocument script = JLMLDocumentLoader.Load(fileText);

            Assert.NotNull(script);
        }
Exemplo n.º 4
0
        public string Visit(JLMLDocument value)
        {
            BaseScript = value;

            pageContext.AddRange(value.Attributes.Cast <HeaderValue>().ToDictionary(o => o.Attribute, e => e.Value));

            // Get values that could only be attributes in html
            IEnumerable <string> objList = ResolveChildElementsOptions(value.Children).Select(o => o.Accept(this));

            return(HtmlGenerator.CreateHtmlBase(pageContext, objList));
        }
Exemplo n.º 5
0
        public async Task TestDocumentToHtmlAsync()
        {
            string fileText = await File.ReadAllTextAsync("test.jlml", Encoding.UTF8);

            JLMLDocument script = JLMLDocumentLoader.Load(fileText);

            var transpiler = new Transpiler.HtmlTranspiler();
            var html       = transpiler.ToScript(script);

            Assert.Equal(
                @"<html><head></head><body><div class=""container""><nav class=""navbar"" role=""navigation"" aria-label=""main navigation""><div class=""navbar-brand""><a class=""navbar-item"" href=""https://bulma.io""><img src=""https://bulma.io/images/bulma-logo.png"" width=""112"" height=""28"" /></a><a role=""button"" class=""navbar-burger"" aria-label=""menu"" aria-expanded=""false"" data-target=""navbarBasicExample""><span aria-hidden=""true"" /><span aria-hidden=""true"" /><span aria-hidden=""true"" /></a></div><div id=""navbarBasicExample"" class=""navbar-menu""><div class=""navbar-start""><a class=""navbar-item"">Home</a><a class=""navbar-item"">Documentation</a></div><div class=""navbar-end""><div class=""navbar-item""><a class=""button is-primary""><strong >Sign up</strong></a><a class=""button is-light"">Log in</a></div></div></div></nav><nav class=""navbar"" role=""navigation"" aria-label=""main navigation""><div class=""navbar-brand""><a class=""navbar-item"" href=""https://bulma.io""><img src=""https://bulma.io/images/bulma-logo.png"" width=""112"" height=""28"" /></a><a role=""button"" class=""navbar-burger"" aria-label=""menu"" aria-expanded=""false"" data-target=""navbarBasicExample""><span aria-hidden=""true"" /><span aria-hidden=""true"" /><span aria-hidden=""true"" /></a></div><div id=""navbarBasicExample"" class=""navbar-menu""><div class=""navbar-start""><a class=""navbar-item"">Home</a><a class=""navbar-item"">Documentation</a></div><div class=""navbar-end""><div class=""navbar-item""><a class=""button is-primary""><strong >Sign up</strong></a><a class=""button is-light"">Log in</a></div></div></div></nav><figure ><img src=""https://mr9madness.github.io/JLML/samples/img/img_food.jpg"" /><figcaption >Shows a car when on a screen thats under 1000 pixels, a girl when its under 600, and otherwise food</figcaption></figure><form ><input class=""form-control"" type=""text"" name=""testInput"" placeholder=""Placeholder Value"">Default Value<label class=""col-form"">Default Value</label><figcaption class=""form-text text-muted"">Help text</figcaption></input><select ><option value=""top action"" /><option value=""show secret option 2"" /><option value=""show secret option 2"" /><option value=""show secret option 2"" /><option value=""show secret option 2"" /></select><input class=""form-row horizontal"" name=""testInput"">imported input</input><button type=""submit"" class=""btn btn-primary"">Submit</button></form></div></body></html>"
                , html
                );
        }
Exemplo n.º 6
0
        public void TestString()
        {
            string jlmlHeaders = @"set page.title ""Quick example"";";
            string jlmlContent = @"form: {
			input: {type: ""text""; placeholder: ""Test value""}
			select: {values: (""First"", ""Second"");option: for value in values { value: value;}}
			}"            ;

            JLMLDocument script = JLMLDocumentLoader.Load(jlmlHeaders + jlmlContent);

            var transpiler = new Transpiler.HtmlTranspiler();
            var html       = transpiler.ToScript(script);

            Assert.Equal(
                @"<html><head></head><body><form ><input type=""text"" placeholder=""Test value"" /><select ><option value=""Second"" /><option value=""Second"" /></select></form></body></html>",
                html
                );
        }
Exemplo n.º 7
0
        public void TestString()
        {
            string jlmlHeaders = @"set page.title ""Quick example"";";
            string jlmlContent = @"form: {
			input: {type: ""text""; placeholder: ""Test value""}
			select: {values: (""First"", ""Second"");option: for value in values { value: value;}}
			}"            ;

            JLMLDocument script = JLMLDocumentLoader.Load(jlmlHeaders + jlmlContent);

            Assert.NotNull(script);

            var baseElement = script.Children.FirstOrDefault();

            Assert.NotNull(baseElement);
            Assert.Equal(0, baseElement.Attributes.Count);
            Assert.Equal(2, baseElement.Children.Count);
        }
Exemplo n.º 8
0
        public IElement?Visit(ImportOptions options)
        {
            string importText = "";
            var    path       = pageContext[options.IsFromSpecified
                                ? options.FromDestination : options.ElementName
                                ].ToString();

            if (string.IsNullOrWhiteSpace(path))
            {
                throw new System.ArgumentNullException(
                          nameof(options),
                          $"Cannot find value ({(options.IsFromSpecified ? options.FromDestination : options.ElementName)}) in page context"
                          );
            }

            bool takeCertainElement = false;

            if (!path.EndsWith(".jlml"))
            {
                if (!options.IsFromSpecified)
                {
                    path += ".jlml";
                }
                else if (options.ElementName.EndsWith(".jlml"))
                {
                    path += options.ElementName;
                }
                else
                {
                    path = $"{path}{options.ElementName}.jlml";
                }
            }
            else
            {
                takeCertainElement = true;
            }
            if (File.Exists(path))
            {
                importText = File.ReadAllText(path);
            }
            else
            {
                HttpClient client = new();
                try
                {
                    var resource = client.GetStringAsync(path).GetAwaiter().GetResult();

                    if (!string.IsNullOrWhiteSpace(resource))
                    {
                        importText = resource;
                    }
                }
                catch (Exception) { }
            }

            try {
                JLMLDocument document = JLMLDocumentLoader.Load(importText);
                if (!takeCertainElement)
                {
                    return(document.FirstOrDefault());
                }
                return(document.FirstOrDefault(options.ElementName));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(null);
        }