Giter Site home page Giter Site logo

gotidy's Introduction

GoTidy

Simple package that wraps libtidy.

Install Package

GoTidy uses CGO to link to Libtidy. Libtidy must be a shared library. OSX Lion comes with Libtidy, but it is a static library which will cause all sorts of problems. See below about compiling Tidy as a shared library under OSX.

go get github.com/JalfResi/GoTidy

And import into your program like so:

import "github.com/JalfResi/GoTidy"

Install Example Binary

go get github.com/JalfResi/GoTidy/gotidy

Example usage of example binary:

$ echo "<html><body><p>Rad" | gotidy

Example

package main

import (
	"fmt"
	"log"
	"github.com/JalfResi/GoTidy"
)

func main() {

	// Create an instance of Tidy
	t := tidy.New()
	defer t.Free()

	// Set our options
	t.OutputXml(true)
	t.AddXmlDecl(true)
	t.QuoteAmpersand(true)
	t.TidyMark(false)

	// Tidy our source HTML!
	output, err := t.Tidy("<title id='bob' class='frank'>Welcome</title><p>Hello, 世界</p><p>Foo!")
	if err != nil {
		log.Fatal(err, output)
	}
	fmt.Println(output)
}		

The package if very straight forward: simply create an instance of Tidy using New(), set the options you want and then call the Tidy() instance method, passing it the string of HTML to tidy. The Tidy() method returns the output (if any) and maybe an Error object.

Compiling Libtidy as a shared library under OSX

This is relatively easy to do. Simply download the Tidy source code, and compile as per the following instructions. This has been known to work under OSX Lion.

sh build/gnuauto/setup.sh && ./configure && make
make install

Note that you will either need to run make install as root, or with sudo make install.

Thanks

Thanks obviously to Dave Raggett for the awesome HTML Tidy library. Thanks to Evan Shaw, Gustavo Niemeyer, Matt Kane's Brain and TJ Yang for help on golang-nuts Thanks to shark and remy-o on irc.freenode.net#go-nuts with some of the trickier C casting stuff

To-do

  • Modify Tidy() method to accept Reader interface
  • MOAR Documentation!
  • Unit Tests ya fool!

gotidy's People

Contributors

endeveit avatar

Stargazers

 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

gotidy's Issues

How can I compile under debian bookworm/sid

Dear Author!

I could not build GoTidy on my debian box.

kacper@zbigii:~/Robocze/GoLang/GoTidy (master *$)$ go vet
# _/home/kacper/Robocze/GoLang/GoTidy
./options.go:341:25: could not determine kind of name for C.TidyBurstSlides
./options.go:85:25: could not determine kind of name for C.TidyDropFontTags
./options.go:130:25: could not determine kind of name for C.TidyHideEndTags
./options.go:433:27: could not determine kind of name for C.TidyLanguage
./options.go:510:27: could not determine kind of name for C.TidySlideStyle
kacper@zbigii:~/Robocze/GoLang/GoTidy (master *$)$ dpkg -L libtidy-dev | grep 'usr/include/tidy/.*h'
/usr/include/tidy/buffio.h
/usr/include/tidy/platform.h
/usr/include/tidy/tidy.h
/usr/include/tidy/tidybuffio.h
/usr/include/tidy/tidyenum.h
/usr/include/tidy/tidyplatform.h
kacper@zbigii:~/Robocze/GoLang/GoTidy (master *$)$ for sc in C.TidyBurstSlides C.TidyDropFontTags C.TidyHideEndTags C.TidyLanguage C.TidySlideStyle ; do dpkg -L libtidy-dev | grep 'include/tidy/.*h' | xargs grep "$sc"; echo $?; done
123
123
123
123
123
kacper@zbigii:~/Robocze/GoLang/GoTidy (master *$)$ for sc in TidyBurstSlides TidyDropFontTags TidyHideEndTags TidyLanguage TidySlideStyle ; do dpkg -L libtidy-dev | grep 'include/tidy/.*h' | xargs grep "$sc"; echo $?; done
123
123
123
123
123

I can see the header files are missing. Maybe they are specific to Mac OS X? Aren't they?

Yours sincerely.
KAcper Perschke

Panic - tree has lost its integrity

I am getting

Panic - tree has lost its integrity

when run following program:

package main

import (
    "fmt"
    "log"

    "github.com/JalfResi/GoTidy"
)

func main() {
    t := tidy.New()
    defer t.Free()

    t.InputXml(true)
    t.OutputXml(true)
    t.AddXmlDecl(true) // Without this option everything is OK.

    output, err := t.Tidy("test")
    if err != nil {
        log.Fatal(err, output)
    }
    fmt.Println(output)
}

I guess this is libtidy issue, but probably you can change Go program to not exit when libtidy can't parse XML.

panic on ShowBodyOnly

Trying to set ShowBodyOnly:

    t := tidy.New()
    t.ShowBodyOnly(1)

But it results in a panic:

Assertion failed: (option_defs[ optId ].type == TidyInteger), function SetOptionInt, file ../../src/config.c, line 381.
SIGABRT: abort
PC=0x7fff8782e002 m=6
signal arrived during cgo execution

goroutine 5 [syscall, locked to thread]:
runtime.cgocall(0x42fed20, 0xc8200b1740, 0x0)
    /usr/local/Cellar/go/1.5.3/libexec/src/runtime/cgocall.go:120 +0x11b fp=0xc8200b16f0 sp=0xc8200b16c0
github.com/JalfResi/GoTidy._Cfunc_tidyOptSetInt(0x5804200, 0xc80000003b, 0x1, 0xc800000000)
    ??:0 +0x39 fp=0xc8200b1740 sp=0xc8200b16f0
github.com/JalfResi/GoTidy.(*Tidy).optSetInt(0xc820330540, 0xc80000003b, 0x1, 0xc820015f80, 0x0, 0x0)
    /usr/local/lib/go/src/github.com/JalfResi/GoTidy/options.go:543 +0x42 fp=0xc8200b1768 sp=0xc8200b1740
github.com/JalfResi/GoTidy.(*Tidy).optSetAutoBool(0xc820330540, 0xc80000003b, 0x1, 0x5804200, 0x0, 0x0)
    /usr/local/lib/go/src/github.com/JalfResi/GoTidy/options.go:530 +0x4f fp=0xc8200b17c8 sp=0xc8200b1768
github.com/JalfResi/GoTidy.(*Tidy).ShowBodyOnly(0xc820330540, 0x1, 0xc8200b18a8, 0x0, 0x0)
    /usr/local/lib/go/src/github.com/JalfResi/GoTidy/options.go:260 +0x3f fp=0xc8200b1800 sp=0xc8200b17c8

Installing on Windows

Could you give some installation instruction for Windows? Using simple go get results in error:

fatal error: tidy.h: No such file or directory compilation terminated.

turn off output

I'm getting a lot of output from GoTidy -- warnings and errors.

is there a way to turn off warnings? errors? both?

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.