Giter VIP home page Giter VIP logo

jsonhtml's Introduction

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

jsonhtml's People

Contributors

mantyr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.