jsonhtml

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

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

Go to latest
Published: Nov 9, 2016 License: MIT Imports: 8 Imported by: 0

README

JsonHtml - генератор Html на основе валидного JSON объекта

[![Build Status] (https://travis-ci.org/mantyr/jsonhtml.svg?branch=master)] (https://travis-ci.org/mantyr/jsonhtml) [![GoDoc] (https://godoc.org/github.com/mantyr/jsonhtml?status.png)] (https://godoc.org/github.com/mantyr/jsonhtml) [Software License] (LICENSE.md) [![Go Report Card] (https://goreportcard.com/badge/github.com/mantyr/jsonhtml)] (https://goreportcard.com/report/github.com/mantyr/jsonhtml)

Эта библиотека пример выполнения тестового задания - реальное использование её не рекомендуется по следующей причине - не сохраняется порядок элементов. Для более подробной информации смотрите примечание.

Installation

$ go get github.com/mantyr/jsonhtml

Examples:

go run ./*.go ./testdata/file1.json 
<ul><li><h3>Title #1</h3><div>Hello, World 1!</div></li><li><h3>Title #2</h3><div>Hello, World 2!</div></li></ul>

go run ./*.go ./testdata/file1.json ./testdata/file2.json 
<ul><li><h3>Title #1</h3><div>Hello, World 1!</div></li><li><h3>Title #2</h3><div>Hello, World 2!</div></li></ul>

go run ./*.go ./testdata/file1.json ./testdata/file2.json ./testdata/file3.json ./testdata/file4.json 
<ul><li><h3>Title #1</h3><div>Hello, World 1!</div></li><li><h3>Title #2</h3><div>Hello, World 2!</div></li></ul>
<ul><li><span>Title #1</span><content><ul><li><p>Example 1</p><header>header 1</header></li></ul></content></li><li><div>div 1</div></li></ul>
<p>hello1</p>
<p id="my-id" class="my-class">hello</p><p class="my-class1 my-class2">example&lt;a&gt;asd&lt;/a&gt;</p>


go run ./*.go no_file.json
2016/11/09 08:10:33 Convert error file no_file.json open no_file.json: no such file or directory
package main

import (
    "github.com/mantyr/jsonhtml"
    "flag"
    "fmt"
    "log"
)

func main() {
    flag.Parse()
    for _, file_address := range flag.Args() {
        s, err := jsonhtml.ConvertFile(file_address)

        if err != nil {
            log.Println("Convert error file", file_address, err.Error())
            continue
        }
        fmt.Println(s)
    }
}
package main

import (
    "github.com/mantyr/jsonhtml"
    "fmt"
)

func main() {
    js := `
    {
        "p.my-class#my-id": "hello",
        "p.my-class1.my-class2":"example<a>asd</a>"
    }
    `
    val, err := ConvertString(js)

    fmt.Println(val, err) // `<p id="my-id" class="my-class">hello</p><p class="my-class1 my-class2">example&lt;a&gt;asd&lt;/a&gt;</p>`
}

Примечание

Эта библиотека пример выполнения тестового задания - реальное использование её не рекомендуется по следующей причине.

Вначале JSON конвертируется в interface{} стандартным encoding/json, потом разбирается полученный объект.

Так как encoding/json для объектов использует map, а сам по себе map не гарантирует порядок следования элементов то эта библиотека так же не гарантирует порядок элементов

Если кто-то будет заинтересован в этой библиотеке вы можете связаться со мною что бы заменить encoding/json на что-то другое для сохранения порядка элементов

Author

Oleg Shevelev

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(data []byte) (s string, err error)

func ConvertFile

func ConvertFile(filename string) (s string, err error)

func ConvertString

func ConvertString(data string) (string, error)

func Encode

func Encode(v interface{}) (s string, err error)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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