margrave/element

Source   Edit  

Types

KnownTags = enum
  tagNone = "", tagParagraph = "p", tagLineBreak = "br", tagHeader1 = "h1",
  tagHeader2 = "h2", tagHeader3 = "h3", tagHeader4 = "h4", tagHeader5 = "h5",
  tagHeader6 = "h6", tagUnorderedList = "ul", tagOrderedList = "ol",
  tagListItem = "li", tagBlockquote = "blockquote", tagSuperscript = "sup",
  tagSubscript = "sub", tagItalic = "em", tagBold = "strong",
  tagPreformatted = "pre", tagCode = "code", tagUnderline = "u",
  tagStrikethrough = "s", tagImage = "img", tagInput = "input", tagLinked = "a",
  tagPicture = "picture", tagVideo = "video", tagAudio = "audio",
  tagSource = "source", tagOther = "unknownTag"
Enum of tags used in this library. Source   Edit  
MargraveElement {.acyclic.} = ref object
  case isText*: bool
  of true:
    str*: NativeString       ## Text of a text element.
                             ## Can contain HTML, escaping chars must be done beforehand.
  else:
    tag*: KnownTags ## The tag of an HTML element.
                    ## 
                    ## If equal to `otherTag`, the `tag` attribute is used.
                    ## Such elements can also indicate having no ending tag
                    ## with the `emptyTag` attribute.
    attrs*: OrderedTable[NativeString, NativeString] ## Attributes of an HTML element.
    content*: seq[MargraveElement] ## Inner HTML elements of an HTML element.

An individual node.

Can be text, or an HTML element.

HTML element contains tag, attributes, and sequence of nodes.

Source   Edit  

Consts

EmptyTags = {tagNone, tagLineBreak, tagImage, tagInput, tagSource, tagOther}
Source   Edit  

Procs

func `$`(elem: MargraveElement): string {....raises: [KeyError], tags: [],
    forbids: [].}
Outputs a margrave element as HTML. Source   Edit  
func `[]`(elem: MargraveElement; i: BackwardsIndex): MargraveElement {.
    ...raises: [], tags: [], forbids: [].}
Source   Edit  
func `[]`(elem: MargraveElement; i: int): MargraveElement {....raises: [],
    tags: [], forbids: [].}
Indexes elem.content. Source   Edit  
func `[]=`(elem: MargraveElement; i: BackwardsIndex; el: MargraveElement) {.
    ...raises: [], tags: [], forbids: [].}
Indexes elem.content. Source   Edit  
func `[]=`(elem: MargraveElement; i: int; el: MargraveElement) {....raises: [],
    tags: [], forbids: [].}
Indexes elem.content. Source   Edit  
func add(elem, cont: MargraveElement) {....raises: [], tags: [], forbids: [].}
Adds to elem.content. Source   Edit  
func add(elem: MargraveElement; cont: seq[MargraveElement]) {....raises: [],
    tags: [], forbids: [].}
Appends nodes to elem.content. Source   Edit  
func add(elem: MargraveElement; str: NativeString) {....raises: [], tags: [],
    forbids: [].}
Adds a text node to elem.content. Source   Edit  
proc attr(elem: MargraveElement; key, val: NativeString) {....raises: [], tags: [],
    forbids: [].}
Adds attribute to element Source   Edit  
proc attr(elem: MargraveElement; key: NativeString): NativeString {.
    ...raises: [KeyError], tags: [], forbids: [].}
Source   Edit  
proc attrEscaped(elem: MargraveElement; key, val: NativeString) {....raises: [],
    tags: [], forbids: [].}
Adds attribute to element escaped Source   Edit  
proc delAttr(elem: MargraveElement; key: NativeString) {....raises: [], tags: [],
    forbids: [].}
Deletes attribute of element Source   Edit  
proc hasAttr(elem: MargraveElement; key: NativeString): bool {....raises: [],
    tags: [], forbids: [].}
Checks if element has attribute Source   Edit  
func newElem(tag: KnownTags; content: seq[MargraveElement] = @[]): MargraveElement {.
    ...raises: [], tags: [], forbids: [].}
Creates a new element node with tag tag and content nodes content. Source   Edit  
func newStr(s: NativeString): MargraveElement {....raises: [], tags: [],
    forbids: [].}
Creates a new text node with text s. Source   Edit  
func paragraphIfText(elem: MargraveElement): MargraveElement {....raises: [],
    tags: [], forbids: [].}
If elem is a text node, turns it into a <p> element. Otherwise returns elem. Source   Edit  
proc style(elem: MargraveElement; style: NativeString) {....raises: [], tags: [],
    forbids: [].}
Adds style to element Source   Edit  
func toCstring(elem: MargraveElement): cstring {....raises: [KeyError], tags: [],
    forbids: [].}
Outputs a margrave element as HTML as a cstring, mostly for JS. Source   Edit  
func toNativeString(elem: MargraveElement): NativeString {....raises: [KeyError],
    tags: [], forbids: [].}
Source   Edit  

Templates

template isEmpty(tag: KnownTags): bool
Returns true if tag is an empty tag, i.e. it has no ending tag. Source   Edit