htmltojs

package module
v0.0.0-...-ce0ea8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2023 License: MIT Imports: 7 Imported by: 0

README

Actions Status

htmltojs

Convert HTML to JavaScript


install

go get github.com/saihon/htmltojs

example


package main

import (
	"log"
	"os"
	"strings"

	htmltojs "github.com/saihon/htmltojs"
)

func main() {
	r := strings.NewReader(`
<div id="content">
	text node 1
	<ul style="list-style-type: none;">
	  <li class="list">foo</li>
	  <li class="list">bar</li>
	  <li class="list">baz</li>
	</ul>
	text node 2
</div>
<script>
(function () {
  console.log('hello world');
})();
</script>`)

	h := htmltojs.New()

	if err := h.Parse(r); err != nil {
		log.Fatal(err)
	}

	h.WriteTo(os.Stdout)
}

Output


var _a = document.createElement("div");
_a.id = "content";
document.body.appendChild(_a);
var _b = document.createTextNode("text node 1");
_a.appendChild(_b);
var _c = document.createElement("ul");
_c.style.listStyleType = "none";
_a.appendChild(_c);
var _d = document.createElement("li");
_d.className = "list";
_c.appendChild(_d);
var _e = document.createTextNode("foo");
_d.appendChild(_e);
var _f = document.createElement("li");
_f.className = "list";
_c.appendChild(_f);
var _g = document.createTextNode("bar");
_f.appendChild(_g);
var _h = document.createElement("li");
_h.className = "list";
_c.appendChild(_h);
var _i = document.createTextNode("baz");
_h.appendChild(_i);
var _j = document.createTextNode("text node 2");
_a.appendChild(_j);
(function () {
  console.log('hello world');
})();



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data []atom.Atom

func (*Data) Add

func (d *Data) Add(atoms ...atom.Atom)

func (*Data) Del

func (d *Data) Del(atom atom.Atom)

func (*Data) Set

func (d *Data) Set(atoms ...atom.Atom)

type Excludes

type Excludes struct {
	*Data
}

Excludes

type Filter

type Filter interface {
	Set(...atom.Atom)
	Add(...atom.Atom)
	Del(atom.Atom)
	// contains filtered or unexported methods
}

type HTMLtoJS

type HTMLtoJS struct {
	// buffering converted javascript string
	Buffer bytes.Buffer

	Variable Variable

	Ignores  Filter
	Excludes Filter
	Includes Filter

	// A string of elements to append if there is no parent node.
	// such as "document.body", "document.documentElement"
	DefaultParent string
}

HTMLtoJS is buffers and some settings implementation

func New

func New() *HTMLtoJS

New returns new HTMLtoJS with default settings

func (*HTMLtoJS) Parse

func (h *HTMLtoJS) Parse(r io.Reader) error

Parse parses the HTML data and buffers results.

func (*HTMLtoJS) String

func (h *HTMLtoJS) String() string

String returns string data from buffer.

func (*HTMLtoJS) WriteTo

func (h *HTMLtoJS) WriteTo(w io.Writer) (int64, error)

WriteTo writes data to the specified writer.

type Ignores

type Ignores struct {
	*Data
}

Ignores

type Includes

type Includes struct {
	*Data
}

Includes

type Variable

type Variable struct {
	// prefix of variable name. that for an avoid the reserved words.
	Prefix string
	// contains filtered or unexported fields
}

Variable is generate variable name.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL