# This file is part of NIT ( http://www.nitlanguage.org ). # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # HTML output facilities module html # A html page # # You can define subclass and override methods head and body # # class MyPage # super HTMLPage # redef body do add("p").text("Hello World!") # end # # HTMLPage use fluent interface so you can chain calls as: # add("div").attr("id", "mydiv").text("My Div") class HTMLPage super Streamable # Define head content fun head do end # Define body content fun body do end private var root = new HTMLTag("html") private var current: HTMLTag = root private var stack = new List[HTMLTag] redef fun write_to(stream) do root.children.clear open("head") head close("head") open("body") body close("body") stream.write "" root.write_to(stream) end # Add a html tag to the current element # add("div").attr("id", "mydiv").text("My Div") fun add(tag: String): HTMLTag do var node = new HTMLTag(tag) current.add(node) return node end # Add a raw html string # add_html("top") fun add_html(html: String) do current.add(new HTMLRaw(html)) # Open a html tag # open("ul") # add("li").text("item1") # add("li").text("item2") # close("ul") fun open(tag: String): HTMLTag do stack.push(current) current = add(tag) return current end # Close previously opened tag # Ensure: tag = previous.tag fun close(tag: String) do if not tag == current.tag then print "Error: Trying to close '{tag}', last opened tag was '{current.tag}'." abort end current = stack.pop end end class HTMLTag super Streamable # HTML tagname: 'div' for
var tag: String init(tag: String) do self.tag = tag self.is_void = (once ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]).has(tag) end # Is the HTML element a void element? # # assert new HTMLTag("img").is_void == true # assert new HTMLTag("p").is_void == false var is_void: Bool init with_attrs(tag: String, attrs: Map[String, String]) do self.tag = tag self.attrs = attrs end # Tag attributes map var attrs: Map[String, String] = new HashMap[String, String] # Get the attributed value of 'prop' or null if 'prop' is undifened # var img = new HTMLTag("img") # img.attr("src", "./image.png").attr("alt", "image") # assert img.get_attr("src") == "./image.png" fun get_attr(key: String): nullable String do if not attrs.has_key(key) then return null return attrs[key] end # Set a 'value' for 'key' # var img = new HTMLTag("img") # img.attr("src", "./image.png").attr("alt", "image") # assert img.write_to_string == """""" fun attr(key: String, value: String): HTMLTag do attrs[key] = value return self end # Add a CSS class to the HTML tag # var img = new HTMLTag("img") # img.add_class("logo").add_class("fullpage") # assert img.write_to_string == """""" fun add_class(klass: String): HTMLTag do classes.add(klass) return self end # CSS classes var classes: Set[String] = new HashSet[String] # Add multiple CSS classes # var img = new HTMLTag("img") # img.add_classes(["logo", "fullpage"]) # assert img.write_to_string == """""" fun add_classes(classes: Collection[String]): HTMLTag do self.classes.add_all(classes) return self end # Set a CSS 'value' for 'prop' # var img = new HTMLTag("img") # img.css("border", "2px solid black").css("position", "absolute") # assert img.write_to_string == """""" fun css(prop: String, value: String): HTMLTag do css_props[prop] = value return self end private var css_props: Map[String, String] = new HashMap[String, String] # Get CSS value for 'prop' # var img = new HTMLTag("img") # img.css("border", "2px solid black").css("position", "absolute") # assert img.get_css("border") == "2px solid black" # assert img.get_css("color") == null fun get_css(prop: String): nullable String do if not css_props.has_key(prop) then return null return css_props[prop] end # Replace `self` by `parent`. # # var elem = new HTMLTag("li") # elem.add_outer(new HTMLTag("ul")) # assert elem.write_to_string == "Hello World!
" # Text is escaped see: `standard::String::html_escape` fun text(txt: String): HTMLTag do children.clear append(txt) return self end # Append text to element # var p = new HTMLTag("p") # p.append("Hello") # p.add(new HTMLTag("br")) # p.append("World!") # assert p.write_to_string == "Hello
World!
Hello