Giter VIP home page Giter VIP logo

gohtml's Introduction

GoHTML - HTML formatter for Go

wercker status GoDoc

GoHTML is an HTML formatter for Go. You can format HTML source codes by using this package.

Install

go get -u github.com/yosssi/gohtml

Example

Example Go source code:

package main

import (
	"fmt"

	"github.com/yosssi/gohtml"
)

func main() {
	h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
	alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
	font-size: 16px;
	font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
	fmt.Println(gohtml.Format(h))
}

Output:

<!DOCTYPE html>
<html>
  <head>
    <title>
      This is a title.
    </title>
    <script type="text/javascript">
      alert('aaa');
      if (0 < 1) {
      	alert('bbb');
      }
    </script>
    <style type="text/css">
      body {font-size: 14px;}
      h1 {
      	font-size: 16px;
      	font-weight: bold;
      }
    </style>
  </head>
  <body>
    <form>
      <input type="name">
      <p>
        AAA
        <br>
        BBB>
      </p>
    </form>
    <!-- This is a comment. -->
  </body>
</html>

Output Formatted HTML with Line No

You can output formatted HTML source codes with line no by calling FormatWithLineNo:

package main

import (
	"fmt"

	"github.com/yosssi/gohtml"
)

func main() {
	h := `<!DOCTYPE html><html><head><title>This is a title.</title><script type="text/javascript">
alert('aaa');
if (0 < 1) {
	alert('bbb');
}
</script><style type="text/css">
body {font-size: 14px;}
h1 {
	font-size: 16px;
	font-weight: bold;
}
</style></head><body><form><input type="name"><p>AAA<br>BBB></p></form><!-- This is a comment. --></body></html>`
	fmt.Println(gohtml.FormatWithLineNo(h))
}

Output:

 1  <!DOCTYPE html>
 2  <html>
 3    <head>
 4      <title>
 5        This is a title.
 6      </title>
 7      <script type="text/javascript">
 8        alert('aaa');
 9        if (0 < 1) {
10        	alert('bbb');
11        }
12      </script>
13      <style type="text/css">
14        body {font-size: 14px;}
15        h1 {
16        	font-size: 16px;
17        	font-weight: bold;
18        }
19      </style>
20    </head>
21    <body>
22      <form>
23        <input type="name">
24        <p>
25          AAA
26          <br>
27          BBB>
28        </p>
29      </form>
30      <!-- This is a comment. -->
31    </body>
32  </html>

Format Go html/template Package's Template's Execute Result

You can format Go html/template package's template's execute result by passing Writer to the tpl.Execute:

package main

import (
	"os"
	"text/template"

	"github.com/yosssi/gohtml"
)

func main() {

	tpl, err := template.New("test").Parse("<html><head></head><body>{{.Msg}}</body></html>")

	if err != nil {
		panic(err)
	}

	data := map[string]interface{}{"Msg": "Hello!"}

	err = tpl.Execute(gohtml.NewWriter(os.Stdout), data)

	if err != nil {
		panic(err)
	}
}

Output:

<html>
  <head>
  </head>
  <body>
    Hello!
  </body>
</html>

Docs

gohtml's People

Contributors

adamcolton avatar anthonyfok avatar flashover avatar fvbommel avatar keijiyoshida avatar kjmrknsn avatar nn-user avatar oxyduck-g avatar yosssi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gohtml's Issues

*gohtml.Writer does not implement the io.Writer contract

From the io.Writer documentation:

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p).

However, (*gohtml.Writer).Write returns the number of output bytes it writes to the underlying Writer instead of the number of input bytes it consumes.

Newline removed before <pre>

package main

import (
	"fmt"

	"github.com/yosssi/gohtml"
)

func main() {
	h := `<h1>Pre</h1>
	
<pre>some code</pre>

<h1>Post</h1>
`
	fmt.Println(gohtml.Format(h))
}

Prints

<h1>
  Pre
</h1><pre>some code</pre>
<h1>
  Post
</h1>

I understand the need for whitespace-preservation inside pre, but the above looks wrong to me.

code, textarea and pre should not be changed

Preformatted tags like pre, code and textarea should be kept unchanged and with the original indentation.
Changes inside those tags produce changes in visualization and in values inside a form.

Indentation is removed from JS within <script> tags

Hi! Thanks for creating the gohtml package. I'm using it in Orbiton for formatting HTML from within the editor.

However, JavaScript code that is embedded within <script> tags seems to have indentation removed, so that:

<script>
  if (1 + 1 == 2) {
      x = 42
  }
</script>

Is formatted like this:

<script>
  if (1 + 1 == 2) {
  x = 42
  }
</script>

Suggestion on inline tags

It would be nice if some tags did not take their own lines, for instance, tags that are usually inline like: "a", "image", "span", "h1" and "h2" could be kept like this:

<p>
  Paragraph text with <span>an inline span element.</span>
</p>

Various spacing issues- may not be supported for various reasons

These may not be supported for prettifying:

  • Space at the end of a tag is left there: <a href="blah/" onmousedown="some_script" >
  • A script tag will not have its indentions formatted.

The script tag part seems to leave the indention of:

<script>
  window.customElements.define(
		'name',
		class extends HTMLElement {
			constructor() {
				super();
				let template = document.getElementById('name');
				let templateContent = template.content;

				const shadowRoot = this.attachShadow({mode: 'open'}).appendChild(templateContent.cloneNode(true));
			}
		}
	);
</script>

So if I have the above, where most of the opening space is tabs, the prettifier seems to leave it alone. I think it would probably be safe normalize the space to the same as other HTML elements. Just makes it look strange otherwise.

However, there may be completely valid reasons for not trying to do these, as prettying HTML is a fairly complex procedure I imagine with all kinds of corner cases.

Thanks for taking the time to read this.

Subsequent formatting increases indentation

My use case is a program that uses gohtml to format an html file.

Unfortunately, the current behavior is that calling gohtml.Format() on previous result of gohtml.Format() creates additional indentation on each step, which makes this uses case impossible.

An example:

package main

import (
    "fmt"

    "github.com/yosssi/gohtml"
)

func main() {
    s := "<title>my title</title>"
    s = gohtml.Format(s)
    fmt.Printf("s1:\n%s\n\n", s)
    s = gohtml.Format(s)
    fmt.Printf("s2:\n%s\n\n", s)
    s = gohtml.Format(s)
    fmt.Printf("s3:\n%s\n\n", s)
}

Result:

 go run t.go
s1:
<title>
  my title
</title>

s2:
<title>
    my title
</title>

s3:
<title>
      my title
</title>

As you can see, "my title" gets indented more after each call to Format().

Desired behavior is that s2 and s3 are exactly the same as s1.

inline tags

Inline tags defined in InlineTags variable are not "inlined"

<span>Hello World</span>

shows as

<span>
    Hello World
</span>

Is there anything I missed ? May be an option ?

Cheers

Jean-Luc

HTML entities should be preserved

HTML entities (e.g. &amp;) should be preserved in the output. This is especially important for entities that can mess up the HTML output if converted back into their corresponding character, such as &lt;, &gt;, and &amp;:

gohtml.Format("<div>0 &lt; 1. great insight! &lt;/sarcasm&gt; over&amp;out.</div>")

gives:

<div>
    0 < 1. great insight! </sarcasm> over&out.
</div>

instead should be:

<div>
    0 &lt; 1. great insight! &lt;/sarcasm&gt; over&amp;out.
</div>

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.