Esempio n. 1
0
func TidyBytes(src []byte) []byte {
	td := C.tidyCreate()
	C.tidyParseString(td, (*C.tmbchar)(unsafe.Pointer(&src[0])))
	C.tidyCleanAndRepair(td)
	var blen C.unsigned
	C.tidySaveString(td, nil, &blen)
	buf := make([]byte, int(blen))
	C.tidySaveString(td, (*C.tmbchar)(unsafe.Pointer(&buf[0])), &blen)
	return buf
}
Esempio n. 2
0
func (this *Tidy) Tidy(htmlSource string) (string, error) {
	input := C.CString(htmlSource)
	defer C.free(unsafe.Pointer(input))

	var output C.TidyBuffer
	defer C.tidyBufFree(&output)

	var rc C.int = -1

	rc = C.tidySetErrorBuffer(this.tdoc, &this.errbuf) // Capture diagnostics

	if rc >= 0 {
		rc = C.tidyParseString(this.tdoc, (*C.tmbchar)(input)) // Parse the input
	}

	if rc >= 0 {
		rc = C.tidyCleanAndRepair(this.tdoc) // Tidy it up!
	}

	if rc >= 0 {
		rc = C.tidyRunDiagnostics(this.tdoc) // Kvetch
	}

	if rc > 1 { // If error, force output.
		if C.tidyOptSetBool(this.tdoc, C.TidyForceOutput, C.yes) == 0 {
			rc = -1
		}
	}

	if rc >= 0 {
		rc = C.tidySaveBuffer(this.tdoc, &output) // Pretty Print
	}

	if rc >= 0 {
		if rc > 0 {
			err := errors.New(C.GoStringN((*C.char)(unsafe.Pointer(this.errbuf.bp)), C.int(this.errbuf.size)))
			return C.GoStringN((*C.char)(unsafe.Pointer(output.bp)), C.int(output.size)), err
		}
		return C.GoStringN((*C.char)(unsafe.Pointer(output.bp)), C.int(output.size)), nil
	}
	return "", os.NewSyscallError(fmt.Sprintf("A severe error (%d) occurred.\n", int(rc)), errors.New(string(rc)))
}