src/happyx/spa/tag

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"

Types

TagRef = ref object
  name*: string
  parent*: TagRef
  attrs*: StringTableRef
  args*: seq[string]
  children*: seq[TagRef]
  isText*: bool              ## Ignore attributes and children when true
  onlyChildren*: bool        ## Ignore self and shows only children
  

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.
func `[]`(self: TagRef; attrName: string): string {.inline, ...raises: [KeyError],
    tags: [], forbids: [].}
func `[]`(self: TagRef; index: int): TagRef {.inline, ...raises: [], tags: [],
    forbids: [].}
Returns tag by index
func `[]=`(self: TagRef; attrName: string; attrValue: string) {.inline,
    ...raises: [], tags: [], forbids: [].}
Sets a new value for attribute or create new attribute
proc add(self: TagRef; tags: varargs[TagRef]) {....raises: [], tags: [],
    forbids: [].}
Adds other tag into self tag

Example:

var
  rootTag = tag"div"
  child1 = tag"p"
  child2 = tag"p"
rootTag.add(child1, child2)
proc addArg(self: TagRef; arg: string) {....raises: [], tags: [], forbids: [].}
Adds arg into tag

Example

var tag = initTag("div")
echo tag  # <div></div>
tag.addArg("data-1")
echo tag  # <div data-1></div>
proc addArgIter(self: TagRef; arg: string) {....raises: [], tags: [], forbids: [].}

Adds argument into current tag and all children

See also addArg

func findByTag(self: TagRef; tag: string): seq[TagRef] {....raises: [], tags: [],
    forbids: [].}
Finds all tags by name
func get(self: TagRef; tag: string): TagRef {....raises: [ValueError], tags: [],
    forbids: [].}
Returns tag by name
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.
proc initTag(name: string; isText: bool; children: seq[TagRef] = @[];
             onlyChildren: bool = false): TagRef {....raises: [], tags: [],
    forbids: [].}
Initializes a new 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 tag(tag: TagRef): TagRef {.inline, ...raises: [], tags: [], forbids: [].}
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!"
proc toSeqIter(self: TagRef): seq[TagRef] {....raises: [], tags: [], forbids: [].}