Tag ✨
Provides a Tag type that represents an HTML tag. The file includes several functions for initializing new HTML tags, adding child tags to existing tags, and getting the level of nesting for a tag within its parent tags. The file also includes a function for converting a tag and its child tags to a string representation with proper indentation and attributes.
Usage 🔨
var html = initTag( "div", @[ textTag("Hello, world!"), textTag("This is text tag"), ] )
It's low-level usage. At high-level you'll write HTML with buildHtml macro:
var html = buildHtml: tDiv: "Hello, world!" "This is text tag"
Consts
NimKeywords = ["if", "elif", "else", "using", "type", "of", "in", "notin", "and", "binding", "mixin", "type", "div", "mod", "case", "while", "for", "method", "proc", "func", "iterator", "template", "converter", "macro", "typed", "untyped", "int", "int8", "int16", "int32", "int64", "int128", "float", "float32", "float64", "string", "cstring", "when", "defined", "declared", "import", "from", "try", "except", "finally", "as", "var", "let", "const"]
UnclosedTags = ["area", "base", "basefont", "br", "col", "frame", "hr", "img", "isindex", "link", "meta", "param", "wbr", "source", "input", "!DOCTYPE"]
Procs
func `$`(self: TagRef): string {....raises: [Exception, ValueError], tags: [RootEffect], forbids: [].}
- This function returns a string representation of the current tag and its child tags, if any. The function formats the tag with proper indentation based on the level of nesting and includes any attributes specified for the tag. If the tag is a text tag, the function simply returns the tag's name.
proc addArgIter(self: TagRef; arg: string) {....raises: [], tags: [], forbids: [].}
-
Adds argument into current tag and all children
See also addArg
func getAttribute(self: TagRef; attrName: string; default: string = ""): string {. inline, ...raises: [KeyError], tags: [], forbids: [].}
- Returns attribute value if exists or default value if not.
proc initTag(name: string; attrs: StringTableRef; children: seq[TagRef] = @[]; onlyChildren: bool = false): TagRef {....raises: [], tags: [], forbids: [].}
-
Initializes a new HTML tag with the given name, attributes, and children.
Args:
- name: The name of the HTML tag to create.
- attrs: The attributes to set for the HTML tag.
- children: The children to add to the HTML tag.
Returns:
- A reference to the newly created HTML tag.
proc initTag(name: string; children: seq[TagRef] = @[]; onlyChildren: bool = false): TagRef {....raises: [], tags: [], forbids: [].}
-
Initializes a new HTML tag without attributes but with children
Args:
- name: tag name
- children: The children to add to the HTML tag.
Returns:
- A reference to the newly created HTML tag.
proc initTag(name: string; isText: bool; attrs: StringTableRef; children: seq[TagRef] = @[]; onlyChildren: bool = false): TagRef {. ...raises: [], tags: [], forbids: [].}
-
Initializes a new HTML tag with the given name, whether it's text or not, attributes, and children.
Args:
- name: The name of the HTML tag to create.
- isText: Whether the tag represents text.
- attrs: The attributes to set for the HTML tag.
- children: The children to add to the HTML tag.
Returns:
- A reference to the newly created HTML tag.
func lvl(self: TagRef): int {....raises: [], tags: [], forbids: [].}
-
This function returns the level of nesting of the current tag within its parent tags.
Example:
var root = tag"div" child1 = tag"h1" child2 = tag"h2" root.add(child1) child1.add(child2) assert child2.lvl == 2 assert child1.lvl == 1 assert root.lvl == 0
proc tag(name: string): TagRef {.inline, ...raises: [], tags: [], forbids: [].}
-
Shortcut for initTag func
Example:
var root = tag"div"
proc tagFromString(source: string): TagRef {.inline, ...raises: [IOError, OSError, ValueError, Exception], tags: [ReadIOEffect, RootEffect, WriteIOEffect], forbids: [].}
- Translates raw HTML string into TagRef
proc textTag(text: string): TagRef {.inline, ...raises: [], tags: [], forbids: [].}
-
Shortcur for initTag func
Example:
var root = textTag"Hello, world!"